Skip to content

Commit b890597

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 0afc50f commit b890597

File tree

5 files changed

+36
-44
lines changed

5 files changed

+36
-44
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ and this project adheres to
88

99
## [Unreleased]
1010

11+
### Changed
12+
13+
- ♻️(frontend) redirect to doc after duplicate
14+
1115
### Fixed
1216

1317
- 🌐(frontend) keep simple tag during export #1154

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
@@ -443,9 +443,10 @@ test.describe('Doc Header', () => {
443443
page.getByText('Document duplicated successfully!'),
444444
).toBeVisible();
445445

446-
await page.goto('/');
447-
448446
const duplicateTitle = 'Copy of ' + docTitle;
447+
await verifyDocName(page, duplicateTitle);
448+
449+
await page.goto('/');
449450

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

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

481-
const duplicateDuplicateTitle = 'Copy of ' + childTitle;
482+
const duplicateTitle = 'Copy of ' + childTitle;
483+
484+
await verifyDocName(page, duplicateTitle);
485+
482486
await expect(
483-
page.getByTestId('doc-tree').getByText(duplicateDuplicateTitle),
487+
page.getByTestId('doc-tree').getByText(duplicateTitle),
484488
).toBeVisible();
485489
});
486490
});

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)