feat: edit user and add admin permission
This commit is contained in:
@@ -12,6 +12,7 @@ import { DeleteButtonForPopOver } from "../components/PopOverButtons/PopOverButt
|
|||||||
import { getFaPermissions } from "../utils/getFaPermissions";
|
import { getFaPermissions } from "../utils/getFaPermissions";
|
||||||
import { getFaProvince } from "../utils/getFaProvince";
|
import { getFaProvince } from "../utils/getFaProvince";
|
||||||
import { getFaCityName } from "../utils/getFaCityName";
|
import { getFaCityName } from "../utils/getFaCityName";
|
||||||
|
import { Tooltip } from "../components/Tooltip/Tooltip";
|
||||||
|
|
||||||
const Users: React.FC = () => {
|
const Users: React.FC = () => {
|
||||||
const { profile } = useUserProfileStore();
|
const { profile } = useUserProfileStore();
|
||||||
@@ -38,19 +39,32 @@ const Users: React.FC = () => {
|
|||||||
)) || "-",
|
)) || "-",
|
||||||
getFaProvince(item?.province || ""),
|
getFaProvince(item?.province || ""),
|
||||||
getFaCityName(item?.city || ""),
|
getFaCityName(item?.city || ""),
|
||||||
item?.mobile === profile?.mobile ? (
|
|
||||||
<Typography variant="body2" className="text-gray-400">
|
|
||||||
-
|
|
||||||
</Typography>
|
|
||||||
) : (
|
|
||||||
<Popover key={i}>
|
<Popover key={i}>
|
||||||
|
<Tooltip title="ویرایش" position="right">
|
||||||
|
<Button
|
||||||
|
variant="edit"
|
||||||
|
access="add"
|
||||||
|
onClick={() => {
|
||||||
|
openDrawer({
|
||||||
|
title: "ویرایش کاربر",
|
||||||
|
content: (
|
||||||
|
<SubmitNewUser
|
||||||
|
province={profile?.province || ""}
|
||||||
|
onSuccess={refetch}
|
||||||
|
item={item}
|
||||||
|
/>
|
||||||
|
),
|
||||||
|
});
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
</Tooltip>
|
||||||
<DeleteButtonForPopOver
|
<DeleteButtonForPopOver
|
||||||
access="add"
|
access="add"
|
||||||
api={`users/${item?._id || item?.Id}`}
|
api={`users/${item?._id || item?.Id}`}
|
||||||
getData={refetch}
|
getData={refetch}
|
||||||
/>
|
/>
|
||||||
</Popover>
|
</Popover>,
|
||||||
),
|
|
||||||
];
|
];
|
||||||
});
|
});
|
||||||
setTableData(d);
|
setTableData(d);
|
||||||
|
|||||||
@@ -17,25 +17,42 @@ import {
|
|||||||
zValidateAutoComplete,
|
zValidateAutoComplete,
|
||||||
} from "../../data/getFormTypeErrors";
|
} from "../../data/getFormTypeErrors";
|
||||||
|
|
||||||
|
interface UserItem {
|
||||||
|
_id?: string;
|
||||||
|
Id?: string;
|
||||||
|
mobile?: string;
|
||||||
|
fullname?: string;
|
||||||
|
permissions?: string[];
|
||||||
|
province?: string;
|
||||||
|
city?: string;
|
||||||
|
}
|
||||||
|
|
||||||
interface SubmitNewUserProps {
|
interface SubmitNewUserProps {
|
||||||
province: string;
|
province: string;
|
||||||
onSuccess?: () => void;
|
onSuccess?: () => void;
|
||||||
|
item?: UserItem | null;
|
||||||
}
|
}
|
||||||
|
|
||||||
const schema = z.object({
|
const getSchema = (isEdit: boolean) =>
|
||||||
|
z.object({
|
||||||
mobile: zValidateMobile("موبایل"),
|
mobile: zValidateMobile("موبایل"),
|
||||||
password: zValidateString("کلمه عبور"),
|
password: isEdit ? z.string().optional() : zValidateString("کلمه عبور"),
|
||||||
fullname: zValidateString("نام کامل"),
|
fullname: zValidateString("نام کامل"),
|
||||||
permissions: zValidateAutoComplete("دسترسیها"),
|
permissions: zValidateAutoComplete("دسترسیها"),
|
||||||
city: zValidateAutoComplete("شهر"),
|
city: zValidateAutoComplete("شهر"),
|
||||||
});
|
});
|
||||||
|
|
||||||
type FormValues = z.infer<typeof schema>;
|
type FormValues = z.infer<ReturnType<typeof getSchema>>;
|
||||||
|
|
||||||
export const SubmitNewUser: React.FC<SubmitNewUserProps> = ({ onSuccess }) => {
|
export const SubmitNewUser: React.FC<SubmitNewUserProps> = ({
|
||||||
|
onSuccess,
|
||||||
|
item,
|
||||||
|
}) => {
|
||||||
const { profile } = useUserProfileStore();
|
const { profile } = useUserProfileStore();
|
||||||
const showToast = useToast();
|
const showToast = useToast();
|
||||||
const { closeDrawer } = useDrawerStore();
|
const { closeDrawer } = useDrawerStore();
|
||||||
|
const isEdit = !!item;
|
||||||
|
const schema = useMemo(() => getSchema(isEdit), [isEdit]);
|
||||||
|
|
||||||
const {
|
const {
|
||||||
control,
|
control,
|
||||||
@@ -44,7 +61,15 @@ export const SubmitNewUser: React.FC<SubmitNewUserProps> = ({ onSuccess }) => {
|
|||||||
formState: { errors, isSubmitting },
|
formState: { errors, isSubmitting },
|
||||||
} = useForm<FormValues>({
|
} = useForm<FormValues>({
|
||||||
resolver: zodResolver(schema),
|
resolver: zodResolver(schema),
|
||||||
defaultValues: {
|
defaultValues: item
|
||||||
|
? {
|
||||||
|
mobile: item.mobile ?? "",
|
||||||
|
password: "",
|
||||||
|
fullname: item.fullname ?? "",
|
||||||
|
permissions: Array.isArray(item.permissions) ? item.permissions : [],
|
||||||
|
city: item.city ? [item.city] : [],
|
||||||
|
}
|
||||||
|
: {
|
||||||
mobile: "",
|
mobile: "",
|
||||||
password: "",
|
password: "",
|
||||||
fullname: "",
|
fullname: "",
|
||||||
@@ -53,11 +78,16 @@ export const SubmitNewUser: React.FC<SubmitNewUserProps> = ({ onSuccess }) => {
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
const submitUserMutation = useApiMutation({
|
const createUserMutation = useApiMutation({
|
||||||
api: "user",
|
api: "user",
|
||||||
method: "post",
|
method: "post",
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const updateUserMutation = useApiMutation({
|
||||||
|
api: `user/${item?._id ?? item?.Id ?? ""}`,
|
||||||
|
method: "put",
|
||||||
|
});
|
||||||
|
|
||||||
const cityOptions = useMemo(() => {
|
const cityOptions = useMemo(() => {
|
||||||
return getCitiesOfProvinceInfo(profile?.province || "").map((item) => ({
|
return getCitiesOfProvinceInfo(profile?.province || "").map((item) => ({
|
||||||
key: item.en,
|
key: item.en,
|
||||||
@@ -65,18 +95,23 @@ export const SubmitNewUser: React.FC<SubmitNewUserProps> = ({ onSuccess }) => {
|
|||||||
}));
|
}));
|
||||||
}, [profile?.province]);
|
}, [profile?.province]);
|
||||||
|
|
||||||
|
const hasAdminPermission = profile?.permissions?.includes("admin");
|
||||||
|
|
||||||
const permissionOptions = useMemo(() => {
|
const permissionOptions = useMemo(() => {
|
||||||
return [
|
const options = [
|
||||||
{ key: "add", value: "ثبت کاربر" },
|
{ key: "add", value: "ثبت کاربر" },
|
||||||
{ key: "submit", value: "ثبت بازرسی" },
|
{ key: "submit", value: "ثبت بازرسی" },
|
||||||
];
|
];
|
||||||
}, []);
|
if (hasAdminPermission) {
|
||||||
|
options.push({ key: "admin", value: "ادمین" });
|
||||||
|
}
|
||||||
|
return options;
|
||||||
|
}, [hasAdminPermission]);
|
||||||
|
|
||||||
const onSubmit = async (data: FormValues) => {
|
const onSubmit = async (data: FormValues) => {
|
||||||
try {
|
try {
|
||||||
const payload = {
|
const basePayload = {
|
||||||
mobile: data.mobile,
|
mobile: data.mobile,
|
||||||
password: data.password,
|
|
||||||
fullname: data.fullname,
|
fullname: data.fullname,
|
||||||
pic: "",
|
pic: "",
|
||||||
permissions: data.permissions as string[],
|
permissions: data.permissions as string[],
|
||||||
@@ -87,15 +122,34 @@ export const SubmitNewUser: React.FC<SubmitNewUserProps> = ({ onSuccess }) => {
|
|||||||
: "",
|
: "",
|
||||||
};
|
};
|
||||||
|
|
||||||
await submitUserMutation.mutateAsync(payload);
|
if (isEdit) {
|
||||||
|
const payload =
|
||||||
|
data.password && String(data.password).trim() !== ""
|
||||||
|
? { ...basePayload, password: data.password }
|
||||||
|
: basePayload;
|
||||||
|
await updateUserMutation.mutateAsync(payload);
|
||||||
|
showToast("کاربر با موفقیت ویرایش شد", "success");
|
||||||
|
} else {
|
||||||
|
if (!data.password || String(data.password).trim() === "") {
|
||||||
|
showToast("کلمه عبور را وارد کنید", "error");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const payload = { ...basePayload, password: data.password };
|
||||||
|
await createUserMutation.mutateAsync(payload);
|
||||||
showToast("کاربر با موفقیت ثبت شد", "success");
|
showToast("کاربر با موفقیت ثبت شد", "success");
|
||||||
|
}
|
||||||
closeDrawer();
|
closeDrawer();
|
||||||
if (onSuccess) {
|
if (onSuccess) {
|
||||||
onSuccess();
|
onSuccess();
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(error);
|
console.error(error);
|
||||||
showToast("مشکلی پیش آمده است، ممکن است کاربر تکراری باشد!", "error");
|
showToast(
|
||||||
|
isEdit
|
||||||
|
? "مشکلی پیش آمده است!"
|
||||||
|
: "مشکلی پیش آمده است، ممکن است کاربر تکراری باشد!",
|
||||||
|
"error",
|
||||||
|
);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -198,9 +252,13 @@ export const SubmitNewUser: React.FC<SubmitNewUserProps> = ({ onSuccess }) => {
|
|||||||
type="submit"
|
type="submit"
|
||||||
variant="submit"
|
variant="submit"
|
||||||
fullWidth
|
fullWidth
|
||||||
disabled={isSubmitting || submitUserMutation.isPending}
|
disabled={
|
||||||
|
isSubmitting ||
|
||||||
|
createUserMutation.isPending ||
|
||||||
|
updateUserMutation.isPending
|
||||||
|
}
|
||||||
>
|
>
|
||||||
ثبت کاربر
|
{isEdit ? "ذخیره تغییرات" : "ثبت کاربر"}
|
||||||
</Button>
|
</Button>
|
||||||
</Grid>
|
</Grid>
|
||||||
</form>
|
</form>
|
||||||
|
|||||||
@@ -1,6 +1,9 @@
|
|||||||
export function getFaPermissions(permission: string) {
|
export function getFaPermissions(permission: string) {
|
||||||
let faPermission = "";
|
let faPermission = "";
|
||||||
switch (permission) {
|
switch (permission) {
|
||||||
|
case "admin":
|
||||||
|
faPermission = "ادمین";
|
||||||
|
break;
|
||||||
case "users":
|
case "users":
|
||||||
faPermission = "کاربران";
|
faPermission = "کاربران";
|
||||||
break;
|
break;
|
||||||
@@ -28,9 +31,7 @@ export function getFaPermissions(permission: string) {
|
|||||||
case "submit":
|
case "submit":
|
||||||
faPermission = "ثبت بازرسی";
|
faPermission = "ثبت بازرسی";
|
||||||
break;
|
break;
|
||||||
case "admin":
|
|
||||||
faPermission = "مدیر";
|
|
||||||
break;
|
|
||||||
default:
|
default:
|
||||||
faPermission = permission;
|
faPermission = permission;
|
||||||
break;
|
break;
|
||||||
|
|||||||
Reference in New Issue
Block a user