Skip to content

Commit dca747b

Browse files
authored
Avenmia/add workshop dropdown (#125)
* Adding dropdown option for the workshop * Adding second dropdown * Fixing blank answer issue and refactoring dropdown * Refactoring dropdown * Fixing dropdown enabled and disabled * Fixing dropdown styling * Updating delimiter * Removing extra console.log * Fixing enabled and disabled for multiselect andclearing text
1 parent ea74296 commit dca747b

File tree

5 files changed

+137
-10
lines changed

5 files changed

+137
-10
lines changed

prisma/migrations/20230312022837_initial_migration/migration.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ VALUES
221221
'16',
222222
'What is your workshop location? (Select one answer)',
223223
16,
224-
'option'
224+
'dropdown'
225225
);
226226

227227
INSERT INTO

src/components/survey/demographicssurvey.tsx

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,20 @@ export interface SurveyAnswer {
1616
}
1717

1818
export type QuestionDirection = "Prev" | "Next";
19-
export type QuestionType = "option" | "multiSelect" | "text" | "number";
19+
export type QuestionType =
20+
| "option"
21+
| "multiSelect"
22+
| "text"
23+
| "number"
24+
| "dropdown";
2025
export type AnswerType = "option" | "text" | "number" | "optionText";
2126
export interface DemographicSurveyInfo {
2227
questionNumber: number;
2328
totalQuestions: number;
2429
}
2530

