mirror of
https://github.com/muerwre/vault-frontend.git
synced 2025-04-25 12:56:41 +07:00
comment form
This commit is contained in:
parent
9531edcd19
commit
1990783fa3
9 changed files with 67 additions and 33 deletions
|
@ -33,7 +33,8 @@ export const nodeSetCurrent = (current: INodeState['current']) => ({
|
|||
type: NODE_ACTIONS.SET_CURRENT,
|
||||
});
|
||||
|
||||
export const nodePostComment = () => ({
|
||||
export const nodePostComment = (id: number) => ({
|
||||
id,
|
||||
type: NODE_ACTIONS.POST_COMMENT,
|
||||
});
|
||||
|
||||
|
@ -47,7 +48,8 @@ export const nodeSetComments = (comments: IComment[]) => ({
|
|||
type: NODE_ACTIONS.SET_COMMENTS,
|
||||
});
|
||||
|
||||
export const nodeSetCommentData = (comment_data: IComment) => ({
|
||||
comment_data,
|
||||
export const nodeSetCommentData = (id: number, comment: IComment) => ({
|
||||
id,
|
||||
comment,
|
||||
type: NODE_ACTIONS.SET_COMMENT_DATA,
|
||||
});
|
||||
|
|
|
@ -35,8 +35,8 @@ const setComments = (state: INodeState, { comments }: ReturnType<typeof nodeSetC
|
|||
|
||||
const setCommentData = (
|
||||
state: INodeState,
|
||||
{ comment_data }: ReturnType<typeof nodeSetCommentData>
|
||||
) => assocPath(['comment_data'], comment_data, state);
|
||||
{ id, comment }: ReturnType<typeof nodeSetCommentData>
|
||||
) => assocPath(['comment_data', id], comment, state);
|
||||
|
||||
export const NODE_HANDLERS = {
|
||||
[NODE_ACTIONS.SAVE]: setSaveErrors,
|
||||
|
|
|
@ -7,7 +7,7 @@ export type INodeState = Readonly<{
|
|||
editor: INode;
|
||||
current: INode;
|
||||
comments: IComment[];
|
||||
comment_data: IComment;
|
||||
comment_data: Record<number, IComment>;
|
||||
|
||||
error: string;
|
||||
errors: Record<string, string>;
|
||||
|
@ -25,7 +25,7 @@ const INITIAL_STATE: INodeState = {
|
|||
files: [],
|
||||
},
|
||||
current: { ...EMPTY_NODE },
|
||||
comment_data: { ...EMPTY_COMMENT },
|
||||
comment_data: { 0: { ...EMPTY_COMMENT } },
|
||||
comments: [],
|
||||
|
||||
is_loading: false,
|
||||
|
|
|
@ -47,6 +47,7 @@ function* onNodeLoad({ id, node_type }: ReturnType<typeof nodeLoadNode>) {
|
|||
yield put(nodeSetLoading(true));
|
||||
yield put(nodeSetLoadingComments(true));
|
||||
yield put(nodeSetSaveErrors({}));
|
||||
yield put(nodeSetCommentData(0, { ...EMPTY_COMMENT }));
|
||||
|
||||
if (node_type) yield put(nodeSetCurrent({ ...EMPTY_NODE, type: node_type }));
|
||||
|
||||
|
@ -77,26 +78,28 @@ function* onNodeLoad({ id, node_type }: ReturnType<typeof nodeLoadNode>) {
|
|||
return;
|
||||
}
|
||||
|
||||
function* onPostComment() {
|
||||
function* onPostComment({ id }: ReturnType<typeof nodePostComment>) {
|
||||
const { current, comment_data } = yield select(selectNode);
|
||||
|
||||
yield put(nodeSetSendingComment(true));
|
||||
const {
|
||||
data: { comment },
|
||||
data: { comment, id: target_id },
|
||||
error,
|
||||
} = yield call(reqWrapper, postNodeComment, { data: comment_data, id: current.id });
|
||||
} = yield call(reqWrapper, postNodeComment, { data: comment_data[id], id: current.id });
|
||||
yield put(nodeSetSendingComment(false));
|
||||
|
||||
if (error || !comment) {
|
||||
return yield put(nodeSetSaveErrors({ error: error || ERRORS.EMPTY_RESPONSE }));
|
||||
}
|
||||
|
||||
console.log({ comment });
|
||||
const { current: current_node } = yield select(selectNode);
|
||||
|
||||
const { comments } = yield select(selectNode);
|
||||
|
||||
yield put(nodeSetComments([comment, ...comments]));
|
||||
yield put(nodeSetCommentData({ ...EMPTY_COMMENT }));
|
||||
if (current_node && current_node.id === current.id) {
|
||||
// if user still browsing that node
|
||||
const { comments } = yield select(selectNode);
|
||||
yield put(nodeSetComments([comment, ...comments]));
|
||||
yield put(nodeSetCommentData(0, { ...EMPTY_COMMENT }));
|
||||
}
|
||||
}
|
||||
|
||||
export default function* nodeSaga() {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue