mirror of
https://github.com/cssgunc/compass.git
synced 2025-04-20 10:30:16 -04:00
diverted responsibility of handleRowChange to Drawer instead of Table to remove repetition
This commit is contained in:
parent
08d3be079d
commit
2bf3df68f8
|
@ -1,4 +1,4 @@
|
||||||
import { FunctionComponent, ReactNode } from "react";
|
import { Dispatch, FunctionComponent, ReactNode, SetStateAction } from "react";
|
||||||
import React, { useState } from "react";
|
import React, { useState } from "react";
|
||||||
import { ChevronDoubleLeftIcon } from "@heroicons/react/24/solid";
|
import { ChevronDoubleLeftIcon } from "@heroicons/react/24/solid";
|
||||||
import {
|
import {
|
||||||
|
@ -23,7 +23,7 @@ type DrawerProps = {
|
||||||
editableContent?: any;
|
editableContent?: any;
|
||||||
onSave?: (content: any) => void;
|
onSave?: (content: any) => void;
|
||||||
rowContent?: any;
|
rowContent?: any;
|
||||||
onRowUpdate?: (content: any) => void;
|
setData: Dispatch<SetStateAction<any>>;
|
||||||
};
|
};
|
||||||
|
|
||||||
interface EditContent {
|
interface EditContent {
|
||||||
|
@ -37,13 +37,23 @@ const Drawer: FunctionComponent<DrawerProps> = ({
|
||||||
onSave,
|
onSave,
|
||||||
editableContent,
|
editableContent,
|
||||||
rowContent,
|
rowContent,
|
||||||
onRowUpdate,
|
setData,
|
||||||
}) => {
|
}) => {
|
||||||
const [isOpen, setIsOpen] = useState(false);
|
const [isOpen, setIsOpen] = useState(false);
|
||||||
const [isFull, setIsFull] = useState(false);
|
const [isFull, setIsFull] = useState(false);
|
||||||
const [isFavorite, setIsFavorite] = useState(false);
|
const [isFavorite, setIsFavorite] = useState(false);
|
||||||
const [tempRowContent, setTempRowContent] = useState(rowContent);
|
const [tempRowContent, setTempRowContent] = useState(rowContent);
|
||||||
|
|
||||||
|
const onRowUpdate = (updatedRow: any) => {
|
||||||
|
setData((prevData: any) => (
|
||||||
|
prevData.map((row: any) => (
|
||||||
|
row.id === updatedRow.id
|
||||||
|
? updatedRow
|
||||||
|
: row
|
||||||
|
))
|
||||||
|
))
|
||||||
|
};
|
||||||
|
|
||||||
const handleTempRowContentChange = (e) => {
|
const handleTempRowContentChange = (e) => {
|
||||||
const { name, value } = e.target;
|
const { name, value } = e.target;
|
||||||
console.log(name);
|
console.log(name);
|
||||||
|
|
|
@ -26,16 +26,6 @@ export default function ResourceTable({ data, setData }: ResourceTableProps ) {
|
||||||
"employee",
|
"employee",
|
||||||
])
|
])
|
||||||
|
|
||||||
const handleRowUpdate = (updatedRow: Resource) => {
|
|
||||||
setData(prevData => (
|
|
||||||
prevData.map(row => (
|
|
||||||
row.id === updatedRow.id
|
|
||||||
? updatedRow
|
|
||||||
: row
|
|
||||||
))
|
|
||||||
))
|
|
||||||
};
|
|
||||||
|
|
||||||
const columns: ColumnDef<Resource, any>[] = [
|
const columns: ColumnDef<Resource, any>[] = [
|
||||||
columnHelper.accessor("name", {
|
columnHelper.accessor("name", {
|
||||||
header: () => (
|
header: () => (
|
||||||
|
@ -47,7 +37,7 @@ export default function ResourceTable({ data, setData }: ResourceTableProps ) {
|
||||||
<RowOpenAction
|
<RowOpenAction
|
||||||
title={info.getValue()}
|
title={info.getValue()}
|
||||||
rowData={info.row.original}
|
rowData={info.row.original}
|
||||||
onRowUpdate={handleRowUpdate}
|
setData={setData}
|
||||||
/>
|
/>
|
||||||
),
|
),
|
||||||
}),
|
}),
|
||||||
|
|
|
@ -1,14 +1,14 @@
|
||||||
import Drawer from "@/components/Drawer/Drawer";
|
import Drawer from "@/components/Drawer/Drawer";
|
||||||
import { useState } from "react";
|
import DataPoint from "@/utils/models/DataPoint";
|
||||||
import { DataPoint } from "@/components/Table/Table";
|
import { Dispatch, SetStateAction, useState } from "react";
|
||||||
|
|
||||||
type RowOpenActionProps = {
|
type RowOpenActionProps<T extends DataPoint> = {
|
||||||
title: string,
|
title: string,
|
||||||
rowData: DataPoint,
|
rowData: T,
|
||||||
onRowUpdate: (updatedRow: DataPoint) => void;
|
setData: Dispatch<SetStateAction<T[]>>
|
||||||
}
|
}
|
||||||
|
|
||||||
export const RowOpenAction = ({ title, rowData, onRowUpdate }: RowOpenActionProps) => {
|
export function RowOpenAction<T extends DataPoint>({ title, rowData, setData }: RowOpenActionProps<T>) {
|
||||||
const [pageContent, setPageContent] = useState("");
|
const [pageContent, setPageContent] = useState("");
|
||||||
|
|
||||||
const handleDrawerContentChange = (newContent: string) => {
|
const handleDrawerContentChange = (newContent: string) => {
|
||||||
|
@ -25,7 +25,7 @@ export const RowOpenAction = ({ title, rowData, onRowUpdate }: RowOpenActionProp
|
||||||
editableContent={pageContent}
|
editableContent={pageContent}
|
||||||
rowContent={rowData}
|
rowContent={rowData}
|
||||||
onSave={handleDrawerContentChange}
|
onSave={handleDrawerContentChange}
|
||||||
onRowUpdate={onRowUpdate}
|
setData={setData}
|
||||||
>
|
>
|
||||||
{pageContent}
|
{pageContent}
|
||||||
</Drawer>
|
</Drawer>
|
||||||
|
|
|
@ -27,16 +27,6 @@ export default function ServiceTable({ data, setData }: ServiceTableProps ) {
|
||||||
"employee",
|
"employee",
|
||||||
])
|
])
|
||||||
|
|
||||||
const handleRowUpdate = (updatedRow: Service) => {
|
|
||||||
setData(prevData => (
|
|
||||||
prevData.map(row => (
|
|
||||||
row.id === updatedRow.id
|
|
||||||
? updatedRow
|
|
||||||
: row
|
|
||||||
))
|
|
||||||
))
|
|
||||||
};
|
|
||||||
|
|
||||||
const columns: ColumnDef<Service, any>[] = [
|
const columns: ColumnDef<Service, any>[] = [
|
||||||
columnHelper.accessor("name", {
|
columnHelper.accessor("name", {
|
||||||
header: () => (
|
header: () => (
|
||||||
|
@ -48,7 +38,7 @@ export default function ServiceTable({ data, setData }: ServiceTableProps ) {
|
||||||
<RowOpenAction
|
<RowOpenAction
|
||||||
title={info.getValue()}
|
title={info.getValue()}
|
||||||
rowData={info.row.original}
|
rowData={info.row.original}
|
||||||
onRowUpdate={handleRowUpdate}
|
setData={setData}
|
||||||
/>
|
/>
|
||||||
),
|
),
|
||||||
}),
|
}),
|
||||||
|
@ -106,5 +96,5 @@ export default function ServiceTable({ data, setData }: ServiceTableProps ) {
|
||||||
}),
|
}),
|
||||||
];
|
];
|
||||||
|
|
||||||
return <Table<Service> data={data} setData={setData} columns={columns} />
|
return <Table data={data} setData={setData} columns={columns} />
|
||||||
};
|
};
|
||||||
|
|
|
@ -12,8 +12,6 @@ type UserTableProps = {
|
||||||
setData: Dispatch<SetStateAction<User[]>>
|
setData: Dispatch<SetStateAction<User[]>>
|
||||||
}
|
}
|
||||||
|
|
||||||
//TODO: Remove dependecy on `data`. Only `setData` is needed. Do for others as well
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Table componenet used for displaying users
|
* Table componenet used for displaying users
|
||||||
* @param props.users List of users to be displayed by the table
|
* @param props.users List of users to be displayed by the table
|
||||||
|
@ -48,7 +46,7 @@ export default function UserTable({ data, setData }: UserTableProps ) {
|
||||||
<RowOpenAction
|
<RowOpenAction
|
||||||
title={info.getValue()}
|
title={info.getValue()}
|
||||||
rowData={info.row.original}
|
rowData={info.row.original}
|
||||||
onRowUpdate={handleRowUpdate}
|
setData={setData}
|
||||||
/>
|
/>
|
||||||
),
|
),
|
||||||
}),
|
}),
|
||||||
|
|
Loading…
Reference in New Issue
Block a user