Skip to content
This repository has been archived by the owner on Jul 2, 2022. It is now read-only.

Commit

Permalink
[Improvement] Move get comments action to saga (#48)
Browse files Browse the repository at this point in the history
  • Loading branch information
Qolzam committed Mar 24, 2018
1 parent c883265 commit 4c40069
Show file tree
Hide file tree
Showing 17 changed files with 233 additions and 76 deletions.
26 changes: 25 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -98,5 +98,29 @@
"engines": {
"node": "8.9.4",
"npm": "5.6.0"
}
},
"directories": {
"doc": "docs"
},
"repository": {
"type": "git",
"url": "git+https://github.com/Qolzam/react-social-network.git"
},
"keywords": [
"react",
"redux",
"saga",
"redux-saga",
"router",
"react-router",
"firebase",
"social",
"media",
"app",
"mobile"
],
"bugs": {
"url": "https://github.com/Qolzam/react-social-network/issues"
},
"homepage": "https://github.com/Qolzam/react-social-network#readme"
}
79 changes: 14 additions & 65 deletions src/actions/commentActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import StringAPI from 'api/StringAPI'
import { ServerRequestType } from 'constants/serverRequestType'
import { ServerRequestModel } from 'models/server'
import { ServerRequestStatusType } from 'actions/serverRequestStatusType'
import CommentAPI from 'api/CommentAPI'

