-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add pillar option to a new component
- Loading branch information
1 parent
a4d2838
commit f1b740f
Showing
4 changed files
with
111 additions
and
120 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
import { useMemo } from 'react'; | ||
import { gql, useQuery } from '@apollo/client'; | ||
import { SelectInput } from '@the-deep/deep-ui'; | ||
import { isNotDefined } from '@togglecorp/fujs'; | ||
|
||
import { PillarsQuery, PillarsQueryVariables } from '#generated/types'; | ||
|
||
const PILLARS = gql` | ||
query Pillars ( | ||
$projectId: ID!, | ||
$questionnaireId: ID!, | ||
) { | ||
private { | ||
projectScope(pk: $projectId) { | ||
groups (filters: {questionnaire: {pk: $questionnaireId}}){ | ||
items { | ||
id | ||
name | ||
label | ||
parentId | ||
questionnaireId | ||
} | ||
} | ||
} | ||
} | ||
} | ||
`; | ||
|
||
interface PillarProps<T>{ | ||
projectId: string; | ||
name: T; | ||
questionnaireId: string | null; | ||
value: string | null | undefined; | ||
error: string | undefined; | ||
onChange: (value: string | undefined, name: T) => void; | ||
} | ||
|
||
type Pillar = NonNullable<PillarsQuery['private']['projectScope']>['groups']['items'][number]; | ||
|
||
function PillarSelectInput<T extends string>(props: PillarProps<T>) { | ||
const { | ||
projectId, | ||
questionnaireId, | ||
value, | ||
error, | ||
onChange, | ||
name, | ||
} = props; | ||
|
||
const pillarsVariables = useMemo(() => { | ||
if (isNotDefined(projectId) || isNotDefined(questionnaireId)) { | ||
return undefined; | ||
} | ||
return ({ | ||
projectId, | ||
questionnaireId, | ||
}); | ||
}, [ | ||
projectId, | ||
questionnaireId, | ||
]); | ||
|
||
const { | ||
data: pillarsResponse, | ||
} = useQuery<PillarsQuery, PillarsQueryVariables>( | ||
PILLARS, | ||
{ | ||
skip: isNotDefined(pillarsVariables), | ||
variables: pillarsVariables, | ||
}, | ||
); | ||
|
||
const pillarsOptions = pillarsResponse?.private?.projectScope?.groups.items || []; | ||
|
||
const pillarKeySelector = (data: Pillar) => data.id; | ||
const pillarLabelSelector = (data: Pillar) => data.label; | ||
|
||
return ( | ||
<SelectInput | ||
name={name} | ||
label="Pillar and Sub pillar" | ||
value={value} | ||
error={error} | ||
onChange={onChange} | ||
keySelector={pillarKeySelector} | ||
labelSelector={pillarLabelSelector} | ||
options={pillarsOptions} | ||
/> | ||
); | ||
} | ||
|
||
export default PillarSelectInput; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters