Skip to content
This repository was archived by the owner on Feb 10, 2025. It is now read-only.

Commit ebe0a68

Browse files
authored
Merge pull request #917 from City-of-Helsinki/fix-admin-add-resources
fix: can't add new resources
2 parents e7ef161 + a8805c6 commit ebe0a68

5 files changed

Lines changed: 32 additions & 77 deletions

File tree

apps/admin-ui/src/component/Resources/resource-editor/NewResourceModal.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -174,10 +174,10 @@ const NewResourceModal = ({
174174
<ParentSelector
175175
label={t("ResourceModal.selectSpace")}
176176
onChange={(parent) => setValue("spacePk", parent)}
177-
unitPk={unit.pk as number}
178-
parentPk={null}
177+
unitPk={unit.pk ?? 0}
178+
value={state.resource.spacePk ?? null}
179179
placeholder={t("ResourceModal.selectSpace")}
180-
disableNull
180+
noParentless
181181
errorText={getValidationError("spacePk")}
182182
/>
183183

apps/admin-ui/src/component/Resources/resource-editor/ResourceEditor.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,8 +218,8 @@ const ResourceEditor = ({ resourcePk, unitPk }: Props) => {
218218
label={t("ResourceModal.selectSpace")}
219219
onChange={(parent) => setValue("spacePk", parent)}
220220
unitPk={unitPk}
221-
parentPk={state.resourceEdit.spacePk as number}
222-
disableNull
221+
value={state.resourceEdit.spacePk ?? null}
222+
noParentless
223223
/>
224224
{languages.map((lang) => {
225225
const fieldName = `name${upperFirst(lang)}`;

apps/admin-ui/src/component/Spaces/space-editor/ParentSelector.tsx

Lines changed: 24 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,98 +1,58 @@
1-
import React, { useState } from "react";
1+
import React from "react";
22
import { useQuery } from "@apollo/client";
33
import { Select } from "hds-react";
44
import i18next from "i18next";
5-
import {
6-
Maybe,
7-
Query,
8-
QueryUnitByPkArgs,
9-
SpaceType,
10-
} from "common/types/gql-types";
5+
import type { Query, QueryUnitByPkArgs } from "common/types/gql-types";
6+
import { filterNonNullable } from "common/src/helpers";
117
import { SPACE_HIERARCHY_QUERY } from "./queries";
128
import { spacesAsHierarchy } from "./util";
139

1410
type Props = {
1511
unitPk: number;
16-
spacePk?: number | null;
17-
parentPk: number | null;
12+
value: number | null;
1813
label: string;
1914
placeholder?: string;
2015
helperText?: string;
21-
disableNull?: boolean;
16+
noParentless?: boolean;
2217
errorText?: string;
18+
// TODO why is the label sent upstream?
2319
onChange: (val: number | null, name?: string) => void;
2420
};
2521

2622
type ParentType = { label: string; value: number | null };
2723

28-
const getChildrenFor = (spacePk: number, hierarchy: SpaceType[]) => {
29-
return hierarchy.filter((s) => s.parent?.pk === spacePk);
30-
};
31-
32-
const getChildrenRecursive = (spacePk: number, hierarchy: SpaceType[]) => {
33-
const newChildren = getChildrenFor(spacePk, hierarchy);
34-
return newChildren.concat(
35-
newChildren.flatMap((s) => getChildrenFor(s.pk as number, hierarchy))
36-
);
37-
};
38-
39-
const independentSpaceOption = {
24+
const parentLessOption = {
25+
// TODO don't use floating i18n.t (use the hook)
4026
label: i18next.t("SpaceEditor.noParent"),
4127
value: null,
4228
};
4329

44-
const getParent = (v: Maybe<number> | undefined, options: ParentType[]) =>
45-
options.find((po) => po.value === v) || options[0];
46-
4730
const ParentSelector = ({
4831
unitPk,
49-
spacePk,
5032
onChange,
51-
parentPk,
33+
value,
5234
label,
5335
placeholder,
54-
disableNull = false,
36+
noParentless = false,
5537
helperText,
5638
errorText,
5739
}: Props): JSX.Element | null => {
58-
const [parentOptions, setParentOptions] = useState([] as ParentType[]);
59-
60-
useQuery<Query, QueryUnitByPkArgs>(SPACE_HIERARCHY_QUERY, {
40+
const { data } = useQuery<Query, QueryUnitByPkArgs>(SPACE_HIERARCHY_QUERY, {
6141
variables: { pk: unitPk },
62-
onCompleted: ({ unitByPk }) => {
63-
const parentSpaces = unitByPk?.spaces?.map((s) => s as SpaceType);
64-
if (parentSpaces) {
65-
const unitSpaces = spacesAsHierarchy(parentSpaces, "\u2007");
66-
67-
const children = spacePk
68-
? getChildrenRecursive(spacePk, unitSpaces).map((s) => s.pk)
69-
: [];
70-
71-
const additionalOptions = unitSpaces
72-
.filter((space) => space.pk !== spacePk)
73-
.filter((space) => children.includes(space.pk))
74-
.map((space) => ({
75-
label: space.nameFi ?? "-",
76-
value: space.pk ?? null,
77-
}));
42+
skip: !unitPk,
43+
});
7844

79-
const options = [] as {
80-
label: string;
81-
value: number | null;
82-
}[];
45+
const parentSpaces = filterNonNullable(data?.unitByPk?.spaces);
46+
const unitSpaces = spacesAsHierarchy(parentSpaces, "\u2007");
8347

84-
if (!disableNull) {
85-
options.push(independentSpaceOption);
86-
}
48+
// NOTE there used to be children filtering, but it filtered out all possible options
8749

88-
setParentOptions(options.concat(additionalOptions));
89-
}
90-
},
91-
});
50+
const opts = unitSpaces.map((space) => ({
51+
label: space.nameFi ?? "-",
52+
value: space.pk ?? null,
53+
}));
9254

93-
if (parentOptions.length === 0) {
94-
return null;
95-
}
55+
const options = noParentless ? opts : [...opts, parentLessOption];
9656

9757
return (
9858
<Select
@@ -101,12 +61,9 @@ const ParentSelector = ({
10161
placeholder={placeholder}
10262
required
10363
helper={helperText}
104-
options={parentOptions}
105-
value={
106-
parentPk || !disableNull
107-
? getParent(parentPk, parentOptions)
108-
: undefined
109-
}
64+
options={options}
65+
disabled={options.length === 0}
66+
value={options.find((po) => po.value === value) ?? null}
11067
onChange={(selected: ParentType) =>
11168
onChange(selected.value, selected.label)
11269
}

apps/admin-ui/src/component/Spaces/space-editor/SpaceEditor.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -349,8 +349,7 @@ const SpaceEditor = ({ space, unit }: Props): JSX.Element | null => {
349349
helperText={t("SpaceModal.page1.parentHelperText")}
350350
label={t("SpaceModal.page1.parentLabel")}
351351
onChange={(parentPk) => setValue({ parentPk })}
352-
parentPk={state.spaceEdit?.parentPk as number}
353-
spacePk={space}
352+
value={state.spaceEdit?.parentPk ?? null}
354353
placeholder={t("SpaceModal.page1.parentPlaceholder")}
355354
unitPk={unit}
356355
/>

apps/admin-ui/src/component/Spaces/space-editor/new-space-modal/Page1.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,8 @@ const Page1 = ({
9696
<br />
9797
<ParentSelector
9898
label={t("SpaceModal.page1.parentLabel")}
99-
unitPk={unit.pk as number}
100-
spacePk={null}
101-
parentPk={editorState.parentPk || null}
99+
unitPk={unit.pk ?? 0}
100+
value={editorState.parentPk}
102101
onChange={(parentPk, parentName) =>
103102
dispatch({
104103
type: "setParent",

0 commit comments

Comments
 (0)