mirror of
https://github.com/muerwre/vault-frontend.git
synced 2025-04-25 12:56:41 +07:00
fixed node actions
This commit is contained in:
parent
e8effb92f1
commit
0bc2ff250f
13 changed files with 107 additions and 201 deletions
|
@ -7,11 +7,6 @@ export const nodeSet = (node: Partial<INodeState>) => ({
|
|||
type: NODE_ACTIONS.SET,
|
||||
});
|
||||
|
||||
export const nodeSetSaveErrors = (errors: IValidationErrors) => ({
|
||||
errors,
|
||||
type: NODE_ACTIONS.SET_SAVE_ERRORS,
|
||||
});
|
||||
|
||||
export const nodeGotoNode = (id: INode['id'], node_type: INode['type']) => ({
|
||||
id,
|
||||
node_type,
|
||||
|
@ -60,22 +55,6 @@ export const nodeSetComments = (comments: IComment[]) => ({
|
|||
type: NODE_ACTIONS.SET_COMMENTS,
|
||||
});
|
||||
|
||||
export const nodeLike = (id: INode['id']) => ({
|
||||
type: NODE_ACTIONS.LIKE,
|
||||
id,
|
||||
});
|
||||
|
||||
export const nodeStar = (id: INode['id']) => ({
|
||||
type: NODE_ACTIONS.STAR,
|
||||
id,
|
||||
});
|
||||
|
||||
export const nodeLock = (id: INode['id'], is_locked: boolean) => ({
|
||||
type: NODE_ACTIONS.LOCK,
|
||||
id,
|
||||
is_locked,
|
||||
});
|
||||
|
||||
export const nodeLockComment = (id: number, is_locked: boolean, nodeId: number) => ({
|
||||
type: NODE_ACTIONS.LOCK_COMMENT,
|
||||
nodeId,
|
||||
|
@ -83,11 +62,6 @@ export const nodeLockComment = (id: number, is_locked: boolean, nodeId: number)
|
|||
is_locked,
|
||||
});
|
||||
|
||||
export const nodeSetEditor = (editor: INode) => ({
|
||||
type: NODE_ACTIONS.SET_EDITOR,
|
||||
editor,
|
||||
});
|
||||
|
||||
export const nodeSetCoverImage = (current_cover_image?: IFile) => ({
|
||||
type: NODE_ACTIONS.SET_COVER_IMAGE,
|
||||
current_cover_image,
|
||||
|
|
|
@ -29,23 +29,17 @@ export const NODE_ACTIONS = {
|
|||
GOTO_NODE: `${prefix}GOTO_NODE`,
|
||||
SET: `${prefix}SET`,
|
||||
|
||||
LIKE: `${prefix}LIKE`,
|
||||
STAR: `${prefix}STAR`,
|
||||
LOCK: `${prefix}LOCK`,
|
||||
LOCK_COMMENT: `${prefix}LOCK_COMMENT`,
|
||||
EDIT_COMMENT: `${prefix}EDIT_COMMENT`,
|
||||
LOAD_MORE_COMMENTS: `${prefix}LOAD_MORE_COMMENTS`,
|
||||
|
||||
SET_SAVE_ERRORS: `${prefix}SET_SAVE_ERRORS`,
|
||||
SET_LOADING: `${prefix}SET_LOADING`,
|
||||
SET_LOADING_COMMENTS: `${prefix}SET_LOADING_COMMENTS`,
|
||||
SET_SENDING_COMMENT: `${prefix}SET_SENDING_COMMENT`,
|
||||
SET_CURRENT: `${prefix}SET_CURRENT`,
|
||||
SET_EDITOR: `${prefix}SET_EDITOR`,
|
||||
|
||||
POST_LOCAL_COMMENT: `${prefix}POST_LOCAL_COMMENT`,
|
||||
SET_COMMENTS: `${prefix}SET_COMMENTS`,
|
||||
SET_RELATED: `${prefix}SET_RELATED`,
|
||||
|
||||
SET_COVER_IMAGE: `${prefix}SET_COVER_IMAGE`,
|
||||
};
|
||||
|
|
|
@ -5,10 +5,8 @@ import {
|
|||
nodeSetComments,
|
||||
nodeSetCoverImage,
|
||||
nodeSetCurrent,
|
||||
nodeSetEditor,
|
||||
nodeSetLoading,
|
||||
nodeSetLoadingComments,
|
||||
nodeSetSaveErrors,
|
||||
nodeSetSendingComment,
|
||||
} from './actions';
|
||||
import { INodeState } from './reducer';
|
||||
|
@ -18,9 +16,6 @@ const setData = (state: INodeState, { node }: ReturnType<typeof nodeSet>) => ({
|
|||
...node,
|
||||
});
|
||||
|
||||
const setSaveErrors = (state: INodeState, { errors }: ReturnType<typeof nodeSetSaveErrors>) =>
|
||||
assocPath(['errors'], errors, state);
|
||||
|
||||
const setLoading = (state: INodeState, { is_loading }: ReturnType<typeof nodeSetLoading>) =>
|
||||
assocPath(['is_loading'], is_loading, state);
|
||||
|
||||
|
@ -40,9 +35,6 @@ const setSendingComment = (
|
|||
const setComments = (state: INodeState, { comments }: ReturnType<typeof nodeSetComments>) =>
|
||||
assocPath(['comments'], comments, state);
|
||||
|
||||
const setEditor = (state: INodeState, { editor }: ReturnType<typeof nodeSetEditor>) =>
|
||||
assocPath(['editor'], editor, state);
|
||||
|
||||
const setCoverImage = (
|
||||
state: INodeState,
|
||||
{ current_cover_image }: ReturnType<typeof nodeSetCoverImage>
|
||||
|
@ -50,12 +42,10 @@ const setCoverImage = (
|
|||
|
||||
export const NODE_HANDLERS = {
|
||||
[NODE_ACTIONS.SET]: setData,
|
||||
[NODE_ACTIONS.SET_SAVE_ERRORS]: setSaveErrors,
|
||||
[NODE_ACTIONS.SET_LOADING]: setLoading,
|
||||
[NODE_ACTIONS.SET_LOADING_COMMENTS]: setLoadingComments,
|
||||
[NODE_ACTIONS.SET_CURRENT]: setCurrent,
|
||||
[NODE_ACTIONS.SET_SENDING_COMMENT]: setSendingComment,
|
||||
[NODE_ACTIONS.SET_COMMENTS]: setComments,
|
||||
[NODE_ACTIONS.SET_EDITOR]: setEditor,
|
||||
[NODE_ACTIONS.SET_COVER_IMAGE]: setCoverImage,
|
||||
};
|
||||
|
|
|
@ -1,11 +1,9 @@
|
|||
import { call, put, select, takeLatest, takeLeading } from 'redux-saga/effects';
|
||||
|
||||
import { COMMENTS_DISPLAY, EMPTY_NODE, NODE_ACTIONS, NODE_EDITOR_DATA } from './constants';
|
||||
import { COMMENTS_DISPLAY, EMPTY_NODE, NODE_ACTIONS } from './constants';
|
||||
import {
|
||||
nodeGotoNode,
|
||||
nodeLike,
|
||||
nodeLoadNode,
|
||||
nodeLock,
|
||||
nodeLockComment,
|
||||
nodePostLocalComment,
|
||||
nodeSet,
|
||||
|
@ -13,27 +11,11 @@ import {
|
|||
nodeSetCurrent,
|
||||
nodeSetLoadingComments,
|
||||
} from './actions';
|
||||
import {
|
||||
apiGetNodeComments,
|
||||
apiLockComment,
|
||||
apiLockNode,
|
||||
apiPostComment,
|
||||
apiPostNodeHeroic,
|
||||
apiPostNodeLike,
|
||||
apiPostNodeTags,
|
||||
} from './api';
|
||||
import { apiGetNodeComments, apiLockComment, apiPostComment } from './api';
|
||||
import { flowSetNodes } from '../flow/actions';
|
||||
import { modalSetShown, modalShowDialog } from '../modal/actions';
|
||||
import { selectFlowNodes } from '../flow/selectors';
|
||||
import { URLS } from '~/constants/urls';
|
||||
import { selectNode } from './selectors';
|
||||
import { INode, Unwrap } from '../types';
|
||||
import { NODE_EDITOR_DIALOGS } from '~/constants/dialogs';
|
||||
import { DIALOGS } from '~/redux/modal/constants';
|
||||
import { has } from 'ramda';
|
||||
import { selectLabListNodes } from '~/redux/lab/selectors';
|
||||
import { labSetList } from '~/redux/lab/actions';
|
||||
import { apiPostNode } from '~/redux/node/api';
|
||||
import { showErrorToast } from '~/utils/errors/showToast';
|
||||
|
||||
export function* updateNodeEverywhere(node) {
|
||||
|
@ -156,63 +138,6 @@ function* onPostComment({ nodeId, comment, callback }: ReturnType<typeof nodePos
|
|||
}
|
||||
}
|
||||
|
||||
function* onLikeSaga({ id }: ReturnType<typeof nodeLike>) {
|
||||
const { current }: ReturnType<typeof selectNode> = yield select(selectNode);
|
||||
|
||||
try {
|
||||
const count = current.like_count || 0;
|
||||
|
||||
yield call(updateNodeEverywhere, {
|
||||
...current,
|
||||
is_liked: !current.is_liked,
|
||||
like_count: current.is_liked ? Math.max(count - 1, 0) : count + 1,
|
||||
});
|
||||
|
||||
const data: Unwrap<typeof apiPostNodeLike> = yield call(apiPostNodeLike, { id });
|
||||
|
||||
yield call(updateNodeEverywhere, {
|
||||
...current,
|
||||
is_liked: data.is_liked,
|
||||
like_count: data.is_liked ? count + 1 : Math.max(count - 1, 0),
|
||||
});
|
||||
} catch {}
|
||||
}
|
||||
|
||||
function* onStarSaga({ id }: ReturnType<typeof nodeLike>) {
|
||||
try {
|
||||
const {
|
||||
current,
|
||||
current: { is_heroic },
|
||||
} = yield select(selectNode);
|
||||
|
||||
yield call(updateNodeEverywhere, { ...current, is_heroic: !is_heroic });
|
||||
|
||||
const data: Unwrap<typeof apiPostNodeHeroic> = yield call(apiPostNodeHeroic, { id });
|
||||
|
||||
yield call(updateNodeEverywhere, { ...current, is_heroic: data.is_heroic });
|
||||
} catch {}
|
||||
}
|
||||
|
||||
function* onLockSaga({ id, is_locked }: ReturnType<typeof nodeLock>) {
|
||||
const { current }: ReturnType<typeof selectNode> = yield select(selectNode);
|
||||
|
||||
try {
|
||||
yield call(updateNodeEverywhere, {
|
||||
...current,
|
||||
deleted_at: is_locked ? new Date().toISOString() : null,
|
||||
});
|
||||
|
||||
const data: Unwrap<typeof apiLockNode> = yield call(apiLockNode, { id, is_locked });
|
||||
|
||||
yield call(updateNodeEverywhere, {
|
||||
...current,
|
||||
deleted_at: data.deleted_at || undefined,
|
||||
});
|
||||
} catch {
|
||||
yield call(updateNodeEverywhere, { ...current, deleted_at: current.deleted_at });
|
||||
}
|
||||
}
|
||||
|
||||
function* onLockCommentSaga({ nodeId, id, is_locked }: ReturnType<typeof nodeLockComment>) {
|
||||
const { comments }: ReturnType<typeof selectNode> = yield select(selectNode);
|
||||
|
||||
|
@ -239,9 +164,6 @@ 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.LIKE, onLikeSaga);
|
||||
yield takeLatest(NODE_ACTIONS.STAR, onStarSaga);
|
||||
yield takeLatest(NODE_ACTIONS.LOCK, onLockSaga);
|
||||
yield takeLatest(NODE_ACTIONS.LOCK_COMMENT, onLockCommentSaga);
|
||||
yield takeLeading(NODE_ACTIONS.LOAD_MORE_COMMENTS, onNodeLoadMoreComments);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue