-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature/select single questions #24
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
673c41c
Add question type forms in a questionnaire
subinasr c1e0812
Add select single and multiple questions type
barshathakuri 892cf6c
Add pillar options
barshathakuri 5ef64eb
Add options to single and multi question type
barshathakuri 0be33d0
Resolved remaining conflicts
barshathakuri a4d2838
Fix lint error
barshathakuri f1b740f
Add pillar option to a new component
barshathakuri 5998ac3
Add a choice collection component and fix latest PR comment
barshathakuri eb4b143
Fix latest PR comment
barshathakuri 88b6ff6
Remove unused variables
barshathakuri c813c67
Fix latest PR comments
barshathakuri 7acd703
Fix lint error
barshathakuri aeb2a38
Fix lint error
barshathakuri File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Submodule backend
updated
7 files
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,120 @@ | ||
import { useMemo, useState } from 'react'; | ||
import { isNotDefined } from '@togglecorp/fujs'; | ||
import { gql, useQuery } from '@apollo/client'; | ||
import { SearchSelectInput } from '@the-deep/deep-ui'; | ||
|
||
import { | ||
ChoiceCollectionsQuery, | ||
ChoiceCollectionsQueryVariables, | ||
} from '#generated/types'; | ||
|
||
const CHOICE_COLLECTIONS = gql` | ||
query ChoiceCollections( | ||
$projectId: ID!, | ||
$questionnaireId: ID!, | ||
$search:String | ||
) { | ||
private { | ||
projectScope(pk: $projectId) { | ||
id | ||
choiceCollections( | ||
filters: { | ||
questionnaire: {pk: $questionnaireId}, | ||
label: {iContains: $search } | ||
} | ||
) { | ||
count | ||
items { | ||
id | ||
label | ||
name | ||
questionnaireId | ||
} | ||
} | ||
} | ||
} | ||
} | ||
`; | ||
|
||
type ChoiceCollection = NonNullable<ChoiceCollectionsQuery['private']['projectScope']>['choiceCollections']['items'][number]; | ||
|
||
const choiceCollectionKeySelector = (d: ChoiceCollection) => d.id; | ||
const choiceCollectionLabelSelector = (d: ChoiceCollection) => d.label; | ||
|
||
interface Props<T> { | ||
projectId: string; | ||
questionnaireId: string | null; | ||
name: T; | ||
label: string; | ||
onChange: (value: string | undefined, name: T) => void; | ||
value: string | null | undefined; | ||
error: string | undefined; | ||
} | ||
|
||
function ChoiceCollectionSelectInput<T extends string>(props: Props<T>) { | ||
const { | ||
projectId, | ||
questionnaireId, | ||
name, | ||
value, | ||
label, | ||
onChange, | ||
error, | ||
} = props; | ||
|
||
const [ | ||
choiceCollectionOptions, | ||
setChoiceCollectionOptions, | ||
] = useState<ChoiceCollection[] | undefined | null>(); | ||
|
||
const [search, setSearch] = useState<string>(); | ||
const [opened, setOpened] = useState(false); | ||
|
||
const optionsVariables = useMemo(() => { | ||
if (isNotDefined(projectId) || isNotDefined(questionnaireId) | ||
) { | ||
return undefined; | ||
} | ||
return ({ | ||
projectId, | ||
questionnaireId, | ||
search, | ||
}); | ||
}, [ | ||
projectId, | ||
questionnaireId, | ||
search, | ||
]); | ||
|
||
const { | ||
data: choiceCollectionsResponse, | ||
loading: choiceCollectionLoading, | ||
} = useQuery< | ||
ChoiceCollectionsQuery, | ||
ChoiceCollectionsQueryVariables | ||
>(CHOICE_COLLECTIONS, { | ||
skip: isNotDefined(optionsVariables) || !opened, | ||
variables: optionsVariables, | ||
}); | ||
const searchOption = choiceCollectionsResponse?.private.projectScope?.choiceCollections.items; | ||
|
||
return ( | ||
<SearchSelectInput | ||
name={name} | ||
error={error} | ||
disabled={choiceCollectionLoading} | ||
keySelector={choiceCollectionKeySelector} | ||
labelSelector={choiceCollectionLabelSelector} | ||
onChange={onChange} | ||
onSearchValueChange={setSearch} | ||
onOptionsChange={setChoiceCollectionOptions} | ||
label={label} | ||
searchOptions={searchOption} | ||
options={choiceCollectionOptions} | ||
onShowDropdownChange={setOpened} | ||
value={value} | ||
/> | ||
); | ||
} | ||
|
||
export default ChoiceCollectionSelectInput; |
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,94 @@ | ||
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 | ||
} | ||
} | ||
} | ||
} | ||
} | ||
`; | ||
|
||
type Pillar = NonNullable<PillarsQuery['private']['projectScope']>['groups']['items'][number]; | ||
|
||
const pillarKeySelector = (data: Pillar) => data.id; | ||
const pillarLabelSelector = (data: Pillar) => data.label; | ||
|
||
interface PillarProps<T>{ | ||
projectId: string; | ||
name: T; | ||
questionnaireId: string | null; | ||
value: string | null | undefined; | ||
error: string | undefined; | ||
onChange: (value: string | undefined, name: T) => void; | ||
} | ||
|
||
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, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's fetch |
||
loading: pillarsLoading, | ||
} = useQuery<PillarsQuery, PillarsQueryVariables>( | ||
PILLARS, | ||
{ | ||
skip: isNotDefined(pillarsVariables), | ||
variables: pillarsVariables, | ||
}, | ||
); | ||
|
||
const pillarsOptions = pillarsResponse?.private?.projectScope?.groups.items ?? []; | ||
|
||
return ( | ||
<SelectInput | ||
name={name} | ||
label="Pillar and Sub pillar" | ||
value={value} | ||
error={error} | ||
onChange={onChange} | ||
keySelector={pillarKeySelector} | ||
labelSelector={pillarLabelSelector} | ||
options={pillarsOptions} | ||
disabled={pillarsLoading} | ||
/> | ||
); | ||
} | ||
|
||
export default PillarSelectInput; |
13 changes: 13 additions & 0 deletions
13
src/components/questionPreviews/SelectMultipleQuestionPreview/index.module.css
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,13 @@ | ||
.preview { | ||
display: flex; | ||
flex-direction: column; | ||
padding: var(--dui-spacing-extra-large) var(--dui-spacing-large); | ||
gap: var(--dui-spacing-medium); | ||
|
||
.checkbox-list { | ||
display: flex; | ||
flex-direction: column; | ||
gap: var(--dui-spacing-small); | ||
width: 10rem; | ||
} | ||
} |
114 changes: 114 additions & 0 deletions
114
src/components/questionPreviews/SelectMultipleQuestionPreview/index.tsx
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,114 @@ | ||
import { useCallback, useMemo } from 'react'; | ||
import { gql, useQuery } from '@apollo/client'; | ||
import { | ||
_cs, | ||
isNotDefined, | ||
noOp, | ||
} from '@togglecorp/fujs'; | ||
import { | ||
Checkbox, | ||
ListView, | ||
TextOutput, | ||
} from '@the-deep/deep-ui'; | ||
|
||
import { MultipleOptionListQuery, MultipleOptionListQueryVariables } from '#generated/types'; | ||
|
||
import styles from './index.module.css'; | ||
|
||
const MULTIPLE_OPTION_LIST = gql` | ||
query MultipleOptionList( | ||
$projectId: ID!, | ||
$choiceCollectionId: ID!, | ||
) { | ||
private { | ||
projectScope(pk: $projectId) { | ||
choiceCollection(pk: $choiceCollectionId) { | ||
id | ||
label | ||
name | ||
choices { | ||
id | ||
label | ||
name | ||
} | ||
} | ||
} | ||
} | ||
} | ||
`; | ||
|
||
type CheckboxType = NonNullable<NonNullable<MultipleOptionListQuery['private']['projectScope']>['choiceCollection']>['choices'][number]; | ||
const checkboxKeySelector = (d: CheckboxType) => d.id; | ||
|
||
interface Props { | ||
className?: string; | ||
label?: string; | ||
hint?: string | null; | ||
choiceCollectionId: string | undefined | null; | ||
projectId: string; | ||
} | ||
|
||
function SelectMultipleQuestionPreview(props: Props) { | ||
const { | ||
className, | ||
label, | ||
hint, | ||
choiceCollectionId, | ||
projectId, | ||
} = props; | ||
|
||
const optionListVariables = useMemo(() => { | ||
if (isNotDefined(projectId) || isNotDefined(choiceCollectionId)) { | ||
return undefined; | ||
} | ||
return ({ | ||
projectId, | ||
choiceCollectionId, | ||
}); | ||
}, [ | ||
projectId, | ||
choiceCollectionId, | ||
]); | ||
|
||
const { | ||
data: optionsListResponse, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's fetch |
||
loading: OptionsListLoading, | ||
} = useQuery<MultipleOptionListQuery, MultipleOptionListQueryVariables>( | ||
MULTIPLE_OPTION_LIST, | ||
{ | ||
skip: isNotDefined(optionListVariables), | ||
variables: optionListVariables, | ||
}, | ||
); | ||
|
||
const checkboxListRendererParams = useCallback((_: string, datum: CheckboxType) => ({ | ||
label: datum?.label, | ||
name: 'choiceCollection', | ||
value: false, | ||
readOnly: true, | ||
onChange: noOp, | ||
}), []); | ||
|
||
return ( | ||
<div className={_cs(styles.preview, className)}> | ||
<TextOutput | ||
value={label ?? 'Title'} | ||
description={hint ?? 'Choose One'} | ||
spacing="none" | ||
block | ||
/> | ||
<ListView | ||
className={styles.checkboxList} | ||
data={optionsListResponse?.private?.projectScope?.choiceCollection?.choices} | ||
keySelector={checkboxKeySelector} | ||
renderer={Checkbox} | ||
rendererParams={checkboxListRendererParams} | ||
filtered={false} | ||
errored={false} | ||
pending={OptionsListLoading} | ||
/> | ||
</div> | ||
); | ||
} | ||
|
||
export default SelectMultipleQuestionPreview; |
14 changes: 14 additions & 0 deletions
14
src/components/questionPreviews/SelectOneQuestionPreview/index.module.css
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,14 @@ | ||
.preview { | ||
display: flex; | ||
flex-direction: column; | ||
padding: var(--dui-spacing-extra-large) var(--dui-spacing-large); | ||
gap: var(--dui-spacing-medium); | ||
|
||
.question-list { | ||
display: flex; | ||
align-items: flex-start; | ||
flex-direction: column; | ||
gap: var(--dui-spacing-small); | ||
width: 10rem; | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's fetch
loading
as well. We need to disable the SelectInput when the query is loading.