@phsa.tec/design-system-react 0.1.0
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.
- package/.eslintrc.json +7 -0
- package/.storybook/main.ts +16 -0
- package/.storybook/preview.ts +15 -0
- package/README.md +36 -0
- package/components.json +21 -0
- package/jest.config.ts +25 -0
- package/next.config.ts +7 -0
- package/package.json +88 -0
- package/postcss.config.mjs +8 -0
- package/public/file.svg +1 -0
- package/public/globe.svg +1 -0
- package/public/next.svg +1 -0
- package/public/vercel.svg +1 -0
- package/public/window.svg +1 -0
- package/src/app/columns.tsx +178 -0
- package/src/app/favicon.ico +0 -0
- package/src/app/fonts/GeistMonoVF.woff +0 -0
- package/src/app/fonts/GeistVF.woff +0 -0
- package/src/app/globals.css +94 -0
- package/src/app/layout.tsx +35 -0
- package/src/app/page.tsx +7 -0
- package/src/components/actions/AlertDialog/AlertDialog.tsx +45 -0
- package/src/components/actions/AlertDialog/alert-dialog.stories.tsx +21 -0
- package/src/components/actions/AlertDialog/index.ts +1 -0
- package/src/components/actions/Button/Button.stories.ts +38 -0
- package/src/components/actions/Button/Button.tsx +23 -0
- package/src/components/actions/Button/index.ts +1 -0
- package/src/components/actions/Collapsible/index.ts +1 -0
- package/src/components/actions/Dialog/Dialog.stories.tsx +70 -0
- package/src/components/actions/Dialog/Dialog.tsx +87 -0
- package/src/components/actions/Dialog/components/DialogWithActions/index.tsx +40 -0
- package/src/components/actions/Dialog/index.ts +1 -0
- package/src/components/actions/Steps/Steps.stories.tsx +25 -0
- package/src/components/actions/Steps/Steps.tsx +51 -0
- package/src/components/actions/Steps/index.ts +1 -0
- package/src/components/actions/index.ts +5 -0
- package/src/components/dataDisplay/Avatar/Avatar.stories.tsx +22 -0
- package/src/components/dataDisplay/Avatar/Avatar.tsx +21 -0
- package/src/components/dataDisplay/Avatar/index.ts +2 -0
- package/src/components/dataDisplay/Badge/Badge.stories.tsx +36 -0
- package/src/components/dataDisplay/Badge/index.ts +1 -0
- package/src/components/dataDisplay/Card/Card.stories.tsx +24 -0
- package/src/components/dataDisplay/Card/Card.tsx +34 -0
- package/src/components/dataDisplay/Card/index.ts +1 -0
- package/src/components/dataDisplay/DataPairList/DataPairList.tsx +56 -0
- package/src/components/dataDisplay/DataPairList/data-pair-list.stories.tsx +87 -0
- package/src/components/dataDisplay/DataPairList/index.ts +2 -0
- package/src/components/dataDisplay/DataPairList/types.ts +10 -0
- package/src/components/dataDisplay/DropDownMenu/index.ts +1 -0
- package/src/components/dataDisplay/ErrorMessage/ErrorMessage.tsx +6 -0
- package/src/components/dataDisplay/ErrorMessage/index.ts +1 -0
- package/src/components/dataDisplay/Icon/Icon.stories.tsx +21 -0
- package/src/components/dataDisplay/Icon/Icon.tsx +47 -0
- package/src/components/dataDisplay/Icon/index.ts +1 -0
- package/src/components/dataDisplay/Icon/types.ts +6 -0
- package/src/components/dataDisplay/Label/Label.stories.tsx +21 -0
- package/src/components/dataDisplay/Label/Label.tsx +10 -0
- package/src/components/dataDisplay/Label/index.ts +1 -0
- package/src/components/dataDisplay/Table/Table.tsx +173 -0
- package/src/components/dataDisplay/Table/columns.tsx +223 -0
- package/src/components/dataDisplay/Table/components/DynamicTable/data-table-column-header.tsx +72 -0
- package/src/components/dataDisplay/Table/components/DynamicTable/data-table-pagination.tsx +91 -0
- package/src/components/dataDisplay/Table/components/DynamicTable/data-table-toolbar.tsx +17 -0
- package/src/components/dataDisplay/Table/components/DynamicTable/data-table-view-options.tsx +58 -0
- package/src/components/dataDisplay/Table/components/DynamicTable/data-table.stories.tsx +118 -0
- package/src/components/dataDisplay/Table/components/DynamicTable/index.tsx +136 -0
- package/src/components/dataDisplay/Table/components/DynamicTable/types.ts +43 -0
- package/src/components/dataDisplay/Table/custom/CustomTable/data-table-column-header.tsx +71 -0
- package/src/components/dataDisplay/Table/custom/CustomTable/data-table-faceted-filter.tsx +147 -0
- package/src/components/dataDisplay/Table/custom/CustomTable/data-table-pagination.tsx +97 -0
- package/src/components/dataDisplay/Table/custom/CustomTable/data-table-row-actions.tsx +78 -0
- package/src/components/dataDisplay/Table/custom/CustomTable/data-table-toolbar.tsx +60 -0
- package/src/components/dataDisplay/Table/custom/CustomTable/data-table-view-options.tsx +59 -0
- package/src/components/dataDisplay/Table/custom/CustomTable/data-table.tsx +145 -0
- package/src/components/dataDisplay/Table/custom/CustomTable/data.ts +71 -0
- package/src/components/dataDisplay/Table/custom/CustomTable/index.tsx +34 -0
- package/src/components/dataDisplay/Table/custom/CustomTable/schema.ts +11 -0
- package/src/components/dataDisplay/Table/index.ts +2 -0
- package/src/components/dataDisplay/Table/table.stories.tsx +147 -0
- package/src/components/dataDisplay/Table/types.ts +15 -0
- package/src/components/dataDisplay/Tabs/Tabs.stories.tsx +34 -0
- package/src/components/dataDisplay/Tabs/Tabs.tsx +53 -0
- package/src/components/dataDisplay/Tabs/index.ts +1 -0
- package/src/components/dataDisplay/Text/Text.stories.tsx +66 -0
- package/src/components/dataDisplay/Text/Text.tsx +56 -0
- package/src/components/dataDisplay/Text/index.ts +1 -0
- package/src/components/dataDisplay/index.ts +8 -0
- package/src/components/dataInput/Input/components/Input/Input.stories.tsx +99 -0
- package/src/components/dataInput/Input/components/Input/InputBase.tsx +50 -0
- package/src/components/dataInput/Input/components/Input/__tests__/Input.test.tsx +100 -0
- package/src/components/dataInput/Input/components/Input/index.tsx +257 -0
- package/src/components/dataInput/Input/components/InputBase/__tests__/InputBase.test.tsx +120 -0
- package/src/components/dataInput/Input/components/InputBase/index.tsx +89 -0
- package/src/components/dataInput/Input/components/MaskInput/__tests__/mask-input.test.tsx +67 -0
- package/src/components/dataInput/Input/components/MaskInput/index.ts +1 -0
- package/src/components/dataInput/Input/components/MaskInput/mask-input.stories.tsx +59 -0
- package/src/components/dataInput/Input/components/MaskInput/mask-input.tsx +43 -0
- package/src/components/dataInput/Input/components/MultipleInput/MultipleInput.tsx +36 -0
- package/src/components/dataInput/Input/components/MultipleInput/MultipleInputBase.tsx +100 -0
- package/src/components/dataInput/Input/components/MultipleInput/MultipleMaskInput.tsx +35 -0
- package/src/components/dataInput/Input/components/MultipleInput/MultipleNumberInput.tsx +35 -0
- package/src/components/dataInput/Input/components/MultipleInput/index.ts +2 -0
- package/src/components/dataInput/Input/components/MultipleInput/multiple-input.stories.tsx +71 -0
- package/src/components/dataInput/Input/components/NumberInput/__tests__/number-input.test.tsx +95 -0
- package/src/components/dataInput/Input/components/NumberInput/index.ts +1 -0
- package/src/components/dataInput/Input/components/NumberInput/number-input.stories.tsx +76 -0
- package/src/components/dataInput/Input/components/NumberInput/number-input.tsx +68 -0
- package/src/components/dataInput/Input/index.ts +4 -0
- package/src/components/dataInput/Select/MultiSelect/MultiSelect.stories.tsx +119 -0
- package/src/components/dataInput/Select/MultiSelect/MultiSelectBase.tsx +135 -0
- package/src/components/dataInput/Select/MultiSelect/index.tsx +75 -0
- package/src/components/dataInput/Select/Select.stories.tsx +61 -0
- package/src/components/dataInput/Select/Select.tsx +73 -0
- package/src/components/dataInput/Select/SelectBase.tsx +58 -0
- package/src/components/dataInput/Select/index.ts +2 -0
- package/src/components/dataInput/Switch/Switch.stories.tsx +75 -0
- package/src/components/dataInput/Switch/Switch.tsx +52 -0
- package/src/components/dataInput/Switch/index.ts +1 -0
- package/src/components/dataInput/checkbox/Checkbox.tsx +57 -0
- package/src/components/dataInput/checkbox/Checkbox_old.tsx +58 -0
- package/src/components/dataInput/checkbox/Checkout.stories.tsx +62 -0
- package/src/components/dataInput/checkbox/index.ts +1 -0
- package/src/components/dataInput/form/Form.tsx +47 -0
- package/src/components/dataInput/form/index.ts +3 -0
- package/src/components/dataInput/index.ts +5 -0
- package/src/components/feedback/Spinner/index.ts +1 -0
- package/src/components/feedback/Toast/Toast.stories.tsx +45 -0
- package/src/components/feedback/Toast/index.ts +2 -0
- package/src/components/feedback/index.ts +2 -0
- package/src/components/index.ts +6 -0
- package/src/components/layout/Crud/components/Table/index.tsx +183 -0
- package/src/components/layout/Crud/components/Table/types.ts +15 -0
- package/src/components/layout/Crud/crud.stories.tsx +317 -0
- package/src/components/layout/Crud/hook/useCrudLayout/index.tsx +94 -0
- package/src/components/layout/Crud/hook/useRequest/index.tsx +156 -0
- package/src/components/layout/Crud/index.tsx +295 -0
- package/src/components/layout/Crud/store/CrudLayoutStore.ts +75 -0
- package/src/components/layout/Crud/types.ts +14 -0
- package/src/components/layout/Drawer/CustomDrawer/index.tsx +33 -0
- package/src/components/layout/Drawer/Drawer.stories.tsx +80 -0
- package/src/components/layout/Drawer/index.ts +2 -0
- package/src/components/layout/PageLayout/PageLayout.stories.tsx +42 -0
- package/src/components/layout/PageLayout/index.tsx +28 -0
- package/src/components/layout/Separator/index.ts +1 -0
- package/src/components/layout/Sheet/Sheet.stories.tsx +28 -0
- package/src/components/layout/Sheet/Sheet.tsx +22 -0
- package/src/components/layout/Sheet/index.ts +1 -0
- package/src/components/layout/Sidebar/Sidebar.stories.tsx +116 -0
- package/src/components/layout/Sidebar/Sidebar.tsx +50 -0
- package/src/components/layout/Sidebar/components/app-sidebar.tsx +203 -0
- package/src/components/layout/Sidebar/components/footer-sidebar.tsx +17 -0
- package/src/components/layout/Sidebar/components/header-sidebar.tsx +90 -0
- package/src/components/layout/Sidebar/components/menus.tsx +55 -0
- package/src/components/layout/Sidebar/components/nav-projects.tsx +88 -0
- package/src/components/layout/Sidebar/components/nav-user.tsx +114 -0
- package/src/components/layout/Sidebar/components/team-switcher.tsx +85 -0
- package/src/components/layout/Sidebar/index.ts +2 -0
- package/src/components/layout/Sidebar/provider/index.tsx +51 -0
- package/src/components/layout/Tabs/Tabs.tsx +51 -0
- package/src/components/layout/Tabs/index.ts +1 -0
- package/src/components/layout/Tabs/tabs.stories.tsx +57 -0
- package/src/components/layout/index.ts +6 -0
- package/src/components/navigation/Breadcrumbs/Breadcrumbs.tsx +66 -0
- package/src/components/navigation/Breadcrumbs/index.ts +2 -0
- package/src/components/navigation/index.ts +1 -0
- package/src/components/ui/alert-dialog.tsx +141 -0
- package/src/components/ui/alert.tsx +59 -0
- package/src/components/ui/avatar.tsx +50 -0
- package/src/components/ui/badge.tsx +40 -0
- package/src/components/ui/breadcrumb.tsx +115 -0
- package/src/components/ui/button.tsx +57 -0
- package/src/components/ui/card.tsx +83 -0
- package/src/components/ui/checkbox.tsx +34 -0
- package/src/components/ui/collapsible.tsx +11 -0
- package/src/components/ui/command.tsx +153 -0
- package/src/components/ui/dialog.tsx +124 -0
- package/src/components/ui/drawer.tsx +117 -0
- package/src/components/ui/dropdown-menu.tsx +201 -0
- package/src/components/ui/form.tsx +179 -0
- package/src/components/ui/input.tsx +24 -0
- package/src/components/ui/label.tsx +30 -0
- package/src/components/ui/popover.tsx +33 -0
- package/src/components/ui/select.tsx +161 -0
- package/src/components/ui/separator.tsx +31 -0
- package/src/components/ui/sheet.tsx +140 -0
- package/src/components/ui/sidebar.tsx +763 -0
- package/src/components/ui/skeleton.tsx +15 -0
- package/src/components/ui/sonner.tsx +31 -0
- package/src/components/ui/spinner.tsx +54 -0
- package/src/components/ui/switch.tsx +33 -0
- package/src/components/ui/table.tsx +120 -0
- package/src/components/ui/tabs.tsx +55 -0
- package/src/components/ui/toast.tsx +130 -0
- package/src/components/ui/toaster.tsx +35 -0
- package/src/components/ui/tooltip.tsx +32 -0
- package/src/hooks/use-mobile.tsx +19 -0
- package/src/hooks/use-toast.ts +191 -0
- package/src/index.ts +1 -0
- package/src/lib/utils.ts +6 -0
- package/tailwind.config.ts +83 -0
- package/tsconfig.json +27 -0
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
import { useCallback, useMemo } from "react";
|
|
2
|
+
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
|
|
3
|
+
// import { ConfigResponse } from "@/service/config/types";
|
|
4
|
+
|
|
5
|
+
// esse ConfigResponse
|
|
6
|
+
|
|
7
|
+
export type useRequestProps<ListData, CreateData, UpdateData> = {
|
|
8
|
+
queryKey: string;
|
|
9
|
+
createRequest?: (data: CreateData) => Promise<ListData | void>;
|
|
10
|
+
updateRequest?: (data: UpdateData) => Promise<ListData | void>;
|
|
11
|
+
deleteRequest?: (id: string) => Promise<void>;
|
|
12
|
+
listRequest: (
|
|
13
|
+
page?: number,
|
|
14
|
+
limit?: number
|
|
15
|
+
) => Promise<{ data: ListData[]; total: number }>;
|
|
16
|
+
page: number;
|
|
17
|
+
limit: number;
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
export function useRequest<
|
|
21
|
+
ListData extends { _id: string },
|
|
22
|
+
CreateData,
|
|
23
|
+
UpdateData
|
|
24
|
+
>({
|
|
25
|
+
queryKey,
|
|
26
|
+
updateRequest = () => Promise.resolve(),
|
|
27
|
+
deleteRequest = () => Promise.resolve(),
|
|
28
|
+
createRequest = () => Promise.resolve(),
|
|
29
|
+
listRequest,
|
|
30
|
+
page,
|
|
31
|
+
limit,
|
|
32
|
+
}: useRequestProps<ListData, CreateData, UpdateData>) {
|
|
33
|
+
const queryClient = useQueryClient();
|
|
34
|
+
|
|
35
|
+
const queryKeys = useMemo(() => {
|
|
36
|
+
return [queryKey, page, limit];
|
|
37
|
+
}, [queryKey, page, limit]);
|
|
38
|
+
|
|
39
|
+
// Query para buscar configurações
|
|
40
|
+
const {
|
|
41
|
+
data: queryData,
|
|
42
|
+
isLoading: isLoadingList,
|
|
43
|
+
error: queryError,
|
|
44
|
+
refetch,
|
|
45
|
+
} = useQuery({
|
|
46
|
+
queryKey: queryKeys,
|
|
47
|
+
queryFn: () => listRequest(page, limit),
|
|
48
|
+
staleTime: 5 * 60 * 1000, // 5 minutos
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
const listData = queryData?.data || [];
|
|
52
|
+
const total = queryData?.total || 0;
|
|
53
|
+
|
|
54
|
+
// Mutation para criar/atualizar configuração
|
|
55
|
+
const {
|
|
56
|
+
mutateAsync: createMutation,
|
|
57
|
+
isPending: isCreating,
|
|
58
|
+
error: createError,
|
|
59
|
+
} = useMutation({
|
|
60
|
+
mutationFn: createRequest,
|
|
61
|
+
onSuccess: () => {
|
|
62
|
+
queryClient.invalidateQueries({ queryKey: queryKeys });
|
|
63
|
+
},
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
const {
|
|
67
|
+
mutateAsync: updateMutation,
|
|
68
|
+
isPending: isUpdating,
|
|
69
|
+
error: updateError,
|
|
70
|
+
} = useMutation({
|
|
71
|
+
mutationFn: updateRequest,
|
|
72
|
+
onSuccess: () => {
|
|
73
|
+
queryClient.invalidateQueries({ queryKey: queryKeys });
|
|
74
|
+
},
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
// Mutation para excluir configuração
|
|
78
|
+
const {
|
|
79
|
+
mutateAsync: deleteMutation,
|
|
80
|
+
isPending: isDeleting,
|
|
81
|
+
error: deleteError,
|
|
82
|
+
} = useMutation({
|
|
83
|
+
mutationFn: (id: string) => deleteRequest(id),
|
|
84
|
+
onSuccess: (_, id) => {
|
|
85
|
+
// Invalidar todas as páginas para garantir consistência
|
|
86
|
+
queryClient.invalidateQueries({
|
|
87
|
+
queryKey: [queryKey],
|
|
88
|
+
exact: false,
|
|
89
|
+
});
|
|
90
|
+
},
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
const error =
|
|
94
|
+
queryError || createError || updateError || deleteError
|
|
95
|
+
? (queryError || createError || updateError || deleteError) instanceof
|
|
96
|
+
Error
|
|
97
|
+
? (queryError || createError || updateError || deleteError)?.message
|
|
98
|
+
: "Erro desconhecido"
|
|
99
|
+
: null;
|
|
100
|
+
|
|
101
|
+
// Função para criar nova configuração
|
|
102
|
+
const createActionRequest = useCallback(
|
|
103
|
+
async (configData: CreateData) => {
|
|
104
|
+
try {
|
|
105
|
+
await createMutation(configData);
|
|
106
|
+
return true;
|
|
107
|
+
} catch (err) {
|
|
108
|
+
console.error("Erro ao criar configuração:", err);
|
|
109
|
+
return false;
|
|
110
|
+
}
|
|
111
|
+
},
|
|
112
|
+
[createMutation]
|
|
113
|
+
);
|
|
114
|
+
|
|
115
|
+
// Função para atualizar configuração existente
|
|
116
|
+
const updateActionRequest = useCallback(
|
|
117
|
+
async (configData: UpdateData) => {
|
|
118
|
+
try {
|
|
119
|
+
await updateMutation(configData);
|
|
120
|
+
return true;
|
|
121
|
+
} catch (err) {
|
|
122
|
+
console.error("Erro ao atualizar configuração:", err);
|
|
123
|
+
return false;
|
|
124
|
+
}
|
|
125
|
+
},
|
|
126
|
+
[updateMutation]
|
|
127
|
+
);
|
|
128
|
+
|
|
129
|
+
const deleteActionRequest = useCallback(
|
|
130
|
+
async (key: string) => {
|
|
131
|
+
try {
|
|
132
|
+
await deleteMutation(key);
|
|
133
|
+
return true;
|
|
134
|
+
} catch (err) {
|
|
135
|
+
console.error("Erro ao excluir configuração:", err);
|
|
136
|
+
return false;
|
|
137
|
+
}
|
|
138
|
+
},
|
|
139
|
+
[deleteMutation]
|
|
140
|
+
);
|
|
141
|
+
|
|
142
|
+
const refreshConfigs = useCallback(() => {
|
|
143
|
+
return refetch();
|
|
144
|
+
}, [refetch]);
|
|
145
|
+
|
|
146
|
+
return {
|
|
147
|
+
listData,
|
|
148
|
+
total,
|
|
149
|
+
isLoading: isLoadingList || isCreating || isUpdating || isDeleting,
|
|
150
|
+
error,
|
|
151
|
+
createActionRequest,
|
|
152
|
+
updateActionRequest,
|
|
153
|
+
deleteActionRequest,
|
|
154
|
+
refreshConfigs,
|
|
155
|
+
};
|
|
156
|
+
}
|
|
@@ -0,0 +1,295 @@
|
|
|
1
|
+
import { AlertCircle, Edit, Plus, Trash2 } from "lucide-react";
|
|
2
|
+
import { Table } from "./components/Table";
|
|
3
|
+
import { CrudLayoutProps } from "./types";
|
|
4
|
+
import { Button } from "@/components/ui/button";
|
|
5
|
+
import { useCallback, useEffect, useMemo, useState } from "react";
|
|
6
|
+
import { Form } from "@/components/ui/form";
|
|
7
|
+
import {
|
|
8
|
+
Dialog,
|
|
9
|
+
DialogHeader,
|
|
10
|
+
DialogContent,
|
|
11
|
+
DialogTitle,
|
|
12
|
+
DialogDescription,
|
|
13
|
+
DialogFooter,
|
|
14
|
+
} from "@/components/ui/dialog";
|
|
15
|
+
import {
|
|
16
|
+
useTitleModal,
|
|
17
|
+
useDescriptionModal,
|
|
18
|
+
useSubmitButtonText,
|
|
19
|
+
useCreateButtonText,
|
|
20
|
+
useCrudLayoutActions,
|
|
21
|
+
useOpenEditModal,
|
|
22
|
+
useDeleteId,
|
|
23
|
+
useEditId,
|
|
24
|
+
useSendingButtonText,
|
|
25
|
+
} from "./store/CrudLayoutStore";
|
|
26
|
+
import {
|
|
27
|
+
AlertDialog,
|
|
28
|
+
AlertDialogAction,
|
|
29
|
+
AlertDialogCancel,
|
|
30
|
+
AlertDialogContent,
|
|
31
|
+
AlertDialogDescription,
|
|
32
|
+
AlertDialogFooter,
|
|
33
|
+
AlertDialogHeader,
|
|
34
|
+
AlertDialogTitle,
|
|
35
|
+
} from "@/components/ui/alert-dialog";
|
|
36
|
+
import { useRequest } from "./hook/useRequest";
|
|
37
|
+
import { Alert, AlertDescription } from "@/components/ui/alert";
|
|
38
|
+
import { toast } from "sonner";
|
|
39
|
+
|
|
40
|
+
export const CrudLayout = <
|
|
41
|
+
ListData extends { _id: string },
|
|
42
|
+
CreateData,
|
|
43
|
+
UpdateData
|
|
44
|
+
>({
|
|
45
|
+
columns,
|
|
46
|
+
title,
|
|
47
|
+
createRequest,
|
|
48
|
+
updateRequest,
|
|
49
|
+
deleteRequest,
|
|
50
|
+
listRequest = () => Promise.resolve({ data: [], total: 0 }),
|
|
51
|
+
formComponent,
|
|
52
|
+
actions = [],
|
|
53
|
+
form,
|
|
54
|
+
}: CrudLayoutProps<ListData, CreateData, UpdateData>) => {
|
|
55
|
+
const [isModalOpen, setIsModalOpen] = useState(false);
|
|
56
|
+
const [modalMode, setModalMode] = useState<"create" | "edit">("create");
|
|
57
|
+
const [page, setPage] = useState(1);
|
|
58
|
+
const [limit, setLimit] = useState(10);
|
|
59
|
+
|
|
60
|
+
const titleModal = useTitleModal();
|
|
61
|
+
const descriptionModal = useDescriptionModal();
|
|
62
|
+
const submitButtonText = useSubmitButtonText();
|
|
63
|
+
const createButtonText = useCreateButtonText();
|
|
64
|
+
const sendingButtonText = useSendingButtonText();
|
|
65
|
+
const deleteId = useDeleteId();
|
|
66
|
+
const editId = useEditId();
|
|
67
|
+
const openEditModal = useOpenEditModal();
|
|
68
|
+
const { setDeleteId, setOpenEditModal, setEditId } = useCrudLayoutActions();
|
|
69
|
+
|
|
70
|
+
const isEditMode = useMemo(() => modalMode === "edit", [modalMode]);
|
|
71
|
+
|
|
72
|
+
const {
|
|
73
|
+
listData,
|
|
74
|
+
total,
|
|
75
|
+
error,
|
|
76
|
+
createActionRequest,
|
|
77
|
+
updateActionRequest,
|
|
78
|
+
deleteActionRequest,
|
|
79
|
+
isLoading,
|
|
80
|
+
} = useRequest({
|
|
81
|
+
queryKey: "configs",
|
|
82
|
+
createRequest,
|
|
83
|
+
updateRequest,
|
|
84
|
+
deleteRequest,
|
|
85
|
+
listRequest,
|
|
86
|
+
page,
|
|
87
|
+
limit,
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
useEffect(() => {
|
|
91
|
+
if (openEditModal && editId) {
|
|
92
|
+
const itemToEdit = listData.find((item: ListData) => item._id === editId);
|
|
93
|
+
if (itemToEdit) {
|
|
94
|
+
form.reset(itemToEdit);
|
|
95
|
+
setModalMode("edit");
|
|
96
|
+
setIsModalOpen(true);
|
|
97
|
+
setOpenEditModal(false);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
}, [editId, form, listData, openEditModal, setOpenEditModal]);
|
|
101
|
+
|
|
102
|
+
const resetForm = useCallback(() => {
|
|
103
|
+
form.reset({});
|
|
104
|
+
const formValues = form.getValues();
|
|
105
|
+
Object.keys(formValues).forEach((key) => {
|
|
106
|
+
form.setValue(key, "");
|
|
107
|
+
});
|
|
108
|
+
}, [form]);
|
|
109
|
+
|
|
110
|
+
const customColumns = useMemo(() => {
|
|
111
|
+
const editButton = actions.includes("update") && {
|
|
112
|
+
id: "actions",
|
|
113
|
+
header: "Ações",
|
|
114
|
+
cell: ({ row }) => {
|
|
115
|
+
const data = row.original;
|
|
116
|
+
return (
|
|
117
|
+
<div className="flex gap-2">
|
|
118
|
+
{actions.includes("update") && (
|
|
119
|
+
<Button
|
|
120
|
+
variant="outline"
|
|
121
|
+
size="sm"
|
|
122
|
+
onClick={() => {
|
|
123
|
+
setEditId(data._id);
|
|
124
|
+
setOpenEditModal(true);
|
|
125
|
+
}}
|
|
126
|
+
>
|
|
127
|
+
<Edit className="h-4 w-4" />
|
|
128
|
+
</Button>
|
|
129
|
+
)}
|
|
130
|
+
{actions.includes("delete") && (
|
|
131
|
+
<Button
|
|
132
|
+
variant="outline"
|
|
133
|
+
size="sm"
|
|
134
|
+
onClick={() => setDeleteId(data._id)}
|
|
135
|
+
>
|
|
136
|
+
<Trash2 className="h-4 w-4" />
|
|
137
|
+
</Button>
|
|
138
|
+
)}
|
|
139
|
+
</div>
|
|
140
|
+
);
|
|
141
|
+
},
|
|
142
|
+
};
|
|
143
|
+
|
|
144
|
+
const data = [...columns];
|
|
145
|
+
|
|
146
|
+
if (editButton) data.push(editButton);
|
|
147
|
+
|
|
148
|
+
return data;
|
|
149
|
+
}, [columns, actions, setEditId, setOpenEditModal, setDeleteId]);
|
|
150
|
+
|
|
151
|
+
const closeModal = useCallback(() => {
|
|
152
|
+
setIsModalOpen(false);
|
|
153
|
+
setModalMode("create");
|
|
154
|
+
setEditId(null);
|
|
155
|
+
resetForm();
|
|
156
|
+
}, [setEditId, resetForm]);
|
|
157
|
+
|
|
158
|
+
const onSubmit = useCallback(
|
|
159
|
+
async (data: CreateData | UpdateData) => {
|
|
160
|
+
try {
|
|
161
|
+
let success;
|
|
162
|
+
|
|
163
|
+
if (isEditMode) {
|
|
164
|
+
success = await updateActionRequest(data as UpdateData);
|
|
165
|
+
if (success) {
|
|
166
|
+
toast.success("Configuração atualizada");
|
|
167
|
+
}
|
|
168
|
+
} else {
|
|
169
|
+
success = await createActionRequest(data as CreateData);
|
|
170
|
+
if (success) {
|
|
171
|
+
toast.success("Configuração criada");
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
if (success) {
|
|
176
|
+
closeModal();
|
|
177
|
+
}
|
|
178
|
+
} catch (error) {
|
|
179
|
+
console.error("Erro ao salvar:", error);
|
|
180
|
+
toast.error("Erro ao salvar configuração");
|
|
181
|
+
}
|
|
182
|
+
},
|
|
183
|
+
[createActionRequest, updateActionRequest, isEditMode, closeModal]
|
|
184
|
+
);
|
|
185
|
+
|
|
186
|
+
const onClickCreate = useCallback(() => {
|
|
187
|
+
resetForm();
|
|
188
|
+
setModalMode("create");
|
|
189
|
+
setEditId(null);
|
|
190
|
+
setIsModalOpen(true);
|
|
191
|
+
}, [setEditId, resetForm]);
|
|
192
|
+
|
|
193
|
+
const onDeleteData = useCallback(
|
|
194
|
+
async (dataId: string) => {
|
|
195
|
+
const success = await deleteActionRequest(dataId);
|
|
196
|
+
if (success) {
|
|
197
|
+
toast.success("Configuração deletada com sucesso");
|
|
198
|
+
setDeleteId(null);
|
|
199
|
+
}
|
|
200
|
+
},
|
|
201
|
+
[deleteActionRequest, setDeleteId]
|
|
202
|
+
);
|
|
203
|
+
|
|
204
|
+
const handleModalOpenChange = useCallback(
|
|
205
|
+
(isOpen: boolean) => {
|
|
206
|
+
if (!isOpen) {
|
|
207
|
+
closeModal();
|
|
208
|
+
}
|
|
209
|
+
},
|
|
210
|
+
[closeModal]
|
|
211
|
+
);
|
|
212
|
+
|
|
213
|
+
const handlePageChange = useCallback((newPage: number) => {
|
|
214
|
+
setPage(newPage);
|
|
215
|
+
}, []);
|
|
216
|
+
|
|
217
|
+
const handleLimitChange = useCallback((newLimit: number) => {
|
|
218
|
+
setLimit(newLimit);
|
|
219
|
+
setPage(1); // Reset to first page when changing limit
|
|
220
|
+
}, []);
|
|
221
|
+
|
|
222
|
+
return (
|
|
223
|
+
<div className="container mx-auto p-4 space-y-6 bg-white rounded-md">
|
|
224
|
+
<div className="flex justify-between items-center md:flex-row flex-col gap-4">
|
|
225
|
+
<h1 className="text-2xl font-bold">{title}</h1>
|
|
226
|
+
<Button onClick={onClickCreate} className="w-full md:w-auto">
|
|
227
|
+
<Plus className="mr-2 h-4 w-4" /> {createButtonText}
|
|
228
|
+
</Button>
|
|
229
|
+
</div>
|
|
230
|
+
|
|
231
|
+
<Dialog open={isModalOpen} onOpenChange={handleModalOpenChange}>
|
|
232
|
+
<DialogContent>
|
|
233
|
+
<DialogHeader>
|
|
234
|
+
<DialogTitle>{titleModal}</DialogTitle>
|
|
235
|
+
<DialogDescription>{descriptionModal}</DialogDescription>
|
|
236
|
+
</DialogHeader>
|
|
237
|
+
<Form {...form}>
|
|
238
|
+
<form onSubmit={form.handleSubmit(onSubmit)} className="space-y-4">
|
|
239
|
+
{formComponent}
|
|
240
|
+
<DialogFooter>
|
|
241
|
+
<Button type="submit" disabled={isLoading}>
|
|
242
|
+
{isLoading ? sendingButtonText : submitButtonText}
|
|
243
|
+
</Button>
|
|
244
|
+
</DialogFooter>
|
|
245
|
+
</form>
|
|
246
|
+
</Form>
|
|
247
|
+
</DialogContent>
|
|
248
|
+
</Dialog>
|
|
249
|
+
|
|
250
|
+
{error && (
|
|
251
|
+
<Alert variant="destructive">
|
|
252
|
+
<AlertCircle className="h-4 w-4" />
|
|
253
|
+
<AlertDescription>{error}</AlertDescription>
|
|
254
|
+
</Alert>
|
|
255
|
+
)}
|
|
256
|
+
|
|
257
|
+
<Table<ListData>
|
|
258
|
+
data={listData}
|
|
259
|
+
columns={customColumns}
|
|
260
|
+
isLoading={isLoading}
|
|
261
|
+
pagination={{
|
|
262
|
+
page,
|
|
263
|
+
limit,
|
|
264
|
+
total,
|
|
265
|
+
onPageChange: handlePageChange,
|
|
266
|
+
onLimitChange: handleLimitChange,
|
|
267
|
+
}}
|
|
268
|
+
/>
|
|
269
|
+
<AlertDialog
|
|
270
|
+
open={!!deleteId}
|
|
271
|
+
onOpenChange={(open) => {
|
|
272
|
+
if (!open) {
|
|
273
|
+
setDeleteId(null);
|
|
274
|
+
}
|
|
275
|
+
}}
|
|
276
|
+
>
|
|
277
|
+
<AlertDialogContent>
|
|
278
|
+
<AlertDialogHeader>
|
|
279
|
+
<AlertDialogTitle>{titleModal}</AlertDialogTitle>
|
|
280
|
+
<AlertDialogDescription>{descriptionModal}</AlertDialogDescription>
|
|
281
|
+
</AlertDialogHeader>
|
|
282
|
+
<AlertDialogFooter>
|
|
283
|
+
<AlertDialogCancel>Cancelar</AlertDialogCancel>
|
|
284
|
+
<AlertDialogAction
|
|
285
|
+
onClick={() => onDeleteData(deleteId as string)}
|
|
286
|
+
className="bg-red-600 hover:bg-red-700"
|
|
287
|
+
>
|
|
288
|
+
{isLoading ? sendingButtonText : "Excluir"}
|
|
289
|
+
</AlertDialogAction>
|
|
290
|
+
</AlertDialogFooter>
|
|
291
|
+
</AlertDialogContent>
|
|
292
|
+
</AlertDialog>
|
|
293
|
+
</div>
|
|
294
|
+
);
|
|
295
|
+
};
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { create } from "zustand";
|
|
2
|
+
|
|
3
|
+
export type CrudLayoutStore = {
|
|
4
|
+
titleModal?: string;
|
|
5
|
+
descriptionModal?: string;
|
|
6
|
+
submitButtonText?: string;
|
|
7
|
+
createButtonText?: string;
|
|
8
|
+
isEditMode: boolean;
|
|
9
|
+
deleteId: string | null;
|
|
10
|
+
editId: string | null;
|
|
11
|
+
openEditModal: boolean;
|
|
12
|
+
sendingButtonText?: string;
|
|
13
|
+
actions: {
|
|
14
|
+
setTitleModal: (title: string) => void;
|
|
15
|
+
setDescriptionModal: (description: string) => void;
|
|
16
|
+
setSubmitButtonText: (text: string) => void;
|
|
17
|
+
setCreateButtonText: (text: string) => void;
|
|
18
|
+
setIsEditMode: (isEditMode: boolean) => void;
|
|
19
|
+
setDeleteId: (id: string | null) => void;
|
|
20
|
+
setEditId: (id: string | null) => void;
|
|
21
|
+
setOpenEditModal: (open: boolean) => void;
|
|
22
|
+
setSendingButtonText: (text?: string) => void;
|
|
23
|
+
};
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
export const useCrudLayoutStore = create<CrudLayoutStore>((set) => ({
|
|
27
|
+
titleModal: "",
|
|
28
|
+
descriptionModal: "",
|
|
29
|
+
submitButtonText: "Salvar",
|
|
30
|
+
createButtonText: "Criar",
|
|
31
|
+
isEditMode: false,
|
|
32
|
+
deleteId: null,
|
|
33
|
+
editId: null,
|
|
34
|
+
openEditModal: false,
|
|
35
|
+
actions: {
|
|
36
|
+
setTitleModal: (title) => set({ titleModal: title }),
|
|
37
|
+
setDescriptionModal: (description) =>
|
|
38
|
+
set({ descriptionModal: description }),
|
|
39
|
+
setSubmitButtonText: (text) => set({ submitButtonText: text }),
|
|
40
|
+
setCreateButtonText: (text) => set({ createButtonText: text }),
|
|
41
|
+
setIsEditMode: (isEditMode) => set({ isEditMode }),
|
|
42
|
+
setDeleteId: (id) => set({ deleteId: id }),
|
|
43
|
+
setEditId: (id) => set({ editId: id }),
|
|
44
|
+
setOpenEditModal: (open) => set({ openEditModal: open }),
|
|
45
|
+
setSendingButtonText: (text) => set({ sendingButtonText: text }),
|
|
46
|
+
},
|
|
47
|
+
}));
|
|
48
|
+
|
|
49
|
+
export const useCrudLayoutActions = () =>
|
|
50
|
+
useCrudLayoutStore((state) => state.actions);
|
|
51
|
+
|
|
52
|
+
export const useTitleModal = () =>
|
|
53
|
+
useCrudLayoutStore((state) => state.titleModal);
|
|
54
|
+
|
|
55
|
+
export const useDescriptionModal = () =>
|
|
56
|
+
useCrudLayoutStore((state) => state.descriptionModal);
|
|
57
|
+
|
|
58
|
+
export const useSubmitButtonText = () =>
|
|
59
|
+
useCrudLayoutStore((state) => state.submitButtonText);
|
|
60
|
+
|
|
61
|
+
export const useCreateButtonText = () =>
|
|
62
|
+
useCrudLayoutStore((state) => state.createButtonText);
|
|
63
|
+
|
|
64
|
+
export const useIsEditMode = () =>
|
|
65
|
+
useCrudLayoutStore((state) => state.isEditMode);
|
|
66
|
+
|
|
67
|
+
export const useDeleteId = () => useCrudLayoutStore((state) => state.deleteId);
|
|
68
|
+
|
|
69
|
+
export const useEditId = () => useCrudLayoutStore((state) => state.editId);
|
|
70
|
+
|
|
71
|
+
export const useOpenEditModal = () =>
|
|
72
|
+
useCrudLayoutStore((state) => state.openEditModal);
|
|
73
|
+
|
|
74
|
+
export const useSendingButtonText = () =>
|
|
75
|
+
useCrudLayoutStore((state) => state.sendingButtonText);
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { ColumnDef } from "@tanstack/react-table";
|
|
2
|
+
|
|
3
|
+
export type CrudLayoutProps<ListData, CreateData, UpdateData> = {
|
|
4
|
+
columns: ColumnDef<ListData>[];
|
|
5
|
+
title: string;
|
|
6
|
+
listRequest?: () => Promise<ListData[]>;
|
|
7
|
+
createRequest?: (data?: CreateData) => Promise<ListData>;
|
|
8
|
+
updateRequest?: (data?: UpdateData) => Promise<ListData>;
|
|
9
|
+
deleteRequest?: (key: string) => Promise<void>;
|
|
10
|
+
formComponent: React.ReactNode;
|
|
11
|
+
|
|
12
|
+
form: any;
|
|
13
|
+
actions?: ["create", "update", "delete"];
|
|
14
|
+
};
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import * as React from "react";
|
|
4
|
+
import { Minus, Plus } from "lucide-react";
|
|
5
|
+
// import { Bar, BarChart, ResponsiveContainer } from "recharts";
|
|
6
|
+
|
|
7
|
+
import { Button } from "../../../actions";
|
|
8
|
+
import {
|
|
9
|
+
Drawer,
|
|
10
|
+
DrawerClose,
|
|
11
|
+
DrawerContent,
|
|
12
|
+
DrawerDescription,
|
|
13
|
+
DrawerFooter,
|
|
14
|
+
DrawerHeader,
|
|
15
|
+
DrawerProps,
|
|
16
|
+
DrawerTitle,
|
|
17
|
+
DrawerTrigger,
|
|
18
|
+
} from "../../Drawer";
|
|
19
|
+
|
|
20
|
+
export type CustomDrawerProps = DrawerProps & {
|
|
21
|
+
className?: string;
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
export function CustomDrawer({ className, ...props }: CustomDrawerProps) {
|
|
25
|
+
return (
|
|
26
|
+
<Drawer {...props}>
|
|
27
|
+
{/* <DrawerTrigger asChild>
|
|
28
|
+
<Button variant="outline">Open Drawer</Button>
|
|
29
|
+
</DrawerTrigger> */}
|
|
30
|
+
<DrawerContent className={className}>{props.children}</DrawerContent>
|
|
31
|
+
</Drawer>
|
|
32
|
+
);
|
|
33
|
+
}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import type { Meta, StoryObj } from "@storybook/nextjs";
|
|
2
|
+
|
|
3
|
+
import { Button } from "../../actions/Button";
|
|
4
|
+
|
|
5
|
+
import {
|
|
6
|
+
Drawer,
|
|
7
|
+
DrawerClose,
|
|
8
|
+
DrawerContent,
|
|
9
|
+
DrawerDescription,
|
|
10
|
+
DrawerFooter,
|
|
11
|
+
DrawerHeader,
|
|
12
|
+
DrawerTitle,
|
|
13
|
+
DrawerTrigger,
|
|
14
|
+
CustomDrawer as CustomDrawerComponent,
|
|
15
|
+
CustomDrawerProps,
|
|
16
|
+
} from "./index";
|
|
17
|
+
|
|
18
|
+
const meta = {
|
|
19
|
+
title: "DataDisplay/Drawer",
|
|
20
|
+
render: (args) => {
|
|
21
|
+
return (
|
|
22
|
+
<Drawer>
|
|
23
|
+
<DrawerTrigger>Open</DrawerTrigger>
|
|
24
|
+
<DrawerContent>
|
|
25
|
+
<DrawerHeader>
|
|
26
|
+
<DrawerTitle>Are you absolutely sure?</DrawerTitle>
|
|
27
|
+
<DrawerDescription>This action cannot be undone.</DrawerDescription>
|
|
28
|
+
</DrawerHeader>
|
|
29
|
+
<DrawerFooter>
|
|
30
|
+
<Button>Submit</Button>
|
|
31
|
+
<DrawerClose>
|
|
32
|
+
<Button variant="outline">Cancel</Button>
|
|
33
|
+
</DrawerClose>
|
|
34
|
+
</DrawerFooter>
|
|
35
|
+
</DrawerContent>
|
|
36
|
+
</Drawer>
|
|
37
|
+
);
|
|
38
|
+
},
|
|
39
|
+
parameters: {
|
|
40
|
+
layout: "centered",
|
|
41
|
+
},
|
|
42
|
+
tags: ["autodocs"],
|
|
43
|
+
} satisfies Meta<typeof Drawer>;
|
|
44
|
+
|
|
45
|
+
export default meta;
|
|
46
|
+
type Story = StoryObj<typeof meta>;
|
|
47
|
+
|
|
48
|
+
export const Default: Story = {
|
|
49
|
+
args: {
|
|
50
|
+
children: "Drawer",
|
|
51
|
+
},
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
export const CustomDrawer: StoryObj<CustomDrawerProps> = {
|
|
55
|
+
args: {
|
|
56
|
+
children: "Drawer",
|
|
57
|
+
open: true,
|
|
58
|
+
},
|
|
59
|
+
argTypes: {
|
|
60
|
+
direction: {
|
|
61
|
+
options: ["top", "right", "bottom", "left"],
|
|
62
|
+
control: {
|
|
63
|
+
type: "select",
|
|
64
|
+
},
|
|
65
|
+
},
|
|
66
|
+
className: {
|
|
67
|
+
control: {
|
|
68
|
+
type: "text",
|
|
69
|
+
},
|
|
70
|
+
},
|
|
71
|
+
open: {
|
|
72
|
+
control: {
|
|
73
|
+
type: "boolean",
|
|
74
|
+
},
|
|
75
|
+
},
|
|
76
|
+
},
|
|
77
|
+
render: (args) => {
|
|
78
|
+
return <CustomDrawerComponent {...args} />;
|
|
79
|
+
},
|
|
80
|
+
};
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import type { Meta, StoryObj } from "@storybook/nextjs";
|
|
2
|
+
import { PageLayout } from "./index";
|
|
3
|
+
import { Icon, Text } from "../../../components/dataDisplay";
|
|
4
|
+
import { Button } from "../../../components/actions";
|
|
5
|
+
|
|
6
|
+
const meta: Meta<typeof PageLayout> = {
|
|
7
|
+
title: "Layout/PageLayout",
|
|
8
|
+
component: PageLayout,
|
|
9
|
+
parameters: {
|
|
10
|
+
layout: "fullscreen",
|
|
11
|
+
},
|
|
12
|
+
tags: ["autodocs"],
|
|
13
|
+
argTypes: {
|
|
14
|
+
title: { control: "text" },
|
|
15
|
+
subtitle: { control: "text" },
|
|
16
|
+
header: { control: "text" },
|
|
17
|
+
},
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
export default meta;
|
|
21
|
+
type Story = StoryObj<typeof PageLayout>;
|
|
22
|
+
|
|
23
|
+
export const Default: Story = {
|
|
24
|
+
args: {
|
|
25
|
+
title: "Page Title",
|
|
26
|
+
subtitle: "Page Subtitle",
|
|
27
|
+
children: <Text>Page content goes here.</Text>,
|
|
28
|
+
},
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
export const WithHeader: Story = {
|
|
32
|
+
args: {
|
|
33
|
+
title: "Page Title",
|
|
34
|
+
subtitle: "Page Subtitle",
|
|
35
|
+
header: (
|
|
36
|
+
<Button>
|
|
37
|
+
<Icon name="MdAdd" />
|
|
38
|
+
</Button>
|
|
39
|
+
),
|
|
40
|
+
children: <Text>Page content goes here.</Text>,
|
|
41
|
+
},
|
|
42
|
+
};
|