Skip to content

Commit 69048e3

Browse files
committed
save
1 parent bf37fa1 commit 69048e3

File tree

3 files changed

+80
-37
lines changed

3 files changed

+80
-37
lines changed

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,14 @@ import { Doc } from '../types';
66

77
import { KEY_LIST_DOC } from './useDocs';
88

9-
export const createDoc = async (): Promise<Doc> => {
9+
type CreateDocParams = {
10+
title?: string;
11+
} | void;
12+
13+
export const createDoc = async (params: CreateDocParams): Promise<Doc> => {
1014
const response = await fetchAPI(`documents/`, {
1115
method: 'POST',
16+
body: JSON.stringify({ title: params?.title }),
1217
});
1318

1419
if (!response.ok) {
@@ -25,7 +30,7 @@ interface CreateDocProps {
2530

2631
export function useCreateDoc({ onSuccess, onError }: CreateDocProps) {
2732
const queryClient = useQueryClient();
28-
return useMutation<Doc, APIError>({
33+
return useMutation<Doc, APIError, CreateDocParams>({
2934
mutationFn: createDoc,
3035
onSuccess: (data) => {
3136
void queryClient.resetQueries({
Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,23 @@
11
import { VariantType, useToastProvider } from '@openfun/cunningham-react';
2-
import { useMutation, useQueryClient } from '@tanstack/react-query';
2+
import {
3+
UseMutationOptions,
4+
useMutation,
5+
useQueryClient,
6+
} from '@tanstack/react-query';
37
import { useTranslation } from 'react-i18next';
48

59
import { APIError, errorCauses, fetchAPI } from '@/api';
6-
import { Doc } from '@/docs/doc-management';
10+
import { Doc, LinkReach, LinkRole } from '@/docs/doc-management';
711

812
export type UpdateDocLinkParams = Pick<Doc, 'id' | 'link_reach'> &
913
Partial<Pick<Doc, 'link_role'>>;
1014

15+
type UpdateDocLinkResponse = { link_role: LinkRole; link_reach: LinkReach };
16+
1117
export const updateDocLink = async ({
1218
id,
1319
...params
14-
}: UpdateDocLinkParams): Promise<Doc> => {
20+
}: UpdateDocLinkParams): Promise<UpdateDocLinkResponse> => {
1521
const response = await fetchAPI(`documents/${id}/link-configuration/`, {
1622
method: 'PUT',
1723
body: JSON.stringify({
@@ -26,26 +32,27 @@ export const updateDocLink = async ({
2632
);
2733
}
2834

29-
return response.json() as Promise<Doc>;
35+
return response.json() as Promise<UpdateDocLinkResponse>;
3036
};
3137

32-
interface UpdateDocLinkProps {
33-
onSuccess?: (data: Doc) => void;
38+
type UseUpdateDocLinkOptions = UseMutationOptions<
39+
UpdateDocLinkResponse,
40+
APIError,
41+
UpdateDocLinkParams
42+
> & {
3443
listInvalidQueries?: string[];
35-
}
44+
};
3645

37-
export function useUpdateDocLink({
38-
onSuccess,
39-
listInvalidQueries,
40-
}: UpdateDocLinkProps = {}) {
46+
export function useUpdateDocLink(options?: UseUpdateDocLinkOptions) {
4147
const queryClient = useQueryClient();
4248
const { toast } = useToastProvider();
4349
const { t } = useTranslation();
4450

45-
return useMutation<Doc, APIError, UpdateDocLinkParams>({
51+
return useMutation<UpdateDocLinkResponse, APIError, UpdateDocLinkParams>({
4652
mutationFn: updateDocLink,
47-
onSuccess: (data) => {
48-
listInvalidQueries?.forEach((queryKey) => {
53+
...options,
54+
onSuccess: (data, variables, onMutateResult, context) => {
55+
options?.listInvalidQueries?.forEach((queryKey) => {
4956
void queryClient.invalidateQueries({
5057
queryKey: [queryKey],
5158
});
@@ -59,7 +66,7 @@ export function useUpdateDocLink({
5966
},
6067
);
6168

62-
onSuccess?.(data);
69+
options?.onSuccess?.(data, variables, onMutateResult, context);
6370
},
6471
});
6572
}

src/frontend/apps/impress/src/pages/docs/new/index.tsx

Lines changed: 51 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
1+
import { captureException } from '@sentry/nextjs';
12
import Head from 'next/head';
23
import { useSearchParams } from 'next/navigation';
34
import { useRouter } from 'next/router';
4-
import { ReactElement, useEffect } from 'react';
5+
import { ReactElement, useCallback, useEffect } from 'react';
56

67
import { Loading } from '@/components';
7-
import { LinkReach, useCreateDoc } from '@/features/docs/doc-management';
8+
import {
9+
LinkReach,
10+
LinkRole,
11+
useCreateDoc,
12+
} from '@/features/docs/doc-management';
813
import { useUpdateDocLink } from '@/features/docs/doc-share/api/useUpdateDocLink';
914
import { useSkeletonStore } from '@/features/skeletons';
1015
import { MainLayout } from '@/layouts';
@@ -15,8 +20,8 @@ const Page: NextPageWithLayout = () => {
1520
const router = useRouter();
1621
const searchParams = useSearchParams();
1722
const linkReach = searchParams.get('link-reach');
18-
const linkTitle = searchParams.get('title');
19-
const linkpermission = searchParams.get('linkpermission');
23+
const linkRole = searchParams.get('link-role');
24+
const title = searchParams.get('title');
2025
const peoplesharing = searchParams.get('peoplesharing');
2126

2227
const {
@@ -25,49 +30,75 @@ const Page: NextPageWithLayout = () => {
2530
data: doc,
2631
} = useCreateDoc({
2732
onSuccess: (doc) => {
28-
if (linkReach || linkpermission || peoplesharing) {
33+
if ((linkReach && linkRole) || linkReach || peoplesharing) {
2934
return;
3035
}
3136

32-
router
33-
.push(`/docs/${doc.id}`)
34-
.then(() => {})
35-
.catch(() => {});
37+
redirectToDoc(doc.id);
3638
},
3739
onError: () => {},
3840
});
3941

40-
const { mutate: updateDocLink } = useUpdateDocLink();
42+
const { mutate: updateDocLink } = useUpdateDocLink({
43+
onSuccess: (response, params) => {
44+
if (peoplesharing || !params.id) {
45+
return;
46+
}
47+
48+
redirectToDoc(params.id);
49+
},
50+
onError: (error, params) => {
51+
captureException(error, {
52+
extra: {
53+
docId: params.id,
54+
linkReach,
55+
linkRole,
56+
},
57+
});
58+
59+
if (params.id) {
60+
redirectToDoc(params.id);
61+
}
62+
},
63+
});
64+
65+
const redirectToDoc = useCallback(
66+
(docId: string) => {
67+
void router.push(`/docs/${docId}`);
68+
},
69+
[router],
70+
);
4171

4272
useEffect(() => {
4373
setIsSkeletonVisible(true);
4474
}, [setIsSkeletonVisible]);
4575

76+
// Doc creation effect
4677
useEffect(() => {
4778
if (doc) {
4879
return;
4980
}
5081

51-
createDoc();
52-
}, [createDoc, doc]);
82+
createDoc({
83+
title: title || undefined,
84+
});
85+
}, [createDoc, doc, title]);
5386

5487
useEffect(() => {
5588
if (!linkReach || !doc) {
5689
return;
5790
}
5891

59-
console.log('linkReach', linkReach);
60-
61-
if (!Object.values(LinkReach).includes(linkReach as LinkReach)) {
62-
throw new Error('Invalid link reach value');
63-
}
64-
6592
updateDocLink({
6693
id: doc.id,
6794
link_reach: linkReach as LinkReach,
68-
//link_role: linkRole,
95+
link_role: (linkRole as LinkRole | undefined) || undefined,
6996
});
70-
}, [linkReach, doc, updateDocLink]);
97+
}, [linkReach, doc, updateDocLink, redirectToDoc, linkRole]);
98+
99+
if (!linkReach && linkRole) {
100+
console.warn('link-reach parameter is missing');
101+
}
71102

72103
return <Loading />;
73104
};

0 commit comments

Comments
 (0)