Skip to content

Commit 966e514

Browse files
committed
♻️(frontend) redirect to doc after duplicate
When we duplicate a document from a document page, we now redirect the user to the newly created document.
1 parent ef6d6c6 commit 966e514

File tree

6 files changed

+40
-44
lines changed

6 files changed

+40
-44
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ and this project adheres to
1010

1111
### Changed
1212

13+
- ♻️(frontend) redirect to doc after duplicate #1175
1314
- 🔧(project) change env.d system by using local files #1200
1415

1516
### Fixed

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -442,9 +442,10 @@ test.describe('Doc Header', () => {
442442
page.getByText('Document duplicated successfully!'),
443443
).toBeVisible();
444444

445-
await page.goto('/');
446-
447445
const duplicateTitle = 'Copy of ' + docTitle;
446+
await verifyDocName(page, duplicateTitle);
447+
448+
await page.goto('/');
448449

449450
const row = await getGridRow(page, duplicateTitle);
450451

@@ -477,9 +478,12 @@ test.describe('Doc Header', () => {
477478
page.getByText('Document duplicated successfully!'),
478479
).toBeVisible();
479480

480-
const duplicateDuplicateTitle = 'Copy of ' + childTitle;
481+
const duplicateTitle = 'Copy of ' + childTitle;
482+
483+
await verifyDocName(page, duplicateTitle);
484+
481485
await expect(
482-
page.getByTestId('doc-tree').getByText(duplicateDuplicateTitle),
486+
page.getByTestId('doc-tree').getByText(duplicateTitle),
483487
).toBeVisible();
484488
});
485489
});

src/frontend/apps/impress/src/features/docs/doc-header/__tests__/DocToolBox.spec.tsx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,13 @@ jest.mock('@/features/docs/doc-export/', () => ({
3232
ModalExport: () => <span>ModalExport</span>,
3333
}));
3434

35+
jest.mock('next/router', () => ({
36+
...jest.requireActual('next/router'),
37+
useRouter: () => ({
38+
push: jest.fn(),
39+
}),
40+
}));
41+
3542
const doc = {
3643
nb_accesses: 1,
3744
abilities: {

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

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
import { useTreeContext } from '@gouvfr-lasuite/ui-kit';
2-
import {
3-
Button,
4-
VariantType,
5-
useModal,
6-
useToastProvider,
7-
} from '@openfun/cunningham-react';
2+
import { Button, useModal } from '@openfun/cunningham-react';
83
import { useQueryClient } from '@tanstack/react-query';
4+
import { useRouter } from 'next/router';
95
import { useEffect, useMemo, useState } from 'react';
106
import { useTranslation } from 'react-i18next';
117
import { css } from 'styled-components';
@@ -62,7 +58,7 @@ export const DocToolBox = ({ doc }: DocToolBoxProps) => {
6258
}, [doc, treeContext?.root]);
6359

6460
const queryClient = useQueryClient();
65-
const { toast } = useToastProvider();
61+
const router = useRouter();
6662

6763
const { spacingsTokens, colorsTokens } = useCunninghamTheme();
6864

@@ -74,15 +70,8 @@ export const DocToolBox = ({ doc }: DocToolBoxProps) => {
7470
const { isSmallMobile, isDesktop } = useResponsiveStore();
7571
const copyDocLink = useCopyDocLink(doc.id);
7672
const { mutate: duplicateDoc } = useDuplicateDoc({
77-
onSuccess: () => {
78-
toast(t('Document duplicated successfully!'), VariantType.SUCCESS, {
79-
duration: 3000,
80-
});
81-
},
82-
onError: () => {
83-
toast(t('Failed to duplicate the document...'), VariantType.ERROR, {
84-
duration: 3000,
85-
});
73+
onSuccess: (data) => {
74+
void router.push(`/docs/${data.id}`);
8675
},
8776
});
8877
const { isFeatureFlagActivated } = useAnalytics();

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

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1+
import { VariantType, useToastProvider } from '@openfun/cunningham-react';
12
import {
23
UseMutationOptions,
34
useMutation,
45
useQueryClient,
56
} from '@tanstack/react-query';
7+
import { useTranslation } from 'react-i18next';
68
import * as Y from 'yjs';
79

810
import { APIError, errorCauses, fetchAPI } from '@/api';
911
import { toBase64 } from '@/docs/doc-editor';
10-
import { KEY_DOC_TREE } from '@/docs/doc-tree';
1112
import { KEY_LIST_DOC_VERSIONS } from '@/docs/doc-versioning';
1213

1314
import { useProviderStore } from '../stores';
@@ -52,9 +53,10 @@ type DuplicateDocOptions = UseMutationOptions<
5253
DuplicateDocParams
5354
>;
5455

55-
export function useDuplicateDoc(options: DuplicateDocOptions) {
56+
export function useDuplicateDoc(options?: DuplicateDocOptions) {
5657
const queryClient = useQueryClient();
57-
58+
const { toast } = useToastProvider();
59+
const { t } = useTranslation();
5860
const { provider } = useProviderStore();
5961

6062
const { mutateAsync: updateDoc } = useUpdateDoc({
@@ -86,10 +88,19 @@ export function useDuplicateDoc(options: DuplicateDocOptions) {
8688
void queryClient.resetQueries({
8789
queryKey: [KEY_LIST_DOC],
8890
});
89-
void queryClient.resetQueries({
90-
queryKey: [KEY_DOC_TREE],
91+
92+
toast(t('Document duplicated successfully!'), VariantType.SUCCESS, {
93+
duration: 3000,
9194
});
92-
void options.onSuccess?.(data, variables, context);
95+
96+
void options?.onSuccess?.(data, variables, context);
97+
},
98+
onError: (error, variables, context) => {
99+
toast(t('Failed to duplicate the document...'), VariantType.ERROR, {
100+
duration: 3000,
101+
});
102+
103+
void options?.onError?.(error, variables, context);
93104
},
94105
});
95106
}

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

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
import {
2-
VariantType,
3-
useModal,
4-
useToastProvider,
5-
} from '@openfun/cunningham-react';
1+
import { useModal } from '@openfun/cunningham-react';
62
import { useTranslation } from 'react-i18next';
73

84
import { DropdownMenu, DropdownMenuOption, Icon } from '@/components';
@@ -25,21 +21,9 @@ export const DocsGridActions = ({
2521
openShareModal,
2622
}: DocsGridActionsProps) => {
2723
const { t } = useTranslation();
28-
const { toast } = useToastProvider();
2924

3025
const deleteModal = useModal();
31-
const { mutate: duplicateDoc } = useDuplicateDoc({
32-
onSuccess: () => {
33-
toast(t('Document duplicated successfully!'), VariantType.SUCCESS, {
34-
duration: 3000,
35-
});
36-
},
37-
onError: () => {
38-
toast(t('Failed to duplicate the document...'), VariantType.ERROR, {
39-
duration: 3000,
40-
});
41-
},
42-
});
26+
const { mutate: duplicateDoc } = useDuplicateDoc();
4327

4428
const removeFavoriteDoc = useDeleteFavoriteDoc({
4529
listInvalideQueries: [KEY_LIST_DOC],

0 commit comments

Comments
 (0)