Skip to content

Commit 87f6d8e

Browse files
authored
Fix initial values in edit form when using profiles on nodes (#7623)
* fix use effect for initial profiles * add changelog * update dep
1 parent fa1c1c2 commit 87f6d8e

File tree

2 files changed

+21
-10
lines changed

2 files changed

+21
-10
lines changed

changelog/+profiles.fixed.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix initial values in edit form when using profiles on nodes

frontend/app/src/shared/components/form/profiles-selector.tsx

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,6 @@ export const ProfilesSelector = ({
4242
}: ProfilesSelectorProps) => {
4343
const id = useId();
4444

45-
useEffect(() => {
46-
if (!value && defaultValue) {
47-
onChange(defaultValue);
48-
}
49-
}, [defaultValue]);
50-
5145
const genericSchemas = useAtomValue(genericSchemasAtom);
5246
const profileSchemas = useAtomValue(profileSchemasAtom);
5347

@@ -96,10 +90,6 @@ export const ProfilesSelector = ({
9690

9791
const { data, error, loading } = useQuery(query);
9892

99-
if (loading) return <LoadingIndicator className="p-4" />;
100-
101-
if (error) return <ErrorScreen message={error.message} />;
102-
10393
// Get all profiles name to retrieve the information from the result
10494
const profilesNameList: string[] = profilesList
10595
.map((profile) => profile?.name ?? "")
@@ -114,6 +104,26 @@ export const ProfilesSelector = ({
114104
[]
115105
);
116106

107+
useEffect(() => {
108+
if (!value && defaultValue && profiles.length && !loading) {
109+
const defaultProfiles = defaultValue
110+
.map((defaultProfile) => {
111+
return profiles.find((profile) => {
112+
return profile.id === defaultProfile.id;
113+
});
114+
})
115+
.filter((profile): profile is ProfileData => {
116+
return !!profile?.id;
117+
});
118+
119+
onChange(defaultProfiles);
120+
}
121+
}, [defaultValue, loading, profiles]);
122+
123+
if (loading) return <LoadingIndicator className="p-4" />;
124+
125+
if (error) return <ErrorScreen message={error.message} />;
126+
117127
if (!profiles || profiles.length === 0) return null;
118128

119129
const selectedValues = value ?? [];

0 commit comments

Comments
 (0)