/**
* Get service providers
Expand All @@ -43,18 +44,18 @@ export const dbAddComment = (ownerPostUserId: string, newComment: Comment, callB
let uid: string = state.authorize.uid

let comment: Comment = {
score : 0,
creationDate : moment().unix(),
userDisplayName : state.user.info[uid].fullName,
userAvatar : state.user.info[uid].avatar,
userId : uid,
score: 0,
creationDate: moment().unix(),
userDisplayName: state.user.info[uid].fullName,
userAvatar: state.user.info[uid].avatar,
userId: uid,
postId: newComment.postId,
text: newComment.text
}

return commentService.addComment(comment)
.then((commentKey: string) => {
dispatch(addComment({id: commentKey! ,...comment}))
dispatch(addComment({ id: commentKey!, ...comment }))
callBack()
dispatch(globalActions.hideTopLoading())

Expand All @@ -79,49 +80,10 @@ export const dbAddComment = (ownerPostUserId: string, newComment: Comment, callB
/**
* Get all comments from database
*/
export const dbGetComments = (ownerUserId: string, postId: string) => {
return (dispatch: any, getState: Function) => {
const state = getState()
let uid: string = getState().authorize.uid
if (uid) {
// Set server request status to {Sent}
const getCommentsRequest = createGetCommentsRequest(postId)
dispatch(serverActions.sendRequest(getCommentsRequest))

return commentService.getComments(postId, (comments: {[postId: string]: {[commentId: string]: Comment}}) => {

// Set server request status to {OK}
getCommentsRequest.status = ServerRequestStatusType.OK
dispatch(serverActions.sendRequest(getCommentsRequest))

/**
* Workout getting the number of post's comment and getting three last comments
*/
dispatch(addCommentList(comments))
let commentsCount: number
const post: Post = state.post.userPosts[ownerUserId][postId]
if (!post) {
return
}

const desiredComments = comments[postId]
if (desiredComments) {
commentsCount = Object.keys(desiredComments).length
let sortedObjects = desiredComments as any
// Sort posts with creation date

const commentKeys = Object.keys(sortedObjects)
if (commentKeys.length > 1) {
sortedObjects = _.fromPairs(_.toPairs(sortedObjects)
.sort((a: any, b: any) => parseInt(b[1].creationDate,10) - parseInt(a[1].creationDate,10)).slice(0, 3))

}
post.comments = sortedObjects
post.commentCounter = commentsCount
dispatch(postActions.updatePost(post))
}
})
}
export const dbFetchComments = (ownerUserId: string, postId: string) => {
return {
type: CommentActionType.DB_FETCH_COMMENTS,
payload: {postId, ownerUserId}
}
}

Expand All @@ -134,7 +96,7 @@ export const dbUpdateComment = (comment: Comment) => {

return commentService.updateComment(comment)
.then(() => {
dispatch(updateComment( comment))
dispatch(updateComment(comment))
dispatch(closeCommentEditor(comment))
dispatch(globalActions.hideTopLoading())

Expand Down Expand Up @@ -171,19 +133,6 @@ export const dbDeleteComment = (id?: string | null, postId?: string) => {

}

/**
* Create get comments server request model
*/
const createGetCommentsRequest = (postId: string) => {
const requestId = StringAPI.createServerRequestId(ServerRequestType.CommentGetComments, postId)
return new ServerRequestModel(
ServerRequestType.CommentGetComments,
requestId,
'',
ServerRequestStatusType.Sent
)
}

/* _____________ CRUD State _____________ */

/**
Expand All @@ -201,7 +150,7 @@ export const addComment = (comment: Comment) => {
/**
* Update comment
*/
export const updateComment = ( comment: Comment) => {
export const updateComment = (comment: Comment) => {

return {
type: CommentActionType.UPDATE_COMMENT,
Expand All @@ -213,7 +162,7 @@ export const updateComment = ( comment: Comment) => {
* Add comment list
* @param {[postId: string]: {[commentId: string] : Comment}} postComments an array of comments
*/
export const addCommentList = (postComments: {[postId: string]: {[commentId: string]: Comment}}) => {
export const addCommentList = (postComments: { [postId: string]: { [commentId: string]: Comment } }) => {

return {
type: CommentActionType.ADD_COMMENT_LIST,
Expand Down
4 changes: 3 additions & 1 deletion src/actions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import * as postActions from './postActions'
import * as userActions from './userActions'
import * as voteActions from './voteActions'
import * as localeActions from './localeActions'
import * as serverActions from './serverActions'

export {
authorizeActions,
Expand All @@ -19,5 +20,6 @@ export {
postActions,
userActions,
voteActions,
localeActions
localeActions,
serverActions
}
33 changes: 33 additions & 0 deletions src/api/CommentAPI.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import StringAPI from 'api/StringAPI'
import { ServerRequestType } from 'constants/serverRequestType'
import { ServerRequestModel } from 'models/server'
import { ServerRequestStatusType } from 'actions/serverRequestStatusType'
import { comments } from 'models/comments/commentTypes'
import * as _ from 'lodash'

/**
* Create get comments server request model
*/
const createGetCommentsRequest = (postId: string) => {
const requestId = StringAPI.createServerRequestId(ServerRequestType.CommentGetComments, postId)
return new ServerRequestModel(
ServerRequestType.CommentGetComments,
requestId,
'',
ServerRequestStatusType.Sent
)
}

const sortCommentsByDate = (sortedObjects: comments) => {
const commentKeys = Object.keys(sortedObjects)
if (commentKeys.length > 1) {
return _.fromPairs(_.toPairs(sortedObjects)
.sort((a: any, b: any) => parseInt(b[1].creationDate, 10) - parseInt(a[1].creationDate, 10)).slice(0, 3))

}
}

export default {
createGetCommentsRequest,
sortCommentsByDate
}
10 changes: 9 additions & 1 deletion src/api/CommonAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,15 @@ const getRandomColor = () => {
return color
}

const updateObject = (oldObject: any, updatedProperties: any) => {
return {
...oldObject,
...updatedProperties
}
}

export default {
logger,
getRandomColor
getRandomColor,
updateObject
}
2 changes: 1 addition & 1 deletion src/components/post/PostComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,7 @@ const mapDispatchToProps = (dispatch: any, ownProps: IPostComponentProps) => {
},
goTo: (url: string) => dispatch(push(url)),
setHomeTitle: (title: string) => dispatch(globalActions.setHeaderTitle(title || '')),
getPostComments: (ownerUserId: string, postId: string) => dispatch(commentActions.dbGetComments(ownerUserId, postId))
getPostComments: (ownerUserId: string, postId: string) => dispatch(commentActions.dbFetchComments(ownerUserId, postId))

}
}
Expand Down
1 change: 1 addition & 0 deletions src/constants/commentActionType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ export enum CommentActionType {
UPDATE_COMMENT = 'UPDATE_COMMENT',
CLOSE_COMMENT_EDITOR = 'CLOSE_COMMENT_EDITOR',
OPEN_COMMENT_EDITOR = 'OPEN_COMMENT_EDITOR',
DB_FETCH_COMMENTS = 'DB_FETCH_COMMENTS',
}
3 changes: 2 additions & 1 deletion src/core/services/comments/ICommentService.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { User } from 'core/domain/users'

import { Comment } from 'core/domain/comments'
import { postComments } from 'models/comments/commentTypes'

/**
* Comment service interface
Expand All @@ -11,7 +12,7 @@ import { Comment } from 'core/domain/comments'
export interface ICommentService {

addComment: (comment: Comment) => Promise<string>
getComments: (postId: string, next: (resultComments: { [postId: string]: { [commentId: string]: Comment } }) => void) => void
getComments: (postId: string, next: (resultComments: postComments) => void) => () => void
updateComment: (comment: Comment) => Promise<void>
deleteComment: (commentId: string) => Promise<void>

Expand Down
8 changes: 5 additions & 3 deletions src/data/firestoreClient/services/comments/CommentService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { SocialError } from 'core/domain/common'
import { ICommentService } from 'core/services/comments'
import { Comment } from 'core/domain/comments'
import { injectable } from 'inversify'
import { postComments } from 'models/comments/commentTypes'

/**
* Firbase comment service
Expand Down Expand Up @@ -40,10 +41,10 @@ export class CommentService implements ICommentService {
*
* @memberof CommentService
*/
public getComments: (postId: string, next: (resultComments: { [postId: string]: { [commentId: string]: Comment } }) => void)
=> void = (postId, next) => {
public getComments: (postId: string, next: (resultComments: postComments) => void)
=> () => void = (postId, next) => {
let commentsRef = db.collection(`comments`).where('postId', '==', postId)
commentsRef.onSnapshot((snapshot) => {
const unsubscribe = commentsRef.onSnapshot((snapshot) => {
let parsedData: {[postId: string]: {[commentId: string]: Comment}} = {[postId]: {}}
snapshot.forEach((result) => {
parsedData[postId][result.id] = {
Expand All @@ -55,6 +56,7 @@ export class CommentService implements ICommentService {
next(parsedData)
}
})
return unsubscribe
}

/**
Expand Down
3 changes: 3 additions & 0 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ import './styles/app.css'
* Execute startup functions
*/
import './socialEngine'
import rootSaga from 'sagas/rootSaga'

configureStore.runSaga(rootSaga)

const supportsHistory = 'pushState' in window.history
ReactDOM.render(
Expand Down
3 changes: 3 additions & 0 deletions src/models/comments/commentTypes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import {Comment} from 'core/domain/comments'
export type postComments = {[postId: string]: {[commentId: string]: Comment}}
export type comments = {[commentId: string]: Comment}
5 changes: 5 additions & 0 deletions src/reducers/authorize/authorizeSelector.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const getCurrentUser = (state: any) => (state.user.info && state.authorize.uid) ? state.user.info[state.authorize.uid] : null

export const authorizeSelector = {
getCurrentUser
}
7 changes: 5 additions & 2 deletions src/reducers/authorize/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import { authorizeReducer } from './authorizeReducer'

export {authorizeReducer}
import {authorizeSelector} from './authorizeSelector'
export {
authorizeReducer,
authorizeSelector
}
6 changes: 5 additions & 1 deletion src/reducers/posts/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import { postReducer } from './postReducer'
import { postSelector } from './postSelector'

export {postReducer}
export {
postReducer,
postSelector
}
9 changes: 9 additions & 0 deletions src/reducers/posts/postSelector.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const getPost = (state: any, userId: string, postId: string) => {
return (state.post.userPosts && state.post.userPosts[userId] && state.post.userPosts[userId][postId])
? state.post.userPosts[userId][postId]
: null
}

export const postSelector = {
getPost
}
Loading

0 comments on commit 4c40069

Please sign in to comment.