Skip to content

Commit

Permalink
feat: rename components
Browse files Browse the repository at this point in the history
  • Loading branch information
m-maillot committed Oct 1, 2024
1 parent 29f6632 commit 21cb713
Show file tree
Hide file tree
Showing 7 changed files with 167 additions and 162 deletions.
146 changes: 146 additions & 0 deletions targets/frontend/src/components/data/DataTable.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
import * as React from "react";
import { useState } from "react";
import Box from "@mui/material/Box";
import Table from "@mui/material/Table";
import TableBody from "@mui/material/TableBody";
import TableCell from "@mui/material/TableCell";
import TableContainer from "@mui/material/TableContainer";
import TableRow from "@mui/material/TableRow";
import Checkbox from "@mui/material/Checkbox";
import { Toolbar } from "./Toolbard";
import { Head } from "./Head";
import { Data, HeadCell, Source } from "./type";
import { PublishModal } from "./PublishModal";

type Props<T extends Data> = {
source: Source;
readonly headCells: HeadCell<T>[];
rows: T[];
onClickItem: (id: string) => void;
onClickCreation: () => void;
setSearch: (value: string | undefined) => void;
customFilters?: React.ReactNode;
};

type ItemCheck = {
[id: string]: boolean;
};

export const DataTable = <T extends Data>({
source,
headCells,
rows,
onClickItem,
onClickCreation,
setSearch,
customFilters,
}: Props<T>) => {
const [itemsCheck, setItemCheck] = useState<ItemCheck>({});
const [isOpen, showModal] = useState<boolean>(false);

const handleSelectAllClick = (event: React.ChangeEvent<HTMLInputElement>) => {
setItemCheck(
rows.reduce((acc, item) => {
acc[item.id] = event.target.checked;
return acc;
}, {} as ItemCheck)
);
};

const handleItemCheck = (id: string, checked: boolean) => {
setItemCheck({
...itemsCheck,
[id]: checked,
});
};

const handleClick = (id: string) => {
const atLeastOneChecked = Object.values(itemsCheck).some(
(checked) => checked
);
if (atLeastOneChecked) {
handleItemCheck(id, !itemsCheck[id]);
} else {
onClickItem(id);
}
};

const numSelected = (): number => {
return Object.values(itemsCheck).filter((item) => item).length;
};

return (
<Box sx={{ width: "100%" }}>
<Toolbar
numSelected={numSelected()}
onClickPublish={() => showModal(true)}
onClickCreation={onClickCreation}
setSearch={setSearch}
customFilters={customFilters}
/>
<TableContainer>
<Table sx={{ minWidth: 750 }} size="medium">
<Head
headCells={headCells}
numSelected={numSelected()}
onSelectAllClick={handleSelectAllClick}
rowCount={rows.length}
/>
<TableBody>
{rows.map((row, index) => {
const isItemSelected = itemsCheck[row.id] ?? false;
const labelId = `${source}-table-checkbox-${index}`;

return (
<TableRow
hover
key={row.id}
selected={isItemSelected}
sx={{ cursor: "pointer" }}
>
<TableCell padding="checkbox">
<Checkbox
color="primary"
checked={isItemSelected}
onChange={(event) =>
handleItemCheck(row.id, event.target.checked)
}
/>
</TableCell>
{headCells.map((head) => (
<TableCell
key={head.id}
id={labelId}
onClick={() => handleClick(row.id)}
>
{head.render
? head.render(row[head.dataIndex])
: (row[head.dataIndex] as React.ReactNode)}
</TableCell>
))}
</TableRow>
);
})}
</TableBody>
</Table>
</TableContainer>
{isOpen && (
<PublishModal
source={source}
contents={Object.entries(itemsCheck)
.filter(([_, value]) => value)
.map(([key, _]) => {
const find = rows.find((item) => item.id === key)!!;
return {
id: find.id,
title: find.title,
};
})}
open={isOpen}
onCancel={() => showModal(false)}
onClose={() => showModal(false)}
/>
)}
</Box>
);
};
6 changes: 3 additions & 3 deletions targets/frontend/src/components/data/Head.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@ import Checkbox from "@mui/material/Checkbox";
import * as React from "react";
import { Data, HeadCell } from "./type";

export type EnhancedTableProps<T extends Data> = {
export type HeadProps<T extends Data> = {
readonly headCells: HeadCell<T>[];
numSelected: number;
onSelectAllClick: (event: React.ChangeEvent<HTMLInputElement>) => void;
rowCount: number;
};

export const EnhancedTableHead = <T extends Data>({
export const Head = <T extends Data>({
onSelectAllClick,
numSelected,
rowCount,
headCells,
}: EnhancedTableProps<T>) => {
}: HeadProps<T>) => {
return (
<TableHead>
<TableRow>
Expand Down
18 changes: 11 additions & 7 deletions targets/frontend/src/components/data/Toolbard.tsx
Original file line number Diff line number Diff line change
@@ -1,27 +1,31 @@
import Toolbar from "@mui/material/Toolbar";
import {
Button,
FormGroup,
TextField,
Toolbar as MuiToolbar,
} from "@mui/material";
import { alpha } from "@mui/material/styles";
import Typography from "@mui/material/Typography";
import PublishIcon from "@mui/icons-material/Publish";
import * as React from "react";
import { Button, FormGroup, Stack, TextField } from "@mui/material";

interface EnhancedTableToolbarProps {
interface ToolbarProps {
numSelected: number;
onClickPublish: () => void;
onClickCreation: () => void;
setSearch: (value: string | undefined) => void;
customFilters?: React.ReactNode;
}

export const EnhancedTableToolbar = ({
export const Toolbar = ({
numSelected,
onClickPublish,
onClickCreation,
setSearch,
customFilters = undefined,
}: EnhancedTableToolbarProps) => {
}: ToolbarProps) => {
return (
<Toolbar
<MuiToolbar
sx={[
{
pl: { sm: 2 },
Expand Down Expand Up @@ -74,6 +78,6 @@ export const EnhancedTableToolbar = ({
Créer
</Button>
)}
</Toolbar>
</MuiToolbar>
);
};
147 changes: 1 addition & 146 deletions targets/frontend/src/components/data/index.tsx
Original file line number Diff line number Diff line change
@@ -1,146 +1 @@
import * as React from "react";
import { useState } from "react";
import Box from "@mui/material/Box";
import Table from "@mui/material/Table";
import TableBody from "@mui/material/TableBody";
import TableCell from "@mui/material/TableCell";
import TableContainer from "@mui/material/TableContainer";
import TableRow from "@mui/material/TableRow";
import Checkbox from "@mui/material/Checkbox";
import { EnhancedTableToolbar } from "./Toolbard";
import { EnhancedTableHead } from "./Head";
import { Data, HeadCell, Source } from "./type";
import { PublishModal } from "./PublishModal";

type Props<T extends Data> = {
source: Source;
readonly headCells: HeadCell<T>[];
rows: T[];
onClickItem: (id: string) => void;
onClickCreation: () => void;
setSearch: (value: string | undefined) => void;
customFilters?: React.ReactNode;
};

type ItemCheck = {
[id: string]: boolean;
};

export const EnhancedTable = <T extends Data>({
source,
headCells,
rows,
onClickItem,
onClickCreation,
setSearch,
customFilters,
}: Props<T>) => {
const [itemsCheck, setItemCheck] = useState<ItemCheck>({});
const [isOpen, showModal] = useState<boolean>(false);

const handleSelectAllClick = (event: React.ChangeEvent<HTMLInputElement>) => {
setItemCheck(
rows.reduce((acc, item) => {
acc[item.id] = event.target.checked;
return acc;
}, {} as ItemCheck)
);
};

const handleItemCheck = (id: string, checked: boolean) => {
setItemCheck({
...itemsCheck,
[id]: checked,
});
};

const handleClick = (id: string) => {
const atLeastOneChecked = Object.values(itemsCheck).some(
(checked) => checked
);
if (atLeastOneChecked) {
handleItemCheck(id, !itemsCheck[id]);
} else {
onClickItem(id);
}
};

const numSelected = (): number => {
return Object.values(itemsCheck).filter((item) => item).length;
};

return (
<Box sx={{ width: "100%" }}>
<EnhancedTableToolbar
numSelected={numSelected()}
onClickPublish={() => showModal(true)}
onClickCreation={onClickCreation}
setSearch={setSearch}
customFilters={customFilters}
/>
<TableContainer>
<Table sx={{ minWidth: 750 }} size="medium">
<EnhancedTableHead
headCells={headCells}
numSelected={numSelected()}
onSelectAllClick={handleSelectAllClick}
rowCount={rows.length}
/>
<TableBody>
{rows.map((row, index) => {
const isItemSelected = itemsCheck[row.id] ?? false;
const labelId = `${source}-table-checkbox-${index}`;

return (
<TableRow
hover
key={row.id}
selected={isItemSelected}
sx={{ cursor: "pointer" }}
>
<TableCell padding="checkbox">
<Checkbox
color="primary"
checked={isItemSelected}
onChange={(event) =>
handleItemCheck(row.id, event.target.checked)
}
/>
</TableCell>
{headCells.map((head) => (
<TableCell
key={head.id}
id={labelId}
onClick={() => handleClick(row.id)}
>
{head.render
? head.render(row[head.dataIndex])
: (row[head.dataIndex] as React.ReactNode)}
</TableCell>
))}
</TableRow>
);
})}
</TableBody>
</Table>
</TableContainer>
{isOpen && (
<PublishModal
source={source}
contents={Object.entries(itemsCheck)
.filter(([_, value]) => value)
.map(([key, _]) => {
const find = rows.find((item) => item.id === key)!!;
return {
id: find.id,
title: find.title,
};
})}
open={isOpen}
onCancel={() => showModal(false)}
onClose={() => showModal(false)}
/>
)}
</Box>
);
};
export * from "./DataTable";
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import React, { useState } from "react";

import { useListAgreementQuery } from "./list.query";
import { useRouter } from "next/router";
import { EnhancedTable } from "src/components/data";
import { Tooltip } from "@mui/material";
import GavelIcon from "@mui/icons-material/Gavel";
import Filter from "./Filter";
import { DataTable } from "../../../../components/data";

type AgreementData = {
id: string;
Expand All @@ -22,7 +22,7 @@ export const AgreementList = (): JSX.Element => {
isSupported,
});
return (
<EnhancedTable<AgreementData>
<DataTable<AgreementData>
source="conventions_collectives"
headCells={[
{
Expand Down
Loading

0 comments on commit 21cb713

Please sign in to comment.