Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(admin): user form not dynamically updating as expected #7858

Merged
merged 1 commit into from
Aug 14, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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}
forehalo marked this conversation as resolved.
Show resolved Hide resolved
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
Loading