Skip to content

Commit

Permalink
Add rank question preview
Browse files Browse the repository at this point in the history
- Add edit functionality for SelectOne and SelectMultiple question types
  • Loading branch information
subinasr committed Aug 23, 2023
1 parent 4b98279 commit e572424
Show file tree
Hide file tree
Showing 29 changed files with 520 additions and 69 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
.preview {
display: flex;
flex-direction: column;
padding: var(--dui-spacing-extra-large) var(--dui-spacing-large);
gap: var(--dui-spacing-medium);
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
.preview {
display: flex;
flex-direction: column;
padding: var(--dui-spacing-extra-large) var(--dui-spacing-large);
gap: var(--dui-spacing-medium);

.upload-preview-wrapper {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
.preview {
display: flex;
flex-direction: column;
padding: var(--dui-spacing-extra-large) var(--dui-spacing-large);
gap: var(--dui-spacing-medium);

.upload-preview-wrapper {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
.preview {
display: flex;
flex-direction: column;
padding: var(--dui-spacing-extra-large) var(--dui-spacing-large);
gap: var(--dui-spacing-medium);
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
.preview {
display: flex;
flex-direction: column;
padding: var(--dui-spacing-extra-large) var(--dui-spacing-large);
gap: var(--dui-spacing-medium);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
.item-wrapper {
display: flex;
border: var(--dui-width-separator-thin) solid var(--dui-color-separator);
border-radius: var(--dui-border-radius-card);

.icon {
background-color: var(--color-gray2);
padding: var(--dui-spacing-small);
}
.item {
padding: var(--dui-spacing-small);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import {
GrDrag,
} from 'react-icons/gr';
import {
Element,
} from '@the-deep/deep-ui';

import styles from './index.module.css';

interface Props {
title: string;
}

function RankQuestionItem(props: Props) {
const {
title,
} = props;

return (
<Element
className={styles.itemWrapper}
icons={<GrDrag />}
iconsContainerClassName={styles.icon}
childrenContainerClassName={styles.item}
>
{title}
</Element>
);
}

export default RankQuestionItem;
Original file line number Diff line number Diff line change
@@ -1,6 +1,22 @@
.preview {
display: flex;
flex-direction: column;
padding: var(--dui-spacing-extra-large) var(--dui-spacing-large);
gap: var(--dui-spacing-medium);

.choices-preview {
align-items: flex-start;
gap: var(--dui-spacing-large);

.icon {
color: var(--dui-color-primary);
font-size: var(--dui-font-size-extra-large);
}

.choices {
display: flex;
flex-grow: 1;
flex-direction: column;
gap: var(--dui-spacing-small);
}
}
}
91 changes: 91 additions & 0 deletions src/components/questionPreviews/RankQuestionPreview/index.tsx
Original file line number Diff line number Diff line change
@@ -1,25 +1,100 @@
import { useMemo, useCallback } from 'react';
import {
IoSwapVertical,
} from 'react-icons/io5';
import {
_cs,
isNotDefined,
} from '@togglecorp/fujs';
import {
gql,
useQuery,
} from '@apollo/client';
import {
Element,
ListView,
TextOutput,
} from '@the-deep/deep-ui';

import {
RankChoicesQuery,
RankChoicesQueryVariables,
} from '#generated/types';

import RankQuestionItem from './RankQuestionItem';
import styles from './index.module.css';

const RANK_CHOICES = gql`
query RankChoices (
$projectId: ID!,
$choiceCollectionId: ID!,
) {
private {
projectScope(pk: $projectId) {
choiceCollection(pk: $choiceCollectionId) {
id
name
label
choices {
id
label
name
}
}
}
}
}
`;

type ChoiceType = NonNullable<NonNullable<NonNullable<NonNullable<RankChoicesQuery['private']>['projectScope']>['choiceCollection']>['choices']>[number];
const rankChoiceKeySelector = (c: ChoiceType) => c.id;

interface Props {
className?: string;
label?: string;
hint?: string | null;
choiceCollectionId: string | undefined | null;
projectId: string;
}

function RankQuestionPreview(props: Props) {
const {
className,
label,
hint,
choiceCollectionId,
projectId,
} = props;

const rankChoicesVariables = useMemo(() => {
if (isNotDefined(projectId) || isNotDefined(choiceCollectionId)) {
return undefined;
}
return ({
projectId,
choiceCollectionId,
});
}, [
projectId,
choiceCollectionId,
]);

const {
data: optionsListResponse,
} = useQuery<RankChoicesQuery, RankChoicesQueryVariables>(
RANK_CHOICES,
{
skip: isNotDefined(rankChoicesVariables),
variables: rankChoicesVariables,
},
);

const choices = optionsListResponse?.private?.projectScope?.choiceCollection?.choices ?? [];

const rankChoiceRendererParams = useCallback((_: string, datum: ChoiceType) => ({
title: datum.label,
}), []);

return (
<div className={_cs(styles.preview, className)}>
<TextOutput
Expand All @@ -28,6 +103,22 @@ function RankQuestionPreview(props: Props) {
spacing="none"
block
/>
<Element
className={styles.choicesPreview}
icons={<IoSwapVertical />}
iconsContainerClassName={styles.icon}
>
<ListView
className={styles.choices}
data={choices}
keySelector={rankChoiceKeySelector}
renderer={RankQuestionItem}
rendererParams={rankChoiceRendererParams}
filtered={false}
errored={false}
pending={false}
/>
</Element>
</div>
);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
.preview {
display: flex;
flex-direction: column;
padding: var(--dui-spacing-extra-large) var(--dui-spacing-large);
gap: var(--dui-spacing-medium);

.checkbox-list {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
.preview {
display: flex;
flex-direction: column;
padding: var(--dui-spacing-extra-large) var(--dui-spacing-large);
gap: var(--dui-spacing-medium);

.question-list {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
.preview {
display: flex;
flex-direction: column;
padding: var(--dui-spacing-extra-large) var(--dui-spacing-large);
gap: var(--dui-spacing-medium);
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
.preview {
display: flex;
flex-direction: column;
padding: var(--dui-spacing-extra-large) var(--dui-spacing-large);
gap: var(--dui-spacing-medium);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
.preview {
border: var(--dui-width-separator-thin) solid var(--dui-color-separator);
border-radius: var(--dui-border-radius-card);
padding: var(--dui-spacing-extra-large);
}
.edit-section {
display: flex;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
.preview {
border: var(--dui-width-separator-thin) solid var(--dui-color-separator);
border-radius: var(--dui-border-radius-card);
padding: var(--dui-spacing-extra-large);
}

.edit-section {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
.preview {
border: var(--dui-width-separator-thin) solid var(--dui-color-separator);
border-radius: var(--dui-border-radius-card);
padding: var(--dui-spacing-extra-large);
}

.edit-section {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
.preview {
border: var(--dui-width-separator-thin) solid var(--dui-color-separator);
border-radius: var(--dui-border-radius-card);
padding: var(--dui-spacing-extra-large);
}
.edit-section {
display: flex;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
.preview {
border: var(--dui-width-separator-thin) solid var(--dui-color-separator);
border-radius: var(--dui-border-radius-card);
padding: var(--dui-spacing-extra-large);
}

.edit-section {
Expand Down
7 changes: 5 additions & 2 deletions src/views/QuestionnaireEdit/QuestionPreview/index.module.css
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
.preview {
--input-width: 35rem;
border: none !important;
background-color: var(--dui-color-background);
overflow-y: auto;
gap: var(--dui-spacing-large);

.question-wrapper {
padding: var(--dui-spacing-medium);
align-items: flex-start;
padding: var(--dui-spacing-extra-large);
gap: var(--dui-spacing-extra-large);
}

.question-item {
flex-grow: 1;
padding: var(--dui-spacing-extra-large);
max-width: var(--input-width);
}
}
14 changes: 13 additions & 1 deletion src/views/QuestionnaireEdit/QuestionPreview/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ import {
import {
isNotDefined,
isDefined,
noOp,
} from '@togglecorp/fujs';
import {
TabPanel,
Element,
Checkbox,
QuickActionDropdownMenu,
DropdownMenuItem,
} from '@the-deep/deep-ui';
Expand Down Expand Up @@ -70,6 +72,14 @@ function QuestionPreview(props: QuestionProps) {
>
<Element
className={styles.questionWrapper}
icons={(
// TODO: Fix the selection behavior
<Checkbox
name={undefined}
value={false}
onChange={noOp}
/>
)}
actions={(
<QuickActionDropdownMenu
label={<IoEllipsisVertical />}
Expand Down Expand Up @@ -98,11 +108,13 @@ function QuestionPreview(props: QuestionProps) {
hint={question.hint}
/>
)}
{(question.type === 'RANK') && (
{(question.type === 'RANK') && isDefined(projectId) && (
<RankQuestionPreview
className={styles.questionItem}
label={question.label}
hint={question.hint}
projectId={projectId}
choiceCollectionId={question.choiceCollection?.id}
/>
)}
{(question.type === 'DATE') && (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
.preview {
border: var(--dui-width-separator-thin) solid var(--dui-color-separator);
border-radius: var(--dui-border-radius-card);
padding: var(--dui-spacing-extra-large);
}

.edit-section {
Expand Down
Loading

0 comments on commit e572424

Please sign in to comment.