mirror of
https://github.com/muerwre/vault-frontend.git
synced 2025-04-25 12:56:41 +07:00
#35 refactored node layout
This commit is contained in:
parent
428c7e7a06
commit
d3473eab4c
20 changed files with 406 additions and 344 deletions
13
src/utils/hooks/node/useLoadNode.ts
Normal file
13
src/utils/hooks/node/useLoadNode.ts
Normal file
|
@ -0,0 +1,13 @@
|
|||
import { useEffect } from 'react';
|
||||
import { nodeGotoNode } from '~/redux/node/actions';
|
||||
import { useDispatch } from 'react-redux';
|
||||
|
||||
// useLoadNode loads node on id change
|
||||
export const useLoadNode = (id: any, isLoading: boolean) => {
|
||||
const dispatch = useDispatch();
|
||||
|
||||
useEffect(() => {
|
||||
if (isLoading) return;
|
||||
dispatch(nodeGotoNode(parseInt(id, 10), undefined));
|
||||
}, [dispatch, id]);
|
||||
};
|
19
src/utils/hooks/node/useNodeActions.ts
Normal file
19
src/utils/hooks/node/useNodeActions.ts
Normal file
|
@ -0,0 +1,19 @@
|
|||
import { INode } from '~/redux/types';
|
||||
import { useCallback } from 'react';
|
||||
import { useDispatch } from 'react-redux';
|
||||
import { nodeEdit, nodeLike, nodeLock, nodeStar } from '~/redux/node/actions';
|
||||
|
||||
export const useNodeActions = (node: INode) => {
|
||||
const dispatch = useDispatch();
|
||||
|
||||
const onEdit = useCallback(() => dispatch(nodeEdit(node.id)), [dispatch, nodeEdit, node]);
|
||||
const onLike = useCallback(() => dispatch(nodeLike(node.id)), [dispatch, nodeLike, node]);
|
||||
const onStar = useCallback(() => dispatch(nodeStar(node.id)), [dispatch, nodeStar, node]);
|
||||
const onLock = useCallback(() => dispatch(nodeLock(node.id, !node.deleted_at)), [
|
||||
dispatch,
|
||||
nodeLock,
|
||||
node,
|
||||
]);
|
||||
|
||||
return { onEdit, onLike, onStar, onLock };
|
||||
};
|
34
src/utils/hooks/node/useNodeBlocks.ts
Normal file
34
src/utils/hooks/node/useNodeBlocks.ts
Normal file
|
@ -0,0 +1,34 @@
|
|||
import { INode } from '~/redux/types';
|
||||
import { createElement, FC, useCallback, useMemo } from 'react';
|
||||
import { isNil, prop } from 'ramda';
|
||||
import { INodeComponentProps, NODE_COMPONENTS, NODE_HEADS, NODE_INLINES } from '~/redux/node/constants';
|
||||
|
||||
// useNodeBlocks returns head, block and inline blocks of node
|
||||
export const useNodeBlocks = (node: INode, isLoading: boolean) => {
|
||||
const createNodeBlock = useCallback(
|
||||
(block?: FC<INodeComponentProps>) =>
|
||||
!isNil(block) &&
|
||||
createElement(block, {
|
||||
node,
|
||||
isLoading,
|
||||
}),
|
||||
[node, isLoading]
|
||||
);
|
||||
|
||||
const head = useMemo(
|
||||
() => createNodeBlock(node?.type ? prop(node?.type, NODE_HEADS) : undefined),
|
||||
[node, createNodeBlock]
|
||||
);
|
||||
|
||||
const block = useMemo(
|
||||
() => createNodeBlock(node?.type ? prop(node?.type, NODE_COMPONENTS) : undefined),
|
||||
[node, createNodeBlock]
|
||||
);
|
||||
|
||||
const inline = useMemo(
|
||||
() => createNodeBlock(node?.type ? prop(node?.type, NODE_INLINES) : undefined),
|
||||
[node, createNodeBlock]
|
||||
);
|
||||
|
||||
return { head, block, inline };
|
||||
};
|
17
src/utils/hooks/node/useNodeCoverImage.ts
Normal file
17
src/utils/hooks/node/useNodeCoverImage.ts
Normal file
|
@ -0,0 +1,17 @@
|
|||
import { INode } from '~/redux/types';
|
||||
import { useEffect } from 'react';
|
||||
import { useDispatch } from 'react-redux';
|
||||
import { nodeSetCoverImage } from '~/redux/node/actions';
|
||||
|
||||
export const useNodeCoverImage = (node: INode) => {
|
||||
const dispatch = useDispatch();
|
||||
|
||||
useEffect(() => {
|
||||
if (!node.cover) return;
|
||||
dispatch(nodeSetCoverImage(node.cover));
|
||||
|
||||
return () => {
|
||||
nodeSetCoverImage(undefined);
|
||||
};
|
||||
}, [dispatch, node.cover]);
|
||||
};
|
14
src/utils/hooks/node/useNodePermissions.ts
Normal file
14
src/utils/hooks/node/useNodePermissions.ts
Normal file
|
@ -0,0 +1,14 @@
|
|||
import { useMemo } from 'react';
|
||||
import { canEditNode, canLikeNode, canStarNode } from '~/utils/node';
|
||||
import { useShallowSelect } from '~/utils/hooks/useShallowSelect';
|
||||
import { selectUser } from '~/redux/auth/selectors';
|
||||
import { INode } from '~/redux/types';
|
||||
|
||||
export const useNodePermissions = (node: INode) => {
|
||||
const user = useShallowSelect(selectUser);
|
||||
const edit = useMemo(() => canEditNode(node, user), [node, user]);
|
||||
const like = useMemo(() => canLikeNode(node, user), [node, user]);
|
||||
const star = useMemo(() => canStarNode(node, user), [node, user]);
|
||||
|
||||
return [edit, like, star];
|
||||
};
|
7
src/utils/hooks/useScrollToTop.ts
Normal file
7
src/utils/hooks/useScrollToTop.ts
Normal file
|
@ -0,0 +1,7 @@
|
|||
import { useEffect } from 'react';
|
||||
|
||||
export const useScrollToTop = (deps?: any[]) => {
|
||||
useEffect(() => {
|
||||
window.scrollTo(0, 0);
|
||||
}, deps || []);
|
||||
};
|
4
src/utils/hooks/user/userUser.ts
Normal file
4
src/utils/hooks/user/userUser.ts
Normal file
|
@ -0,0 +1,4 @@
|
|||
import { useShallowSelect } from '~/utils/hooks/useShallowSelect';
|
||||
import { selectUser } from '~/redux/auth/selectors';
|
||||
|
||||
export const useUser = () => useShallowSelect(selectUser);
|
Loading…
Add table
Add a link
Reference in a new issue