mirror of
https://github.com/muerwre/vault-frontend.git
synced 2025-04-25 12:56:41 +07:00
removed almost all node sagas
This commit is contained in:
parent
f76a5a4798
commit
168ba8cc04
30 changed files with 268 additions and 448 deletions
|
@ -1,67 +1,7 @@
|
|||
import { IComment, IFile, INode, ITag, IValidationErrors } from '../types';
|
||||
import { IFile } from '../types';
|
||||
import { NODE_ACTIONS } from './constants';
|
||||
import { INodeState } from './reducer';
|
||||
|
||||
export const nodeSet = (node: Partial<INodeState>) => ({
|
||||
node,
|
||||
type: NODE_ACTIONS.SET,
|
||||
});
|
||||
|
||||
export const nodeGotoNode = (id: INode['id'], node_type: INode['type']) => ({
|
||||
id,
|
||||
node_type,
|
||||
type: NODE_ACTIONS.GOTO_NODE,
|
||||
});
|
||||
|
||||
export const nodeLoadNode = (id: number, order?: 'ASC' | 'DESC') => ({
|
||||
id,
|
||||
order,
|
||||
type: NODE_ACTIONS.LOAD_NODE,
|
||||
});
|
||||
|
||||
export const nodeSetLoading = (is_loading: INodeState['is_loading']) => ({
|
||||
is_loading,
|
||||
type: NODE_ACTIONS.SET_LOADING,
|
||||
});
|
||||
|
||||
export const nodeSetLoadingComments = (is_loading_comments: INodeState['is_loading_comments']) => ({
|
||||
is_loading_comments,
|
||||
type: NODE_ACTIONS.SET_LOADING_COMMENTS,
|
||||
});
|
||||
|
||||
export const nodePostLocalComment = (
|
||||
nodeId: INode['id'],
|
||||
comment: IComment,
|
||||
callback: (e?: string) => void
|
||||
) => ({
|
||||
nodeId,
|
||||
comment,
|
||||
callback,
|
||||
type: NODE_ACTIONS.POST_LOCAL_COMMENT,
|
||||
});
|
||||
|
||||
export const nodeSetSendingComment = (is_sending_comment: boolean) => ({
|
||||
is_sending_comment,
|
||||
type: NODE_ACTIONS.SET_SENDING_COMMENT,
|
||||
});
|
||||
|
||||
export const nodeSetComments = (comments: IComment[]) => ({
|
||||
comments,
|
||||
type: NODE_ACTIONS.SET_COMMENTS,
|
||||
});
|
||||
|
||||
export const nodeLockComment = (id: number, is_locked: boolean, nodeId: number) => ({
|
||||
type: NODE_ACTIONS.LOCK_COMMENT,
|
||||
nodeId,
|
||||
id,
|
||||
is_locked,
|
||||
});
|
||||
|
||||
export const nodeSetCoverImage = (current_cover_image?: IFile) => ({
|
||||
type: NODE_ACTIONS.SET_COVER_IMAGE,
|
||||
current_cover_image,
|
||||
});
|
||||
|
||||
export const nodeLoadMoreComments = () => ({
|
||||
type: NODE_ACTIONS.LOAD_MORE_COMMENTS,
|
||||
});
|
||||
|
|
|
@ -117,7 +117,7 @@ export const apiLockNode = ({ id, is_locked }: ApiLockNodeRequest) =>
|
|||
.post<ApiLockNodeResult>(API.NODE.POST_LOCK(id), { is_locked })
|
||||
.then(cleanResult);
|
||||
|
||||
export const apiLockComment = ({ id, is_locked, current }: ApiLockCommentRequest) =>
|
||||
export const apiLockComment = ({ id, isLocked, nodeId }: ApiLockCommentRequest) =>
|
||||
api
|
||||
.post<ApiLockcommentResult>(API.NODE.LOCK_COMMENT(current, id), { is_locked })
|
||||
.post<ApiLockcommentResult>(API.NODE.LOCK_COMMENT(nodeId, id), { is_locked: isLocked })
|
||||
.then(cleanResult);
|
||||
|
|
|
@ -25,21 +25,6 @@ import { LabAudio } from '~/components/lab/LabAudioBlock';
|
|||
|
||||
const prefix = 'NODE.';
|
||||
export const NODE_ACTIONS = {
|
||||
LOAD_NODE: `${prefix}LOAD_NODE`,
|
||||
GOTO_NODE: `${prefix}GOTO_NODE`,
|
||||
SET: `${prefix}SET`,
|
||||
|
||||
LOCK_COMMENT: `${prefix}LOCK_COMMENT`,
|
||||
EDIT_COMMENT: `${prefix}EDIT_COMMENT`,
|
||||
LOAD_MORE_COMMENTS: `${prefix}LOAD_MORE_COMMENTS`,
|
||||
|
||||
SET_LOADING: `${prefix}SET_LOADING`,
|
||||
SET_LOADING_COMMENTS: `${prefix}SET_LOADING_COMMENTS`,
|
||||
SET_SENDING_COMMENT: `${prefix}SET_SENDING_COMMENT`,
|
||||
|
||||
POST_LOCAL_COMMENT: `${prefix}POST_LOCAL_COMMENT`,
|
||||
SET_COMMENTS: `${prefix}SET_COMMENTS`,
|
||||
|
||||
SET_COVER_IMAGE: `${prefix}SET_COVER_IMAGE`,
|
||||
};
|
||||
|
||||
|
|
|
@ -1,46 +1,13 @@
|
|||
import { assocPath } from 'ramda';
|
||||
import { NODE_ACTIONS } from './constants';
|
||||
import {
|
||||
nodeSet,
|
||||
nodeSetComments,
|
||||
nodeSetCoverImage,
|
||||
nodeSetLoading,
|
||||
nodeSetLoadingComments,
|
||||
nodeSetSendingComment,
|
||||
} from './actions';
|
||||
import { nodeSetCoverImage } from './actions';
|
||||
import { INodeState } from './reducer';
|
||||
|
||||
const setData = (state: INodeState, { node }: ReturnType<typeof nodeSet>) => ({
|
||||
...state,
|
||||
...node,
|
||||
});
|
||||
|
||||
const setLoading = (state: INodeState, { is_loading }: ReturnType<typeof nodeSetLoading>) =>
|
||||
assocPath(['is_loading'], is_loading, state);
|
||||
|
||||
const setLoadingComments = (
|
||||
state: INodeState,
|
||||
{ is_loading_comments }: ReturnType<typeof nodeSetLoadingComments>
|
||||
) => assocPath(['is_loading_comments'], is_loading_comments, state);
|
||||
|
||||
const setSendingComment = (
|
||||
state: INodeState,
|
||||
{ is_sending_comment }: ReturnType<typeof nodeSetSendingComment>
|
||||
) => assocPath(['is_sending_comment'], is_sending_comment, state);
|
||||
|
||||
const setComments = (state: INodeState, { comments }: ReturnType<typeof nodeSetComments>) =>
|
||||
assocPath(['comments'], comments, state);
|
||||
|
||||
const setCoverImage = (
|
||||
state: INodeState,
|
||||
{ current_cover_image }: ReturnType<typeof nodeSetCoverImage>
|
||||
) => assocPath(['current_cover_image'], current_cover_image, state);
|
||||
|
||||
export const NODE_HANDLERS = {
|
||||
[NODE_ACTIONS.SET]: setData,
|
||||
[NODE_ACTIONS.SET_LOADING]: setLoading,
|
||||
[NODE_ACTIONS.SET_LOADING_COMMENTS]: setLoadingComments,
|
||||
[NODE_ACTIONS.SET_SENDING_COMMENT]: setSendingComment,
|
||||
[NODE_ACTIONS.SET_COMMENTS]: setComments,
|
||||
[NODE_ACTIONS.SET_COVER_IMAGE]: setCoverImage,
|
||||
};
|
||||
|
|
|
@ -1,163 +0,0 @@
|
|||
import { call, put, select, takeLatest, takeLeading } from 'redux-saga/effects';
|
||||
|
||||
import { COMMENTS_DISPLAY, EMPTY_NODE, NODE_ACTIONS } from './constants';
|
||||
import {
|
||||
nodeGotoNode,
|
||||
nodeLoadNode,
|
||||
nodeLockComment,
|
||||
nodePostLocalComment,
|
||||
nodeSet,
|
||||
nodeSetComments,
|
||||
nodeSetLoadingComments,
|
||||
} from './actions';
|
||||
import { apiGetNodeComments, apiLockComment, apiPostComment } from './api';
|
||||
import { flowSetNodes } from '../flow/actions';
|
||||
import { selectFlowNodes } from '../flow/selectors';
|
||||
import { selectNode } from './selectors';
|
||||
import { INode, Unwrap } from '../types';
|
||||
import { showErrorToast } from '~/utils/errors/showToast';
|
||||
|
||||
export function* updateNodeEverywhere(node) {
|
||||
const {
|
||||
current: { id },
|
||||
}: ReturnType<typeof selectNode> = yield select(selectNode);
|
||||
|
||||
const flow_nodes: ReturnType<typeof selectFlowNodes> = yield select(selectFlowNodes);
|
||||
|
||||
yield put(
|
||||
flowSetNodes(
|
||||
flow_nodes
|
||||
.map(flow_node => (flow_node.id === node.id ? node : flow_node))
|
||||
.filter(flow_node => !flow_node.deleted_at)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
function* onNodeGoto({ id }: ReturnType<typeof nodeGotoNode>) {
|
||||
if (!id) {
|
||||
return;
|
||||
}
|
||||
|
||||
yield put(nodeLoadNode(id));
|
||||
}
|
||||
|
||||
function* onNodeLoadMoreComments() {
|
||||
try {
|
||||
const {
|
||||
current: { id },
|
||||
comments,
|
||||
}: ReturnType<typeof selectNode> = yield select(selectNode);
|
||||
|
||||
if (!id) {
|
||||
return;
|
||||
}
|
||||
|
||||
const data: Unwrap<typeof apiGetNodeComments> = yield call(apiGetNodeComments, {
|
||||
id,
|
||||
take: COMMENTS_DISPLAY,
|
||||
skip: comments.length,
|
||||
});
|
||||
|
||||
const current: ReturnType<typeof selectNode> = yield select(selectNode);
|
||||
|
||||
if (!data || current.current.id != id) {
|
||||
return;
|
||||
}
|
||||
|
||||
yield put(
|
||||
nodeSet({
|
||||
comments: [...comments, ...data.comments],
|
||||
comment_count: data.comment_count,
|
||||
})
|
||||
);
|
||||
} catch (error) {}
|
||||
}
|
||||
|
||||
function* nodeGetComments(id: INode['id']) {
|
||||
try {
|
||||
const { comments, comment_count }: Unwrap<typeof apiGetNodeComments> = yield call(
|
||||
apiGetNodeComments,
|
||||
{
|
||||
id: id!,
|
||||
take: COMMENTS_DISPLAY,
|
||||
skip: 0,
|
||||
}
|
||||
);
|
||||
|
||||
yield put(
|
||||
nodeSet({
|
||||
comments,
|
||||
comment_count,
|
||||
})
|
||||
);
|
||||
} catch {}
|
||||
}
|
||||
|
||||
function* onNodeLoad({ id }: ReturnType<typeof nodeLoadNode>) {
|
||||
// Comments
|
||||
try {
|
||||
yield put(nodeSetLoadingComments(true));
|
||||
yield call(nodeGetComments, id);
|
||||
|
||||
yield put(
|
||||
nodeSet({
|
||||
is_loading_comments: false,
|
||||
})
|
||||
);
|
||||
} catch {}
|
||||
}
|
||||
|
||||
function* onPostComment({ nodeId, comment, callback }: ReturnType<typeof nodePostLocalComment>) {
|
||||
try {
|
||||
const data: Unwrap<typeof apiPostComment> = yield call(apiPostComment, {
|
||||
data: comment,
|
||||
id: nodeId,
|
||||
});
|
||||
|
||||
const { comments }: ReturnType<typeof selectNode> = yield select(selectNode);
|
||||
|
||||
if (!comment.id) {
|
||||
yield put(nodeSetComments([data.comment, ...comments]));
|
||||
} else {
|
||||
yield put(
|
||||
nodeSet({
|
||||
comments: comments.map(item => (item.id === comment.id ? data.comment : item)),
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
callback();
|
||||
} catch (error) {
|
||||
return callback(error.message);
|
||||
}
|
||||
}
|
||||
|
||||
function* onLockCommentSaga({ nodeId, id, is_locked }: ReturnType<typeof nodeLockComment>) {
|
||||
const { comments }: ReturnType<typeof selectNode> = yield select(selectNode);
|
||||
|
||||
try {
|
||||
const data: Unwrap<typeof apiLockComment> = yield call(apiLockComment, {
|
||||
current: nodeId,
|
||||
id,
|
||||
is_locked,
|
||||
});
|
||||
|
||||
yield put(
|
||||
nodeSetComments(
|
||||
comments.map(comment =>
|
||||
comment.id === id ? { ...comment, deleted_at: data.deleted_at || undefined } : comment
|
||||
)
|
||||
)
|
||||
);
|
||||
} catch (e) {
|
||||
showErrorToast(e);
|
||||
}
|
||||
}
|
||||
|
||||
export default function* nodeSaga() {
|
||||
yield takeLatest(NODE_ACTIONS.GOTO_NODE, onNodeGoto);
|
||||
yield takeLatest(NODE_ACTIONS.LOAD_NODE, onNodeLoad);
|
||||
yield takeLatest(NODE_ACTIONS.POST_LOCAL_COMMENT, onPostComment);
|
||||
yield takeLatest(NODE_ACTIONS.LOCK_COMMENT, onLockCommentSaga);
|
||||
yield takeLeading(NODE_ACTIONS.LOAD_MORE_COMMENTS, onNodeLoadMoreComments);
|
||||
}
|
|
@ -79,8 +79,8 @@ export type ApiLockNodeResult = {
|
|||
|
||||
export type ApiLockCommentRequest = {
|
||||
id: IComment['id'];
|
||||
current: INode['id'];
|
||||
is_locked: boolean;
|
||||
nodeId: INode['id'];
|
||||
isLocked: boolean;
|
||||
};
|
||||
export type ApiLockcommentResult = {
|
||||
deleted_at: string;
|
||||
|
|
|
@ -12,7 +12,6 @@ import authSaga from '~/redux/auth/sagas';
|
|||
import { IAuthState } from '~/redux/auth/types';
|
||||
|
||||
import node, { INodeState } from '~/redux/node/reducer';
|
||||
import nodeSaga from '~/redux/node/sagas';
|
||||
|
||||
import flow, { IFlowState } from '~/redux/flow/reducer';
|
||||
import flowSaga from '~/redux/flow/sagas';
|
||||
|
@ -108,7 +107,6 @@ export function configureStore(): {
|
|||
persistor: Persistor;
|
||||
} {
|
||||
sagaMiddleware.run(authSaga);
|
||||
sagaMiddleware.run(nodeSaga);
|
||||
sagaMiddleware.run(uploadSaga);
|
||||
sagaMiddleware.run(flowSaga);
|
||||
sagaMiddleware.run(playerSaga);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue