-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: João Paulo <[email protected]>
- Loading branch information
Showing
7 changed files
with
375 additions
and
217 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
import { useCallback, useEffect, useState } from "react"; | ||
import { ComponentMeta } from "@storybook/react"; | ||
import DataTable, { Pagination } from "./DataTable"; | ||
import { HStack, IconButton } from "@chakra-ui/react"; | ||
import { MdArrowRightAlt, MdDelete } from "react-icons/md"; | ||
import { format, parseISO } from "date-fns"; | ||
|
||
const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms)); | ||
|
||
const firstPage = { | ||
data: [ | ||
{ | ||
name: "John", | ||
email: "[email protected]", | ||
created_at: new Date().toISOString(), | ||
}, | ||
{ | ||
name: "Patrick", | ||
email: "[email protected]", | ||
created_at: new Date().toISOString(), | ||
}, | ||
], | ||
pagination: { | ||
total: 4, | ||
perPage: 2, | ||
page: 1, | ||
lastPage: 2, | ||
}, | ||
}; | ||
|
||
const secondPage = { | ||
data: [ | ||
{ | ||
name: "James", | ||
email: "[email protected]", | ||
created_at: new Date().toISOString(), | ||
}, | ||
{ | ||
name: "Neymar", | ||
email: "[email protected]", | ||
created_at: new Date().toISOString(), | ||
}, | ||
], | ||
pagination: { | ||
total: 4, | ||
perPage: 2, | ||
page: 2, | ||
lastPage: 2, | ||
}, | ||
}; | ||
|
||
const getColumns = () => [ | ||
{ | ||
Header: "Name", | ||
accessor: "name", | ||
}, | ||
{ | ||
Header: "Email", | ||
accessor: "email", | ||
}, | ||
{ | ||
Header: "Created at", | ||
accessor: (row: { created_at: string }) => | ||
format(parseISO(row.created_at), "Pp"), | ||
id: "createdAt", | ||
}, | ||
{ | ||
Header: "Actions", | ||
Cell: () => ( | ||
<HStack> | ||
<IconButton | ||
aria-label="Edit user" | ||
icon={<MdArrowRightAlt size={22} />} | ||
/> | ||
<IconButton aria-label="Delete user" icon={<MdDelete size={22} />} /> | ||
</HStack> | ||
), | ||
}, | ||
]; | ||
|
||
export const Primary = () => { | ||
const [loading, setLoading] = useState(false); | ||
const [data, setData] = useState<typeof firstPage["data"]>([]); | ||
const [pagination, setPagination] = useState<Pagination>({} as Pagination); | ||
const [page, setPage] = useState(1); | ||
|
||
const loadData = useCallback(async () => { | ||
setLoading(true); | ||
const dataToUse: { [x: number]: typeof firstPage } = { | ||
1: firstPage, | ||
2: secondPage, | ||
}; | ||
|
||
await sleep(1000); | ||
setData(dataToUse[page].data); | ||
setPagination(dataToUse[page].pagination); | ||
setLoading(false); | ||
}, [page]); | ||
|
||
useEffect(() => { | ||
loadData(); | ||
}, [loadData]); | ||
|
||
return ( | ||
<DataTable | ||
data={data} | ||
columns={getColumns()} | ||
perPage={2} | ||
pagination={pagination} | ||
page={page} | ||
onChangePage={setPage} | ||
isLoading={loading} | ||
/> | ||
); | ||
}; | ||
|
||
const config = { | ||
title: "DataTable", | ||
component: DataTable, | ||
} as ComponentMeta<typeof DataTable>; | ||
|
||
export default config; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
import DataTable from "@/components/DataTable"; | ||
|
||
import { useMutation, useQuery, useQueryClient } from "react-query"; | ||
import api from "@/services/api"; | ||
import { Dispatch, SetStateAction, useCallback, useState } from "react"; | ||
import { toast } from "react-toastify"; | ||
import ConfirmDialog from "@/components/ConfirmDialog"; | ||
|
||
export type ColumnsProps = { | ||
currentCell: any; | ||
currentText: string; | ||
setCurrentCell: Dispatch<SetStateAction<null>>; | ||
setCurrentText: Dispatch<SetStateAction<string>>; | ||
onClickDelete: (id: string) => void; | ||
page: number; | ||
searchTerm: string; | ||
appliedFilters: any; | ||
}; | ||
|
||
type Props = { | ||
endpoint: string; | ||
columns: (args: ColumnsProps) => void; | ||
appliedFilters: any; | ||
onClickFilter: () => void; | ||
}; | ||
|
||
const StandardTable = ({ | ||
endpoint, | ||
columns, | ||
appliedFilters, | ||
onClickFilter, | ||
}: Props) => { | ||
const perPage = 5; | ||
const [page, setPage] = useState(1); | ||
const [searchTerm, setSearchTerm] = useState(""); | ||
const [currentCell, setCurrentCell] = useState(null); | ||
const [currentText, setCurrentText] = useState(""); | ||
const [idToDelete, setIdToDelete] = useState<string | null>(null); | ||
const queryClient = useQueryClient(); | ||
const { mutateAsync, isLoading: isLoadingDeletion } = useMutation(() => | ||
api.delete(`/${endpoint}/${idToDelete}`) | ||
); | ||
|
||
const { data, isLoading, error } = useQuery( | ||
[endpoint, page, searchTerm, appliedFilters], | ||
() => | ||
api | ||
.get(endpoint, { | ||
params: { | ||
q: searchTerm, | ||
page, | ||
perPage, | ||
order: "created_at", | ||
...appliedFilters, | ||
}, | ||
}) | ||
.then((response) => response.data) | ||
); | ||
|
||
const onSearchDebounced = useCallback((searchTerm: string) => { | ||
setSearchTerm(searchTerm); | ||
}, []); | ||
|
||
const onConfirmDeletion = async () => { | ||
try { | ||
await mutateAsync(); | ||
queryClient.invalidateQueries([endpoint, page, searchTerm]); | ||
setIdToDelete(null); | ||
toast.success("Item inativado com sucesso!"); | ||
} catch (error: any) { | ||
toast.error(error.response?.data.message); | ||
} | ||
}; | ||
|
||
if (error) { | ||
return <div>Houve um erro: "{(error as { message: string }).message}"</div>; | ||
} | ||
|
||
return ( | ||
<> | ||
<ConfirmDialog | ||
isOpen={!!idToDelete} | ||
onConfirm={onConfirmDeletion} | ||
onClose={() => setIdToDelete(null)} | ||
isLoading={isLoadingDeletion} | ||
/> | ||
<DataTable | ||
columns={columns({ | ||
currentCell, | ||
currentText, | ||
onClickDelete: (id) => { | ||
setIdToDelete(id); | ||
}, | ||
setCurrentCell, | ||
setCurrentText, | ||
page, | ||
searchTerm, | ||
appliedFilters, | ||
})} | ||
data={data?.data} | ||
pagination={data?.pagination} | ||
page={page} | ||
onChangePage={setPage} | ||
perPage={perPage} | ||
isLoading={isLoading} | ||
onSearchDebounced={onSearchDebounced} | ||
inputPlaceholder="Procure por nome..." | ||
onClickFilter={onClickFilter} | ||
/> | ||
</> | ||
); | ||
}; | ||
|
||
export default StandardTable; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { default } from "./StandardTable"; |
Oops, something went wrong.