Skip to content

Commit

Permalink
fix(admin): user form not dynamically updating as expected (#7858)
Browse files Browse the repository at this point in the history
- Fixed the issue that a certain feature must be enabled when creating a user
- Fixed the issue that the modified content was not reset to the default content when exiting the user form without saving after modification
- Fixed the issue that the content did not switch as expected when switching user forms of different users

https://github.com/user-attachments/assets/02567021-9342-4ed1-be77-3bdcbb3d86ab
  • Loading branch information
JimmFly committed Aug 14, 2024
1 parent d5edada commit 994b539
Showing 1 changed file with 24 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,16 @@ function UserForm({
}: UserFormProps) {
const serverConfig = useServerConfig();

const [changes, setChanges] = useState<Partial<UserInput>>({
features: defaultValue?.features ?? [],
});
const defaultUser: Partial<UserInput> = useMemo(
() => ({
name: defaultValue?.name ?? '',
email: defaultValue?.email ?? '',
features: defaultValue?.features ?? [],
}),
[defaultValue?.email, defaultValue?.features, defaultValue?.name]
);

const [changes, setChanges] = useState<Partial<UserInput>>(defaultUser);

const setField = useCallback(
<K extends keyof UserInput>(
Expand Down Expand Up @@ -74,6 +81,15 @@ function UserForm({
[setField]
);

const handleClose = useCallback(() => {
setChanges(defaultUser);
onClose();
}, [defaultUser, onClose]);

useEffect(() => {
setChanges(defaultUser);
}, [defaultUser]);

return (
<div className="flex flex-col h-full gap-1">
<div className=" flex justify-between items-center py-[10px] px-6">
Expand All @@ -82,7 +98,7 @@ function UserForm({
size="icon"
className="w-7 h-7"
variant="ghost"
onClick={onClose}
onClick={handleClose}
>
<XIcon size={20} />
</Button>
Expand All @@ -104,14 +120,14 @@ function UserForm({
<InputItem
label="Name"
field="name"
value={changes.name ?? defaultValue?.name}
value={changes.name}
onChange={setField}
/>
<Separator />
<InputItem
label="Email"
field="email"
value={changes.email ?? defaultValue?.email}
value={changes.email}
onChange={setField}
/>
</div>
Expand All @@ -121,11 +137,7 @@ function UserForm({
<div key={feature}>
<ToggleItem
name={feature}
checked={(
changes.features ??
defaultValue?.features ??
[]
).includes(feature)}
checked={changes.features?.includes(feature) ?? false}
onChange={onFeatureChanged}
/>
{i < serverConfig.availableUserFeatures.length - 1 && (
Expand Down Expand Up @@ -188,7 +200,7 @@ function InputItem({
<Input
type="text"
className="py-2 px-3 text-base font-normal"
defaultValue={value}
value={value}
onChange={onValueChange}
/>
</div>
Expand Down

0 comments on commit 994b539

Please sign in to comment.