mirror of
https://github.com/muerwre/vault-frontend.git
synced 2025-04-25 12:56:41 +07:00
99: made node use SWR
This commit is contained in:
parent
832386d39a
commit
a1dfcc6048
27 changed files with 217 additions and 147 deletions
34
src/utils/hooks/data/useCreateNode.ts
Normal file
34
src/utils/hooks/data/useCreateNode.ts
Normal file
|
@ -0,0 +1,34 @@
|
|||
import { useCallback } from 'react';
|
||||
import { INode } from '~/redux/types';
|
||||
import { apiPostNode } from '~/redux/node/api';
|
||||
import { selectFlowNodes } from '~/redux/flow/selectors';
|
||||
import { flowSetNodes } from '~/redux/flow/actions';
|
||||
import { selectLabListNodes } from '~/redux/lab/selectors';
|
||||
import { labSetList } from '~/redux/lab/actions';
|
||||
import { useShallowSelect } from '~/utils/hooks/useShallowSelect';
|
||||
import { useDispatch } from 'react-redux';
|
||||
|
||||
export const useCreateNode = () => {
|
||||
const dispatch = useDispatch();
|
||||
const flowNodes = useShallowSelect(selectFlowNodes);
|
||||
const labNodes = useShallowSelect(selectLabListNodes);
|
||||
|
||||
return useCallback(
|
||||
async (node: INode) => {
|
||||
const result = await apiPostNode({ node });
|
||||
|
||||
// TODO: use another store here someday
|
||||
if (node.is_promoted) {
|
||||
const updatedNodes = [result.node, ...flowNodes];
|
||||
dispatch(flowSetNodes(updatedNodes));
|
||||
} else {
|
||||
const updatedNodes = [
|
||||
{ node: result.node, comment_count: 0, last_seen: node.created_at },
|
||||
...labNodes,
|
||||
];
|
||||
dispatch(labSetList({ nodes: updatedNodes }));
|
||||
}
|
||||
},
|
||||
[flowNodes, labNodes, dispatch]
|
||||
);
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue