Skip to content

Commit 5329329

Browse files
authored
Merge pull request #329 from TheauW/twartel-error-component
Create the error component
2 parents f2f2a55 + d3a3235 commit 5329329

10 files changed

Lines changed: 132 additions & 86 deletions

File tree

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,6 @@ next-env.d.ts
4444

4545
# storybook
4646
**/.cache
47+
48+
# vscode
49+
**/.vscode

docs/developer/contribute.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818
- Updating or creating Storybook documentation if you are contributing to the `diracx-web-components` library.
1919
- **Writing/Updating Tests:** When you change or add new code, make sure to write or update tests accordingly. This helps maintain the reliability and stability of the codebase.
2020

21-
**Note:** Don't forget to update the `extensions` code if you integrate breaking changes in the `diracx-web-components` library. See [Managing the extension](manage_extension.md) for further details.
21+
**Good to know:** If you create an export function or component in `diracx-web-components`, you must add it to the `index.ts` file and run `npm run build` inside `packages/diracx-web-components` to ensure the pre-commit hook passes.
22+
23+
**Note:** Don't forget to update the `packages/extensions` code if you integrate breaking changes in the `diracx-web-components` library. See [Managing the extension](manage_extension.md) for further details.
2224

2325
### 3. Commit
2426

packages/diracx-web-components/src/components/shared/DataTable.tsx

Lines changed: 46 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -640,62 +640,59 @@ export function DataTable<T extends Record<string, unknown>>({
640640
))}
641641
</>
642642
)}
643-
itemContent={(index, row: Row<T>) => (
644-
<>
645-
<TableCell
646-
padding="checkbox"
647-
style={{
648-
position: "sticky",
649-
left: 0,
650-
zIndex: 1,
651-
width: checkboxWidth,
652-
backgroundColor:
653-
theme.palette.tableRow !== undefined
654-
? index % 2 === 0
655-
? theme.palette.tableRow.odd
656-
: theme.palette.tableRow.even
657-
: theme.palette.background.default,
658-
textOverflow: "ellipsis",
659-
whiteSpace: "nowrap",
660-
}}
661-
>
662-
<Checkbox
663-
name={`select-row-${row.id}`}
664-
checked={row.getIsSelected()}
665-
disabled={!row.getCanSelect()}
666-
onChange={row.getToggleSelectedHandler()}
667-
/>
668-
</TableCell>
669-
{row.getVisibleCells().map((cell) => (
643+
itemContent={(index, row: Row<T>) => {
644+
const rowColor =
645+
theme.palette.tableRow !== undefined
646+
? index % 2 === 0
647+
? theme.palette.tableRow.even
648+
: theme.palette.tableRow.odd
649+
: theme.palette.background.default;
650+
return (
651+
<>
670652
<TableCell
671-
key={cell.id}
653+
padding="checkbox"
672654
style={{
673-
position: cell.column.getIsPinned() ? "sticky" : "static",
674-
left:
675-
cell.column.getIsPinned() === "left"
676-
? checkboxWidth
677-
: undefined,
678-
right:
679-
cell.column.getIsPinned() === "right" ? 0 : undefined,
680-
zIndex: cell.column.getIsPinned() ? 1 : 0,
681-
width: cell.column.getSize(),
682-
backgroundColor: cell.column.getIsPinned()
683-
? theme.palette.tableRow !== undefined
684-
? index % 2 === 0
685-
? theme.palette.tableRow.odd
686-
: theme.palette.tableRow.even
687-
: theme.palette.background.default
688-
: undefined,
689-
overflow: "hidden",
655+
position: "sticky",
656+
left: 0,
657+
zIndex: 1,
658+
width: checkboxWidth,
659+
backgroundColor: rowColor,
690660
textOverflow: "ellipsis",
691661
whiteSpace: "nowrap",
692662
}}
693663
>
694-
{flexRender(cell.column.columnDef.cell, cell.getContext())}
664+
<Checkbox
665+
name={`select-row-${row.id}`}
666+
checked={row.getIsSelected()}
667+
disabled={!row.getCanSelect()}
668+
onChange={row.getToggleSelectedHandler()}
669+
/>
695670
</TableCell>
696-
))}
697-
</>
698-
)}
671+
{row.getVisibleCells().map((cell) => (
672+
<TableCell
673+
key={cell.id}
674+
style={{
675+
position: cell.column.getIsPinned() ? "sticky" : "static",
676+
left:
677+
cell.column.getIsPinned() === "left"
678+
? checkboxWidth
679+
: undefined,
680+
right:
681+
cell.column.getIsPinned() === "right" ? 0 : undefined,
682+
zIndex: cell.column.getIsPinned() ? 1 : 0,
683+
width: cell.column.getSize(),
684+
backgroundColor: rowColor,
685+
overflow: "hidden",
686+
textOverflow: "ellipsis",
687+
whiteSpace: "nowrap",
688+
}}
689+
>
690+
{flexRender(cell.column.columnDef.cell, cell.getContext())}
691+
</TableCell>
692+
))}
693+
</>
694+
);
695+
}}
699696
/>
700697
<TablePagination
701698
component="div"
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { Box, Alert, AlertTitle, Button } from "@mui/material";
2+
import ReplayIcon from "@mui/icons-material/Replay";
3+
4+
/**
5+
* This component is used to display an error message
6+
*
7+
* @param s The error message (optional)
8+
* @param reset The function to call to reset the error (optional)
9+
* @returns Error component
10+
*/
11+
export function ErrorBox({ msg, reset }: { msg?: string; reset?: () => void }) {
12+
return (
13+
<Box
14+
sx={{
15+
display: "flex",
16+
height: 1,
17+
width: 1,
18+
alignItems: "center",
19+
justifyContent: "center",
20+
}}
21+
>
22+
<Alert
23+
severity="error"
24+
action={
25+
reset && (
26+
<Button sx={{ color: "black" }} onClick={reset}>
27+
<ReplayIcon />
28+
</Button>
29+
)
30+
}
31+
>
32+
<AlertTitle>Error</AlertTitle>
33+
{msg ? msg : "Something went wrong"}
34+
</Alert>
35+
</Box>
36+
);
37+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
export { DataTable } from "./DataTable";
22
export { FilterForm } from "./FilterForm";
33
export { FilterToolbar } from "./FilterToolbar";
4+
export { ErrorBox } from "./ErrorBox";
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import React from "react";
2+
import type { Meta, StoryObj } from "@storybook/react";
3+
4+
import { ErrorBox } from "../src/components/shared/ErrorBox";
5+
6+
const meta = {
7+
title: "Shared/ErrorBox",
8+
component: ErrorBox,
9+
parameters: {
10+
layout: "centered",
11+
},
12+
tags: ["autodocs"],
13+
decorators: [
14+
(Story) => {
15+
return <Story />;
16+
},
17+
],
18+
} satisfies Meta<typeof ErrorBox>;
19+
20+
export default meta;
21+
22+
type Story = StoryObj<typeof meta>;
23+
24+
export const Default: Story = {};
Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
11
"use client";
22

3-
export default function Error({
4-
_error,
3+
import { ErrorBox } from "@dirac-grid/diracx-web-components/components";
4+
5+
export default function GlobalError({
6+
error,
57
reset,
68
}: {
7-
_error: Error;
9+
error: Error;
810
reset: () => void;
911
}) {
10-
return (
11-
<div>
12-
<h2>Something went wrong!</h2>
13-
<button onClick={() => reset()}>Try again</button>
14-
</div>
15-
);
12+
return <ErrorBox msg={error.message} reset={reset} />;
1613
}
Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,13 @@
11
"use client";
22

3+
import { ErrorBox } from "@dirac-grid/diracx-web-components/components";
4+
35
export default function GlobalError({
4-
_error,
6+
error,
57
reset,
68
}: {
7-
_error: Error;
9+
error: Error;
810
reset: () => void;
911
}) {
10-
return (
11-
<html>
12-
<body>
13-
<h2>Something went wrong!</h2>
14-
<button onClick={() => reset()}>Try again</button>
15-
</body>
16-
</html>
17-
);
12+
return <ErrorBox msg={error.message} reset={reset} />;
1813
}
Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,13 @@
11
"use client";
22

3-
//Error fallback component
4-
export default function Error({
3+
import { ErrorBox } from "@dirac-grid/diracx-web-components/components";
4+
5+
export default function GlobalError({
56
error,
67
reset,
78
}: {
89
error: Error;
910
reset: () => void;
1011
}) {
11-
return (
12-
<div>
13-
<h2>Something went wrong!</h2>
14-
<button onClick={() => reset()}>Try again</button>
15-
</div>
16-
);
12+
return <ErrorBox msg={error.message} reset={reset} />;
1713
}
Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,13 @@
11
"use client";
22

3-
// Error fallback
3+
import { ErrorBox } from "@dirac-grid/diracx-web-components/components";
4+
45
export default function GlobalError({
56
error,
67
reset,
78
}: {
89
error: Error;
910
reset: () => void;
1011
}) {
11-
return (
12-
<html>
13-
<body>
14-
<h2>Something went wrong!</h2>
15-
<button onClick={() => reset()}>Try again</button>
16-
</body>
17-
</html>
18-
);
12+
return <ErrorBox msg={error.message} reset={reset} />;
1913
}

0 commit comments

Comments
 (0)