Migrate actions,reducers and action types to TS #15
This commit is contained in:
@@ -58,8 +58,8 @@ import * as globalActions from 'actions/globalActions';
|
||||
}
|
||||
|
||||
/**
|
||||
* Register user in database
|
||||
* @param {object} user
|
||||
*
|
||||
* @param user for registering
|
||||
*/
|
||||
export const dbSignup = (user: User) => {
|
||||
return (dispatch: any, getState: any) => {
|
||||
@@ -128,8 +128,8 @@ import * as globalActions from 'actions/globalActions';
|
||||
}
|
||||
|
||||
/**
|
||||
* Register user
|
||||
* @param {object} user
|
||||
* User registeration call
|
||||
* @param user for registering
|
||||
*/
|
||||
export const signup = (user: User) => {
|
||||
return {
|
||||
|
||||
@@ -1,18 +1,22 @@
|
||||
// - Import firebase component
|
||||
import firebase, { firebaseRef } from 'app/firebase/'
|
||||
|
||||
// - Import domain
|
||||
import { User } from "domain/users";
|
||||
import { Circle, UserFollower } from "domain/circles";
|
||||
|
||||
// - Import utility components
|
||||
import moment from 'moment'
|
||||
|
||||
// - Import action types
|
||||
import * as types from 'actionTypes'
|
||||
import { CircleActionType } from 'constants/circleActionType'
|
||||
|
||||
|
||||
// - Import actions
|
||||
import * as globalActions from 'globalActions'
|
||||
import * as postActions from 'postActions'
|
||||
import * as userActions from 'userActions'
|
||||
import * as notifyActions from 'notifyActions'
|
||||
import * as globalActions from 'actions/globalActions'
|
||||
import * as postActions from 'actions/postActions'
|
||||
import * as userActions from 'actions/userActions'
|
||||
import * as notifyActions from 'actions/notifyActions'
|
||||
|
||||
|
||||
|
||||
@@ -25,11 +29,11 @@ import * as notifyActions from 'notifyActions'
|
||||
* Add a circle
|
||||
* @param {string} circleName
|
||||
*/
|
||||
export var dbAddCircle = (circleName) => {
|
||||
return (dispatch, getState) => {
|
||||
export var dbAddCircle = (circleName: string) => {
|
||||
return (dispatch: any, getState: Function) => {
|
||||
|
||||
let uid = getState().authorize.uid
|
||||
let circle = {
|
||||
let uid: string = getState().authorize.uid
|
||||
let circle: Circle = {
|
||||
creationDate: moment().unix(),
|
||||
name: circleName,
|
||||
users: {}
|
||||
@@ -37,10 +41,9 @@ export var dbAddCircle = (circleName) => {
|
||||
|
||||
let circleRef = firebaseRef.child(`userCircles/${uid}/circles`).push(circle)
|
||||
return circleRef.then(() => {
|
||||
dispatch(addCircle(uid, {
|
||||
...circle,
|
||||
id: circleRef.key
|
||||
}))
|
||||
circle.id = circleRef.key;
|
||||
circle.ownerId = uid
|
||||
dispatch(addCircle(circle))
|
||||
|
||||
}, (error) => dispatch(globalActions.showErrorMessage(error.message)))
|
||||
|
||||
@@ -50,37 +53,39 @@ export var dbAddCircle = (circleName) => {
|
||||
/**
|
||||
* Add a user in a circle
|
||||
* @param {string} cid is circle identifier
|
||||
* @param {object} userFollowing is the user for following
|
||||
* @param {User} userFollowing is the user for following
|
||||
*/
|
||||
export var dbAddFollowingUser = (cid, userFollowing) => {
|
||||
return (dispatch, getState) => {
|
||||
export var dbAddFollowingUser = (cid: string, userFollowing: User) => {
|
||||
return (dispatch: any, getState: Function) => {
|
||||
|
||||
let uid = getState().authorize.uid
|
||||
let user = getState().user.info[uid]
|
||||
let uid: string = getState().authorize.uid;
|
||||
let user: User = getState().user.info[uid];
|
||||
|
||||
let userCircle = {
|
||||
let userCircle: User = {
|
||||
creationDate: moment().unix(),
|
||||
fullName: userFollowing.fullName,
|
||||
avatar: userFollowing.avatar || ''
|
||||
}
|
||||
let userFollower = {
|
||||
let userFollower: UserFollower = {
|
||||
creationDate: moment().unix(),
|
||||
fullName: user.fullName,
|
||||
avatar: user.avatar || '',
|
||||
approved: false
|
||||
}
|
||||
let updates = {}
|
||||
let updates: any = {}
|
||||
updates[`userCircles/${uid}/circles/${cid}/users/${userFollowing.userId}`] = userCircle
|
||||
updates[`userCircles/${userFollowing.userId}/circles/-Followers/users/${uid}`] = userFollower
|
||||
return firebaseRef.update(updates).then((result) => {
|
||||
dispatch(addFollowingUser(uid, cid, userFollowing.userId, { ...userCircle }))
|
||||
|
||||
dispatch(notifyActions.dbAddNotify(
|
||||
{
|
||||
description: `${user.fullName} follow you.`,
|
||||
url: `/${uid}`,
|
||||
notifyRecieverUserId: userFollowing.userId, notifierUserId: uid
|
||||
}))
|
||||
dispatch(addFollowingUser(uid, cid, userFollowing.userId as string, { ...userCircle } as User))
|
||||
|
||||
dispatch(notifyActions.dbAddNotify(
|
||||
{
|
||||
description: `${user.fullName} follow you.`,
|
||||
url: `/${uid}`,
|
||||
notifyRecieverUserId: userFollowing.userId as string,
|
||||
notifierUserId: uid,
|
||||
isSeen:false
|
||||
}))
|
||||
|
||||
}, (error) => {
|
||||
dispatch(globalActions.showErrorMessage(error.message))
|
||||
@@ -94,12 +99,12 @@ export var dbAddFollowingUser = (cid, userFollowing) => {
|
||||
* @param {string} cid is circle identifier
|
||||
* @param {string} followingId following user identifier
|
||||
*/
|
||||
export var dbDeleteFollowingUser = (cid, followingId) => {
|
||||
return (dispatch, getState) => {
|
||||
export var dbDeleteFollowingUser = (cid: string, followingId: string) => {
|
||||
return (dispatch: any, getState:Function) => {
|
||||
|
||||
let uid = getState().authorize.uid
|
||||
let uid: string = getState().authorize.uid
|
||||
|
||||
let updates = {}
|
||||
let updates: any = {}
|
||||
updates[`userCircles/${uid}/circles/${cid}/users/${followingId}`] = null
|
||||
updates[`userCircles/${followingId}/circles/-Followers/users/${uid}`] = null
|
||||
return firebaseRef.update(updates).then((result) => {
|
||||
@@ -112,24 +117,24 @@ export var dbDeleteFollowingUser = (cid, followingId) => {
|
||||
|
||||
/**
|
||||
* Update a circle from database
|
||||
* @param {object} newCircle
|
||||
* @param {Circle} newCircle
|
||||
*/
|
||||
export const dbUpdateCircle = (newCircle) => {
|
||||
return (dispatch, getState) => {
|
||||
export const dbUpdateCircle = (newCircle: Circle) => {
|
||||
return (dispatch: any, getState: Function) => {
|
||||
|
||||
// Get current user id
|
||||
var uid = getState().authorize.uid
|
||||
let uid: string = getState().authorize.uid
|
||||
|
||||
// Write the new data simultaneously in the list
|
||||
let updates = {}
|
||||
let circle = getState().circle.userCircles[uid][newCircle.id]
|
||||
let updatedCircle = {
|
||||
let updates: any = {}
|
||||
let circle: Circle = getState().circle.userCircles[uid][newCircle.id!]
|
||||
let updatedCircle : Circle = {
|
||||
name: newCircle.name || circle.name,
|
||||
users: newCircle.users ? newCircle.users : (circle.users || [])
|
||||
}
|
||||
updates[`userCircles/${uid}/circles/${newCircle.id}`] = updatedCircle
|
||||
return firebaseRef.update(updates).then((result) => {
|
||||
dispatch(updateCircle(uid, { id: newCircle.id, ...updatedCircle }))
|
||||
return firebaseRef.update(updates).then(() => {
|
||||
dispatch(updateCircle(uid,{ id: newCircle.id, ...updatedCircle }))
|
||||
}, (error) => {
|
||||
dispatch(globalActions.showErrorMessage(error.message))
|
||||
})
|
||||
@@ -142,14 +147,14 @@ export const dbUpdateCircle = (newCircle) => {
|
||||
* Delete a circle from database
|
||||
* @param {string} id is circle identifier
|
||||
*/
|
||||
export const dbDeleteCircle = (id) => {
|
||||
return (dispatch, getState) => {
|
||||
export const dbDeleteCircle = (id: string) => {
|
||||
return (dispatch: any, getState: Function) => {
|
||||
|
||||
// Get current user id
|
||||
var uid = getState().authorize.uid
|
||||
let uid: string = getState().authorize.uid
|
||||
|
||||
// Write the new data simultaneously in the list
|
||||
var updates = {};
|
||||
let updates: any = {};
|
||||
updates[`userCircles/${uid}/circles/${id}`] = null;
|
||||
|
||||
return firebaseRef.update(updates).then((result) => {
|
||||
@@ -165,19 +170,19 @@ export const dbDeleteCircle = (id) => {
|
||||
* Get all user circles from data base
|
||||
*/
|
||||
export const dbGetCircles = () => {
|
||||
return (dispatch, getState) => {
|
||||
var uid = getState().authorize.uid
|
||||
return (dispatch: any, getState: Function) => {
|
||||
let uid: string = getState().authorize.uid
|
||||
if (uid) {
|
||||
var circlesRef = firebaseRef.child(`userCircles/${uid}/circles`);
|
||||
let circlesRef: any = firebaseRef.child(`userCircles/${uid}/circles`);
|
||||
|
||||
return circlesRef.once('value').then((snapshot) => {
|
||||
var circles = snapshot.val() || {};
|
||||
var parsedCircles = {};
|
||||
return circlesRef.once('value').then((snapshot: any) => {
|
||||
var circles: any = snapshot.val() || {};
|
||||
var parsedCircles: { [circleId: string]: Circle } = {};
|
||||
Object.keys(circles).forEach((circleId) => {
|
||||
if (circleId !== '-Followers' && circles[circleId].users) {
|
||||
Object.keys(circles[circleId].users).filter((v, i, a) => a.indexOf(v) === i).forEach((userId) => {
|
||||
dispatch(postActions.dbGetPostsByUserId(userId))
|
||||
dispatch(userActions.dbGetUserInfoByUserId(userId))
|
||||
dispatch(userActions.dbGetUserInfoByUserId(userId, ""))
|
||||
})
|
||||
}
|
||||
parsedCircles[circleId] = {
|
||||
@@ -195,17 +200,18 @@ export const dbGetCircles = () => {
|
||||
|
||||
|
||||
/**
|
||||
* Get all user circles from data base by user id
|
||||
* Get all user circles from data base by user id
|
||||
* @param uid user identifier
|
||||
*/
|
||||
export const dbGetCirclesByUserId = (uid) => {
|
||||
return (dispatch, getState) => {
|
||||
export const dbGetCirclesByUserId = (uid: string) => {
|
||||
return (dispatch: any, getState: Function) => {
|
||||
|
||||
if (uid) {
|
||||
var circlesRef = firebaseRef.child(`userCircles/${uid}/circles`);
|
||||
let circlesRef = firebaseRef.child(`userCircles/${uid}/circles`);
|
||||
|
||||
return circlesRef.once('value').then((snapshot) => {
|
||||
var circles = snapshot.val() || {};
|
||||
var parsedCircles = {};
|
||||
var parsedCircles: { [circleId: string]: Circle } = {};
|
||||
Object.keys(circles).forEach((circleId) => {
|
||||
parsedCircles[circleId] = {
|
||||
id: circleId,
|
||||
@@ -229,23 +235,23 @@ export const dbGetCirclesByUserId = (uid) => {
|
||||
/**
|
||||
* Add a normal circle
|
||||
* @param {string} uid is user identifier
|
||||
* @param {object} circle
|
||||
* @param {Circle} circle
|
||||
*/
|
||||
export const addCircle = (uid, circle) => {
|
||||
export const addCircle = (circle: Circle) => {
|
||||
return {
|
||||
type: types.ADD_CIRCLE,
|
||||
payload: { uid, circle }
|
||||
type: CircleActionType.ADD_CIRCLE,
|
||||
payload: { circle }
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Update a circle
|
||||
* @param {string} uid is user identifier
|
||||
* @param {object} circle
|
||||
* @param {Circle} circle
|
||||
*/
|
||||
export const updateCircle = (uid, circle) => {
|
||||
export const updateCircle = (uid: string, circle: Circle) => {
|
||||
return {
|
||||
type: types.UPDATE_CIRCLE,
|
||||
type: CircleActionType.UPDATE_CIRCLE,
|
||||
payload: { uid, circle }
|
||||
}
|
||||
}
|
||||
@@ -255,9 +261,9 @@ export const updateCircle = (uid, circle) => {
|
||||
* @param {string} uid is user identifier
|
||||
* @param {string} id is circle identifier
|
||||
*/
|
||||
export const deleteCircle = (uid, id) => {
|
||||
export const deleteCircle = (uid: string, id: string) => {
|
||||
return {
|
||||
type: types.DELETE_CIRCLE,
|
||||
type: CircleActionType.DELETE_CIRCLE,
|
||||
payload: { uid, id }
|
||||
}
|
||||
}
|
||||
@@ -266,11 +272,11 @@ export const deleteCircle = (uid, id) => {
|
||||
/**
|
||||
* Add a list of circle
|
||||
* @param {string} uid
|
||||
* @param {[object]} circles
|
||||
* @param {circleId: string]: Circle} circles
|
||||
*/
|
||||
export const addCircles = (uid, circles) => {
|
||||
export const addCircles = (uid: string, circles: { [circleId: string]: Circle }) => {
|
||||
return {
|
||||
type: types.ADD_LIST_CIRCLE,
|
||||
type: CircleActionType.ADD_LIST_CIRCLE,
|
||||
payload: { uid, circles }
|
||||
}
|
||||
}
|
||||
@@ -280,17 +286,19 @@ export const addCircles = (uid, circles) => {
|
||||
*/
|
||||
export const clearAllCircles = () => {
|
||||
return {
|
||||
type: types.CLEAR_ALL_CIRCLES
|
||||
type: CircleActionType.CLEAR_ALL_CIRCLES
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Open circle settings
|
||||
* @param uid user idenifier
|
||||
* @param id circle identifier
|
||||
*/
|
||||
export const openCircleSettings = (uid, id) => {
|
||||
export const openCircleSettings = (uid: string, id: string) => {
|
||||
return {
|
||||
type: types.OPEN_CIRCLE_SETTINGS,
|
||||
type: CircleActionType.OPEN_CIRCLE_SETTINGS,
|
||||
payload: { uid, id }
|
||||
}
|
||||
|
||||
@@ -298,10 +306,12 @@ export const openCircleSettings = (uid, id) => {
|
||||
|
||||
/**
|
||||
* Close open circle settings
|
||||
* @param uid user identifier
|
||||
* @param id circle identifier
|
||||
*/
|
||||
export const closeCircleSettings = (uid, id) => {
|
||||
export const closeCircleSettings = (uid: string, id: string) => {
|
||||
return {
|
||||
type: types.CLOSE_CIRCLE_SETTINGS,
|
||||
type: CircleActionType.CLOSE_CIRCLE_SETTINGS,
|
||||
payload: { uid, id }
|
||||
}
|
||||
|
||||
@@ -312,11 +322,11 @@ export const closeCircleSettings = (uid, id) => {
|
||||
* @param {string} uid user identifire who want to follow the following user
|
||||
* @param {string} cid circle identifier that following user should be added in
|
||||
* @param {string} followingId following user identifier
|
||||
* @param {object} userCircle information about following user
|
||||
* @param {User} userCircle information about following user
|
||||
*/
|
||||
export const addFollowingUser = (uid, cid, followingId, userCircle) => {
|
||||
export const addFollowingUser = (uid: string, cid: string, followingId: string, userCircle: User) => {
|
||||
return {
|
||||
type: types.ADD_FOLLOWING_USER,
|
||||
type: CircleActionType.ADD_FOLLOWING_USER,
|
||||
payload: { uid, cid, followingId, userCircle }
|
||||
}
|
||||
|
||||
@@ -328,9 +338,9 @@ export const addFollowingUser = (uid, cid, followingId, userCircle) => {
|
||||
* @param {string} cid circle identifier that following user should be added in
|
||||
* @param {string} followingId following user identifier
|
||||
*/
|
||||
export const deleteFollowingUser = (uid, cid, followingId) => {
|
||||
export const deleteFollowingUser = (uid: string, cid: string, followingId: string) => {
|
||||
return {
|
||||
type: types.DELETE_FOLLOWING_USER,
|
||||
type: CircleActionType.DELETE_FOLLOWING_USER,
|
||||
payload: { uid, cid, followingId }
|
||||
}
|
||||
|
||||
@@ -1,231 +0,0 @@
|
||||
// - Import react components
|
||||
import moment from 'moment'
|
||||
import { firebaseRef } from 'app/firebase/'
|
||||
|
||||
// - Import action types
|
||||
import * as types from 'actionTypes'
|
||||
|
||||
// - Import actions
|
||||
import * as globalActions from 'globalActions'
|
||||
import * as notifyActions from 'notifyActions'
|
||||
|
||||
|
||||
|
||||
/* _____________ CRUD DB _____________ */
|
||||
|
||||
/**
|
||||
* Add comment to database
|
||||
* @param {object} newComment user comment
|
||||
* @param {function} callBack will be fired when server responsed
|
||||
*/
|
||||
export const dbAddComment = (newComment, callBack) => {
|
||||
return (dispatch, getState) => {
|
||||
|
||||
dispatch(globalActions.showTopLoading())
|
||||
|
||||
|
||||
var uid = getState().authorize.uid
|
||||
var comment = {
|
||||
postId: newComment.postId,
|
||||
score: 0,
|
||||
text: newComment.text,
|
||||
creationDate: moment().unix(),
|
||||
userDisplayName: getState().user.info[uid].fullName,
|
||||
userAvatar: getState().user.info[uid].avatar,
|
||||
userId: uid
|
||||
}
|
||||
|
||||
var commentRef = firebaseRef.child(`postComments/${newComment.postId}`).push(comment)
|
||||
return commentRef.then(() => {
|
||||
dispatch(addComment(
|
||||
{
|
||||
comment,
|
||||
postId: newComment.postId,
|
||||
id: commentRef.key,
|
||||
editorStatus: false
|
||||
}))
|
||||
callBack()
|
||||
dispatch(globalActions.hideTopLoading())
|
||||
|
||||
if (newComment.ownerPostUserId !== uid)
|
||||
dispatch(notifyActions.dbAddNotify(
|
||||
{
|
||||
description: 'Add comment on your post.',
|
||||
url: `/${newComment.ownerPostUserId}/posts/${newComment.postId}`,
|
||||
notifyRecieverUserId: newComment.ownerPostUserId, notifierUserId: uid
|
||||
}))
|
||||
|
||||
}, (error) => {
|
||||
dispatch(globalActions.showErrorMessage(error.message))
|
||||
dispatch(globalActions.hideTopLoading())
|
||||
|
||||
})
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all comments from database
|
||||
*/
|
||||
export const dbGetComments = () => {
|
||||
return (dispatch, getState) => {
|
||||
var uid = getState().authorize.uid
|
||||
if (uid) {
|
||||
var commentsRef = firebaseRef.child(`postComments`);
|
||||
|
||||
return commentsRef.on('value', (snapshot) => {
|
||||
var comments = snapshot.val() || {};
|
||||
dispatch(addCommentList(comments))
|
||||
|
||||
})
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Update a comment from database
|
||||
* @param {string} id of comment
|
||||
* @param {string} postId is the identifier of the post which comment belong to
|
||||
*/
|
||||
export const dbUpdateComment = (id, postId, text) => {
|
||||
return (dispatch, getState) => {
|
||||
|
||||
dispatch(globalActions.showTopLoading())
|
||||
|
||||
|
||||
// Get current user id
|
||||
var uid = getState().authorize.uid
|
||||
|
||||
// Write the new data simultaneously in the list
|
||||
var updates = {};
|
||||
let comment = getState().comment.postComments[postId][id]
|
||||
updates[`postComments/${postId}/${id}`] = {
|
||||
postId: postId,
|
||||
score: comment.score,
|
||||
text: text,
|
||||
creationDate: comment.creationDate,
|
||||
userDisplayName: comment.userDisplayName,
|
||||
userAvatar: comment.userAvatar,
|
||||
userId: uid
|
||||
}
|
||||
return firebaseRef.update(updates).then((result) => {
|
||||
dispatch(updateComment({ id, postId, text, editorStatus: false }))
|
||||
dispatch(globalActions.hideTopLoading())
|
||||
|
||||
}, (error) => {
|
||||
dispatch(globalActions.showErrorMessage(error.message))
|
||||
dispatch(globalActions.hideTopLoading())
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete a comment from database
|
||||
* @param {string} id of comment
|
||||
* @param {string} postId is the identifier of the post which comment belong to
|
||||
*/
|
||||
export const dbDeleteComment = (id, postId) => {
|
||||
return (dispatch, getState) => {
|
||||
|
||||
dispatch(globalActions.showTopLoading())
|
||||
|
||||
|
||||
// Get current user id
|
||||
var uid = getState().authorize.uid
|
||||
|
||||
// Write the new data simultaneously in the list
|
||||
var updates = {};
|
||||
updates[`postComments/${postId}/${id}`] = null;
|
||||
|
||||
return firebaseRef.update(updates).then((result) => {
|
||||
dispatch(deleteComment(id, postId))
|
||||
dispatch(globalActions.hideTopLoading())
|
||||
|
||||
}, (error) => {
|
||||
dispatch(globalActions.showErrorMessage(error.message))
|
||||
dispatch(globalActions.hideTopLoading())
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/* _____________ CRUD State _____________ */
|
||||
|
||||
|
||||
/**
|
||||
* Add comment
|
||||
* @param {object} data
|
||||
*/
|
||||
export const addComment = (data) => {
|
||||
|
||||
return {
|
||||
type: types.ADD_COMMENT,
|
||||
payload: data
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Update comment
|
||||
* @param {object} data
|
||||
*/
|
||||
export const updateComment = (data) => {
|
||||
|
||||
return {
|
||||
type: types.UPDATE_COMMENT,
|
||||
payload: data
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Add comment list
|
||||
* @param {[object]} postComments an array of comments
|
||||
*/
|
||||
export const addCommentList = (postComments) => {
|
||||
|
||||
return {
|
||||
type: types.ADD_COMMENT_LIST,
|
||||
payload: postComments
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Delete a comment
|
||||
* @param {string} id of comment
|
||||
* @param {string} postId is the identifier of the post which comment belong to
|
||||
*/
|
||||
export const deleteComment = (id, postId) => {
|
||||
return { type: types.DELETE_COMMENT, payload: { id, postId } }
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Clear all data
|
||||
*/
|
||||
export const clearAllData = () => {
|
||||
return {
|
||||
type: types.CLEAR_ALL_DATA_COMMENT
|
||||
}
|
||||
}
|
||||
|
||||
export const openCommentEditor = (comment) => {
|
||||
|
||||
return {
|
||||
type: types.OPEN_COMMENT_EDITOR,
|
||||
payload: comment
|
||||
}
|
||||
}
|
||||
|
||||
export const closeCommentEditor = (comment) => {
|
||||
|
||||
return {
|
||||
type: types.CLOSE_COMMENT_EDITOR,
|
||||
payload: comment
|
||||
}
|
||||
}
|
||||
|
||||
232
app/actions/commentActions.ts
Normal file
232
app/actions/commentActions.ts
Normal file
@@ -0,0 +1,232 @@
|
||||
// - Import react components
|
||||
import moment from 'moment'
|
||||
import { firebaseRef } from 'app/firebase/'
|
||||
|
||||
// - Import domain
|
||||
import { Comment } from "domain/comments";
|
||||
|
||||
// - Import action types
|
||||
import {CommentActionType} from 'constants/commentActionType'
|
||||
|
||||
// - Import actions
|
||||
import * as globalActions from 'actions/globalActions'
|
||||
import * as notifyActions from 'actions/notifyActions'
|
||||
|
||||
|
||||
|
||||
/* _____________ CRUD DB _____________ */
|
||||
|
||||
/**
|
||||
* Add comment to database
|
||||
* @param {object} ownerPostUserId the identifier of the user who is the owner of the post which comment belong to
|
||||
* @param {object} newComment user comment
|
||||
* @param {function} callBack will be fired when server responsed
|
||||
*/
|
||||
export const dbAddComment = (ownerPostUserId: string,newComment: Comment, callBack: Function) => {
|
||||
return (dispatch: any, getState: Function) => {
|
||||
|
||||
dispatch(globalActions.showTopLoading())
|
||||
|
||||
let uid: string = getState().authorize.uid
|
||||
|
||||
let comment: Comment = {
|
||||
score : 0,
|
||||
creationDate : moment().unix(),
|
||||
userDisplayName : getState().user.info[uid].fullName,
|
||||
userAvatar : getState().user.info[uid].avatar,
|
||||
userId : uid,
|
||||
postId: newComment.postId,
|
||||
text: newComment.text
|
||||
}
|
||||
|
||||
let commentRef: any = firebaseRef.child(`postComments/${newComment.postId}`).push(comment)
|
||||
return commentRef.then(() => {
|
||||
dispatch(addComment(newComment))
|
||||
callBack()
|
||||
dispatch(globalActions.hideTopLoading())
|
||||
|
||||
if (ownerPostUserId && ownerPostUserId !== uid)
|
||||
dispatch(notifyActions.dbAddNotify(
|
||||
{
|
||||
description: 'Add comment on your post.',
|
||||
url: `/${ownerPostUserId}/posts/${newComment.postId}`,
|
||||
notifyRecieverUserId: ownerPostUserId, notifierUserId: uid
|
||||
}))
|
||||
|
||||
}, (error: any) => {
|
||||
dispatch(globalActions.showErrorMessage(error.message))
|
||||
dispatch(globalActions.hideTopLoading())
|
||||
|
||||
})
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all comments from database
|
||||
*/
|
||||
export const dbGetComments = () => {
|
||||
return (dispatch: any, getState: Function) => {
|
||||
let uid: string = getState().authorize.uid
|
||||
if (uid) {
|
||||
let commentsRef: any = firebaseRef.child(`postComments`);
|
||||
|
||||
return commentsRef.on('value', (snapshot: any) => {
|
||||
let comments: {[postId: string]: {[commentId: string] : Comment}} = snapshot!.val() || {};
|
||||
dispatch(addCommentList(comments))
|
||||
|
||||
})
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Update a comment from database
|
||||
* @param {string} id of comment
|
||||
* @param {string} postId is the identifier of the post which comment belong to
|
||||
* @param {string} text is the text of comment
|
||||
*/
|
||||
export const dbUpdateComment = (id: string, postId: string, text: string) => {
|
||||
return (dispatch: any, getState: Function) => {
|
||||
|
||||
dispatch(globalActions.showTopLoading())
|
||||
|
||||
|
||||
// Get current user id
|
||||
let uid: string = getState().authorize.uid
|
||||
|
||||
// Write the new data simultaneously in the list
|
||||
let updates: any = {};
|
||||
let comment: Comment = getState().comment.postComments[postId][id]
|
||||
updates[`postComments/${postId}/${id}`] = {
|
||||
postId: postId,
|
||||
score: comment.score,
|
||||
text: text,
|
||||
creationDate: comment.creationDate,
|
||||
userDisplayName: comment.userDisplayName,
|
||||
userAvatar: comment.userAvatar,
|
||||
userId: uid
|
||||
}
|
||||
return firebaseRef.update(updates).then((result) => {
|
||||
dispatch(updateComment( id, postId, text))
|
||||
dispatch(globalActions.hideTopLoading())
|
||||
|
||||
}, (error) => {
|
||||
dispatch(globalActions.showErrorMessage(error.message))
|
||||
dispatch(globalActions.hideTopLoading())
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete a comment from database
|
||||
* @param {string} id of comment
|
||||
* @param {string} postId is the identifier of the post which comment belong to
|
||||
*/
|
||||
export const dbDeleteComment = (id: string, postId: string) => {
|
||||
return (dispatch: any, getState: Function) => {
|
||||
|
||||
dispatch(globalActions.showTopLoading())
|
||||
|
||||
|
||||
// Get current user id
|
||||
let uid: string = getState().authorize.uid
|
||||
|
||||
// Write the new data simultaneously in the list
|
||||
let updates: any = {};
|
||||
updates[`postComments/${postId}/${id}`] = null;
|
||||
|
||||
return firebaseRef.update(updates).then((result) => {
|
||||
dispatch(deleteComment(id, postId))
|
||||
dispatch(globalActions.hideTopLoading())
|
||||
|
||||
}, (error) => {
|
||||
dispatch(globalActions.showErrorMessage(error.message))
|
||||
dispatch(globalActions.hideTopLoading())
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/* _____________ CRUD State _____________ */
|
||||
|
||||
|
||||
/**
|
||||
* Add comment
|
||||
* @param {Comment} data
|
||||
*/
|
||||
export const addComment = (comment:Comment) => {
|
||||
|
||||
return {
|
||||
type: CommentActionType.ADD_COMMENT,
|
||||
payload: comment
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param id comment identifier
|
||||
* @param postId post identefier which comment belong to
|
||||
* @param text the new text for comment
|
||||
*/
|
||||
export const updateComment = ( id: string, postId: string, text: string) => {
|
||||
|
||||
return {
|
||||
type: CommentActionType.UPDATE_COMMENT,
|
||||
payload: {id, postId, text}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Add comment list
|
||||
* @param {[postId: string]: {[commentId: string] : Comment}} postComments an array of comments
|
||||
*/
|
||||
export const addCommentList = (postComments: {[postId: string]: {[commentId: string] : Comment}}) => {
|
||||
|
||||
return {
|
||||
type: CommentActionType.ADD_COMMENT_LIST,
|
||||
payload: postComments
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Delete a comment
|
||||
* @param {string} id of comment
|
||||
* @param {string} postId is the identifier of the post which comment belong to
|
||||
*/
|
||||
export const deleteComment = (id: string, postId: string) => {
|
||||
return { type: CommentActionType.DELETE_COMMENT, payload: { id, postId } }
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Clear all data
|
||||
*/
|
||||
export const clearAllData = () => {
|
||||
return {
|
||||
type: CommentActionType.CLEAR_ALL_DATA_COMMENT
|
||||
}
|
||||
}
|
||||
|
||||
export const openCommentEditor = (comment: Comment) => {
|
||||
|
||||
return {
|
||||
type: CommentActionType.OPEN_COMMENT_EDITOR,
|
||||
payload: comment
|
||||
}
|
||||
}
|
||||
|
||||
export const closeCommentEditor = (comment: Comment) => {
|
||||
|
||||
return {
|
||||
type: CommentActionType.CLOSE_COMMENT_EDITOR,
|
||||
payload: comment
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,19 +1,19 @@
|
||||
// - Import image gallery action types
|
||||
import * as types from 'actionTypes'
|
||||
import {GlobalActionType} from 'constants/globalActionType'
|
||||
|
||||
// - Import actions
|
||||
import * as postActions from 'postActions'
|
||||
import * as commentActions from 'commentActions'
|
||||
import * as userActions from 'userActions'
|
||||
import * as postActions from 'actions/postActions'
|
||||
import * as commentActions from 'actions/commentActions'
|
||||
import * as userActions from 'actions/userActions'
|
||||
|
||||
/**
|
||||
* Progress change
|
||||
* @param {string} percent
|
||||
* @param {boolean} visible
|
||||
*/
|
||||
export const progressChange = (percent, visible) => {
|
||||
export const progressChange = (percent: number, visible: Boolean) => {
|
||||
return {
|
||||
type: types.PROGRESS_CHANGE,
|
||||
type: GlobalActionType.PROGRESS_CHANGE,
|
||||
payload: {percent, visible}
|
||||
}
|
||||
|
||||
@@ -24,7 +24,7 @@ export const progressChange = (percent, visible) => {
|
||||
*/
|
||||
export const defaultDataEnable = () => {
|
||||
return{
|
||||
type: types.DEFAULT_DATA_ENABLE
|
||||
type: GlobalActionType.DEFAULT_DATA_ENABLE
|
||||
}
|
||||
}
|
||||
|
||||
@@ -34,7 +34,7 @@ export const defaultDataEnable = () => {
|
||||
*/
|
||||
export const defaultDataDisable = () => {
|
||||
return{
|
||||
type: types.DEFAULT_DATA_DISABLE
|
||||
type: GlobalActionType.DEFAULT_DATA_DISABLE
|
||||
}
|
||||
}
|
||||
|
||||
@@ -42,14 +42,14 @@ export const defaultDataDisable = () => {
|
||||
// - Show notification of request
|
||||
export const showNotificationRequest = () => {
|
||||
return{
|
||||
type: types.SHOW_SEND_REQUEST_MESSAGE_GLOBAL
|
||||
type: GlobalActionType.SHOW_SEND_REQUEST_MESSAGE_GLOBAL
|
||||
}
|
||||
}
|
||||
|
||||
// - Show notification of success
|
||||
export const showNotificationSuccess = () => {
|
||||
return{
|
||||
type: types.SHOW_REQUEST_SUCCESS_MESSAGE_GLOBAL
|
||||
type: GlobalActionType.SHOW_REQUEST_SUCCESS_MESSAGE_GLOBAL
|
||||
}
|
||||
}
|
||||
|
||||
@@ -58,7 +58,7 @@ export const showNotificationSuccess = () => {
|
||||
*/
|
||||
export const hideMessage = () => {
|
||||
return{
|
||||
type: types.HIDE_MESSAGE_GLOBAL
|
||||
type: GlobalActionType.HIDE_MESSAGE_GLOBAL
|
||||
}
|
||||
|
||||
}
|
||||
@@ -67,9 +67,9 @@ export const hideMessage = () => {
|
||||
* Show error message
|
||||
* @param {string} message
|
||||
*/
|
||||
export const showErrorMessage = (message) => {
|
||||
export const showErrorMessage = (message: string) => {
|
||||
return {
|
||||
type: types.SHOW_ERROR_MESSAGE_GLOBAL,
|
||||
type: GlobalActionType.SHOW_ERROR_MESSAGE_GLOBAL,
|
||||
payload: message
|
||||
}
|
||||
|
||||
@@ -78,9 +78,9 @@ export const showErrorMessage = (message) => {
|
||||
/**
|
||||
* Set header title
|
||||
*/
|
||||
export const setHeaderTitleOpt = (opt,payload) => {
|
||||
return (dispatch,getState) => {
|
||||
switch (opt) {
|
||||
export const setHeaderTitleOpt = (callerKey: string,payload: any) => {
|
||||
return (dispatch: any,getState: Function) => {
|
||||
switch (callerKey) {
|
||||
case 'profile':
|
||||
const userName = getState().user.info && getState().user.info[payload] ? getState().user.info[payload].fullName : ''
|
||||
dispatch(setHeaderTitle(userName))
|
||||
@@ -97,9 +97,9 @@ export const setHeaderTitleOpt = (opt,payload) => {
|
||||
/**
|
||||
* Set header title
|
||||
*/
|
||||
export const setHeaderTitle = (text) => {
|
||||
export const setHeaderTitle = (text: string) => {
|
||||
return{
|
||||
type: types.SET_HEADER_TITLE,
|
||||
type: GlobalActionType.SET_HEADER_TITLE,
|
||||
payload: text
|
||||
}
|
||||
|
||||
@@ -110,7 +110,7 @@ export const setHeaderTitle = (text) => {
|
||||
*/
|
||||
export const openPostWrite = () => {
|
||||
return{
|
||||
type: types.OPEN_POST_WRITE
|
||||
type: GlobalActionType.OPEN_POST_WRITE
|
||||
}
|
||||
|
||||
}
|
||||
@@ -120,7 +120,7 @@ export const openPostWrite = () => {
|
||||
*/
|
||||
export const closePostWrite = () => {
|
||||
return{
|
||||
type: types.CLOSE_POST_WRITE
|
||||
type: GlobalActionType.CLOSE_POST_WRITE
|
||||
}
|
||||
|
||||
}
|
||||
@@ -130,7 +130,7 @@ export const closePostWrite = () => {
|
||||
*/
|
||||
export const showTopLoading = () => {
|
||||
return{
|
||||
type: types.SHOW_TOP_LOADING
|
||||
type: GlobalActionType.SHOW_TOP_LOADING
|
||||
}
|
||||
|
||||
}
|
||||
@@ -140,7 +140,7 @@ export const showTopLoading = () => {
|
||||
*/
|
||||
export const hideTopLoading = () => {
|
||||
return{
|
||||
type: types.HIDE_TOP_LOADING
|
||||
type: GlobalActionType.HIDE_TOP_LOADING
|
||||
}
|
||||
|
||||
}
|
||||
@@ -151,9 +151,9 @@ export const hideTopLoading = () => {
|
||||
/**
|
||||
* Store temp data
|
||||
*/
|
||||
export const temp = (data) => {
|
||||
export const temp = (data: string) => {
|
||||
return{
|
||||
type: types.TEMP,
|
||||
type: GlobalActionType.TEMP,
|
||||
payload: data
|
||||
}
|
||||
|
||||
@@ -163,7 +163,7 @@ export const temp = (data) => {
|
||||
|
||||
// - Load data for guest
|
||||
export const loadDataGuest = () => {
|
||||
return (dispatch,getState) => {
|
||||
return (dispatch: any,getState: Function) => {
|
||||
}
|
||||
|
||||
}
|
||||
@@ -2,49 +2,34 @@
|
||||
import moment from 'moment'
|
||||
import { firebaseRef, firebaseAuth, storageRef } from 'app/firebase/'
|
||||
|
||||
// - Import domain
|
||||
import { Image } from "domain/imageGallery";
|
||||
|
||||
// - Import action types
|
||||
import * as types from 'actionTypes'
|
||||
import {ImageGalleryActionType} from 'constants/imageGalleryActionType'
|
||||
|
||||
// - Import actions
|
||||
import * as globalActions from 'globalActions'
|
||||
import * as globalActions from 'actions/globalActions'
|
||||
|
||||
// - Import app API
|
||||
import FileAPI from 'FileAPI'
|
||||
import FileAPI from 'api/FileAPI'
|
||||
|
||||
/* _____________ UI Actions _____________ */
|
||||
|
||||
|
||||
/**
|
||||
* Clear selected data
|
||||
*/
|
||||
export const clearSelectData = () => {
|
||||
return { type: types.CLEARS_SELECT_IMAGE_GALLERY }
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Clear all data in image gallery store
|
||||
*/
|
||||
export const clearAllData = () => {
|
||||
return {
|
||||
type: types.CLEAT_ALL_DATA_IMAGE_GALLERY
|
||||
}
|
||||
}
|
||||
|
||||
declare const console: any;
|
||||
|
||||
/**
|
||||
* Download images for image gallery
|
||||
*/
|
||||
export const downloadForImageGallery = () => {
|
||||
return (dispatch, getState) => {
|
||||
var uid = getState().authorize.uid
|
||||
return (dispatch: any, getState: Function) => {
|
||||
let uid: string = getState().authorize.uid
|
||||
if (uid) {
|
||||
var imagesRef = firebaseRef.child(`userFiles/${uid}/files/images`);
|
||||
let imagesRef: any = firebaseRef.child(`userFiles/${uid}/files/images`);
|
||||
|
||||
return imagesRef.once('value').then((snapshot) => {
|
||||
return imagesRef.once('value').then((snapshot: any) => {
|
||||
var images = snapshot.val() || {};
|
||||
var parsedImages = []
|
||||
var parsedImages: Image[] = []
|
||||
Object.keys(images).forEach((imageId) => {
|
||||
parsedImages.push({
|
||||
id: imageId,
|
||||
@@ -64,18 +49,19 @@ export const downloadForImageGallery = () => {
|
||||
/**
|
||||
* Save image URL in the server
|
||||
* @param {string} imageURL is the URL of image
|
||||
* @param {string} imageFullPath is the folder name + / + file name
|
||||
*/
|
||||
export const dbSaveImage = (imageURL,imageFullPath) => {
|
||||
return (dispatch, getState) => {
|
||||
export const dbSaveImage = (imageURL: string,imageFullPath: string) => {
|
||||
return (dispatch: any, getState: Function) => {
|
||||
|
||||
var uid = getState().authorize.uid
|
||||
var image = {
|
||||
let uid: string = getState().authorize.uid
|
||||
var image: Image = {
|
||||
creationDate: moment().unix(),
|
||||
deletationDate: '',
|
||||
deleteDate: '',
|
||||
URL: imageURL,
|
||||
fullPath:imageFullPath,
|
||||
ownerUserId: uid,
|
||||
lastEditDate: '',
|
||||
lastEditDate: 0,
|
||||
deleted: false
|
||||
}
|
||||
|
||||
@@ -94,14 +80,14 @@ export const dbSaveImage = (imageURL,imageFullPath) => {
|
||||
* Delete an image from database
|
||||
* @param {string} id of image
|
||||
*/
|
||||
export const dbDeleteImage = (id) => {
|
||||
return (dispatch, getState) => {
|
||||
export const dbDeleteImage = (id: string) => {
|
||||
return (dispatch: any, getState: Function) => {
|
||||
|
||||
// Get current user id
|
||||
var uid = getState().authorize.uid
|
||||
let uid: string = getState().authorize.uid;
|
||||
|
||||
// Write the new data simultaneously in the list
|
||||
var updates = {};
|
||||
var updates: any = {};
|
||||
updates[`userFiles/${uid}/files/images/${id}`] = null;
|
||||
|
||||
return firebaseRef.update(updates).then((result) => {
|
||||
@@ -119,30 +105,30 @@ export const dbDeleteImage = (id) => {
|
||||
* @param {file} file
|
||||
* @param {string} fileName
|
||||
*/
|
||||
export const dbUploadImage = (file, fileName) => {
|
||||
return (dispatch, getState) => {
|
||||
export const dbUploadImage = (file: any, fileName: string) => {
|
||||
return (dispatch: any, getState: Function) => {
|
||||
// Create a storage refrence
|
||||
var storegeFile = storageRef.child(`images/${fileName}`)
|
||||
let storegeFile: any = storageRef.child(`images/${fileName}`)
|
||||
|
||||
// Upload file
|
||||
var task = storegeFile.put(file)
|
||||
let task: any = storegeFile.put(file)
|
||||
dispatch(globalActions.showTopLoading())
|
||||
|
||||
// Upload storage bar
|
||||
task.on('state_changed', (snapshot) => {
|
||||
task.on('state_changed', (snapshot: any) => {
|
||||
var percentage = (snapshot.bytesTransferred / snapshot.totalBytes) * 100
|
||||
dispatch(globalActions.progressChange(percentage, true))
|
||||
|
||||
|
||||
}, (error) => {
|
||||
}, (error: any) => {
|
||||
dispatch(globalActions.showErrorMessage(error.code))
|
||||
dispatch(globalActions.hideTopLoading())
|
||||
|
||||
}, (complete) => {
|
||||
}, (complete?: any ) => {
|
||||
dispatch(globalActions.progressChange(100, false))
|
||||
dispatch(dbSaveImage(fileName))
|
||||
dispatch(dbSaveImage(fileName,''))
|
||||
dispatch(globalActions.hideTopLoading())
|
||||
|
||||
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -151,9 +137,9 @@ export const dbUploadImage = (file, fileName) => {
|
||||
* Download image from server
|
||||
* @param {string} fileName
|
||||
*/
|
||||
export const dbDownloadImage = (fileName) => {
|
||||
export const dbDownloadImage = (fileName: string) => {
|
||||
|
||||
return (dispatch, getState) => {
|
||||
return (dispatch: any, getState: Function) => {
|
||||
if (fileName == 'noImage')
|
||||
return {}
|
||||
if (getState().imageGallery.imageURLList[fileName] && fileName !== '')
|
||||
@@ -164,14 +150,14 @@ export const dbDownloadImage = (fileName) => {
|
||||
dispatch(sendImageRequest(fileName))
|
||||
|
||||
// Create a reference to the file we want to download
|
||||
var starsRef = storageRef.child(`images/${fileName}`);
|
||||
let starsRef: any = storageRef.child(`images/${fileName}`);
|
||||
|
||||
// Get the download URL
|
||||
starsRef.getDownloadURL().then((url) => {
|
||||
starsRef.getDownloadURL().then((url: string) => {
|
||||
// Insert url into an <img> tag to "download"
|
||||
if (!getState().imageGallery.imageURLList[fileName] || fileName === '')
|
||||
dispatch(setImageURL(fileName, url))
|
||||
}).catch((error) => {
|
||||
}).catch((error:any) => {
|
||||
|
||||
// A full list of error codes is available at
|
||||
// https://firebase.google.com/docs/storage/web/handle-errors
|
||||
@@ -204,46 +190,55 @@ export const dbDownloadImage = (fileName) => {
|
||||
|
||||
/**
|
||||
* Add image list to image gallery
|
||||
* @param {[object]} images is an array of images
|
||||
* @param {Image[]} images is an array of images
|
||||
*/
|
||||
export const addImageList = (images) => {
|
||||
return { type: types.ADD_IMAGE_LIST_GALLERY, images }
|
||||
export const addImageList = (images: Image[]) => {
|
||||
return { type: ImageGalleryActionType.ADD_IMAGE_LIST_GALLERY,payload: images }
|
||||
}
|
||||
|
||||
/**
|
||||
* Add image to image gallery
|
||||
* @param {object} image
|
||||
* @param {Image} image
|
||||
*/
|
||||
export const addImage = (image) => {
|
||||
return { type: types.ADD_IMAGE_GALLERY, image }
|
||||
export const addImage = (image: Image) => {
|
||||
return { type: ImageGalleryActionType.ADD_IMAGE_GALLERY, payload: image }
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete an image
|
||||
* @param {string} id is an image identifier
|
||||
*/
|
||||
export const deleteImage = (id) => {
|
||||
return { type: types.DELETE_IMAGE, id }
|
||||
export const deleteImage = (id: string) => {
|
||||
return { type: ImageGalleryActionType.DELETE_IMAGE, payload: id }
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete an image
|
||||
*/
|
||||
export const setImageURL = (name, url) => {
|
||||
export const setImageURL = (name: string, url: string) => {
|
||||
return {
|
||||
type: types.SET_IMAGE_URL,
|
||||
type: ImageGalleryActionType.SET_IMAGE_URL,
|
||||
payload: { name, url }
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Clear all data in image gallery store
|
||||
*/
|
||||
export const clearAllData = () => {
|
||||
return {
|
||||
type: ImageGalleryActionType.CLEAT_ALL_DATA_IMAGE_GALLERY
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Send image request
|
||||
*/
|
||||
export const sendImageRequest = (name) => {
|
||||
export const sendImageRequest = (name: string) => {
|
||||
return {
|
||||
type: types.SEND_IMAGE_REQUEST,
|
||||
type: ImageGalleryActionType.SEND_IMAGE_REQUEST,
|
||||
payload: name
|
||||
}
|
||||
|
||||
@@ -2,12 +2,15 @@
|
||||
import moment from 'moment'
|
||||
import { firebaseRef } from 'app/firebase/'
|
||||
|
||||
// - Import domain
|
||||
import { Notification } from "domain/notifications";
|
||||
|
||||
// - Import action types
|
||||
import * as types from 'actionTypes'
|
||||
import {NotificationActionType} from 'constants/notificationActionType'
|
||||
|
||||
// - Import actions
|
||||
import * as globalActions from 'globalActions'
|
||||
import * as userActions from 'userActions'
|
||||
import * as globalActions from 'actions/globalActions'
|
||||
import * as userActions from 'actions/userActions'
|
||||
|
||||
|
||||
/* _____________ CRUD DB _____________ */
|
||||
@@ -16,21 +19,23 @@ import * as userActions from 'userActions'
|
||||
* Add notificaition to database
|
||||
* @param {object} newNotify user notificaition
|
||||
*/
|
||||
export const dbAddNotify = (newNotify) => {
|
||||
return (dispatch, getState) => {
|
||||
export const dbAddNotify = (newNotify: Notification) => {
|
||||
return (dispatch: any, getState: Function) => {
|
||||
|
||||
var uid = getState().authorize.uid
|
||||
var notify = {
|
||||
let uid: string = getState().authorize.uid
|
||||
|
||||
let notify: Notification = {
|
||||
isSeen: false,
|
||||
description: newNotify.description,
|
||||
url: newNotify.url,
|
||||
notifierUserId: newNotify.notifierUserId,
|
||||
isSeen: false
|
||||
notifyRecieverUserId: newNotify.notifyRecieverUserId
|
||||
}
|
||||
|
||||
var notifyRef = firebaseRef.child(`userNotifies/${newNotify.notifyRecieverUserId}`).push(notify)
|
||||
let notifyRef: any = firebaseRef.child(`userNotifies/${newNotify.notifyRecieverUserId}`).push(notify)
|
||||
return notifyRef.then(() => {
|
||||
dispatch(addNotify())
|
||||
}, (error) => dispatch(globalActions.showErrorMessage(error.message)))
|
||||
}, (error: any) => dispatch(globalActions.showErrorMessage(error.message)))
|
||||
|
||||
}
|
||||
}
|
||||
@@ -39,17 +44,17 @@ export const dbAddNotify = (newNotify) => {
|
||||
* Get all notificaitions from database
|
||||
*/
|
||||
export const dbGetNotifies = () => {
|
||||
return (dispatch, getState) => {
|
||||
var uid = getState().authorize.uid
|
||||
return (dispatch: any, getState: Function) => {
|
||||
let uid: string = getState().authorize.uid
|
||||
if (uid) {
|
||||
var notifiesRef = firebaseRef.child(`userNotifies/${uid}`);
|
||||
let notifiesRef: any = firebaseRef.child(`userNotifies/${uid}`);
|
||||
|
||||
return notifiesRef.on('value', (snapshot) => {
|
||||
var notifies = snapshot.val() || {};
|
||||
return notifiesRef.on('value', (snapshot: any) => {
|
||||
let notifies: {[notifyId: string]: Notification} = snapshot.val() || {};
|
||||
|
||||
Object.keys(notifies).forEach((key => {
|
||||
if (!getState().user.info[notifies[key].notifierUserId]) {
|
||||
dispatch(userActions.dbGetUserInfoByUserId(notifies[key].notifierUserId))
|
||||
dispatch(userActions.dbGetUserInfoByUserId(notifies[key].notifierUserId,''))
|
||||
|
||||
}
|
||||
}))
|
||||
@@ -65,14 +70,14 @@ export const dbGetNotifies = () => {
|
||||
* Delete a notificaition from database
|
||||
* @param {string} id of notificaition
|
||||
*/
|
||||
export const dbDeleteNotify = (id) => {
|
||||
return (dispatch, getState) => {
|
||||
export const dbDeleteNotify = (id: string) => {
|
||||
return (dispatch: any, getState: Function) => {
|
||||
|
||||
// Get current user id
|
||||
var uid = getState().authorize.uid
|
||||
var uid: string = getState().authorize.uid
|
||||
|
||||
// Write the new data simultaneously in the list
|
||||
var updates = {};
|
||||
var updates: any = {};
|
||||
updates[`userNotifies/${uid}/${id}`] = null;
|
||||
|
||||
return firebaseRef.update(updates).then((result) => {
|
||||
@@ -88,21 +93,21 @@ export const dbDeleteNotify = (id) => {
|
||||
* Make seen a notificaition from database
|
||||
* @param {string} id of notificaition
|
||||
*/
|
||||
export const dbSeenNotify = (id) => {
|
||||
return (dispatch, getState) => {
|
||||
export const dbSeenNotify = (id: string) => {
|
||||
return (dispatch: any, getState: Function) => {
|
||||
|
||||
// Get current user id
|
||||
var uid = getState().authorize.uid
|
||||
let notify = getState().notify.userNotifies[id]
|
||||
let updatedNotify = {
|
||||
let uid: string = getState().authorize.uid
|
||||
let notify: Notification = getState().notify.userNotifies[id]
|
||||
|
||||
|
||||
let updates: any = {};
|
||||
updates[`userNotifies/${uid}/${id}`] = {
|
||||
description: notify.description,
|
||||
url: notify.url,
|
||||
notifierUserId: notify.notifierUserId,
|
||||
isSeen: true
|
||||
}
|
||||
// Write the new data simultaneously in the list
|
||||
var updates = {};
|
||||
updates[`userNotifies/${uid}/${id}`] = updatedNotify;
|
||||
|
||||
return firebaseRef.update(updates).then((result) => {
|
||||
dispatch(seenNotify(id))
|
||||
@@ -116,23 +121,22 @@ export const dbSeenNotify = (id) => {
|
||||
|
||||
/**
|
||||
* Add notificaition
|
||||
* @param {object} data
|
||||
*/
|
||||
export const addNotify = () => {
|
||||
|
||||
return {
|
||||
type: types.ADD_NOTIFY
|
||||
type: NotificationActionType.ADD_NOTIFY
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Add notificaition list
|
||||
* @param {[object]} userNotifies an array of notificaitions
|
||||
* @param {[notifyId: string]: Notification} userNotifies an array of notificaitions
|
||||
*/
|
||||
export const addNotifyList = (userNotifies) => {
|
||||
export const addNotifyList = (userNotifies: {[notifyId: string]: Notification}) => {
|
||||
|
||||
return {
|
||||
type: types.ADD_NOTIFY_LIST,
|
||||
type: NotificationActionType.ADD_NOTIFY_LIST,
|
||||
payload: userNotifies
|
||||
}
|
||||
}
|
||||
@@ -142,8 +146,8 @@ export const addNotifyList = (userNotifies) => {
|
||||
* Delete a notificaition
|
||||
* @param {string} id of notificaition
|
||||
*/
|
||||
export const deleteNotify = (id) => {
|
||||
return { type: types.DELETE_NOTIFY, payload: id }
|
||||
export const deleteNotify = (id: string) => {
|
||||
return { type: NotificationActionType.DELETE_NOTIFY, payload: id }
|
||||
|
||||
}
|
||||
|
||||
@@ -151,8 +155,8 @@ export const deleteNotify = (id) => {
|
||||
* Change notificaition to has seen status
|
||||
* @param {string} id of notificaition
|
||||
*/
|
||||
export const seenNotify = (id) => {
|
||||
return { type: types.SEEN_NOTIFY, payload: id }
|
||||
export const seenNotify = (id: string) => {
|
||||
return { type: NotificationActionType.SEEN_NOTIFY, payload: id }
|
||||
|
||||
}
|
||||
|
||||
@@ -161,7 +165,7 @@ export const seenNotify = (id) => {
|
||||
*/
|
||||
export const clearAllNotifications = () => {
|
||||
return {
|
||||
type: types.CLEAR_ALL_DATA_NOTIFY
|
||||
type: NotificationActionType.CLEAR_ALL_DATA_NOTIFY
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,6 +4,9 @@ import { Action } from "redux";
|
||||
// - Import firebase component
|
||||
import firebase, { firebaseRef } from '../firebase';
|
||||
|
||||
// - Import domain
|
||||
import { Post } from "domain/posts";
|
||||
|
||||
// - Import utility components
|
||||
import moment from 'moment';
|
||||
|
||||
@@ -13,7 +16,7 @@ import { PostActionType } from 'constants/postActionType';
|
||||
// - Import actions
|
||||
import * as globalActions from 'actions/globalActions';
|
||||
|
||||
|
||||
declare const console: any;
|
||||
|
||||
|
||||
|
||||
@@ -23,27 +26,28 @@ import * as globalActions from 'actions/globalActions';
|
||||
|
||||
/**
|
||||
* Add a normal post
|
||||
* @param {object} newPost
|
||||
* @param {function} callBack
|
||||
* @param {any} newPost
|
||||
* @param {Function} callBack
|
||||
*/
|
||||
export var dbAddPost = (newPost: any, callBack: any) => {
|
||||
return (dispatch: any, getState: any) => {
|
||||
export var dbAddPost = (newPost: any, callBack: Function) => {
|
||||
return (dispatch: any, getState: Function) => {
|
||||
|
||||
var uid = getState().authorize.uid
|
||||
var post = {
|
||||
let uid: string = getState().authorize.uid
|
||||
let post: Post = {
|
||||
postTypeId: 0,
|
||||
creationDate: moment().unix(),
|
||||
deletationDate: '',
|
||||
deleteDate: 0,
|
||||
score: 0,
|
||||
viewCount: 0,
|
||||
body: newPost.body,
|
||||
ownerUserId: uid,
|
||||
ownerDisplayName: newPost.name,
|
||||
ownerAvatar: newPost.avatar,
|
||||
lastEditDate: '',
|
||||
lastEditDate: 0,
|
||||
tags: newPost.tags || [],
|
||||
commentCounter: 0,
|
||||
image: '',
|
||||
imageFullPath:'',
|
||||
video: '',
|
||||
disableComments: newPost.disableComments,
|
||||
disableSharing: newPost.disableSharing,
|
||||
@@ -51,7 +55,7 @@ export var dbAddPost = (newPost: any, callBack: any) => {
|
||||
}
|
||||
|
||||
|
||||
var postRef = firebaseRef.child(`userPosts/${uid}/posts`).push(post)
|
||||
let postRef: any = firebaseRef.child(`userPosts/${uid}/posts`).push(post)
|
||||
return postRef.then(() => {
|
||||
dispatch(addPost(uid, {
|
||||
...post,
|
||||
@@ -68,23 +72,23 @@ export var dbAddPost = (newPost: any, callBack: any) => {
|
||||
* @param {object} newPost
|
||||
* @param {function} callBack
|
||||
*/
|
||||
export const dbAddImagePost = (newPost: any, callBack: any) => {
|
||||
return (dispatch: any, getState: any) => {
|
||||
export const dbAddImagePost = (newPost: any, callBack: Function) => {
|
||||
return (dispatch: any, getState: Function) => {
|
||||
|
||||
dispatch(globalActions.showTopLoading())
|
||||
|
||||
var uid = getState().authorize.uid
|
||||
var post = {
|
||||
let uid: string = getState().authorize.uid
|
||||
let post: Post = {
|
||||
postTypeId: 1,
|
||||
creationDate: moment().unix(),
|
||||
deletationDate: '',
|
||||
deleteDate: 0,
|
||||
score: 0,
|
||||
viewCount: 0,
|
||||
body: newPost.body,
|
||||
ownerUserId: uid,
|
||||
ownerDisplayName: newPost.name,
|
||||
ownerAvatar: newPost.avatar,
|
||||
lastEditDate: '',
|
||||
lastEditDate: 0,
|
||||
tags: newPost.tags || [],
|
||||
commentCounter: 0,
|
||||
image: newPost.image || '',
|
||||
@@ -115,9 +119,9 @@ export const dbAddImagePost = (newPost: any, callBack: any) => {
|
||||
* @param {object} newPost
|
||||
* @param {func} callBack //TODO: anti pattern should change to parent state or move state to redux
|
||||
*/
|
||||
export const dbUpdatePost = (newPost: any, callBack: any) => {
|
||||
export const dbUpdatePost = (newPost: any, callBack: Function) => {
|
||||
console.log(newPost)
|
||||
return (dispatch : any, getState: any) => {
|
||||
return (dispatch : any, getState: Function) => {
|
||||
|
||||
dispatch(globalActions.showTopLoading())
|
||||
|
||||
@@ -126,11 +130,11 @@ export const dbUpdatePost = (newPost: any, callBack: any) => {
|
||||
|
||||
// Write the new data simultaneously in the list
|
||||
let updates: any = {};
|
||||
let post = getState().post.userPosts[uid][newPost.id]
|
||||
let updatedPost = {
|
||||
let post: Post = getState().post.userPosts[uid][newPost.id]
|
||||
let updatedPost: Post = {
|
||||
postTypeId: post.postTypeId,
|
||||
creationDate: post.creationDate,
|
||||
deletationDate: '',
|
||||
deleteDate: 0,
|
||||
score: post.score,
|
||||
viewCount: post.viewCount,
|
||||
body: newPost.body ? newPost.body : post.body || '',
|
||||
@@ -141,6 +145,7 @@ export const dbUpdatePost = (newPost: any, callBack: any) => {
|
||||
tags: newPost.tags ? newPost.tags : (post.tags || []),
|
||||
commentCounter: post.commentCounter,
|
||||
image: newPost.image ? newPost.image : post.image,
|
||||
imageFullPath: newPost.imageFullPath!,
|
||||
video: '',
|
||||
disableComments: newPost.disableComments !== undefined ? newPost.disableComments : (post.disableComments ? post.disableComments : false),
|
||||
disableSharing: newPost.disableSharing !== undefined ? newPost.disableSharing : (post.disableSharing ? post.disableSharing : false),
|
||||
@@ -167,7 +172,7 @@ export const dbUpdatePost = (newPost: any, callBack: any) => {
|
||||
* @param {string} id is post identifier
|
||||
*/
|
||||
export const dbDeletePost = (id: string) => {
|
||||
return (dispatch:any, getState:any) => {
|
||||
return (dispatch:any, getState:Function) => {
|
||||
|
||||
dispatch(globalActions.showTopLoading())
|
||||
|
||||
@@ -175,7 +180,7 @@ export const dbDeletePost = (id: string) => {
|
||||
let uid : string = getState().authorize.uid
|
||||
|
||||
// Write the new data simultaneously in the list
|
||||
var updates : any= {};
|
||||
let updates : any= {};
|
||||
updates[`userPosts/${uid}/posts/${id}`] = null;
|
||||
|
||||
return firebaseRef.update(updates).then((result) => {
|
||||
@@ -191,17 +196,17 @@ export const dbDeletePost = (id: string) => {
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all user posts from data base
|
||||
* Get all user posts from data base
|
||||
*/
|
||||
export const dbGetPosts = () => {
|
||||
return (dispatch:any, getState:any) => {
|
||||
var uid = getState().authorize.uid
|
||||
let uid: string = getState().authorize.uid
|
||||
if (uid) {
|
||||
var postsRef = firebaseRef.child(`userPosts/${uid}/posts`);
|
||||
let postsRef: any = firebaseRef.child(`userPosts/${uid}/posts`);
|
||||
|
||||
return postsRef.once('value').then((snapshot) => {
|
||||
var posts = snapshot.val() || {};
|
||||
var parsedPosts : any = {};
|
||||
return postsRef.once('value').then((snapshot: any) => {
|
||||
var posts: any = snapshot.val() || {};
|
||||
var parsedPosts : {[postId: string]: Post} = {};
|
||||
Object.keys(posts).forEach((postId) => {
|
||||
parsedPosts[postId] = {
|
||||
id: postId,
|
||||
@@ -217,14 +222,16 @@ export const dbGetPosts = () => {
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all user posts from data base
|
||||
*/
|
||||
* Get all user posts from data base
|
||||
* @param uid post owner identifier
|
||||
* @param postId post identifier
|
||||
*/
|
||||
export const dbGetPostById = (uid:string, postId:string) => {
|
||||
return (dispatch: any, getState: any) => {
|
||||
return (dispatch: any, getState: Function) => {
|
||||
if (uid) {
|
||||
var postsRef = firebaseRef.child(`userPosts/${uid}/posts/${postId}`);
|
||||
let postsRef: any = firebaseRef.child(`userPosts/${uid}/posts/${postId}`);
|
||||
|
||||
return postsRef.once('value').then((snapshot) => {
|
||||
return postsRef.once('value').then((snapshot: any) => {
|
||||
const newPost = snapshot.val() || {};
|
||||
const post = {
|
||||
id: postId,
|
||||
@@ -239,17 +246,18 @@ export const dbGetPostById = (uid:string, postId:string) => {
|
||||
|
||||
|
||||
/**
|
||||
* Get all user posts from data base by user id
|
||||
* Get all user posts from data base by user id
|
||||
* @param uid posts owner identifier
|
||||
*/
|
||||
export const dbGetPostsByUserId = (uid: string) => {
|
||||
return (dispatch: any, getState: any) => {
|
||||
return (dispatch: any, getState: Function) => {
|
||||
|
||||
if (uid) {
|
||||
let postsRef = firebaseRef.child(`userPosts/${uid}/posts`);
|
||||
let postsRef: any = firebaseRef.child(`userPosts/${uid}/posts`);
|
||||
|
||||
return postsRef.once('value').then((snapshot) => {
|
||||
let posts = snapshot.val() || {};
|
||||
let parsedPosts: any = {};
|
||||
return postsRef.once('value').then((snapshot: any) => {
|
||||
let posts: any = snapshot.val() || {};
|
||||
let parsedPosts: {[postId: string]: Post} = {};
|
||||
Object.keys(posts).forEach((postId) => {
|
||||
parsedPosts[postId] = {
|
||||
id: postId,
|
||||
@@ -274,9 +282,9 @@ export const dbGetPostsByUserId = (uid: string) => {
|
||||
/**
|
||||
* Add a normal post
|
||||
* @param {string} uid is user identifier
|
||||
* @param {object} post
|
||||
* @param {Post} post
|
||||
*/
|
||||
export const addPost = (uid: string, post: any) => {
|
||||
export const addPost = (uid: string, post: Post) => {
|
||||
return {
|
||||
type: PostActionType.ADD_POST,
|
||||
payload: { uid, post }
|
||||
@@ -286,9 +294,9 @@ export const addPost = (uid: string, post: any) => {
|
||||
/**
|
||||
* Update a post
|
||||
* @param {string} uid is user identifier
|
||||
* @param {object} post
|
||||
* @param {Post} post
|
||||
*/
|
||||
export const updatePost = (uid: string, post: any) => {
|
||||
export const updatePost = (uid: string, post: Post) => {
|
||||
return {
|
||||
type: PostActionType.UPDATE_POST,
|
||||
payload: { uid, post }
|
||||
@@ -313,7 +321,7 @@ export const deletePost = (uid: string, id: string) => {
|
||||
* @param {string} uid
|
||||
* @param {[object]} posts
|
||||
*/
|
||||
export const addPosts = (uid: string, posts: any) => {
|
||||
export const addPosts = (uid: string, posts: {[postId: string]: Post}) => {
|
||||
return {
|
||||
type: PostActionType.ADD_LIST_POST,
|
||||
payload: { uid, posts }
|
||||
|
||||
@@ -1,210 +0,0 @@
|
||||
// - Import react components
|
||||
import { firebaseRef } from 'app/firebase/'
|
||||
|
||||
// - Import action types
|
||||
import * as types from 'actionTypes'
|
||||
|
||||
// - Import actions
|
||||
import * as globalActions from 'globalActions'
|
||||
import * as userActions from 'userActions'
|
||||
|
||||
|
||||
|
||||
/* _____________ CRUD DB _____________ */
|
||||
|
||||
|
||||
/**
|
||||
* Get user info from database
|
||||
*/
|
||||
export const dbGetUserInfo = () => {
|
||||
return (dispatch, getState) => {
|
||||
var uid = getState().authorize.uid
|
||||
if (uid) {
|
||||
var userInfoRef = firebaseRef.child(`users/${uid}/info`);
|
||||
|
||||
return userInfoRef.once('value').then((snapshot) => {
|
||||
var userInfo = snapshot.val() || {};
|
||||
dispatch(addUserInfo(uid, {
|
||||
avatar: userInfo.avatar,
|
||||
email: userInfo.email,
|
||||
fullName: userInfo.fullName,
|
||||
banner: userInfo.banner,
|
||||
tagLine: userInfo.tagLine
|
||||
}))
|
||||
}, error => console.log(error));
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get user info from database
|
||||
* @param {string} uid
|
||||
*/
|
||||
export const dbGetUserInfoByUserId = (uid, sw) => {
|
||||
return (dispatch, getState) => {
|
||||
if (uid) {
|
||||
var userInfoRef = firebaseRef.child(`users/${uid}/info`);
|
||||
|
||||
return userInfoRef.once('value').then((snapshot) => {
|
||||
var userInfo = snapshot.val() || {};
|
||||
dispatch(addUserInfo(uid, {
|
||||
avatar: userInfo.avatar,
|
||||
email: userInfo.email,
|
||||
fullName: userInfo.fullName,
|
||||
banner: userInfo.banner,
|
||||
tagLine: userInfo.tagLine
|
||||
}))
|
||||
switch (sw) {
|
||||
case 'header':
|
||||
dispatch(globalActions.setHeaderTitle(userInfo.fullName))
|
||||
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}, error => console.log(error));
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Updata user information
|
||||
* @param {object} newInfo
|
||||
*/
|
||||
export const dbUpdateUserInfo = (newInfo) => {
|
||||
return (dispatch, getState) => {
|
||||
|
||||
// Get current user id
|
||||
var uid = getState().authorize.uid
|
||||
|
||||
// Write the new data simultaneously in the list
|
||||
let updates = {};
|
||||
let info = getState().user.info[uid]
|
||||
let updatedInfo = {
|
||||
avatar: newInfo.avatar || info.avatar || '',
|
||||
banner: newInfo.banner || info.banner || 'https://firebasestorage.googleapis.com/v0/b/open-social-33d92.appspot.com/o/images%2F751145a1-9488-46fd-a97e-04018665a6d3.JPG?alt=media&token=1a1d5e21-5101-450e-9054-ea4a20e06c57',
|
||||
email: newInfo.email || info.email || '',
|
||||
fullName: newInfo.fullName || info.fullName || '',
|
||||
tagLine: newInfo.tagLine || info.tagLine || ''
|
||||
}
|
||||
updates[`users/${uid}/info`] = updatedInfo
|
||||
return firebaseRef.update(updates).then((result) => {
|
||||
|
||||
dispatch(updateUserInfo(uid, updatedInfo))
|
||||
dispatch(closeEditProfile())
|
||||
}, (error) => {
|
||||
dispatch(globalActions.showErrorMessage(error.message))
|
||||
})
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// - Get people info from database
|
||||
export const dbGetPeopleInfo = () => {
|
||||
return (dispatch, getState) => {
|
||||
var uid = getState().authorize.uid
|
||||
if (uid) {
|
||||
var peopleRef = firebaseRef.child(`users`);
|
||||
|
||||
return peopleRef.once('value').then((snapshot) => {
|
||||
let people = snapshot.val() || {};
|
||||
|
||||
let parsedPeople = {};
|
||||
Object.keys(people).forEach((userId) => {
|
||||
if (userId !== uid) {
|
||||
let userInfo = people[userId].info
|
||||
parsedPeople[userId] = {
|
||||
avatar: userInfo.avatar,
|
||||
email: userInfo.email,
|
||||
fullName: userInfo.fullName,
|
||||
banner: userInfo.banner,
|
||||
tagLine: userInfo.tagLine
|
||||
}
|
||||
}
|
||||
|
||||
})
|
||||
dispatch(addPeopleInfo(parsedPeople))
|
||||
}, error => console.log(error));
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* _____________ CRUD State _____________ */
|
||||
|
||||
|
||||
/**
|
||||
* Add user information
|
||||
* @param {string} uid is the user identifier
|
||||
* @param {object} info is the information about user
|
||||
*/
|
||||
export const addUserInfo = (uid, info) => {
|
||||
return {
|
||||
type: types.ADD_USER_INFO,
|
||||
payload: { uid, info }
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Add people information
|
||||
* @param {[object]} infoList is the lst of information about users
|
||||
*/
|
||||
export const addPeopleInfo = (infoList) => {
|
||||
return {
|
||||
type: types.ADD_PEOPLE_INFO,
|
||||
payload: infoList
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Update user information
|
||||
* @param {string} uid is the user identifier
|
||||
* @param {object} info is the information about user
|
||||
*/
|
||||
export const updateUserInfo = (uid, info) => {
|
||||
return {
|
||||
type: types.UPDATE_USER_INFO,
|
||||
payload: { uid, info }
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* User info
|
||||
* @param {object} info
|
||||
*/
|
||||
export const userInfo = (info) => {
|
||||
return {
|
||||
type: types.USER_INFO,
|
||||
info
|
||||
}
|
||||
}
|
||||
|
||||
export const clearAllData = () => {
|
||||
return {
|
||||
type: types.CLEAR_ALL_DATA_USER
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Open edit profile
|
||||
*/
|
||||
export const openEditProfile = () => {
|
||||
return {
|
||||
type: types.OPEN_EDIT_PROFILE
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Close edit profile
|
||||
*/
|
||||
export const closeEditProfile = () => {
|
||||
return {
|
||||
type: types.CLOSE_EDIT_PROFILE
|
||||
}
|
||||
|
||||
}
|
||||
215
app/actions/userActions.ts
Normal file
215
app/actions/userActions.ts
Normal file
@@ -0,0 +1,215 @@
|
||||
// - Import react components
|
||||
import { firebaseRef } from 'app/firebase/'
|
||||
|
||||
// - Import domain
|
||||
import { Profile } from "domain/users";
|
||||
|
||||
// - Import action types
|
||||
import {UserActionType} from 'constants/userActionType'
|
||||
|
||||
// - Import actions
|
||||
import * as globalActions from 'actions/globalActions'
|
||||
import * as userActions from 'actions/userActions'
|
||||
|
||||
declare const console: any;
|
||||
|
||||
/* _____________ CRUD DB _____________ */
|
||||
|
||||
|
||||
/**
|
||||
* Get user info from database
|
||||
*/
|
||||
export const dbGetUserInfo = () => {
|
||||
return (dispatch: any, getState: Function) => {
|
||||
let uid: string = getState().authorize.uid
|
||||
if (uid) {
|
||||
let userProfileRef: any = firebaseRef.child(`users/${uid}/info`);
|
||||
|
||||
return userProfileRef.once('value').then((snapshot: any) => {
|
||||
let userProfile: Profile = snapshot.val() || {};
|
||||
|
||||
dispatch(addUserInfo(uid, {
|
||||
avatar: userProfile.avatar,
|
||||
email: userProfile.email,
|
||||
fullName: userProfile.fullName,
|
||||
banner: userProfile.banner,
|
||||
tagLine: userProfile.tagLine
|
||||
}))
|
||||
}, (error: any) => console.log(error));
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get user info from database
|
||||
* @param {string} uid
|
||||
* @param {string} callerKey
|
||||
*/
|
||||
export const dbGetUserInfoByUserId = (uid: string, callerKey: string) => {
|
||||
return (dispatch: any, getState: Function) => {
|
||||
if (uid) {
|
||||
let userProfileRef: any = firebaseRef.child(`users/${uid}/info`);
|
||||
|
||||
return userProfileRef.once('value').then((snapshot: any) => {
|
||||
let userProfile = snapshot.val() || {};
|
||||
dispatch(addUserInfo(uid, {
|
||||
avatar: userProfile.avatar,
|
||||
email: userProfile.email,
|
||||
fullName: userProfile.fullName,
|
||||
banner: userProfile.banner,
|
||||
tagLine: userProfile.tagLine
|
||||
}))
|
||||
switch (callerKey) {
|
||||
case 'header':
|
||||
dispatch(globalActions.setHeaderTitle(userProfile.fullName))
|
||||
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}, (error: any) => console.log(error));
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Updata user information
|
||||
* @param {object} newInfo
|
||||
*/
|
||||
export const dbUpdateUserInfo = (newProfile: Profile) => {
|
||||
return (dispatch: any, getState: Function) => {
|
||||
|
||||
// Get current user id
|
||||
let uid: string = getState().authorize.uid
|
||||
|
||||
// Write the new data simultaneously in the list
|
||||
let updates: any = {};
|
||||
let profile = getState().user.info[uid]
|
||||
let updateProfie: Profile = {
|
||||
avatar: newProfile.avatar || profile.avatar || '',
|
||||
banner: newProfile.banner || profile.banner || 'https://firebasestorage.googleapis.com/v0/b/open-social-33d92.appspot.com/o/images%2F751145a1-9488-46fd-a97e-04018665a6d3.JPG?alt=media&token=1a1d5e21-5101-450e-9054-ea4a20e06c57',
|
||||
email: newProfile.email || profile.email || '',
|
||||
fullName: newProfile.fullName || profile.fullName || '',
|
||||
tagLine: newProfile.tagLine || profile.tagLine || ''
|
||||
}
|
||||
updates[`users/${uid}/info`] = updateProfie
|
||||
return firebaseRef.update(updates).then((result) => {
|
||||
|
||||
dispatch(updateUserInfo(uid, updateProfie))
|
||||
dispatch(closeEditProfile())
|
||||
}, (error) => {
|
||||
dispatch(globalActions.showErrorMessage(error.message))
|
||||
})
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// - Get people info from database
|
||||
export const dbGetPeopleInfo = () => {
|
||||
return (dispatch: any, getState: Function) => {
|
||||
let uid: string = getState().authorize.uid
|
||||
if (uid) {
|
||||
let peopleRef: any = firebaseRef.child(`users`);
|
||||
|
||||
return peopleRef.once('value').then((snapshot: any) => {
|
||||
let people = snapshot.val() || {};
|
||||
|
||||
let parsedPeople: {[userId: string]: Profile} = {};
|
||||
Object.keys(people).forEach((userId) => {
|
||||
if (userId !== uid) {
|
||||
let userInfo = people[userId].info
|
||||
parsedPeople[userId] = {
|
||||
avatar: userInfo.avatar,
|
||||
email: userInfo.email,
|
||||
fullName: userInfo.fullName,
|
||||
banner: userInfo.banner,
|
||||
tagLine: userInfo.tagLine
|
||||
}
|
||||
}
|
||||
|
||||
})
|
||||
dispatch(addPeopleInfo(parsedPeople))
|
||||
}, (error: any) => console.log(error));
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* _____________ CRUD State _____________ */
|
||||
|
||||
|
||||
/**
|
||||
* Add user information
|
||||
* @param {string} uid is the user identifier
|
||||
* @param {Profile} info is the information about user
|
||||
*/
|
||||
export const addUserInfo = (uid: string, info: Profile) => {
|
||||
return {
|
||||
type: UserActionType.ADD_USER_INFO,
|
||||
payload: { uid, info }
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Add people information
|
||||
* @param {[userId: string]: Profile} infoList is the lst of information about users
|
||||
*/
|
||||
export const addPeopleInfo = (infoList: {[userId: string]: Profile}) => {
|
||||
return {
|
||||
type: UserActionType.ADD_PEOPLE_INFO,
|
||||
payload: infoList
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Update user information
|
||||
* @param {string} uid is the user identifier
|
||||
* @param {Profile} info is the information about user
|
||||
*/
|
||||
export const updateUserInfo = (uid: string, info: Profile) => {
|
||||
return {
|
||||
type: UserActionType.UPDATE_USER_INFO,
|
||||
payload: { uid, info }
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* User info
|
||||
* @param {Profile} info
|
||||
*/
|
||||
export const userInfo = (info: Profile) => {
|
||||
return {
|
||||
type: UserActionType.USER_INFO,
|
||||
info
|
||||
}
|
||||
}
|
||||
|
||||
export const clearAllData = () => {
|
||||
return {
|
||||
type: UserActionType.CLEAR_ALL_DATA_USER
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Open edit profile
|
||||
*/
|
||||
export const openEditProfile = () => {
|
||||
return {
|
||||
type: UserActionType.OPEN_EDIT_PROFILE
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Close edit profile
|
||||
*/
|
||||
export const closeEditProfile = () => {
|
||||
return {
|
||||
type: UserActionType.CLOSE_EDIT_PROFILE
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,130 +0,0 @@
|
||||
// - Import react components
|
||||
import {createAction as action} from 'redux-actions'
|
||||
import moment from 'moment'
|
||||
import { firebaseRef } from 'app/firebase/'
|
||||
|
||||
// - Import action types
|
||||
import * as types from 'actionTypes'
|
||||
|
||||
// - Import actions
|
||||
import * as globalActions from 'globalActions'
|
||||
import * as notifyActions from 'notifyActions'
|
||||
|
||||
|
||||
/* _____________ CRUD DB _____________ */
|
||||
|
||||
/**
|
||||
* Add vote to database
|
||||
* @param {string} postId is the identifier of the post which user vote
|
||||
*/
|
||||
export const dbAddVote = (postId,ownerPostUserId) => {
|
||||
return (dispatch, getState) => {
|
||||
|
||||
var uid = getState().authorize.uid
|
||||
var vote = {
|
||||
postId: postId,
|
||||
creationDate: moment().unix(),
|
||||
userDisplayName: getState().user.info[uid].fullName,
|
||||
userAvatar: getState().user.info[uid].avatar,
|
||||
userId: uid
|
||||
}
|
||||
|
||||
var voteRef = firebaseRef.child(`postVotes/${postId}`).push(vote)
|
||||
return voteRef.then(() => {
|
||||
dispatch(addVote(
|
||||
{
|
||||
vote,
|
||||
postId: postId,
|
||||
id: voteRef.key
|
||||
}))
|
||||
if(uid !== ownerPostUserId)
|
||||
dispatch(notifyActions.dbAddNotify(
|
||||
{
|
||||
description:'Vote on your post.',
|
||||
url:`/${ownerPostUserId}/posts/${postId}`,
|
||||
notifyRecieverUserId:ownerPostUserId,notifierUserId:uid
|
||||
}))
|
||||
|
||||
}, (error) => dispatch(globalActions.showErrorMessage(error.message)))
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all votes from database
|
||||
*/
|
||||
export const dbGetVotes = () => {
|
||||
return (dispatch, getState) => {
|
||||
var uid = getState().authorize.uid
|
||||
if (uid) {
|
||||
var votesRef = firebaseRef.child(`postVotes`);
|
||||
|
||||
return votesRef.on('value',(snapshot) => {
|
||||
var votes = snapshot.val() || {};
|
||||
dispatch(addVoteList(votes))
|
||||
})
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Delete a vote from database
|
||||
* @param {string} id of vote
|
||||
* @param {string} postId is the identifier of the post which vote belong to
|
||||
*/
|
||||
export const dbDeleteVote = (postId) => {
|
||||
return (dispatch, getState) => {
|
||||
|
||||
// Get current user id
|
||||
var uid = getState().authorize.uid
|
||||
|
||||
// Write the new data simultaneously in the list
|
||||
var updates = {};
|
||||
let votes = getState().vote.postVotes[postId]
|
||||
let id = Object.keys(votes).filter((key)=> votes[key].userId === uid)[0]
|
||||
console.log(' Id : ',id)
|
||||
|
||||
updates[`postVotes/${postId}/${id}`] = null;
|
||||
|
||||
return firebaseRef.update(updates).then((result) => {
|
||||
dispatch(deleteVote({id, postId}))
|
||||
}, (error) => dispatch(globalActions.showErrorMessage(error.message)))
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a vote
|
||||
* @param {object} data
|
||||
*/
|
||||
export const addVote = (data) => {
|
||||
return { type: types.ADD_VOTE, payload: data }
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* delete a vote
|
||||
* @param {object} data
|
||||
*/
|
||||
export const deleteVote = (data) => {
|
||||
return { type: types.DELETE_VOTE, payload: data }
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Ad a list of vote
|
||||
* @param {object} data
|
||||
*/
|
||||
export const addVoteList = (data) => {
|
||||
return { type: types.ADD_VOTE_LIST, payload: data }
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Clear all data
|
||||
*/
|
||||
export const clearAllvotes = () => {
|
||||
return { type: types.CLEAR_ALL_DATA_VOTE }
|
||||
}
|
||||
135
app/actions/voteActions.ts
Normal file
135
app/actions/voteActions.ts
Normal file
@@ -0,0 +1,135 @@
|
||||
import moment from 'moment'
|
||||
import { firebaseRef } from 'app/firebase/'
|
||||
|
||||
// - Import action types
|
||||
import {VoteActionType} from 'constants/voteActionType'
|
||||
|
||||
// - Import domain
|
||||
import { Vote } from "domain/votes";
|
||||
|
||||
// - Import actions
|
||||
import * as globalActions from 'actions/globalActions'
|
||||
import * as notifyActions from 'actions/notifyActions'
|
||||
|
||||
declare const console: any;
|
||||
/* _____________ CRUD DB _____________ */
|
||||
|
||||
/**
|
||||
* Add vote to database
|
||||
* @param {string} postId is the identifier of the post which user vote
|
||||
* @param {string} ownerPostUserId is the identifier of the post owner which user vote
|
||||
*/
|
||||
export const dbAddVote = (postId: string,ownerPostUserId: string) => {
|
||||
return (dispatch: any, getState: Function) => {
|
||||
|
||||
let uid: string = getState().authorize.uid;
|
||||
var vote: Vote = {
|
||||
postId: postId,
|
||||
creationDate: moment().unix(),
|
||||
userDisplayName: getState().user.info[uid].fullName,
|
||||
userAvatar: getState().user.info[uid].avatar,
|
||||
userId: uid
|
||||
}
|
||||
|
||||
var voteRef = firebaseRef.child(`postVotes/${postId}`).push(vote)
|
||||
return voteRef.then(() => {
|
||||
dispatch(addVote(
|
||||
{
|
||||
...vote,
|
||||
postId: postId,
|
||||
id: voteRef.key
|
||||
}))
|
||||
if(uid !== ownerPostUserId)
|
||||
dispatch(notifyActions.dbAddNotify(
|
||||
{
|
||||
description:'Vote on your post.',
|
||||
url:`/${ownerPostUserId}/posts/${postId}`,
|
||||
notifyRecieverUserId:ownerPostUserId,notifierUserId:uid,
|
||||
isSeen:false
|
||||
}))
|
||||
|
||||
}, (error) => dispatch(globalActions.showErrorMessage(error.message)))
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all votes from database
|
||||
*/
|
||||
export const dbGetVotes = () => {
|
||||
return (dispatch: any, getState: Function) => {
|
||||
let uid: string = getState().authorize.uid
|
||||
if (uid) {
|
||||
let votesRef: any = firebaseRef.child(`postVotes`);
|
||||
|
||||
return votesRef.on('value',(snapshot: any) => {
|
||||
let postVotes: {[postId:string]: {[voteId: string]: Vote}} = snapshot.val() || {};
|
||||
dispatch(addVoteList(postVotes))
|
||||
})
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Delete a vote from database
|
||||
* @param {string} id of vote
|
||||
* @param {string} postId is the identifier of the post which vote belong to
|
||||
*/
|
||||
export const dbDeleteVote = (postId: string) => {
|
||||
return (dispatch: any, getState: Function) => {
|
||||
|
||||
// Get current user id
|
||||
let uid: string = getState().authorize.uid;
|
||||
|
||||
// Write the new data simultaneously in the list
|
||||
var updates: any = {};
|
||||
let votes: {[voteId: string]: Vote} = getState().vote.postVotes[postId]
|
||||
let id: string = Object.keys(votes).filter((key)=> votes[key].userId === uid)[0]
|
||||
|
||||
|
||||
updates[`postVotes/${postId}/${id}`] = null;
|
||||
|
||||
return firebaseRef.update(updates).then((result) => {
|
||||
dispatch(deleteVote(id, postId))
|
||||
})
|
||||
.catch((error: any) => dispatch(globalActions.showErrorMessage(error.message)))
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a vote
|
||||
* @param {Vote} vote
|
||||
*/
|
||||
export const addVote = (vote: Vote) => {
|
||||
return { type: VoteActionType.ADD_VOTE, payload: vote }
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* delete a vote
|
||||
* @param {string} id vote identifier
|
||||
* @param {string} postId post identifier which vote on
|
||||
*/
|
||||
export const deleteVote = (id: string, postId: string) => {
|
||||
return { type: VoteActionType.DELETE_VOTE, payload: {id, postId} }
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Ad a list of vote
|
||||
* @param {[postId:string]: {[voteId: string]: Vote}} votes a list of vote
|
||||
*/
|
||||
export const addVoteList = (votes: {[postId:string]: {[voteId: string]: Vote}}) => {
|
||||
return { type: VoteActionType.ADD_VOTE_LIST, payload: votes }
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Clear all data
|
||||
*/
|
||||
export const clearAllvotes = () => {
|
||||
return { type: VoteActionType.CLEAR_ALL_DATA_VOTE }
|
||||
}
|
||||
Reference in New Issue
Block a user