31+
export const MULTI_ANSWER_DELIMITER = "---";
32+
2633
export default function DemographicsSurvey() {
2734
const [currentQuestion, setCurrentQuestion] = useState(0);
2835
const [surveyCompleted, setSurveyCompleted] = useState(false);
@@ -86,8 +93,8 @@ export default function DemographicsSurvey() {
8693
}
8794
const questionId = surveyData[currentQuestion]?.questionId;
8895
let answers = [answer];
89-
if (answer?.includes(";")) {
90-
answers = answer.split(";");
96+
if (answer?.includes(MULTI_ANSWER_DELIMITER)) {
97+
answers = answer.split(MULTI_ANSWER_DELIMITER).filter((a) => a !== "");
9198
}
9299

93100
// TODO: Fix these conditionals
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
import { useEffect, useMemo, useState } from "react";
2+
import type { SurveyAnswer } from "./demographicssurvey";
3+
4+
interface DropdownAnswersProps {
5+
answers: SurveyAnswer[];
6+
updateCurrentAnswer: (val: string) => void;
7+
setDisabled: (val: boolean) => void;
8+
}
9+
10+
export default function DropdownAnswers({
11+
answers,
12+
updateCurrentAnswer,
13+
setDisabled,
14+
}: DropdownAnswersProps) {
15+
const uniqueCounties = useMemo(() => {
16+
const counties = answers.map((a) => a.answer.split("-")[0]);
17+
return ["--", ...new Set(counties)] as string[];
18+
}, [answers]);
19+
20+
const [county, setCounty] = useState(uniqueCounties[0] ?? "");
21+
22+
const getFilteredWorkshopsByCounty = (val: string) => {
23+
const filteredWorkshops: string[] = answers
24+
.map((a) => a.answer)
25+
.filter((c) => c.includes(val));
26+
return [
27+
"--",
28+
...(filteredWorkshops.map((c) => c.split("-")[1]) as string[]),
29+
];
30+
};
31+
32+
const [countyWorkshops, setCountyWorkshops] = useState<string[]>(
33+
getFilteredWorkshopsByCounty(uniqueCounties[0] ?? "")
34+
);
35+
36+
useEffect(() => {
37+
if (county === "--") {
38+
setDisabled(true);
39+
}
40+
setCountyWorkshops(getFilteredWorkshopsByCounty(county));
41+
}, [county, answers]);
42+
43+
const handleChange = (val: string) => {
44+
updateCurrentAnswer(`${county}-${val}`);
45+
if (county !== "--" && val !== "--") {
46+
setDisabled(false);
47+
}
48+
if (county === "--" || val === "--") {
49+
setDisabled(true);
50+
}
51+
};
52+
53+
return (
54+
<>
55+
<div className="flex flex-col">
56+
<>
57+
<label htmlFor="county" className="text-med mb-5 mr-2">
58+
Select your county
59+
</label>
60+
<select
61+
id="county"
62+
className="form-select mb-5 w-64 rounded"
63+
onChange={(e) => setCounty((e.target as HTMLSelectElement).value)}
64+
>
65+
{uniqueCounties.map((c, index) => (
66+
<option key={index} value={c}>
67+
{c}
68+
</option>
69+
))}
70+
</select>
71+
</>
72+
{countyWorkshops.length > 0 && (
73+
<>
74+
<label htmlFor="workshop" className="mb-5 mr-2 text-base">
75+
Select your workshop
76+
</label>
77+
<select
78+
id="workshop"
79+
className="form-select mb-5 w-64 rounded"
80+
onChange={(e) =>
81+
handleChange((e.target as HTMLSelectElement).value)
82+
}
83+
>
84+
{countyWorkshops.map((w, index) => (
85+
<option key={index} value={w}>
86+
{w}
87+
</option>
88+
))}
89+
</select>
90+
</>
91+
)}
92+
</div>
93+
</>
94+
);
95+
}

src/components/survey/multiSelectAnswers.tsx

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import { useRef, useState } from "react";
2-
import type { SurveyAnswer } from "./demographicssurvey";
2+
import {
3+
MULTI_ANSWER_DELIMITER,
4+
type SurveyAnswer,
5+
} from "./demographicssurvey";
36

47
interface MultiSelectAnswersProps {
58
answers: SurveyAnswer[];
@@ -26,9 +29,13 @@ export default function MultiSelectAnswers({
2629
const checkedTextAnswers = Array.from(checkboxesText).filter(
2730
(cb) => (cb as HTMLInputElement).checked
2831
);
32+
const uncheckedTextAnswers = Array.from(checkboxesText).filter(
33+
(cb) => !(cb as HTMLInputElement).checked
34+
);
2935
const values = checkedAnswers
3036
.map((ca) => (ca as HTMLInputElement).value)
31-
.join(";");
37+
.join(MULTI_ANSWER_DELIMITER);
38+
3239
const textValues = checkedTextAnswers.map(
3340
(ca) =>
3441
(ca as HTMLInputElement).value +
@@ -38,13 +45,23 @@ export default function MultiSelectAnswers({
3845
(ca) => (ca as HTMLInputElement).value
3946
);
4047
setDisabledInput(answerValues);
41-
const textValuesConcat = textValues.join(";");
48+
const textValuesConcat = textValues.join(MULTI_ANSWER_DELIMITER);
4249
if (values.length > 0) {
43-
updateCurrentAnswer(values + ";" + textValuesConcat);
50+
updateCurrentAnswer(values + MULTI_ANSWER_DELIMITER + textValuesConcat);
4451
} else {
4552
updateCurrentAnswer(textValuesConcat);
4653
}
47-
54+
if (textValuesConcat.length === 0) {
55+
// clear text boxes
56+
uncheckedTextAnswers.forEach((ca) => {
57+
(ca.nextElementSibling?.children[0] as HTMLInputElement).value = "";
58+
return;
59+
});
60+
}
61+
if (values.length === 0 && textValuesConcat.length === 0) {
62+
setDisabled(true);
63+
return;
64+
}
4865
setDisabled(false);
4966
};
5067

@@ -64,7 +81,6 @@ export default function MultiSelectAnswers({
6481
</>
6582
);
6683
};
67-
console.log("Disabled inputs are:", disabledInput);
6884
const checkBoxText = (a: SurveyAnswer, index: number) => {
6985
return (
7086
<>

src/components/survey/surveyquestion.tsx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import type {
99
import RadioButtonAnswers from "./radioButtonAnswers";
1010
import MultiSelectAnswers from "./multiSelectAnswers";
1111
import TextAnswer from "./textAnswers";
12+
import DropdownAnswers from "./dropdownAnswers";
1213

1314
interface SurveyQuestionProps {
1415
surveyInfo: DemographicSurveyInfo;
@@ -57,6 +58,14 @@ export default function SurveyQuestion({
5758
number={questionType === "number"}
5859
/>
5960
);
61+
case "dropdown":
62+
return (
63+
<DropdownAnswers
64+
setDisabled={setDisabled}
65+
updateCurrentAnswer={setSelectedAnswer}
66+
answers={answers}
67+
/>
68+
);
6069
default:
6170
return null;
6271
}

0 commit comments

Comments
 (0)