refactor: remove ternary operators for better readability
Replace ternary operators with if-statements and helper functions in: - DisableUserButton component - user list disabled status column - EditUserPage disable item configuration
This commit is contained in:
1 parent
26c83d8853
commit
fbac0c8752
3 files changed
+53
-29
No files matched your search
+34
-17
@@ -16,22 +16,29 @@ export const DisableUserButton = ({ user }: DisableUserButtonProps) => {
|
|||||||
const t = useI18n();
|
const t = useI18n();
|
||||||
const [isPending, startTransition] = useTransition();
|
const [isPending, startTransition] = useTransition();
|
||||||
const changeDisabledMutation = clientApi.user.changeDisabled.useMutation();
|
const changeDisabledMutation = clientApi.user.changeDisabled.useMutation();
|
||||||
|
const isDisabled = user.disabled;
|
||||||
|
|
||||||
|
const getSuccessMessage = () => {
|
||||||
|
if (isDisabled) {
|
||||||
|
return {
|
||||||
|
title: t("user.action.enable.success", { defaultValue: "User enabled" }),
|
||||||
|
message: t("user.action.enable.successDescription", { defaultValue: "User has been enabled" }),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
return {
|
||||||
|
title: t("user.action.disable.success", { defaultValue: "User disabled" }),
|
||||||
|
message: t("user.action.disable.successDescription", { defaultValue: "User has been disabled" }),
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
const handleToggle = () => {
|
const handleToggle = () => {
|
||||||
startTransition(async () => {
|
startTransition(async () => {
|
||||||
try {
|
try {
|
||||||
await changeDisabledMutation.mutateAsync({
|
await changeDisabledMutation.mutateAsync({
|
||||||
userId: user.id,
|
userId: user.id,
|
||||||
disabled: !user.disabled,
|
disabled: !isDisabled,
|
||||||
});
|
|
||||||
showSuccessNotification({
|
|
||||||
title: user.disabled
|
|
||||||
? t("user.action.enable.success", { defaultValue: "User enabled" })
|
|
||||||
: t("user.action.disable.success", { defaultValue: "User disabled" }),
|
|
||||||
message: user.disabled
|
|
||||||
? t("user.action.enable.successDescription", { defaultValue: "User has been enabled" })
|
|
||||||
: t("user.action.disable.successDescription", { defaultValue: "User has been disabled" }),
|
|
||||||
});
|
});
|
||||||
|
showSuccessNotification(getSuccessMessage());
|
||||||
} catch {
|
} catch {
|
||||||
showErrorNotification({
|
showErrorNotification({
|
||||||
title: t("common.notification.update.error"),
|
title: t("common.notification.update.error"),
|
||||||
@@ -41,21 +48,31 @@ export const DisableUserButton = ({ user }: DisableUserButtonProps) => {
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
const tooltipLabel = user.disabled
|
const getTooltipLabel = () => {
|
||||||
? t("user.action.enable.description", { defaultValue: "Enable this user" })
|
if (isDisabled) {
|
||||||
: t("user.action.disable.description", { defaultValue: "Disable this user" });
|
return t("user.action.enable.description", { defaultValue: "Enable this user" });
|
||||||
|
}
|
||||||
|
return t("user.action.disable.description", { defaultValue: "Disable this user" });
|
||||||
|
};
|
||||||
|
|
||||||
|
const getButtonLabel = () => {
|
||||||
|
if (isDisabled) {
|
||||||
|
return t("user.action.enable.label", { defaultValue: "Enable user" });
|
||||||
|
}
|
||||||
|
return t("user.action.disable.label", { defaultValue: "Disable user" });
|
||||||
|
};
|
||||||
|
|
||||||
|
const getButtonColor = () => (isDisabled ? "green" : "yellow");
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Tooltip label={tooltipLabel} position="top">
|
<Tooltip label={getTooltipLabel()} position="top">
|
||||||
<Button
|
<Button
|
||||||
onClick={handleToggle}
|
onClick={handleToggle}
|
||||||
loading={isPending || changeDisabledMutation.isPending}
|
loading={isPending || changeDisabledMutation.isPending}
|
||||||
color={user.disabled ? "green" : "yellow"}
|
color={getButtonColor()}
|
||||||
variant="light"
|
variant="light"
|
||||||
>
|
>
|
||||||
{user.disabled
|
{getButtonLabel()}
|
||||||
? t("user.action.enable.label", { defaultValue: "Enable user" })
|
|
||||||
: t("user.action.disable.label", { defaultValue: "Disable user" })}
|
|
||||||
</Button>
|
</Button>
|
||||||
</Tooltip>
|
</Tooltip>
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -62,6 +62,19 @@ export default async function EditUserPage(props: Props) {
|
|||||||
const searchEngines = await api.searchEngine.getSelectable();
|
const searchEngines = await api.searchEngine.getSelectable();
|
||||||
const isSelf = session?.user.id === user.id;
|
const isSelf = session?.user.id === user.id;
|
||||||
const isCredentialsUser = user.provider === "credentials";
|
const isCredentialsUser = user.provider === "credentials";
|
||||||
|
const isDisabled = user.disabled;
|
||||||
|
|
||||||
|
const getDisableItemLabel = () => {
|
||||||
|
if (isDisabled) return t("user.action.enable.label");
|
||||||
|
return t("user.action.disable.label");
|
||||||
|
};
|
||||||
|
|
||||||
|
const getDisableItemDescription = () => {
|
||||||
|
if (isDisabled) {
|
||||||
|
return t("management.page.user.action.enable.description", { defaultValue: "Re-enable access for this user" });
|
||||||
|
}
|
||||||
|
return t("management.page.user.action.disable.description", { defaultValue: "Prevent this user from logging in" });
|
||||||
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Stack>
|
<Stack>
|
||||||
@@ -98,14 +111,8 @@ export default async function EditUserPage(props: Props) {
|
|||||||
|
|
||||||
<DangerZoneRoot>
|
<DangerZoneRoot>
|
||||||
<DangerZoneItem
|
<DangerZoneItem
|
||||||
label={user.disabled ? t("user.action.enable.label") : t("user.action.disable.label")}
|
label={getDisableItemLabel()}
|
||||||
description={
|
description={getDisableItemDescription()}
|
||||||
user.disabled
|
|
||||||
? t("management.page.user.action.enable.description", { defaultValue: "Re-enable access for this user" })
|
|
||||||
: t("management.page.user.action.disable.description", {
|
|
||||||
defaultValue: "Prevent this user from logging in",
|
|
||||||
})
|
|
||||||
}
|
|
||||||
action={<DisableUserButton user={user} />}
|
action={<DisableUserButton user={user} />}
|
||||||
/>
|
/>
|
||||||
<DangerZoneItem
|
<DangerZoneItem
|
||||||
|
|||||||
@@ -56,10 +56,10 @@ export const UserListComponent = ({ initialUserList }: UserListComponentProps) =
|
|||||||
accessorKey: "disabled",
|
accessorKey: "disabled",
|
||||||
header: t("user.field.status.label", { defaultValue: "Status" }),
|
header: t("user.field.status.label", { defaultValue: "Status" }),
|
||||||
size: 100,
|
size: 100,
|
||||||
Cell: ({ row }) =>
|
Cell: ({ row }) => {
|
||||||
row.original.disabled ? (
|
if (!row.original.disabled) return null;
|
||||||
<Badge color="red">{t("user.field.status.disabled", { defaultValue: "Disabled" })}</Badge>
|
return <Badge color="red">{t("user.field.status.disabled", { defaultValue: "Disabled" })}</Badge>;
|
||||||
) : null,
|
},
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
[t],
|
[t],
|
||||||
|
|||||||
Reference in new issue
Block a user