Skip to content

Commit f5a35c6

Browse files
committed
✨(frontend) add doc grid actions button
Add document action buttons to the document grid: - update document - delete document
1 parent 8100422 commit f5a35c6

File tree

4 files changed

+137
-17
lines changed

4 files changed

+137
-17
lines changed

CHANGELOG.md

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

1313
- 🤡(demo) generate dummy documents on dev users #120
1414
- ✨(frontend) create side modal component #134
15+
- ✨(frontend) Doc grid actions (update / delete) #136
1516

1617
## Changed
1718

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

Lines changed: 57 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -171,14 +171,9 @@ test.describe('Documents Grid', () => {
171171
const responsePage1 = await responsePromisePage1;
172172
expect(responsePage1.ok()).toBeTruthy();
173173

174-
const docNamePage1 = datagridPage1
175-
.getByRole('row')
176-
.nth(1)
177-
.getByRole('cell')
178-
.nth(1);
179-
180-
await expect(docNamePage1).toHaveText(/.*/);
181-
const textDocNamePage1 = await docNamePage1.textContent();
174+
await expect(
175+
datagridPage1.getByRole('row').nth(1).getByRole('cell').nth(1),
176+
).toHaveText(/.*/);
182177

183178
await page.getByLabel('Go to page 2').click();
184179

@@ -189,17 +184,62 @@ test.describe('Documents Grid', () => {
189184
const responsePage2 = await responsePromisePage2;
190185
expect(responsePage2.ok()).toBeTruthy();
191186

192-
const docNamePage2 = datagridPage2
193-
.getByRole('row')
194-
.nth(1)
195-
.getByRole('cell')
196-
.nth(1);
187+
await expect(
188+
datagridPage2.getByRole('row').nth(1).getByRole('cell').nth(1),
189+
).toHaveText(/.*/);
190+
});
191+
192+
test('it updates document', async ({ page }) => {
193+
const datagrid = page
194+
.getByLabel('Datagrid of the documents page 1')
195+
.getByRole('table');
196+
197+
const docRow = datagrid.getByRole('row').nth(1).getByRole('cell');
198+
199+
const docName = await docRow.nth(1).textContent();
200+
201+
await docRow.getByLabel('Open the document options').click();
202+
203+
await page.getByText('Update document').click();
204+
205+
await page.getByLabel('Document name').fill(`${docName} updated`);
206+
207+
await page.getByText('Validate the modification').click();
208+
209+
await expect(datagrid.getByText(`${docName} updated`)).toBeVisible();
210+
});
211+
212+
test('it deletes the document', async ({ page }) => {
213+
const datagrid = page
214+
.getByLabel('Datagrid of the documents page 1')
215+
.getByRole('table');
216+
217+
const docRow = datagrid.getByRole('row').nth(1).getByRole('cell');
218+
219+
const docName = await docRow.nth(1).textContent();
220+
221+
await docRow.getByLabel('Open the document options').click();
222+
223+
await page
224+
.getByRole('button', {
225+
name: 'Delete document',
226+
})
227+
.click();
228+
229+
await expect(
230+
page.locator('h2').getByText(`Deleting the document "${docName}"`),
231+
).toBeVisible();
197232

198-
await expect(datagridPage2.getByLabel('Loading data')).toBeHidden();
233+
await page
234+
.getByRole('button', {
235+
name: 'Confirm deletion',
236+
})
237+
.click();
199238

200-
await expect(docNamePage2).toHaveText(/.*/);
201-
const textDocNamePage2 = await docNamePage2.textContent();
239+
await expect(
240+
page.getByText('The document has been deleted.'),
241+
).toBeVisible();
202242

203-
expect(textDocNamePage1 !== textDocNamePage2).toBeTruthy();
243+
await expect(datagrid.getByText(docName!)).toBeHidden();
204244
});
205245
});

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ import {
1717

1818
import { PAGE_SIZE } from '../conf';
1919

20+
import { DocsGridActions } from './DocsGridActions';
21+
2022
const DocsGridStyle = createGlobalStyle`
2123
& .c__datagrid{
2224
max-height: 91%;
@@ -191,6 +193,12 @@ export const DocsGrid = () => {
191193
);
192194
},
193195
},
196+
{
197+
id: 'column-actions',
198+
renderCell: ({ row }) => {
199+
return <DocsGridActions doc={row} />;
200+
},
201+
},
194202
]}
195203
rows={docs}
196204
isLoading={isLoading}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import { Button } from '@openfun/cunningham-react';
2+
import React, { useState } from 'react';
3+
import { useTranslation } from 'react-i18next';
4+
5+
import { Box, DropButton, IconOptions, Text } from '@/components';
6+
import {
7+
Doc,
8+
ModalRemoveDoc,
9+
ModalUpdateDoc,
10+
} from '@/features/docs/doc-management';
11+
12+
interface DocsGridActionsProps {
13+
doc: Doc;
14+
}
15+
16+
export const DocsGridActions = ({ doc }: DocsGridActionsProps) => {
17+
const { t } = useTranslation();
18+
const [isModalUpdateOpen, setIsModalUpdateOpen] = useState(false);
19+
const [isModalRemoveOpen, setIsModalRemoveOpen] = useState(false);
20+
const [isDropOpen, setIsDropOpen] = useState(false);
21+
22+
return (
23+
<>
24+
<DropButton
25+
button={
26+
<IconOptions
27+
isOpen={isDropOpen}
28+
aria-label={t('Open the document options')}
29+
/>
30+
}
31+
onOpenChange={(isOpen) => setIsDropOpen(isOpen)}
32+
isOpen={isDropOpen}
33+
>
34+
<Box>
35+
{doc.abilities.partial_update && (
36+
<Button
37+
onClick={() => {
38+
setIsModalUpdateOpen(true);
39+
setIsDropOpen(false);
40+
}}
41+
color="primary-text"
42+
icon={<span className="material-icons">edit</span>}
43+
size="small"
44+
>
45+
<Text $theme="primary">{t('Update document')}</Text>
46+
</Button>
47+
)}
48+
{doc.abilities.destroy && (
49+
<Button
50+
onClick={() => {
51+
setIsModalRemoveOpen(true);
52+
setIsDropOpen(false);
53+
}}
54+
color="primary-text"
55+
icon={<span className="material-icons">delete</span>}
56+
size="small"
57+
>
58+
<Text $theme="primary">{t('Delete document')}</Text>
59+
</Button>
60+
)}
61+
</Box>
62+
</DropButton>
63+
{isModalUpdateOpen && (
64+
<ModalUpdateDoc onClose={() => setIsModalUpdateOpen(false)} doc={doc} />
65+
)}
66+
{isModalRemoveOpen && (
67+
<ModalRemoveDoc onClose={() => setIsModalRemoveOpen(false)} doc={doc} />
68+
)}
69+
</>
70+
);
71+
};

0 commit comments

Comments
 (0)