This commit is contained in:
+36
-12
@@ -17,30 +17,54 @@ type Props = {
|
|||||||
} & SelectHTMLAttributes<HTMLSelectElement>
|
} & SelectHTMLAttributes<HTMLSelectElement>
|
||||||
|
|
||||||
const Select: FC<Props> = (props: Props) => {
|
const Select: FC<Props> = (props: Props) => {
|
||||||
|
const {
|
||||||
|
className,
|
||||||
|
items,
|
||||||
|
error_text,
|
||||||
|
placeholder,
|
||||||
|
label,
|
||||||
|
isNotRequired,
|
||||||
|
value,
|
||||||
|
...selectProps
|
||||||
|
} = props
|
||||||
|
|
||||||
|
const hasMatchingItem =
|
||||||
|
value !== undefined &&
|
||||||
|
value !== '' &&
|
||||||
|
items?.some((item) => String(item.value) === String(value))
|
||||||
|
|
||||||
|
const selectValue =
|
||||||
|
!value || hasMatchingItem || !items?.length
|
||||||
|
? (value ?? '')
|
||||||
|
: ''
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className='w-full relative'>
|
<div className='w-full relative'>
|
||||||
{
|
{
|
||||||
props.label &&
|
label &&
|
||||||
<div className='flex items-center gap-1'>
|
<div className='flex items-center gap-1'>
|
||||||
<label className='text-sm'>
|
<label className='text-sm'>
|
||||||
{props.label}
|
{label}
|
||||||
</label>
|
</label>
|
||||||
{props.isNotRequired && (
|
{isNotRequired && (
|
||||||
<span className='text-xs text-description'>(اختیاری)</span>
|
<span className='text-xs text-description'>(اختیاری)</span>
|
||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
}
|
}
|
||||||
<select {...props} className={clx(
|
<select
|
||||||
|
{...selectProps}
|
||||||
|
value={selectValue}
|
||||||
|
className={clx(
|
||||||
'w-full text-black block border appearance-none border-border !bg-white px-2.5 h-10 text-sm rounded-[10px] bg-gray',
|
'w-full text-black block border appearance-none border-border !bg-white px-2.5 h-10 text-sm rounded-[10px] bg-gray',
|
||||||
props.className,
|
className,
|
||||||
props.label && 'mt-1'
|
label && 'mt-1'
|
||||||
)}>
|
)}>
|
||||||
{
|
{
|
||||||
props.placeholder &&
|
placeholder &&
|
||||||
<option value="" disabled selected>{props.placeholder}</option>
|
<option value="" disabled>{placeholder}</option>
|
||||||
}
|
}
|
||||||
{
|
{
|
||||||
props.items?.map((item) => {
|
items?.map((item) => {
|
||||||
return (
|
return (
|
||||||
<option key={item.value} value={item.value}>
|
<option key={item.value} value={item.value}>
|
||||||
{item.label}
|
{item.label}
|
||||||
@@ -51,12 +75,12 @@ const Select: FC<Props> = (props: Props) => {
|
|||||||
</select>
|
</select>
|
||||||
<ArrowDown2 size={16} color='black' className={clx(
|
<ArrowDown2 size={16} color='black' className={clx(
|
||||||
'absolute z-0 top-3 left-2',
|
'absolute z-0 top-3 left-2',
|
||||||
props.label && 'top-10'
|
label && 'top-10'
|
||||||
)} />
|
)} />
|
||||||
{
|
{
|
||||||
props.error_text && props.error_text !== '' ?
|
error_text && error_text !== '' ?
|
||||||
<Error
|
<Error
|
||||||
errorText={props.error_text}
|
errorText={error_text}
|
||||||
/>
|
/>
|
||||||
: null
|
: null
|
||||||
}
|
}
|
||||||
|
|||||||
+80
-88
@@ -1,156 +1,148 @@
|
|||||||
import { type FC, useEffect } from 'react'
|
import Button from "@/components/Button";
|
||||||
import { useFormik } from 'formik'
|
import Input from "@/components/Input";
|
||||||
import * as Yup from 'yup'
|
import Select from "@/components/Select";
|
||||||
import { TickCircle } from 'iconsax-react'
|
import { extractErrorMessage } from "@/config/func";
|
||||||
import { toast } from 'react-toastify'
|
import { Pages } from "@/config/Pages";
|
||||||
import { useNavigate, useParams } from 'react-router-dom'
|
import type { ErrorType } from "@/helpers/types";
|
||||||
import Button from '@/components/Button'
|
import { useGetRoles } from "@/pages/roles/hooks/useRolesData";
|
||||||
import Input from '@/components/Input'
|
import { useFormik } from "formik";
|
||||||
import Select from '@/components/Select'
|
import { TickCircle } from "iconsax-react";
|
||||||
import { useGetAdminById, useUpdateAdmin } from './hooks/useAdminData'
|
import { type FC, useEffect } from "react";
|
||||||
import { useGetRoles } from '@/pages/roles/hooks/useRolesData'
|
import { useNavigate, useParams } from "react-router-dom";
|
||||||
import type { CreateAdminType } from './types/Types'
|
import { toast } from "react-toastify";
|
||||||
import { Pages } from '@/config/Pages'
|
import * as Yup from "yup";
|
||||||
import type { ErrorType } from '@/helpers/types'
|
import { useGetAdminById, useUpdateAdmin } from "./hooks/useAdminData";
|
||||||
import { extractErrorMessage } from '@/config/func'
|
import type { CreateAdminType } from "./types/Types";
|
||||||
|
|
||||||
const UpdateAdmin: FC = () => {
|
const UpdateAdmin: FC = () => {
|
||||||
const navigate = useNavigate()
|
const navigate = useNavigate();
|
||||||
const { id } = useParams()
|
const { id } = useParams();
|
||||||
const { data: adminData, isLoading } = useGetAdminById(id!)
|
const { data: adminData, isLoading } = useGetAdminById(id!);
|
||||||
const { mutate: updateAdmin, isPending } = useUpdateAdmin()
|
const { mutate: updateAdmin, isPending } = useUpdateAdmin();
|
||||||
const { data: rolesData } = useGetRoles()
|
const { data: rolesData } = useGetRoles();
|
||||||
|
|
||||||
const roles = rolesData?.data?.map(role => ({
|
const roles =
|
||||||
|
rolesData?.data?.map((role) => ({
|
||||||
label: role.name,
|
label: role.name,
|
||||||
value: role.id
|
value: role.id,
|
||||||
})) || []
|
})) || [];
|
||||||
|
|
||||||
const admin = adminData?.data
|
const admin = adminData?.data;
|
||||||
|
|
||||||
const formik = useFormik<CreateAdminType>({
|
const formik = useFormik<CreateAdminType>({
|
||||||
initialValues: {
|
initialValues: {
|
||||||
phone: '',
|
phone: "",
|
||||||
firstName: '',
|
firstName: "",
|
||||||
lastName: '',
|
lastName: "",
|
||||||
roleId: '',
|
roleId: "",
|
||||||
},
|
},
|
||||||
validationSchema: Yup.object().shape({
|
validationSchema: Yup.object().shape({
|
||||||
phone: Yup.string().required('شماره تلفن الزامی است'),
|
phone: Yup.string().required("شماره تلفن الزامی است"),
|
||||||
firstName: Yup.string().required('نام الزامی است'),
|
firstName: Yup.string().required("نام الزامی است"),
|
||||||
lastName: Yup.string().required('نام خانوادگی الزامی است'),
|
lastName: Yup.string().required("نام خانوادگی الزامی است"),
|
||||||
roleId: Yup.string().required('نقش الزامی است'),
|
roleId: Yup.string().required("نقش الزامی است"),
|
||||||
}),
|
}),
|
||||||
onSubmit: (values) => {
|
onSubmit: (values) => {
|
||||||
if (!id) {
|
if (!id) {
|
||||||
toast.error('شناسه مدیر یافت نشد')
|
toast.error("شناسه مدیر یافت نشد");
|
||||||
return
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
updateAdmin({ id, params: values }, {
|
updateAdmin(
|
||||||
|
{ id, params: values },
|
||||||
|
{
|
||||||
onSuccess: () => {
|
onSuccess: () => {
|
||||||
toast.success('مدیر با موفقیت بهروزرسانی شد')
|
toast.success("مدیر با موفقیت بهروزرسانی شد");
|
||||||
navigate(Pages.admins.list)
|
navigate(Pages.admins.list);
|
||||||
},
|
},
|
||||||
onError: (error: ErrorType) => {
|
onError: (error: ErrorType) => {
|
||||||
toast.error(extractErrorMessage(error))
|
toast.error(extractErrorMessage(error));
|
||||||
},
|
},
|
||||||
})
|
|
||||||
},
|
},
|
||||||
})
|
);
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (admin) {
|
if (admin) {
|
||||||
formik.setValues({
|
formik.setValues({
|
||||||
phone: admin.phone || '',
|
phone: admin.phone || "",
|
||||||
firstName: admin.firstName || '',
|
firstName: admin.firstName || "",
|
||||||
lastName: admin.lastName || '',
|
lastName: admin.lastName || "",
|
||||||
roleId: admin.roles?.[0]?.role?.id || '',
|
roleId: admin.role?.id || "",
|
||||||
})
|
});
|
||||||
}
|
}
|
||||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||||
}, [admin])
|
}, [admin]);
|
||||||
|
|
||||||
if (isLoading) {
|
if (isLoading) {
|
||||||
return (
|
return (
|
||||||
<div className='w-full mt-4 flex justify-center items-center'>
|
<div className="w-full mt-4 flex justify-center items-center">
|
||||||
<div>در حال بارگذاری...</div>
|
<div>در حال بارگذاری...</div>
|
||||||
</div>
|
</div>
|
||||||
)
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!admin) {
|
if (!admin) {
|
||||||
return (
|
return (
|
||||||
<div className='w-full mt-4 flex justify-center items-center'>
|
<div className="w-full mt-4 flex justify-center items-center">
|
||||||
<div>مدیر یافت نشد</div>
|
<div>مدیر یافت نشد</div>
|
||||||
</div>
|
</div>
|
||||||
)
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className='mt-5'>
|
<div className="mt-5">
|
||||||
<div className='flex w-full justify-between items-center'>
|
<div className="flex w-full justify-between items-center">
|
||||||
<div className='text-lg font-light'>
|
<div className="text-lg font-light">ویرایش مدیر</div>
|
||||||
ویرایش مدیر
|
|
||||||
</div>
|
|
||||||
<div>
|
<div>
|
||||||
<Button
|
<Button className="px-5" onClick={() => formik.handleSubmit()} isloading={isPending}>
|
||||||
className='px-5'
|
<div className="flex gap-2 items-center">
|
||||||
onClick={() => formik.handleSubmit()}
|
<TickCircle className="size-5" color="white" />
|
||||||
isloading={isPending}
|
|
||||||
>
|
|
||||||
<div className='flex gap-2 items-center'>
|
|
||||||
<TickCircle className='size-5' color='white' />
|
|
||||||
<div>ذخیره تغییرات</div>
|
<div>ذخیره تغییرات</div>
|
||||||
</div>
|
</div>
|
||||||
</Button>
|
</Button>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div className='mt-6'>
|
<div className="mt-6">
|
||||||
<div className='bg-white rounded-3xl p-6'>
|
<div className="bg-white rounded-3xl p-6">
|
||||||
<div className='mb-4'>
|
<div className="mb-4">
|
||||||
<Input
|
<Input
|
||||||
label='نام'
|
label="نام"
|
||||||
name='firstName'
|
name="firstName"
|
||||||
value={formik.values.firstName}
|
value={formik.values.firstName}
|
||||||
onChange={formik.handleChange}
|
onChange={formik.handleChange}
|
||||||
error_text={formik.touched.firstName && formik.errors.firstName ? formik.errors.firstName : ''}
|
error_text={formik.touched.firstName && formik.errors.firstName ? formik.errors.firstName : ""}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
<div className='mt-6'>
|
<div className="mt-6">
|
||||||
<Input
|
<Input
|
||||||
label='نام خانوادگی'
|
label="نام خانوادگی"
|
||||||
name='lastName'
|
name="lastName"
|
||||||
value={formik.values.lastName}
|
value={formik.values.lastName}
|
||||||
onChange={formik.handleChange}
|
onChange={formik.handleChange}
|
||||||
error_text={formik.touched.lastName && formik.errors.lastName ? formik.errors.lastName : ''}
|
error_text={formik.touched.lastName && formik.errors.lastName ? formik.errors.lastName : ""}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
<div className='mt-6'>
|
<div className="mt-6">
|
||||||
<Input
|
<Input label="شماره تلفن" name="phone" value={formik.values.phone} onChange={formik.handleChange} error_text={formik.touched.phone && formik.errors.phone ? formik.errors.phone : ""} />
|
||||||
label='شماره تلفن'
|
|
||||||
name='phone'
|
|
||||||
value={formik.values.phone}
|
|
||||||
onChange={formik.handleChange}
|
|
||||||
error_text={formik.touched.phone && formik.errors.phone ? formik.errors.phone : ''}
|
|
||||||
/>
|
|
||||||
</div>
|
</div>
|
||||||
<div className='mt-6'>
|
<div className="mt-6">
|
||||||
<Select
|
<Select
|
||||||
label='نقش'
|
label="نقش"
|
||||||
name='roleId'
|
name="roleId"
|
||||||
value={formik.values.roleId}
|
value={formik.values.roleId}
|
||||||
onChange={formik.handleChange}
|
onChange={formik.handleChange}
|
||||||
items={roles}
|
items={roles}
|
||||||
placeholder='نقش را انتخاب کنید'
|
placeholder="نقش را انتخاب کنید"
|
||||||
error_text={formik.touched.roleId && formik.errors.roleId ? formik.errors.roleId : ''}
|
error_text={formik.touched.roleId && formik.errors.roleId ? formik.errors.roleId : ""}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
)
|
);
|
||||||
}
|
};
|
||||||
|
|
||||||
export default UpdateAdmin
|
export default UpdateAdmin;
|
||||||
|
|||||||
@@ -28,6 +28,7 @@ export type Admin = {
|
|||||||
lastName: string;
|
lastName: string;
|
||||||
phone: string;
|
phone: string;
|
||||||
roles: AdminRole[];
|
roles: AdminRole[];
|
||||||
|
role: RoleDetail;
|
||||||
};
|
};
|
||||||
|
|
||||||
export type GetAdminsParams = {
|
export type GetAdminsParams = {
|
||||||
|
|||||||
Reference in New Issue
Block a user