diff --git a/src/components/PillarSelectInput/index.tsx b/src/components/PillarSelectInput/index.tsx new file mode 100644 index 0000000..7033ee4 --- /dev/null +++ b/src/components/PillarSelectInput/index.tsx @@ -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{ + projectId: string; + name: T; + questionnaireId: string | null; + value: string | null | undefined; + error: string | undefined; + onChange: (value: string | undefined, name: T) => void; +} + +type Pillar = NonNullable['groups']['items'][number]; + +function PillarSelectInput(props: PillarProps) { + 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( + 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 ( + + ); +} + +export default PillarSelectInput; diff --git a/src/components/questionPreviews/SelectOneQuestionPreview/index.module.css b/src/components/questionPreviews/SelectOneQuestionPreview/index.module.css index 2508075..8a243df 100644 --- a/src/components/questionPreviews/SelectOneQuestionPreview/index.module.css +++ b/src/components/questionPreviews/SelectOneQuestionPreview/index.module.css @@ -6,6 +6,7 @@ .question-list { display: flex; + align-items: flex-start; flex-direction: column; gap: var(--dui-spacing-small); width: 10rem; diff --git a/src/views/QuestionnaireEdit/SelectMultipleQuestionsForm/index.tsx b/src/views/QuestionnaireEdit/SelectMultipleQuestionsForm/index.tsx index 44deb6e..b79747f 100644 --- a/src/views/QuestionnaireEdit/SelectMultipleQuestionsForm/index.tsx +++ b/src/views/QuestionnaireEdit/SelectMultipleQuestionsForm/index.tsx @@ -15,7 +15,6 @@ import { import { Button, SearchSelectInput, - SelectInput, TextInput, useAlert, } from '@the-deep/deep-ui'; @@ -31,13 +30,12 @@ import { CreateTextQuestionMutationVariables, ChoiceCollectionsQuery, ChoiceCollectionsQueryVariables, - PillarsQuery, - PillarsQueryVariables, QuestionCreateInput, QuestionTypeEnum, CreateMultipleSelectionQuestionMutation, } from '#generated/types'; import SelectMultipleQuestionPreview from '#components/SelectMultipleQuestionsPreview'; +import PillarSelectInput from '#components/PillarSelectInput'; import styles from './index.module.css'; @@ -59,26 +57,6 @@ const CREATE_MULTIPLE_SELECTION_QUESTION = gql` } `; -const PILLARS = gql` - query Pillars ( - $projectId: ID! - ) { - private { - projectScope(pk: $projectId) { - groups { - items { - id - name - label - parentId - questionnaireId - } - } - } - } - } -`; - const CHOICE_COLLECTIONS = gql` query ChoiceCollections( $projectId: ID!, @@ -91,7 +69,7 @@ const CHOICE_COLLECTIONS = gql` choiceCollections( filters: { questionnaire: {pk: $questionnaireId}, - name: {iContains: $search } + name: {iContains: $search } } ) { count @@ -141,7 +119,6 @@ const schema: FormSchema = { }), }; -type Pillar = NonNullable['groups']['items'][number]; type ChoiceCollection = NonNullable['choiceCollections']['items'][number]; const choiceCollectionKeySelector = (d: ChoiceCollection) => d.id; @@ -159,28 +136,6 @@ function SelectMultipleQuestionsForm(props: Props) { } = props; const alert = useAlert(); - - const pillarsVariables = useMemo(() => { - if (isNotDefined(projectId)) { - return undefined; - } - return ({ - projectId, - }); - }, [ - projectId, - ]); - - const { - data: pillarsResponse, - } = useQuery( - PILLARS, - { - skip: isNotDefined(pillarsVariables), - variables: pillarsVariables, - }, - ); - const [opened, setOpened] = useState(false); const [search, setSearch] = useState(); const [ @@ -213,11 +168,6 @@ function SelectMultipleQuestionsForm(props: Props) { variables: optionsVariables, }); - const pillarsOptions = pillarsResponse?.private?.projectScope?.groups.items || []; - - const pillarKeySelector = (data: Pillar) => data.id; - const pillarLabelSelector = (data: Pillar) => data.label; - const [ triggerQuestionCreate, { loading: createQuestionPending }, @@ -326,15 +276,13 @@ function SelectMultipleQuestionsForm(props: Props) { onShowDropdownChange={setOpened} value={formValue.choiceCollection} /> -