@centreon/ui 24.4.39 → 24.4.40

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (95) hide show
  1. package/package.json +14 -19
  2. package/public/mockServiceWorker.js +1 -1
  3. package/src/Button/Icon/index.tsx +1 -1
  4. package/src/Button/Save/StartIcon.tsx +3 -3
  5. package/src/Button/Save/index.tsx +5 -9
  6. package/src/Checkbox/Checkbox.tsx +2 -2
  7. package/src/Checkbox/CheckboxGroup/index.tsx +2 -2
  8. package/src/Dashboard/Item.tsx +1 -1
  9. package/src/Dashboard/Layout.tsx +2 -2
  10. package/src/Dialog/index.tsx +1 -1
  11. package/src/FallbackPage/FallbackPage.tsx +3 -3
  12. package/src/FileDropZone/index.tsx +1 -3
  13. package/src/Form/Inputs/CheckboxGroup.tsx +4 -1
  14. package/src/Form/Inputs/index.tsx +1 -3
  15. package/src/Form/Inputs/models.ts +1 -9
  16. package/src/Graph/LineChart/BasicComponents/Lines/Threshold/Circle.tsx +2 -2
  17. package/src/Graph/LineChart/BasicComponents/Lines/Threshold/index.tsx +4 -5
  18. package/src/Graph/LineChart/BasicComponents/Thresholds.tsx +2 -2
  19. package/src/Graph/LineChart/BasicComponents/useFilterLines.ts +1 -1
  20. package/src/Graph/LineChart/InteractiveComponents/AnchorPoint/GuidingLines.tsx +2 -2
  21. package/src/Graph/LineChart/InteractiveComponents/Annotations/Annotation/index.tsx +3 -2
  22. package/src/Graph/LineChart/InteractiveComponents/Annotations/EventAnnotations.tsx +1 -1
  23. package/src/Graph/LineChart/Legend/useLegend.ts +3 -3
  24. package/src/Graph/LineChart/helpers/doc.ts +13 -16
  25. package/src/Graph/LineChart/helpers/index.ts +1 -1
  26. package/src/Graph/LineChart/index.stories.tsx +2 -4
  27. package/src/Graph/SingleBar/Thresholds.tsx +2 -2
  28. package/src/Graph/Text/Text.stories.tsx +4 -60
  29. package/src/Graph/common/timeSeries/index.ts +3 -3
  30. package/src/InputField/Select/Autocomplete/Connected/index.tsx +7 -10
  31. package/src/InputField/Select/Autocomplete/Draggable/SortableList.tsx +1 -1
  32. package/src/InputField/Select/Autocomplete/Draggable/SortableListContent.tsx +1 -1
  33. package/src/InputField/Select/Autocomplete/Draggable/index.tsx +1 -1
  34. package/src/InputField/Select/Autocomplete/index.tsx +115 -121
  35. package/src/InputField/Select/IconPopover/index.tsx +2 -2
  36. package/src/InputField/Select/index.tsx +1 -1
  37. package/src/InputField/Text/index.tsx +2 -2
  38. package/src/Listing/Cell/DataCell.tsx +1 -15
  39. package/src/Listing/Header/ListingHeader.tsx +1 -1
  40. package/src/Listing/Listing.styles.ts +3 -2
  41. package/src/Listing/index.stories.tsx +1 -12
  42. package/src/Listing/index.tsx +2 -1
  43. package/src/Module/index.tsx +4 -2
  44. package/src/RichTextEditor/RichTextEditor.tsx +1 -12
  45. package/src/SortableItems/index.tsx +7 -2
  46. package/src/ThemeProvider/index.tsx +0 -24
  47. package/src/TimePeriods/CustomTimePeriod/CompactCustomTimePeriod.styles.ts +7 -6
  48. package/src/TimePeriods/CustomTimePeriod/PopoverCustomTimePeriod/PickersStartEndDate.tsx +3 -8
  49. package/src/TimePeriods/CustomTimePeriod/PopoverCustomTimePeriod/models.ts +2 -0
  50. package/src/TimePeriods/DateTimePickerInput.tsx +19 -56
  51. package/src/TimePeriods/ResolutionTimePeriod.cypress.spec.tsx +9 -12
  52. package/src/TimePeriods/TimePeriods.cypress.spec.tsx +33 -9
  53. package/src/TimePeriods/helpers/index.ts +1 -1
  54. package/src/TimePeriods/index.stories.tsx +4 -12
  55. package/src/TimePeriods/index.tsx +2 -2
  56. package/src/api/QueryProvider.tsx +1 -1
  57. package/src/api/TestQueryProvider.tsx +1 -1
  58. package/src/api/useFetchQuery/index.ts +23 -27
  59. package/src/api/useMutationQuery/index.ts +21 -45
  60. package/src/components/Button/Icon/IconButton.tsx +2 -6
  61. package/src/components/DataTable/DataTable.stories.tsx +0 -40
  62. package/src/components/DataTable/DataTable.styles.ts +0 -3
  63. package/src/components/DataTable/DataTable.tsx +3 -3
  64. package/src/components/DataTable/Item/DataTableItem.styles.ts +2 -7
  65. package/src/components/DataTable/Item/DataTableItem.tsx +4 -4
  66. package/src/components/DataTable/index.ts +1 -3
  67. package/src/components/Form/Dashboard/DashboardForm.tsx +12 -15
  68. package/src/components/Layout/PageLayout/PageLayout.tsx +1 -1
  69. package/src/components/Layout/PageLayout/PageLayoutActions.tsx +0 -1
  70. package/src/components/Layout/PageLayout/PageLayoutBody.tsx +0 -1
  71. package/src/components/Layout/PageLayout/PageLayoutHeader.tsx +1 -5
  72. package/src/components/Layout/PageLayout/index.ts +1 -3
  73. package/src/components/Modal/Modal.styles.ts +1 -1
  74. package/src/components/Tooltip/ConfirmationTooltip/ConfirmationTooltip.stories.tsx +3 -3
  75. package/src/components/Tooltip/ConfirmationTooltip/ConfirmationTooltip.tsx +1 -1
  76. package/src/components/Tooltip/ConfirmationTooltip/models.ts +1 -1
  77. package/src/index.ts +2 -2
  78. package/src/queryParameters/url/index.ts +1 -5
  79. package/src/screens/dashboard/DashboardsDetail.stories.tsx +108 -0
  80. package/src/screens/dashboard/DashboardsOverview.stories.tsx +281 -0
  81. package/src/utils/index.ts +1 -2
  82. package/src/utils/useDateTimePickerAdapter.ts +309 -0
  83. package/src/utils/{useLicenseExpirationWarning.test.tsx → useLicenseExpirationWarning.cypress.spec.tsx} +37 -48
  84. package/src/utils/useLicenseExpirationWarning.ts +18 -18
  85. package/src/Form/Form.cypress.spec.tsx +0 -133
  86. package/src/Form/Inputs/List/Content.tsx +0 -62
  87. package/src/Form/Inputs/List/List.styles.ts +0 -29
  88. package/src/Form/Inputs/List/List.tsx +0 -58
  89. package/src/Form/Inputs/List/useList.ts +0 -81
  90. package/src/Module/Module.cypress.spec.tsx +0 -129
  91. package/src/components/DataTable/DataListing.tsx +0 -6
  92. package/src/components/DataTable/DataTable.cypress.spec.tsx +0 -193
  93. package/src/components/Layout/PageLayout/PageQuickAccess.tsx +0 -76
  94. package/src/components/Layout/PageLayout.cypress.spec.tsx +0 -66
  95. package/src/utils/usePluralizedTranslation.ts +0 -21
