add users and fix modal
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import { Stack, Loader, Title, Group, ActionIcon, Tooltip, Table, ScrollArea } from "@mantine/core";
|
||||
import { createForm, createShipment, editForm, editShipment, getForms } from "@/services/api";
|
||||
import { createForm, createShipment, editForm, editShipment, getForm, getForms } from "@/services/api";
|
||||
import { t } from "@/config/i18n";
|
||||
import { useLocation, useNavigate, useSearchParams } from "react-router";
|
||||
import { IconPlus } from "@tabler/icons-react";
|
||||
@@ -18,11 +18,20 @@ export function Forms() {
|
||||
const isCreate = location.pathname === "/dashboard/forms/create";
|
||||
const isEdit = location.pathname.includes("/edit");
|
||||
|
||||
const editId = useMemo(() => {
|
||||
if (isEdit) {
|
||||
return location.pathname.split("/")[3]
|
||||
}
|
||||
return null
|
||||
}, [location, isEdit])
|
||||
|
||||
const closeModal = () => {
|
||||
navigate("/dashboard/forms");
|
||||
};
|
||||
|
||||
const { isPending, data } = getForms(searchParams);
|
||||
const { data: currentForm } = getForm(Number(editId), { enabled: !!editId });
|
||||
|
||||
const { data: allForms } = getForms();
|
||||
|
||||
const seasons = useMemo(() => {
|
||||
@@ -159,6 +168,12 @@ export function Forms() {
|
||||
filters={searchParams}
|
||||
onFilterChange={onFilterChange}
|
||||
/>
|
||||
<FormModal
|
||||
opened={isEdit}
|
||||
onClose={closeModal}
|
||||
currentForm={currentForm}
|
||||
handleSubmit={handleEditForm}
|
||||
/>
|
||||
<ScrollArea type="auto">
|
||||
<Table striped>
|
||||
<Table.Thead>
|
||||
@@ -177,9 +192,7 @@ export function Forms() {
|
||||
data.map((form) => (
|
||||
<FormRow
|
||||
form={form}
|
||||
isEdit={isEdit}
|
||||
closeModal={closeModal}
|
||||
handleSubmit={handleEditForm}
|
||||
key={form.id}
|
||||
/>
|
||||
))
|
||||
}
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
import { ActionIcon, Group, Loader, ScrollArea, Stack, Table, Title, Tooltip } from "@mantine/core";
|
||||
import { t } from "@/config/i18n";
|
||||
import { createProductor, editProductor, getProductors } from "@/services/api";
|
||||
import { createProductor, editProductor, getProductor, getProductors } from "@/services/api";
|
||||
import { IconPlus } from "@tabler/icons-react";
|
||||
import ProductorRow from "@/components/Productors/Row";
|
||||
import { useLocation, useNavigate, useSearchParams } from "react-router";
|
||||
import { ProductorModal } from "@/components/Productors/Modal";
|
||||
import { useCallback, useMemo } from "react";
|
||||
import { useCallback, useMemo, useState } from "react";
|
||||
import type { Productor, ProductorInputs } from "@/services/resources/productors";
|
||||
import ProductorsFilters from "@/components/Productors/Filter";
|
||||
|
||||
@@ -17,12 +17,20 @@ export default function Productors() {
|
||||
const isCreate = location.pathname === "/dashboard/productors/create";
|
||||
const isEdit = location.pathname.includes("/edit");
|
||||
|
||||
const editId = useMemo(() => {
|
||||
if (isEdit) {
|
||||
return location.pathname.split("/")[3]
|
||||
}
|
||||
return null
|
||||
}, [location, isEdit])
|
||||
|
||||
const closeModal = () => {
|
||||
navigate("/dashboard/productors");
|
||||
};
|
||||
|
||||
const {data: productors, isPending} = getProductors(searchParams);
|
||||
const {data: allProductors } = getProductors();
|
||||
const { data: productors, isPending } = getProductors(searchParams);
|
||||
const { data: currentProductor } = getProductor(Number(editId), { enabled: !!editId });
|
||||
const { data: allProductors } = getProductors();
|
||||
|
||||
const names = useMemo(() => {
|
||||
return allProductors?.map((productor: Productor) => (productor.name))
|
||||
@@ -88,6 +96,12 @@ export default function Productors() {
|
||||
onClose={closeModal}
|
||||
handleSubmit={handleCreateProductor}
|
||||
/>
|
||||
<ProductorModal
|
||||
opened={isEdit}
|
||||
onClose={closeModal}
|
||||
currentProductor={currentProductor}
|
||||
handleSubmit={handleEditProductor}
|
||||
/>
|
||||
</Group>
|
||||
<ProductorsFilters
|
||||
names={names || []}
|
||||
@@ -111,10 +125,7 @@ export default function Productors() {
|
||||
productors.map((productor) => (
|
||||
<ProductorRow
|
||||
productor={productor}
|
||||
isEdit={isEdit}
|
||||
closeModal={closeModal}
|
||||
handleSubmit={handleEditProductor}
|
||||
|
||||
key={productor.id}
|
||||
/>
|
||||
))
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { ActionIcon, Group, Loader, ScrollArea, Stack, Table, Title, Tooltip } from "@mantine/core";
|
||||
import { t } from "@/config/i18n";
|
||||
import { createProduct, editProduct, getProducts } from "@/services/api";
|
||||
import { createProduct, editProduct, getProduct, getProducts } from "@/services/api";
|
||||
import { IconPlus } from "@tabler/icons-react";
|
||||
import ProductRow from "@/components/Products/Row";
|
||||
import { useLocation, useNavigate, useSearchParams } from "react-router";
|
||||
@@ -17,12 +17,20 @@ export default function Products() {
|
||||
const isCreate = location.pathname === "/dashboard/products/create";
|
||||
const isEdit = location.pathname.includes("/edit");
|
||||
|
||||
const editId = useMemo(() => {
|
||||
if (isEdit) {
|
||||
return location.pathname.split("/")[3]
|
||||
}
|
||||
return null
|
||||
}, [location, isEdit])
|
||||
|
||||
const closeModal = () => {
|
||||
navigate("/dashboard/products");
|
||||
};
|
||||
|
||||
const {data: products, isPending} = getProducts(searchParams);
|
||||
const {data: allProducts } = getProducts();
|
||||
const { data: products, isPending } = getProducts(searchParams);
|
||||
const { data: currentProduct } = getProduct(Number(editId), { enabled: !!editId });
|
||||
const { data: allProducts } = getProducts();
|
||||
|
||||
const names = useMemo(() => {
|
||||
return allProducts?.map((product: Product) => (product.name))
|
||||
@@ -60,7 +68,6 @@ export default function Products() {
|
||||
values.forEach(value => {
|
||||
params.append(filter, value);
|
||||
});
|
||||
|
||||
return params;
|
||||
});
|
||||
}, [searchParams, setSearchParams])
|
||||
@@ -94,6 +101,12 @@ export default function Products() {
|
||||
filters={searchParams}
|
||||
onFilterChange={onFilterChange}
|
||||
/>
|
||||
<ProductModal
|
||||
opened={isEdit}
|
||||
onClose={closeModal}
|
||||
currentProduct={currentProduct}
|
||||
handleSubmit={handleEditProduct}
|
||||
/>
|
||||
<ScrollArea type="auto">
|
||||
<Table striped>
|
||||
<Table.Thead>
|
||||
@@ -112,10 +125,7 @@ export default function Products() {
|
||||
products.map((product) => (
|
||||
<ProductRow
|
||||
product={product}
|
||||
isEdit={isEdit}
|
||||
closeModal={closeModal}
|
||||
handleSubmit={handleEditProduct}
|
||||
|
||||
key={product.id}
|
||||
/>
|
||||
))
|
||||
}
|
||||
|
||||
@@ -1,5 +1,128 @@
|
||||
import { ActionIcon, Group, Loader, ScrollArea, Stack, Table, Title, Tooltip } from "@mantine/core";
|
||||
import { t } from "@/config/i18n";
|
||||
import { createUser, editUser, getUser, getUsers } from "@/services/api";
|
||||
import { IconPlus } from "@tabler/icons-react";
|
||||
import UserRow from "@/components/Users/Row";
|
||||
import { useLocation, useNavigate, useSearchParams } from "react-router";
|
||||
import { UserModal } from "@/components/Users/Modal";
|
||||
import { useCallback, useMemo } from "react";
|
||||
import { type User, type UserInputs } from "@/services/resources/users";
|
||||
import UsersFilters from "@/components/Users/Filter";
|
||||
|
||||
export default function Users() {
|
||||
const [ searchParams, setSearchParams ] = useSearchParams();
|
||||
const location = useLocation();
|
||||
const navigate = useNavigate();
|
||||
|
||||
const isCreate = location.pathname === "/dashboard/users/create";
|
||||
const isEdit = location.pathname.includes("/edit");
|
||||
|
||||
const editId = useMemo(() => {
|
||||
if (isEdit) {
|
||||
return location.pathname.split("/")[3]
|
||||
}
|
||||
return null
|
||||
}, [location, isEdit])
|
||||
|
||||
const closeModal = () => {
|
||||
navigate("/dashboard/users");
|
||||
};
|
||||
|
||||
const {data: users, isPending} = getUsers(searchParams);
|
||||
const { data: currentUser } = getUser(Number(editId), { enabled: !!editId });
|
||||
|
||||
const {data: allUsers } = getUsers();
|
||||
|
||||
const names = useMemo(() => {
|
||||
return allUsers?.map((user: User) => (user.name))
|
||||
.filter((season, index, array) => array.indexOf(season) === index)
|
||||
}, [allUsers])
|
||||
|
||||
const createUserMutation = createUser();
|
||||
const editUserMutation = editUser();
|
||||
|
||||
const handleCreateUser = useCallback(async (user: UserInputs) => {
|
||||
await createUserMutation.mutateAsync(user);
|
||||
closeModal();
|
||||
}, [createUserMutation]);
|
||||
|
||||
const handleEditUser = useCallback(async (user: UserInputs, id?: number) => {
|
||||
if (!id)
|
||||
return;
|
||||
await editUserMutation.mutateAsync({
|
||||
id: id,
|
||||
user: user
|
||||
});
|
||||
closeModal();
|
||||
}, []);
|
||||
|
||||
const onFilterChange = useCallback((values: string[], filter: string) => {
|
||||
setSearchParams(prev => {
|
||||
const params = new URLSearchParams(prev);
|
||||
params.delete(filter);
|
||||
|
||||
values.forEach(value => {
|
||||
params.append(filter, value);
|
||||
});
|
||||
return params;
|
||||
});
|
||||
}, [searchParams, setSearchParams])
|
||||
|
||||
if (!users || isPending)
|
||||
return <Loader/>
|
||||
|
||||
return (
|
||||
<></>
|
||||
<Stack>
|
||||
<Group justify="space-between">
|
||||
<Title order={2}>{t("all users", {capfirst: true})}</Title>
|
||||
<Tooltip label={t("create user", {capfirst: true})}>
|
||||
<ActionIcon
|
||||
onClick={(e) => {
|
||||
e.stopPropagation();
|
||||
navigate(`/dashboard/users/create`);
|
||||
}}
|
||||
>
|
||||
<IconPlus/>
|
||||
</ActionIcon>
|
||||
</Tooltip>
|
||||
<UserModal
|
||||
opened={isCreate}
|
||||
onClose={closeModal}
|
||||
handleSubmit={handleCreateUser}
|
||||
/>
|
||||
<UserModal
|
||||
opened={isEdit}
|
||||
onClose={closeModal}
|
||||
currentUser={currentUser}
|
||||
handleSubmit={handleEditUser}
|
||||
/>
|
||||
</Group>
|
||||
<UsersFilters
|
||||
names={names || []}
|
||||
filters={searchParams}
|
||||
onFilterChange={onFilterChange}
|
||||
/>
|
||||
<ScrollArea type="auto">
|
||||
<Table striped>
|
||||
<Table.Thead>
|
||||
<Table.Tr>
|
||||
<Table.Th>{t("name", {capfirst: true})}</Table.Th>
|
||||
<Table.Th>{t("email", {capfirst: true})}</Table.Th>
|
||||
<Table.Th>{t("actions", {capfirst: true})}</Table.Th>
|
||||
</Table.Tr>
|
||||
</Table.Thead>
|
||||
<Table.Tbody>
|
||||
{
|
||||
users.map((user) => (
|
||||
<UserRow
|
||||
user={user}
|
||||
key={user.id}
|
||||
/>
|
||||
))
|
||||
}
|
||||
</Table.Tbody>
|
||||
</Table>
|
||||
</ScrollArea>
|
||||
</Stack>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user