Skip to content

Commit fa40253

Browse files
committed
🚚(frontend) reduce features coupling
Move some components and assets to `doc-management` to reduce coupling between features: - SimpleDocItem from `doc-grid` to `doc-management` - useCreateChildDoc from `doc-tree` to `doc-management` - isOwnerOrAdmin from `doc-tree` to `doc-management`
1 parent eaf5e22 commit fa40253

File tree

15 files changed

+60
-45
lines changed

15 files changed

+60
-45
lines changed

src/frontend/apps/impress/src/features/docs/doc-management/api/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
export * from './useCreateChildDoc';
12
export * from './useCreateDoc';
23
export * from './useCreateFavoriteDoc';
34
export * from './useDeleteFavoriteDoc';

src/frontend/apps/impress/src/features/docs/doc-tree/api/useCreateChildren.tsx renamed to src/frontend/apps/impress/src/features/docs/doc-management/api/useCreateChildDoc.tsx

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@ import { useMutation, useQueryClient } from '@tanstack/react-query';
22

33
import { APIError, errorCauses, fetchAPI } from '@/api';
44

5-
import { Doc, KEY_LIST_DOC } from '../../doc-management';
5+
import { Doc, KEY_LIST_DOC } from '..';
66

7-
export type CreateDocParam = Pick<Doc, 'title'> & {
7+
export type CreateChildDocParam = Pick<Doc, 'title'> & {
88
parentId: string;
99
};
1010

11-
export const createDocChildren = async ({
11+
export const createChildDoc = async ({
1212
title,
1313
parentId,
14-
}: CreateDocParam): Promise<Doc> => {
14+
}: CreateChildDocParam): Promise<Doc> => {
1515
const response = await fetchAPI(`documents/${parentId}/children/`, {
1616
method: 'POST',
1717
body: JSON.stringify({
@@ -26,19 +26,19 @@ export const createDocChildren = async ({
2626
return response.json() as Promise<Doc>;
2727
};
2828

29-
interface CreateDocProps {
30-
onSuccess: (data: Doc) => void;
29+
interface UseCreateChildDocProps {
30+
onSuccess: (doc: Doc) => void;
3131
}
3232

33-
export function useCreateChildrenDoc({ onSuccess }: CreateDocProps) {
33+
export function useCreateChildDoc({ onSuccess }: UseCreateChildDocProps) {
3434
const queryClient = useQueryClient();
35-
return useMutation<Doc, APIError, CreateDocParam>({
36-
mutationFn: createDocChildren,
37-
onSuccess: (data) => {
35+
return useMutation<Doc, APIError, CreateChildDocParam>({
36+
mutationFn: createChildDoc,
37+
onSuccess: (doc) => {
3838
void queryClient.resetQueries({
3939
queryKey: [KEY_LIST_DOC],
4040
});
41-
onSuccess(data);
41+
onSuccess(doc);
4242
},
4343
});
4444
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
export * from './ModalRemoveDoc';
2+
export * from './SimpleDocItem';

src/frontend/apps/impress/src/features/docs/doc-management/utils.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,9 @@ export const base64ToYDoc = (base64: string) => {
2222
export const base64ToBlocknoteXmlFragment = (base64: string) => {
2323
return base64ToYDoc(base64).getXmlFragment('document-store');
2424
};
25+
26+
export const isOwnerOrAdmin = (doc: Doc): boolean => {
27+
return doc.user_roles.some(
28+
(role) => role === Role.OWNER || role === Role.ADMIN,
29+
);
30+
};

src/frontend/apps/impress/src/features/docs/doc-search/components/DocSearchItem.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { Box, Icon } from '@/components';
22
import { QuickSearchItemContent } from '@/components/quick-search/';
3-
import { Doc } from '@/docs/doc-management';
4-
import { SimpleDocItem } from '@/docs/docs-grid/';
3+
import { Doc, SimpleDocItem } from '@/docs/doc-management';
54
import { useResponsiveStore } from '@/stores';
65

76
type DocSearchItemProps = {

src/frontend/apps/impress/src/features/docs/doc-tree/components/DocTree.tsx

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,14 @@ import { css } from 'styled-components';
1010

1111
import { Box, StyledLink } from '@/components';
1212
import { useCunninghamTheme } from '@/cunningham';
13+
import {
14+
Doc,
15+
KEY_SUB_PAGE,
16+
SimpleDocItem,
17+
useDoc,
18+
useDocStore,
19+
} from '@/docs/doc-management';
1320

14-
import { Doc, KEY_SUB_PAGE, useDoc, useDocStore } from '../../doc-management';
15-
import { SimpleDocItem } from '../../docs-grid';
1621
import { useDocTree } from '../api/useDocTree';
1722
import { useMoveDoc } from '../api/useMove';
1823
import { canDrag, canDrop, serializeDocToSubPage } from '../utils';

src/frontend/apps/impress/src/features/docs/doc-tree/components/DocTreeItemActions.tsx

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,18 @@ import { useTranslation } from 'react-i18next';
1010
import { css } from 'styled-components';
1111

1212
import { Box, BoxButton, Icon } from '@/components';
13+
import {
14+
Doc,
15+
ModalRemoveDoc,
16+
isOwnerOrAdmin,
17+
useCopyDocLink,
18+
useCreateChildDoc,
19+
} from '@/docs/doc-management';
1320
import { useLeftPanelStore } from '@/features/left-panel';
1421

15-
import { Doc, ModalRemoveDoc, useCopyDocLink } from '../../doc-management';
16-
import { useCreateChildrenDoc } from '../api/useCreateChildren';
1722
import { useDetachDoc } from '../api/useDetach';
1823
import MoveDocIcon from '../assets/doc-extract-bold.svg';
1924
import { useTreeUtils } from '../hooks';
20-
import { isOwnerOrAdmin } from '../utils';
2125

2226
type DocTreeItemActionsProps = {
2327
doc: Doc;
@@ -92,7 +96,7 @@ export const DocTreeItemActions = ({
9296
},
9397
];
9498

95-
const { mutate: createChildrenDoc } = useCreateChildrenDoc({
99+
const { mutate: createChildDoc } = useCreateChildDoc({
96100
onSuccess: (doc) => {
97101
onCreateSuccess?.(doc);
98102
togglePanel();
@@ -144,7 +148,7 @@ export const DocTreeItemActions = ({
144148
e.stopPropagation();
145149
e.preventDefault();
146150

147-
createChildrenDoc({
151+
createChildDoc({
148152
parentId: doc.id,
149153
});
150154
}}

src/frontend/apps/impress/src/features/docs/doc-tree/utils.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { TreeViewDataType } from '@gouvfr-lasuite/ui-kit';
22

3-
import { Doc, Role } from '../doc-management';
3+
import { Doc, isOwnerOrAdmin } from '../doc-management';
44

55
export const serializeDocToSubPage = (doc: Doc): Doc => {
66
return { ...doc, childrenCount: doc.numchild };
@@ -14,12 +14,6 @@ export const subPageToTree = (children: Doc[]): TreeViewDataType<Doc>[] => {
1414
return children;
1515
};
1616

17-
export const isOwnerOrAdmin = (doc: Doc): boolean => {
18-
return doc.user_roles.some(
19-
(role) => role === Role.OWNER || role === Role.ADMIN,
20-
);
21-
};
22-
2317
export const canDrag = (doc: Doc): boolean => {
2418
return isOwnerOrAdmin(doc);
2519
};

src/frontend/apps/impress/src/features/docs/docs-grid/components/DocsGridItem.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,20 @@ import { css } from 'styled-components';
55

66
import { Box, Icon, StyledLink, Text } from '@/components';
77
import { useCunninghamTheme } from '@/cunningham';
8-
import { Doc, LinkReach } from '@/docs/doc-management';
8+
import { Doc, LinkReach, SimpleDocItem } from '@/docs/doc-management';
99
import { DocShareModal } from '@/docs/doc-share';
1010
import { useResponsiveStore } from '@/stores';
1111

1212
import { useResponsiveDocGrid } from '../hooks/useResponsiveDocGrid';
1313

1414
import { DocsGridActions } from './DocsGridActions';
1515
import { DocsGridItemSharedButton } from './DocsGridItemSharedButton';
16-
import { SimpleDocItem } from './SimpleDocItem';
16+
1717
type DocsGridItemProps = {
1818
doc: Doc;
1919
dragMode?: boolean;
2020
};
21+
2122
export const DocsGridItem = ({ doc, dragMode = false }: DocsGridItemProps) => {
2223
const { t } = useTranslation();
2324
const { isDesktop } = useResponsiveStore();
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
export * from './DocsGrid';
22
export * from './DocsGridActions';
3-
export * from './SimpleDocItem';
43
export * from './DocsGridLoader';

src/frontend/apps/impress/src/features/left-panel/components/LeftPanelFavoriteItem.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ import { css } from 'styled-components';
33

44
import { Box, StyledLink } from '@/components';
55
import { useCunninghamTheme } from '@/cunningham';
6-
import { Doc } from '@/docs/doc-management';
6+
import { Doc, SimpleDocItem } from '@/docs/doc-management';
77
import { DocShareModal } from '@/docs/doc-share';
8-
import { DocsGridActions, SimpleDocItem } from '@/docs/docs-grid';
8+
import { DocsGridActions } from '@/docs/docs-grid';
99
import { useResponsiveStore } from '@/stores';
1010

1111
type LeftPanelFavoriteItemProps = {

src/frontend/apps/impress/src/features/left-panel/components/LeftPanelHeaderButton.tsx

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,13 @@ import { Button } from '@openfun/cunningham-react';
33
import { useRouter } from 'next/router';
44
import { useTranslation } from 'react-i18next';
55

6-
import { Doc, useCreateDoc, useDocStore } from '@/features/docs';
7-
import { useCreateChildrenDoc } from '@/features/docs/doc-tree/api/useCreateChildren';
8-
import { isOwnerOrAdmin } from '@/features/docs/doc-tree/utils';
6+
import {
7+
Doc,
8+
isOwnerOrAdmin,
9+
useCreateChildDoc,
10+
useCreateDoc,
11+
useDocStore,
12+
} from '@/docs/doc-management';
913

1014
import { useLeftPanelStore } from '../stores';
1115

@@ -44,7 +48,7 @@ export const LeftPanelHeaderDocButton = () => {
4448
const { togglePanel } = useLeftPanelStore();
4549
const treeContext = useTreeContext<Doc>();
4650
const tree = treeContext?.treeData;
47-
const { mutate: createChildrenDoc } = useCreateChildrenDoc({
51+
const { mutate: createChildDoc } = useCreateChildDoc({
4852
onSuccess: (doc) => {
4953
tree?.addRootNode(doc);
5054
tree?.selectNodeById(doc.id);
@@ -54,19 +58,20 @@ export const LeftPanelHeaderDocButton = () => {
5458
});
5559

5660
const onCreateDoc = () => {
57-
if (treeContext && treeContext.root) {
58-
createChildrenDoc({
59-
parentId: treeContext.root.id,
60-
});
61+
if (!treeContext?.root) {
62+
return;
6163
}
64+
65+
createChildDoc({
66+
parentId: treeContext.root.id,
67+
});
6268
};
6369

70+
const disabled =
71+
(currentDoc && !isOwnerOrAdmin(currentDoc)) || !treeContext?.root;
72+
6473
return (
65-
<Button
66-
color="tertiary"
67-
onClick={onCreateDoc}
68-
disabled={currentDoc && !isOwnerOrAdmin(currentDoc)}
69-
>
74+
<Button color="tertiary" onClick={onCreateDoc} disabled={disabled}>
7075
{t('New page')}
7176
</Button>
7277
);

0 commit comments

Comments
 (0)