1- import React , { useState } from "react" ;
1+ import React from "react" ;
22import { useQuery } from "@apollo/client" ;
33import { Select } from "hds-react" ;
44import 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" ;
117import { SPACE_HIERARCHY_QUERY } from "./queries" ;
128import { spacesAsHierarchy } from "./util" ;
139
1410type 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
2622type 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-
4730const 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 }
0 commit comments