@@ -10,9 +10,6 @@ const useStyles = makeStyles()((theme) => ({
10
10
  gridGap: theme.spacing(2.5),
11
11
  gridTemplateColumns: `repeat(auto-fill, ${theme.spacing(45)})`
12
12
  },
13
- '&[data-variant="listing"]': {
14
- height: '100%'
15
- },
16
13
  '&[data-variant][data-is-empty="true"]': {
17
14
  display: 'flex',
18
15
  justifyContent: 'center',
@@ -1,11 +1,11 @@
1
- import { ReactElement, ReactNode } from 'react';
1
+ import React, { ReactElement, ReactNode } from 'react';
2
2
 
3
3
  import { useStyles } from './DataTable.styles';
4
4
 
5
5
  type DataTableProps = {
6
- children?: ReactNode | Array<ReactNode>;
6
+ children: ReactNode | Array<ReactNode>;
7
7
  isEmpty?: boolean;
8
- variant: 'grid' | 'listing';
8
+ variant?: 'grid';
9
9
  };
10
10
 
11
11
  /** *
@@ -1,18 +1,13 @@
1
1
  import { makeStyles } from 'tss-react/mui';
2
2
 
3
3
  const useStyles = makeStyles()((theme) => ({
4
- actions: {
5
- display: 'flex',
6
- flexDirection: 'row',
7
- justifyContent: 'space-between'
8
- },
9
4
  dataTableItem: {
10
5
  '& .MuiCardActionArea-root': {
11
6
  alignItems: 'flex-start',
12
7
  display: 'flex',
13
8
  flexDirection: 'column',
14
9
  height: '100%',
15
- justifyContent: 'flex-start'
10
+ justifyContent: 'space-between'
16
11
  },
17
12
  '& .MuiCardActions-root': {
18
13
  '& > span': {
@@ -20,13 +15,13 @@ const useStyles = makeStyles()((theme) => ({
20
15
  gap: theme.spacing(1)
21
16
  },
22
17
  display: 'flex',
18
+
23
19
  justifyContent: 'space-between'
24
20
  },
25
21
  borderRadius: theme.shape.borderRadius,
26
22
  display: 'flex',
27
23
  flexDirection: 'column',
28
24
  height: '186px',
29
- justifyContent: 'space-between',
30
25
  p: {
31
26
  color: theme.palette.text.secondary,
32
27
  letterSpacing: '0',
@@ -22,7 +22,7 @@ export interface DataTableItemProps {
22
22
  description?: string;
23
23
  hasActions?: boolean;
24
24
  hasCardAction?: boolean;
25
- labelsDelete?: {
25
+ labelsDelete: {
26
26
  cancel: string;
27
27
  confirm: {
28
28
  label: string;
@@ -76,20 +76,20 @@ const DataTableItem = forwardRef(
76
76
  {hasActions && (
77
77
  <MuiCardActions>
78
78
  <span>
79
- {onDelete && labelsDelete && (
79
+ {onDelete && (
80
80
  <ConfirmationTooltip
81
81
  confirmVariant="error"
82
82
  labels={labelsDelete}
83
83
  onConfirm={onDelete}
84
84
  >
85
- {({ toggleTooltip }) => (
85
+ {(openTooltip) => (
86
86
  <IconButton
87
87
  aria-label="delete"
88
88
  data-testid="delete"
89
89
  icon={<DeleteIcon />}
90
90
  size="small"
91
91
  variant="ghost"
92
- onClick={toggleTooltip}
92
+ onClick={openTooltip}
93
93
  />
94
94
  )}
95
95
  </ConfirmationTooltip>
@@ -2,11 +2,9 @@ import { DataTable as DataTableRoot } from './DataTable';
2
2
  import { DataTableItem } from './Item/DataTableItem';
3
3
  import { DataTableItemSkeleton } from './Item/DataTableItemSkeleton';
4
4
  import { DataTableEmptyState } from './EmptyState/DataTableEmptyState';
5
- import { DataListing } from './DataListing';
6
5
 
7
6
  export const DataTable = Object.assign(DataTableRoot, {
8
7
  EmptyState: DataTableEmptyState,
9
8
  Item: DataTableItem,
10
- ItemSkeleton: DataTableItemSkeleton,
11
- Listing: DataListing
9
+ ItemSkeleton: DataTableItemSkeleton
12
10
  });
@@ -1,8 +1,7 @@
1
1
  import { ReactElement, useCallback, useMemo } from 'react';
2
2
 
3
- import { string, number, object } from 'yup';
3
+ import * as Yup from 'yup';
4
4
  import { useTranslation } from 'react-i18next';
5
- import { equals } from 'ramda';
6
5
 
7
6
  import { InputType } from '../../../Form/Inputs/models';
8
7
  import { Form, FormProps } from '../../../Form';
@@ -85,8 +84,8 @@ const DashboardForm = ({
85
84
  }
86
85
  ],
87
86
  submit: (values, bag) => onSubmit?.(values, bag),
88
- validationSchema: object({
89
- description: string()
87
+ validationSchema: Yup.object().shape({
88
+ description: Yup.string()
90
89
  .label(labels?.entity?.description || '')
91
90
  .max(
92
91
  180,
@@ -94,19 +93,17 @@ const DashboardForm = ({
94
93
  `${p.label} ${t(labelMustBeMost)} ${p.max} ${t(labelCharacters)}`
95
94
  )
96
95
  .nullable(),
97
- globalRefreshInterval: object({
98
- interval: number().when('type', ([type], schema) => {
99
- if (equals(type, 'manual')) {
100
- schema
101
- .min(1, ({ min }) => t(labelMustBeAtLeast, { min }))
102
- .required(t(labelRequired) as string);
103
- }
104
-
105
- return schema.nullable();
96
+ globalRefreshInterval: Yup.object().shape({
97
+ interval: Yup.number().when('type', {
98
+ is: 'global',
99
+ otherwise: Yup.number().nullable(),
100
+ then: Yup.number()
101
+ .min(1, ({ min }) => t(labelMustBeAtLeast, { min }))
102
+ .required(t(labelRequired) as string)
106
103
  }),
107
- type: string()
104
+ type: Yup.string()
108
105
  }),
109
- name: string()
106
+ name: Yup.string()
110
107
  .label(labels?.entity?.name)
111
108
  .min(3, ({ min, label }) => t(labelMustBeAtLeast, { label, min }))
112
109
  .max(50, ({ max, label }) => t(labelMustBeMost, { label, max }))
@@ -14,7 +14,7 @@ export const PageLayout = ({
14
14
  const { classes } = useStyles();
15
15
 
16
16
  return (
17
- <section className={classes.pageLayout} data-variant={variant} id="page">
17
+ <section className={classes.pageLayout} data-variant={variant}>
18
18
  {children}
19
19
  </section>
20
20
  );
@@ -17,7 +17,6 @@ export const PageLayoutActions = ({
17
17
  <section
18
18
  className={classes.pageLayoutActions}
19
19
  data-row-reverse={rowReverse}
20
- id="actions"
21
20
  >
22
21
  {children}
23
22
  </section>
@@ -17,7 +17,6 @@ export const PageLayoutBody = ({
17
17
  <section
18
18
  className={classes.pageLayoutBody}
19
19
  data-has-background={hasBackground}
20
- id="page-body"
21
20
  >
22
21
  {children}
23
22
  </section>
@@ -11,9 +11,5 @@ export const PageLayoutHeader = ({
11
11
  }: PageLayoutHeaderProps): JSX.Element => {
12
12
  const { classes } = useStyles();
13
13
 
14
- return (
15
- <header className={classes.pageLayoutHeader} id="header">
16
- {children}
17
- </header>
18
- );
14
+ return <header className={classes.pageLayoutHeader}>{children}</header>;
19
15
  };
@@ -2,11 +2,9 @@ import { PageLayout as PageLayoutRoot } from './PageLayout';
2
2
  import { PageLayoutHeader } from './PageLayoutHeader';
3
3
  import { PageLayoutBody } from './PageLayoutBody';
4
4
  import { PageLayoutActions } from './PageLayoutActions';
5
- import { PageQuickAccess } from './PageQuickAccess';
6
5
 
7
6
  export const PageLayout = Object.assign(PageLayoutRoot, {
8
7
  Actions: PageLayoutActions,
9
8
  Body: PageLayoutBody,
10
- Header: PageLayoutHeader,
11
- QuickAccess: PageQuickAccess
9
+ Header: PageLayoutHeader
12
10
  });
@@ -57,7 +57,7 @@ const useStyles = makeStyles<{
57
57
  flexDirection: 'row',
58
58
  gap: theme.spacing(2),
59
59
  justifyContent: 'flex-end',
60
- right: theme.spacing(2.5)
60
+ right: theme.spacing(2)
61
61
  },
62
62
  modalBody: {
63
63
  '& > p': {
@@ -15,7 +15,7 @@ type Story = StoryObj<typeof ConfirmationTooltip>;
15
15
 
16
16
  export const Default: Story = {
17
17
  args: {
18
- children: ({ toggleTooltip }) => (
18
+ children: (toggleTooltip) => (
19
19
  <IconButton icon={<SaveIcon />} onClick={toggleTooltip} />
20
20
  ),
21
21
  labels: {
@@ -30,7 +30,7 @@ export const Default: Story = {
30
30
 
31
31
  export const WithConfirmVariant: Story = {
32
32
  args: {
33
- children: ({ toggleTooltip }) => (
33
+ children: (toggleTooltip) => (
34
34
  <IconButton icon={<DeleteIcon color="error" />} onClick={toggleTooltip} />
35
35
  ),
36
36
  confirmVariant: 'error',
@@ -46,7 +46,7 @@ export const WithConfirmVariant: Story = {
46
46
 
47
47
  export const WithSecondaryLabel: Story = {
48
48
  args: {
49
- children: ({ toggleTooltip }) => (
49
+ children: (toggleTooltip) => (
50
50
  <IconButton icon={<DeleteIcon color="error" />} onClick={toggleTooltip} />
51
51
  ),
52
52
  confirmVariant: 'error',
@@ -49,7 +49,7 @@ export const ConfirmationTooltip = ({
49
49
  return (
50
50
  <ClickAwayListener onClickAway={close}>
51
51
  <div>
52
- {children({ isOpen, toggleTooltip: isOpen ? close : open })}
52
+ {children(isOpen ? close : open)}
53
53
  <Popper
54
54
  anchorEl={anchorElement}
55
55
  className={classes.popper}
@@ -11,7 +11,7 @@ interface Labels {
11
11
  }
12
12
 
13
13
  export interface Props {
14
- children: ({ toggleTooltip, isOpen }) => ReactElement;
14
+ children: (toggleTooltip) => ReactElement;
15
15
  confirmVariant?: ActionVariants;
16
16
  labels: Labels;
17
17
  onConfirm: () => void;
package/src/index.ts CHANGED
@@ -163,8 +163,8 @@ export * from './Graph/common/timeSeries';
163
163
 
164
164
  export { default as TimePeriods } from './TimePeriods';
165
165
  export { default as SimpleCustomTimePeriod } from './TimePeriods/CustomTimePeriod/SimpleCustomTimePeriod';
166
- export { default as DateTimePickerInput } from './TimePeriods/DateTimePickerInput';
167
166
  export * from './ParentSize';
168
- export { default as Subtitle } from './Typography/Subtitle';
167
+
169
168
  export { default as TablePagination } from './Listing/ActionBar/Pagination';
170
169
  export { default as PaginationActions } from './Listing/ActionBar/PaginationActions';
170
+ export { default as Subtitle } from './Typography/Subtitle';
@@ -1,4 +1,4 @@
1
- import { fromPairs, startsWith } from 'ramda';
1
+ import { fromPairs } from 'ramda';
2
2
 
3
3
  import { QueryParameter } from '../models';
4
4
 
@@ -25,10 +25,6 @@ const getUrlQueryParameters = <
25
25
 
26
26
  const entries = [...urlParams.entries()].map<[string, string]>(
27
27
  ([key, value]) => {
28
- if (startsWith('/', value)) {
29
- return [key, value];
30
- }
31
-
32
28
  return [key, JSON.parse(value)];
33
29
  }
34
30
  );
@@ -0,0 +1,108 @@
1
+ /* eslint-disable @typescript-eslint/no-non-null-assertion */
2
+ import React, { ReactElement } from 'react';
3
+
4
+ import { Meta } from '@storybook/react';
5
+
6
+ import {
7
+ Add as AddIcon,
8
+ Settings as SettingsIcon,
9
+ Share as ShareIcon
10
+ } from '@mui/icons-material';
11
+
12
+ import {
13
+ Button,
14
+ IconButton,
15
+ Menu,
16
+ PageHeader,
17
+ PageLayout
18
+ } from '../../components';
19
+
20
+ const meta: Meta = {
21
+ args: {
22
+ actions: {}
23
+ },
24
+ parameters: {
25
+ layout: 'fullscreen'
26
+ },
27
+ title: 'screens/Dashboards detail'
28
+ };
29
+
30
+ export default meta;
31
+
32
+ const DefaultView = (args): ReactElement => {
33
+ const { data } = args;
34
+
35
+ return (
36
+ <PageLayout>
37
+ <PageLayout.Header>
38
+ <PageHeader>
39
+ <PageHeader.Main>
40
+ <PageHeader.Menu>
41
+ <Menu>
42
+ <Menu.Button />
43
+ <Menu.Items>
44
+ <Menu.Item>Menu Item</Menu.Item>
45
+ <Menu.Item>Menu Item</Menu.Item>
46
+ <Menu.Item>Menu Item</Menu.Item>
47
+ <Menu.Divider />
48
+ <Menu.Item>
49
+ <Button
50
+ icon={<AddIcon />}
51
+ iconVariant="start"
52
+ variant="ghost"
53
+ >
54
+ Add item
55
+ </Button>
56
+ </Menu.Item>
57
+ </Menu.Items>
58
+ </Menu>
59
+ </PageHeader.Menu>
60
+ <PageHeader.Title
61
+ description={data.dashboard.description}
62
+ title={data.dashboard.name}
63
+ />
64
+ </PageHeader.Main>
65
+ </PageHeader>
66
+ </PageLayout.Header>
67
+ <PageLayout.Body>
68
+ <PageLayout.Actions>
69
+ <IconButton
70
+ aria-label="edit"
71
+ data-testid="edit"
72
+ icon={<SettingsIcon />}
73
+ size="small"
74
+ variant="ghost"
75
+ />
76
+ <IconButton
77
+ aria-label="share"
78
+ data-testid="share"
79
+ icon={<ShareIcon />}
80
+ size="small"
81
+ variant="ghost"
82
+ />
83
+ </PageLayout.Actions>
84
+ </PageLayout.Body>
85
+ </PageLayout>
86
+ );
87
+ };
88
+
89
+ export const Default = {
90
+ args: {
91
+ data: {
92
+ dashboard: {
93
+ description:
94
+ 'Description et culpa sit commodo ea enim excepteur elit. Velit irure velit tempor culpa commodo eu adipisicing eu proident ullamco.',
95
+ id: 1,
96
+ name: 'Dashboard 1'
97
+ }
98
+ }
99
+ },
100
+ render: DefaultView
101
+ };
102
+
103
+ export const AsEditLayoutState = {
104
+ args: {
105
+ ...Default.args
106
+ },
107
+ render: DefaultView
108
+ };
@@ -0,0 +1,281 @@
1
+ /* eslint-disable @typescript-eslint/no-non-null-assertion */
2
+ import { useEffect } from 'react';
3
+
4
+ import { Meta } from '@storybook/react';
5
+ import { atom, useAtom } from 'jotai';
6
+
7
+ import { Add as AddIcon } from '@mui/icons-material';
8
+
9
+ import {
10
+ Button,
11
+ DashboardForm,
12
+ DashboardFormProps,
13
+ DataTable,
14
+ Modal,
15
+ PageHeader,
16
+ PageLayout
17
+ } from '../../components';
18
+ import { Default as DashboardFormDefaultStory } from '../../components/Form/Dashboard/DashboardForm.stories';
19
+
20
+ const meta: Meta = {
21
+ args: {
22
+ actions: {
23
+ create: {
24
+ label: 'Create a dashboard'
25
+ }
26
+ },
27
+ deleteConfirmation: {
28
+ labels: {
29
+ actions: {
30
+ cancel: 'Cancel',
31
+ confirm: 'Delete'
32
+ },
33
+ description: (name) => (
34
+ <>
35
+ Are you sure you want to delete <strong>{name}</strong> ?
36
+ </>
37
+ ),
38
+ title: 'Delete dashboard'
39
+ }
40
+ },
41
+ form: {
42
+ labels: {
43
+ ...DashboardFormDefaultStory.args!.labels,
44
+ title: {
45
+ create: 'Create dashboard',
46
+ update: 'Update dashboard'
47
+ }
48
+ }
49
+ },
50
+ list: {
51
+ emptyState: {
52
+ labels: {
53
+ actions: {
54
+ create: 'Create dashboard'
55
+ },
56
+ title: 'No dashboards found'
57
+ }
58
+ }
59
+ },
60
+ title: 'Dashboards overview'
61
+ },
62
+ parameters: {
63
+ layout: 'fullscreen'
64
+ },
65
+ title: 'screens/Dashboards overview'
66
+ };
67
+
68
+ export default meta;
69
+
70
+ interface dashboardItem {
71
+ description: string;
72
+ id: number;
73
+ name: string;
74
+ }
75
+
76
+ const dialogStateAtom = atom<{
77
+ item: dashboardItem | null;
78
+ open: boolean;
79
+ variant: DashboardFormProps['variant'];
80
+ }>({
81
+ item: null,
82
+ open: false,
83
+ variant: 'create'
84
+ });
85
+
86
+ const deleteDialogStateAtom = atom<{
87
+ item: dashboardItem | null;
88
+ open: boolean;
89
+ }>({
90
+ item: null,
91
+ open: false
92
+ });
93
+
94
+ const dataDashboardsAtom = atom<Array<dashboardItem>>([]);
95
+
96
+ const DefaultView = (args): JSX.Element => {
97
+ const { data, title, actions, list, form, deleteConfirmation } = args;
98
+ const [dialogState, setDialogState] = useAtom(dialogStateAtom);
99
+ const [deleteDialogState, setDeleteDialogState] = useAtom(
100
+ deleteDialogStateAtom
101
+ );
102
+ const [dataDashboards, setDataDashboards] = useAtom(dataDashboardsAtom);
103
+
104
+ useEffect(() => {
105
+ setDataDashboards(data.dashboards);
106
+ }, [data.dashboards]);
107
+
108
+ const createDashboard = (d): void => {
109
+ const dashboard = { ...d };
110
+ dashboard.id = dataDashboards.length
111
+ ? Math.max(...dataDashboards.map((db) => db.id)) + 1
112
+ : 0;
113
+ setDataDashboards((prev) =>
114
+ [...prev, dashboard].sort((a, b) => a.name.localeCompare(b.name))
115
+ );
116
+ setDialogState({ item: null, open: false, variant: 'create' });
117
+ };
118
+
119
+ const updateDashboard = (d): void => {
120
+ setDataDashboards((prev) =>
121
+ prev
122
+ .map((dashboard) => (dashboard.id === d.id ? d : dashboard))
123
+ .sort((a, b) => a.name.localeCompare(b.name))
124
+ );
125
+ setDialogState({ item: null, open: false, variant: 'update' });
126
+ };
127
+
128
+ const deleteDashboard = (id): void => {
129
+ setDataDashboards((prev) =>
130
+ prev.filter((dashboard) => dashboard.id !== id)
131
+ );
132
+ };
133
+
134
+ return (
135
+ <PageLayout>
136
+ <PageLayout.Header>
137
+ <PageHeader>
138
+ <PageHeader.Main>
139
+ <PageHeader.Title title={title} />
140
+ </PageHeader.Main>
141
+ </PageHeader>
142
+ </PageLayout.Header>
143
+ <PageLayout.Body>
144
+ <PageLayout.Actions>
145
+ {dataDashboards.length !== 0 && (
146
+ <Button
147
+ aria-label="add"
148
+ icon={<AddIcon />}
149
+ iconVariant="start"
150
+ onClick={() =>
151
+ setDialogState({ item: null, open: true, variant: 'create' })
152
+ }
153
+ >
154
+ {actions.create.label}
155
+ </Button>
156
+ )}
157
+ </PageLayout.Actions>
158
+
159
+ <DataTable isEmpty={dataDashboards.length === 0}>
160
+ {dataDashboards.length === 0 ? (
161
+ <DataTable.EmptyState
162
+ labels={list.emptyState.labels}
163
+ onCreate={() =>
164
+ setDialogState({
165
+ item: null,
166
+ open: true,
167
+ variant: 'create'
168
+ })
169
+ }
170
+ />
171
+ ) : (
172
+ dataDashboards.map((dashboard) => (
173
+ <DataTable.Item
174
+ hasActions
175
+ hasCardAction
176
+ description={dashboard.description}
177
+ key={dashboard.id}
178
+ title={dashboard.name}
179
+ onDelete={() =>
180
+ setDeleteDialogState({ item: dashboard, open: true })
181
+ }
182
+ onEdit={() =>
183
+ setDialogState({
184
+ item: dashboard,
185
+ open: true,
186
+ variant: 'update'
187
+ })
188
+ }
189
+ />
190
+ ))
191
+ )}
192
+ </DataTable>
193
+ </PageLayout.Body>
194
+
195
+ <Modal
196
+ open={dialogState.open}
197
+ onClose={() =>
198
+ setDialogState({
199
+ item: null,
200
+ open: false,
201
+ variant: dialogState.variant
202
+ })
203
+ }
204
+ >
205
+ <Modal.Header>
206
+ {form.labels.title[dialogState.variant ?? 'create']}
207
+ </Modal.Header>
208
+ <Modal.Body>
209
+ <DashboardForm
210
+ labels={DashboardFormDefaultStory!.args!.labels!}
211
+ resource={dialogState.item || undefined}
212
+ variant={dialogState.variant}
213
+ onCancel={() =>
214
+ setDialogState({
215
+ item: null,
216
+ open: false,
217
+ variant: dialogState.variant
218
+ })
219
+ }
220
+ onSubmit={(values) =>
221
+ dialogState.variant === 'create'
222
+ ? createDashboard(values)
223
+ : updateDashboard(values)
224
+ }
225
+ />
226
+ </Modal.Body>
227
+ </Modal>
228
+ <Modal
229
+ open={deleteDialogState.open}
230
+ onClose={() =>
231
+ setDeleteDialogState({
232
+ ...deleteDialogState,
233
+ open: false
234
+ })
235
+ }
236
+ >
237
+ <Modal.Header>{deleteConfirmation.labels.title}</Modal.Header>
238
+ <Modal.Body>
239
+ <p>
240
+ {deleteConfirmation.labels.description(
241
+ deleteDialogState.item?.name
242
+ )}
243
+ </p>
244
+ </Modal.Body>
245
+ <Modal.Actions
246
+ isDanger
247
+ labels={deleteConfirmation.labels.actions}
248
+ onCancel={() => setDeleteDialogState({ item: null, open: false })}
249
+ onConfirm={() => {
250
+ deleteDashboard(deleteDialogState.item?.id);
251
+ setDeleteDialogState({ item: null, open: false });
252
+ }}
253
+ />
254
+ </Modal>
255
+ </PageLayout>
256
+ );
257
+ };
258
+
259
+ export const Default = {
260
+ args: {
261
+ data: {
262
+ dashboards: [
263
+ { description: 'Dashboard 1 description', id: 1, name: 'Dashboard 1' },
264
+ { description: 'Dashboard 2 description', id: 2, name: 'Dashboard 2' },
265
+ { description: 'Dashboard 3 description', id: 3, name: 'Dashboard 3' },
266
+ { description: 'Dashboard 4 description', id: 4, name: 'Dashboard 4' },
267
+ { description: 'Dashboard 5 description', id: 5, name: 'Dashboard 5' }
268
+ ]
269
+ }
270
+ },
271
+ render: DefaultView
272
+ };
273
+
274
+ export const AsInitialState = {
275
+ args: {
276
+ data: {
277
+ dashboards: []
278
+ }
279
+ },
280
+ render: DefaultView
281
+ };