Skip to content

Commit

Permalink
Fix what I broke
Browse files Browse the repository at this point in the history
  • Loading branch information
anadis504 committed May 23, 2023
1 parent 0a3d7b8 commit 976f85d
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 78 deletions.
4 changes: 2 additions & 2 deletions src/components/Survey/Editors/SelectItemCondition.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ export const SelectCondition: React.FC<React.PropsWithChildren<SelectorProps>> =
) {
return
}
sItem.answerSpec.options.map((option) => {
sItem.answer.options.map((option) => {
const condition: SurveyItemCondition = {
questionLabel: sItem.question.questionLabel,
triggeringOption: option,
}
possibleItems.push(condition)
})
sItem.answerSpec.factorialOptions?.map((option) => {
sItem.answer.factorialOptions?.map((option) => {
const condition: SurveyItemCondition = {
questionLabel: sItem.question.questionLabel,
triggeringOption: option.name,
Expand Down
12 changes: 6 additions & 6 deletions src/components/Survey/Editors/SurveyEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import CheckBox from "../../../shared-module/components/InputFields/CheckBox"
import TextField from "../../../shared-module/components/InputFields/TextField"
import { baseTheme, primaryFont } from "../../../shared-module/styles"
import {
AnswerSpec,
Answer,
AnswerType,
SumFactor,
Survey,
Expand Down Expand Up @@ -91,7 +91,7 @@ const SurveyEditor: React.FC<React.PropsWithChildren<Props>> = ({ state, setStat
...quest,
id: v4(),
question: { question: "", questionLabel: "", id: v4() },
answer: { ...quest.answerSpec, id: v4() },
answer: { ...quest.answer, id: v4() },
}
const currentIndex = state.content.indexOf(quest)
const newContent = state.content
Expand Down Expand Up @@ -140,14 +140,14 @@ const SurveyEditor: React.FC<React.PropsWithChildren<Props>> = ({ state, setStat
if (typeof newState.content === "undefined") {
newState.content = []
}
const answerObject: AnswerSpec = {
const answerObject: Answer = {
type: AnswerType.None,
options: [],
}
newState.content.push({
id: v4(),
question: { id: v4(), question: "", questionLabel: "" },
answerSpec: answerObject,
answer: answerObject,
conditional: false,
})
setState({ view_type: "exercise-editor", private_spec: newState })
Expand All @@ -168,14 +168,14 @@ const SurveyEditor: React.FC<React.PropsWithChildren<Props>> = ({ state, setStat
if (!e) {
return
}
const answerObject: AnswerSpec = {
const answerObject: Answer = {
type: AnswerType.None,
options: [],
}
newContent.push({
id: v4(),
question: { id: v4(), questionLabel: e[0], question: e[1] },
answerSpec: answerObject,
answer: answerObject,
conditional: false,
})
})
Expand Down
86 changes: 43 additions & 43 deletions src/components/Survey/Editors/SurveyItemEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import CheckBox from "../../../shared-module/components/InputFields/CheckBox"
import TextArea from "../../../shared-module/components/InputFields/TextAreaField"
import { baseTheme, primaryFont } from "../../../shared-module/styles"
import { respondToOrLarger } from "../../../shared-module/styles/respond"
import { AnswerSpec, AnswerType, Survey, SurveyItem } from "../../../util/spec-types/privateSpec"
import { Answer, AnswerType, Survey, SurveyItem } from "../../../util/spec-types/privateSpec"
import {
insertVariablesToText,
parseLabelQuestion,
Expand Down Expand Up @@ -107,23 +107,23 @@ const SurveyItemEditor: React.FC<React.PropsWithChildren<Props>> = ({
},
}
if (newItem.question.questionLabel === "info") {
newItem.answerSpec.type = AnswerType.None
newItem.answerSpec.options = []
newItem.answer.type = AnswerType.None
newItem.answer.options = []
delete newItem.globalVariable
delete newItem.question.mandatory
}
if (newItem.question.questionLabel === "info-header") {
newItem.answerSpec.type = AnswerType.ConsentCheckbox
newItem.answerSpec.options = []
newItem.answer.type = AnswerType.ConsentCheckbox
newItem.answer.options = []
delete newItem.globalVariable
delete newItem.question.mandatory
}
if (
newItem.answerSpec.type === AnswerType.ConsentCheckbox &&
newItem.answer.type === AnswerType.ConsentCheckbox &&
newItem.question.questionLabel !== "info-header"
) {
newItem.answerSpec.type = AnswerType.None
newItem.answerSpec.options = []
newItem.answer.type = AnswerType.None
newItem.answer.options = []
}
onChangeSurveyItem(newItem)
}}
Expand All @@ -146,22 +146,22 @@ const SurveyItemEditor: React.FC<React.PropsWithChildren<Props>> = ({
/>
{item.question.questionLabel && item.question.questionLabel !== "info" && (
<select
aria-label={`select-answerSpec-type-${item.question.questionLabel}.`}
aria-label={`select-answer-type-${item.question.questionLabel}.`}
onChange={(event) => {
const answerSpec: AnswerSpec = item.answerSpec
const answer: Answer = item.answer
const answerType: AnswerType = event.target.value as unknown as AnswerType
if (!answerType) {
return
}
onChangeSurveyItem({
...item,
answerSpec: {
...answerSpec,
answer: {
...answer,
options:
answerType === AnswerType.Dropdown ||
answerType === AnswerType.MultiChoice ||
answerType === AnswerType.RadioGroup
? ([...answerSpec.options] as string[])
? ([...answer.options] as string[])
: [],
type: answerType,
},
Expand All @@ -176,7 +176,7 @@ const SurveyItemEditor: React.FC<React.PropsWithChildren<Props>> = ({
margin-top: 1rem;
margin-bottom: 1rem;
`}
value={item.answerSpec.type}
value={item.answer.type}
>
{Object.values(AnswerType).map((t) => {
if (t === AnswerType.None) {
Expand All @@ -202,12 +202,12 @@ const SurveyItemEditor: React.FC<React.PropsWithChildren<Props>> = ({
})}
</select>
)}
{(item.answerSpec?.type === AnswerType.MultiChoice ||
item.answerSpec?.type === AnswerType.RadioGroup ||
item.answerSpec?.type === AnswerType.Dropdown ||
item.answerSpec?.type === AnswerType.AdvancedDropdown) && (
{(item.answer?.type === AnswerType.MultiChoice ||
item.answer?.type === AnswerType.RadioGroup ||
item.answer?.type === AnswerType.Dropdown ||
item.answer?.type === AnswerType.AdvancedDropdown) && (
<div>
{item.answerSpec.options.length > 0 && (
{item.answer.options.length > 0 && (
<ol
className={css`
width: 100%;
Expand All @@ -219,7 +219,7 @@ const SurveyItemEditor: React.FC<React.PropsWithChildren<Props>> = ({
border-spacing: 0;
`}
>
{item.answerSpec.options.map((o, o_idx) => {
{item.answer.options.map((o, o_idx) => {
return (
<li key={o_idx}>
<StyledInnerEditor>
Expand All @@ -228,18 +228,18 @@ const SurveyItemEditor: React.FC<React.PropsWithChildren<Props>> = ({
value={o as string}
type="text"
onChange={(e) => {
const newAnswerSpec = item.answerSpec
newAnswerSpec.options[o_idx] = e.target.value
onChangeSurveyItem({ ...item, answerSpec: newAnswerSpec })
const newAnswer = item.answer
newAnswer.options[o_idx] = e.target.value
onChangeSurveyItem({ ...item, answer: newAnswer })
}}
/>
<DeleteButton
onClick={() => {
const newAnswer = {
...item.answerSpec,
options: (item.answerSpec.options as string[]).filter((e) => o !== e),
...item.answer,
options: (item.answer.options as string[]).filter((e) => o !== e),
}
onChangeSurveyItem({ ...item, answerSpec: newAnswer })
onChangeSurveyItem({ ...item, answer: newAnswer })
}}
>
{"x"}
Expand All @@ -253,8 +253,8 @@ const SurveyItemEditor: React.FC<React.PropsWithChildren<Props>> = ({
<button
onClick={() => {
const newItem = item as SurveyItem
newItem.answerSpec.options.push("")
onChangeSurveyItem({ ...item, answerSpec: newItem.answerSpec })
newItem.answer.options.push("")
onChangeSurveyItem({ ...item, answer: newItem.answer })
}}
className={css`
flex: 1;
Expand All @@ -267,9 +267,9 @@ const SurveyItemEditor: React.FC<React.PropsWithChildren<Props>> = ({
<CsvReader
parseNoHeaders={(value) => {
const newOptions = value.flat().filter((n) => n === 0 || n)
const newAnswerSpec = item.answerSpec
newAnswerSpec.options = newOptions
onChangeSurveyItem({ ...item, answerSpec: newAnswerSpec })
const newAnswer = item.answer
newAnswer.options = newOptions
onChangeSurveyItem({ ...item, answer: newAnswer })
}}
parseUsingHeaders={() => null}
title={"or upload a csv file"}
Expand All @@ -280,35 +280,35 @@ const SurveyItemEditor: React.FC<React.PropsWithChildren<Props>> = ({
/>
</div>
)}
{item.answerSpec.type === AnswerType.WeightedRadioGroup && (
{item.answer.type === AnswerType.WeightedRadioGroup && (
<>
<ol>
{item.answerSpec.factorialOptions?.map((fop, idx) => {
{item.answer.factorialOptions?.map((fop, idx) => {
return (
<li key={idx}>
<OptionEditor
questionLabel={item.question.questionLabel}
idx={idx + 1}
item={fop}
onDelete={() => {
const newOptions = item.answerSpec.factorialOptions?.filter(
const newOptions = item.answer.factorialOptions?.filter(
(e) => e.id !== fop.id,
)
onChangeSurveyItem({
...item,
answerSpec: { ...item.answerSpec, factorialOptions: newOptions },
answer: { ...item.answer, factorialOptions: newOptions },
})
}}
onChange={(op) => {
const newOptions = item.answerSpec.factorialOptions?.map((option) => {
const newOptions = item.answer.factorialOptions?.map((option) => {
if (option.id !== fop.id) {
return option
}
return op
})
onChangeSurveyItem({
...item,
answerSpec: { ...item.answerSpec, factorialOptions: newOptions },
answer: { ...item.answer, factorialOptions: newOptions },
})
}}
/>
Expand All @@ -319,10 +319,10 @@ const SurveyItemEditor: React.FC<React.PropsWithChildren<Props>> = ({
<button
onClick={() => {
const newItem: SurveyItem = { ...item }
if (typeof newItem.answerSpec.factorialOptions === "undefined") {
newItem.answerSpec.factorialOptions = []
if (typeof newItem.answer.factorialOptions === "undefined") {
newItem.answer.factorialOptions = []
}
newItem.answerSpec.factorialOptions.push({ name: "", value: 0, id: v4() })
newItem.answer.factorialOptions.push({ name: "", value: 0, id: v4() })
onChangeSurveyItem(newItem)
}}
className={css`
Expand All @@ -335,7 +335,7 @@ const SurveyItemEditor: React.FC<React.PropsWithChildren<Props>> = ({
</button>
</>
)}
{item.answerSpec?.type === AnswerType.ConsentCheckbox && (
{item.answer?.type === AnswerType.ConsentCheckbox && (
<TextArea
label="Checkbox text editor"
placeholder={"Consent message"}
Expand All @@ -349,9 +349,9 @@ const SurveyItemEditor: React.FC<React.PropsWithChildren<Props>> = ({
}
`}
onChange={(value) => {
onChangeSurveyItem({ ...item, answerSpec: { ...item.answerSpec, options: [value] } })
onChangeSurveyItem({ ...item, answer: { ...item.answer, options: [value] } })
}}
defaultValue={item.answerSpec.options[0]}
defaultValue={item.answer.options[0]}
/>
)}
<StyledInnerEditor respondTo>
Expand Down
16 changes: 8 additions & 8 deletions src/components/Survey/SurveyExerciseItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const SurveyExerciseitem: React.FC<React.PropsWithChildren<Props>> = ({
updateAnswer,
disabled,
}) => {
switch (item.answerSpec.type) {
switch (item.answer.type) {
case AnswerType.AdvancedDropdown: {
return (
<div
Expand All @@ -42,7 +42,7 @@ const SurveyExerciseitem: React.FC<React.PropsWithChildren<Props>> = ({
}}
chosenOption={(answer as string) ?? null}
disabled={disabled}
options={item.answerSpec.options}
options={item.answer.options}
/>
</div>
)
Expand Down Expand Up @@ -91,7 +91,7 @@ const SurveyExerciseitem: React.FC<React.PropsWithChildren<Props>> = ({
const selectedOptions: string[] = (answer as string[]) ?? []
return (
<div>
{item.answerSpec.options.map((o) => {
{item.answer.options.map((o) => {
return (
<CheckboxWrap key={o} checkedCollor={CHECKED} disabled={disabled}>
<input
Expand All @@ -118,7 +118,7 @@ const SurveyExerciseitem: React.FC<React.PropsWithChildren<Props>> = ({
return (
<form>
<div className="radio">
{item.answerSpec.options.map((option) => {
{item.answer.options.map((option) => {
return (
<RadioGroupWrap key={option} checkedColor={CHECKED} disabled={disabled}>
<input
Expand Down Expand Up @@ -173,7 +173,7 @@ const SurveyExerciseitem: React.FC<React.PropsWithChildren<Props>> = ({
disabled={disabled}
>
<option value="default" disabled label="--"></option>
{item.answerSpec.options.map((option) => {
{item.answer.options.map((option) => {
return (
<Option key={option} value={option}>
{option}
Expand All @@ -192,14 +192,14 @@ const SurveyExerciseitem: React.FC<React.PropsWithChildren<Props>> = ({
aria-label="consent-checkbox"
type="checkbox"
id={item.id}
name={item.answerSpec.options[0]}
name={item.answer.options[0]}
checked={(answer as string)?.length > 0}
onChange={(e) => {
updateAnswer(item.id, e.target.checked ? "checked" : "")
}}
disabled={disabled}
/>
<MarkdownText text={item.answerSpec.options.join("")} />
<MarkdownText text={item.answer.options.join("")} />
</CheckboxWrap>
</div>
)
Expand All @@ -208,7 +208,7 @@ const SurveyExerciseitem: React.FC<React.PropsWithChildren<Props>> = ({
return (
<form>
<div className="radio">
{item.answerSpec.factorialOptions?.map((option) => {
{item.answer.factorialOptions?.map((option) => {
return (
<RadioGroupWrap key={option.id} checkedColor={CHECKED} disabled={disabled}>
<input
Expand Down
4 changes: 2 additions & 2 deletions src/util/spec-types/privateSpec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ export interface SurveyItem {
/** What is asked from the student. */
question: Question
/** Specifies how this item can be answered. */
answerSpec: AnswerSpec
answer: Answer
/** Defines if question is rendered conditionally */
conditional: boolean
/** If this item is conditional, this one defines the rule for the condition. */
Expand All @@ -155,7 +155,7 @@ export interface SurveyItem {
}

/** Specifies how a non-factorial survey item can be answered. */
export interface AnswerSpec {
export interface Answer {
/** Specifies the kind of answer required. */
type: AnswerType
/** The list of predefined options student can choose from. Examples ["yes", "no", "maybe"] or ["dog", "cat", "horse"]. */
Expand Down
Loading

0 comments on commit 976f85d

Please sign in to comment.