Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
108 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import { createAction, handleActions } from "redux-actions"; | ||
import { produce } from "immer"; | ||
import { firestore } from "../../shared/firebase"; | ||
import "moment"; | ||
import moment from "moment"; | ||
|
||
const initialState = { | ||
list: {}, | ||
is_loading: false, | ||
}; | ||
|
||
const SET_COMMENT = "SET_COMMENT"; | ||
const ADD_COMMENT = "ADD_COMMENT"; | ||
const LOADING = "LOADING"; | ||
|
||
const setComment = createAction(SET_COMMENT, (post_id, comment_list) => ({ | ||
post_id, | ||
comment_list, | ||
})); | ||
const addComment = createAction(ADD_COMMENT, (post_id, comment) => ({ | ||
post_id, | ||
comment, | ||
})); | ||
|
||
const loading = createAction(LOADING, (is_loading) => ({ is_loading })); | ||
|
||
const getCommentFB = (post_id) => { | ||
return function (dispatch, getState, { history }) { | ||
if (!post_id) { | ||
return; | ||
} | ||
|
||
const commentDB = firestore.collection("comment"); | ||
|
||
commentDB | ||
.where("post_id", "==", post_id) | ||
.orderBy("insert_dt", "desc") | ||
.get() | ||
.then((docs) => { | ||
let list = []; | ||
|
||
docs.forEach((doc) => { | ||
list.push({ ...doc.data(), id: doc.id }); | ||
}); | ||
// console.log(list); | ||
dispatch(setComment(post_id, list)); | ||
}) | ||
.catch((err) => { | ||
console.log("댓글 정보를 가져오다가 실패했는디요?"); | ||
}); | ||
}; | ||
}; | ||
|
||
export default handleActions( | ||
{ | ||
[SET_COMMENT]: (state, action) => | ||
produce(state, (draft) => { | ||
draft.list[action.payload.post_id] = action.payload.comment_list; | ||
// comment들을 담는 list를 딕셔너리 형으로 만들고 | ||
// post_id를 key, 해당 post의 comment들(딕셔너리)을 배열 형태로 담은 comment_list를 value로 할당. | ||
}), | ||
[ADD_COMMENT]: (state, action) => produce(state, (draft) => {}), | ||
[LOADING]: (state, action) => | ||
produce(state, (draft) => { | ||
draft.is_loading = action.payload.is_loading; | ||
}), | ||
}, | ||
initialState | ||
); | ||
|
||
const actionCreators = { | ||
getCommentFB, | ||
setComment, | ||
addComment, | ||
}; | ||
|
||
export { actionCreators }; |