Skip to content

Commit a092c29

Browse files
AntoLCsampaccoud
authored andcommitted
♻️(frontend) adapt doc visibility to new api
We updated the way we handle the visibility of a doc in the backend. Now we use a new api to update the visibility (documents/{id}/link-configuration/) of a doc. We adapted the frontend to use this new api. We changed the types to reflect the new api and to keep the same logic.
1 parent 9b44e02 commit a092c29

File tree

18 files changed

+128
-57
lines changed

18 files changed

+128
-57
lines changed

src/frontend/apps/e2e/__tests__/app-impress/common.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ export const mockedDocument = async (page: Page, json: object) => {
164164
accesses: [],
165165
abilities: {
166166
destroy: false, // Means not owner
167+
link_configuration: false,
167168
versions_destroy: false,
168169
versions_list: true,
169170
versions_retrieve: true,
@@ -172,7 +173,7 @@ export const mockedDocument = async (page: Page, json: object) => {
172173
partial_update: false, // Means not editor
173174
retrieve: true,
174175
},
175-
is_public: false,
176+
link_reach: 'restricted',
176177
created_at: '2021-09-01T09:00:00Z',
177178
...json,
178179
},

src/frontend/apps/e2e/__tests__/app-impress/doc-editor.spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ test.describe('Doc Editor', () => {
136136
await mockedDocument(page, {
137137
abilities: {
138138
destroy: false, // Means not owner
139+
link_configuration: false,
139140
versions_destroy: false,
140141
versions_list: true,
141142
versions_retrieve: true,

src/frontend/apps/e2e/__tests__/app-impress/doc-header.spec.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ test.describe('Doc Header', () => {
3434
],
3535
abilities: {
3636
destroy: true, // Means owner
37+
link_configuration: true,
3738
versions_destroy: true,
3839
versions_list: true,
3940
versions_retrieve: true,
@@ -42,7 +43,7 @@ test.describe('Doc Header', () => {
4243
partial_update: true,
4344
retrieve: true,
4445
},
45-
is_public: true,
46+
link_reach: 'public',
4647
created_at: '2021-09-01T09:00:00Z',
4748
});
4849

@@ -153,6 +154,7 @@ test.describe('Doc Header', () => {
153154
await mockedDocument(page, {
154155
abilities: {
155156
destroy: false, // Means not owner
157+
link_configuration: true,
156158
versions_destroy: true,
157159
versions_list: true,
158160
versions_retrieve: true,
@@ -184,6 +186,7 @@ test.describe('Doc Header', () => {
184186
await mockedDocument(page, {
185187
abilities: {
186188
destroy: false, // Means not owner
189+
link_configuration: false,
187190
versions_destroy: true,
188191
versions_list: true,
189192
versions_retrieve: true,
@@ -215,6 +218,7 @@ test.describe('Doc Header', () => {
215218
await mockedDocument(page, {
216219
abilities: {
217220
destroy: false, // Means not owner
221+
link_configuration: false,
218222
versions_destroy: false,
219223
versions_list: true,
220224
versions_retrieve: true,

src/frontend/apps/impress/src/features/docs/doc-header/components/DocHeader.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ export const DocHeader = ({ doc, versionId }: DocHeaderProps) => {
8585
$wrap="wrap"
8686
>
8787
<Box $direction="row" $align="center" $gap="0.5rem 2rem" $wrap="wrap">
88-
<DocTagPublic />
88+
<DocTagPublic doc={doc} />
8989
<Text $size="s" $display="inline">
9090
{t('Created at')} <strong>{formatDate(doc.created_at)}</strong>
9191
</Text>

src/frontend/apps/impress/src/features/docs/doc-header/components/DocTagPublic.tsx

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,18 @@
1-
import { useRouter } from 'next/router';
21
import { useTranslation } from 'react-i18next';
32

43
import { Text } from '@/components';
54
import { useCunninghamTheme } from '@/cunningham';
6-
import { KEY_DOC_VISIBILITY, useDoc } from '@/features/docs/doc-management';
5+
import { Doc, LinkReach } from '@/features/docs/doc-management';
76

8-
export const DocTagPublic = () => {
7+
interface DocTagPublicProps {
8+
doc: Doc;
9+
}
10+
11+
export const DocTagPublic = ({ doc }: DocTagPublicProps) => {
912
const { colorsTokens } = useCunninghamTheme();
1013
const { t } = useTranslation();
11-
const {
12-
query: { id },
13-
} = useRouter();
14-
15-
const { data: doc } = useDoc(
16-
{ id: id as string },
17-
{
18-
enabled: !!id,
19-
queryKey: [KEY_DOC_VISIBILITY, { id }],
20-
},
21-
);
2214

23-
if (!doc?.is_public) {
15+
if (doc?.link_reach !== LinkReach.PUBLIC) {
2416
return null;
2517
}
2618

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
export * from './useDoc';
22
export * from './useDocs';
33
export * from './useUpdateDoc';
4+
export * from './useUpdateDocLink';

src/frontend/apps/impress/src/features/docs/doc-management/api/useUpdateDoc.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { APIError, errorCauses, fetchAPI } from '@/api';
44
import { Doc } from '@/features/docs';
55

66
export type UpdateDocParams = Pick<Doc, 'id'> &
7-
Partial<Pick<Doc, 'content' | 'title' | 'is_public'>>;
7+
Partial<Pick<Doc, 'content' | 'title'>>;
88

99
export const updateDoc = async ({
1010
id,
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import { useMutation, useQueryClient } from '@tanstack/react-query';
2+
3+
import { APIError, errorCauses, fetchAPI } from '@/api';
4+
import { Doc } from '@/features/docs';
5+
6+
export type UpdateDocLinkParams = Pick<Doc, 'id'> &
7+
Partial<Pick<Doc, 'link_role' | 'link_reach'>>;
8+
9+
export const updateDocLink = async ({
10+
id,
11+
...params
12+
}: UpdateDocLinkParams): Promise<Doc> => {
13+
const response = await fetchAPI(`documents/${id}/link-configuration/`, {
14+
method: 'PUT',
15+
body: JSON.stringify({
16+
...params,
17+
}),
18+
});
19+
20+
if (!response.ok) {
21+
throw new APIError(
22+
'Failed to update the doc link',
23+
await errorCauses(response),
24+
);
25+
}
26+
27+
return response.json() as Promise<Doc>;
28+
};
29+
30+
interface UpdateDocLinkProps {
31+
onSuccess?: (data: Doc) => void;
32+
listInvalideQueries?: string[];
33+
}
34+
35+
export function useUpdateDocLink({
36+
onSuccess,
37+
listInvalideQueries,
38+
}: UpdateDocLinkProps = {}) {
39+
const queryClient = useQueryClient();
40+
return useMutation<Doc, APIError, UpdateDocLinkParams>({
41+
mutationFn: updateDocLink,
42+
onSuccess: (data) => {
43+
listInvalideQueries?.forEach((queryKey) => {
44+
void queryClient.resetQueries({
45+
queryKey: [queryKey],
46+
});
47+
});
48+
onSuccess?.(data);
49+
},
50+
});
51+
}

src/frontend/apps/impress/src/features/docs/doc-management/components/DocVisibility.tsx

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,20 @@ import { useTranslation } from 'react-i18next';
99

1010
import { Box, Card, IconBG } from '@/components';
1111

12-
import { KEY_DOC_VISIBILITY, KEY_LIST_DOC, useUpdateDoc } from '../api';
13-
import { Doc } from '../types';
12+
import { KEY_DOC, KEY_LIST_DOC, useUpdateDocLink } from '../api';
13+
import { Doc, LinkReach } from '../types';
1414

1515
interface DocVisibilityProps {
1616
doc: Doc;
1717
}
1818

1919
export const DocVisibility = ({ doc }: DocVisibilityProps) => {
2020
const { t } = useTranslation();
21-
const [docPublic, setDocPublic] = useState(doc.is_public);
21+
const [docPublic, setDocPublic] = useState(
22+
doc.link_reach === LinkReach.PUBLIC,
23+
);
2224
const { toast } = useToastProvider();
23-
const api = useUpdateDoc({
25+
const api = useUpdateDocLink({
2426
onSuccess: () => {
2527
toast(
2628
t('The document visiblitity has been updated.'),
@@ -30,13 +32,13 @@ export const DocVisibility = ({ doc }: DocVisibilityProps) => {
3032
},
3133
);
3234
},
33-
listInvalideQueries: [KEY_LIST_DOC, KEY_DOC_VISIBILITY],
35+
listInvalideQueries: [KEY_LIST_DOC, KEY_DOC],
3436
});
3537

3638
return (
3739
<Card
3840
$margin="tiny"
39-
$padding="small"
41+
$padding={{ horizontal: 'small', vertical: 'tiny' }}
4042
aria-label={t('Doc visibility card')}
4143
$direction="row"
4244
$align="center"
@@ -50,10 +52,12 @@ export const DocVisibility = ({ doc }: DocVisibilityProps) => {
5052
onChange={() => {
5153
api.mutate({
5254
id: doc.id,
53-
is_public: !docPublic,
55+
link_reach: docPublic ? LinkReach.RESTRICTED : LinkReach.PUBLIC,
56+
link_role: 'reader',
5457
});
5558
setDocPublic(!docPublic);
5659
}}
60+
disabled={!doc.abilities.link_configuration}
5761
text={t(
5862
docPublic
5963
? 'Anyone on the internet with the link can view'

src/frontend/apps/impress/src/features/docs/doc-management/components/ModalCreateUpdateDoc.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@ export const ModalUpdateDoc = ({ onClose, doc }: ModalUpdateDocProps) => {
7878
buttonText: t('Validate the modification'),
7979
onClose,
8080
initialTitle: doc.title,
81-
isPublic: doc.is_public,
8281
infoText: t('Enter the new name of the selected document.'),
8382
titleModal: t('Update document "{{documentTitle}}"', {
8483
documentTitle: doc.title,

0 commit comments

Comments
 (0)