Skip to content

Commit

Permalink
Avenmia/add workshop dropdown (#125)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
avenmia committed May 19, 2023
1 parent ea74296 commit dca747b
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ VALUES
'16',
'What is your workshop location? (Select one answer)',
16,
'option'
'dropdown'
);

INSERT INTO
Expand Down
13 changes: 10 additions & 3 deletions src/components/survey/demographicssurvey.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,20 @@ export interface SurveyAnswer {
}

export type QuestionDirection = "Prev" | "Next";
export type QuestionType = "option" | "multiSelect" | "text" | "number";
export type QuestionType =
| "option"
| "multiSelect"
| "text"
| "number"
| "dropdown";
export type AnswerType = "option" | "text" | "number" | "optionText";
export interface DemographicSurveyInfo {
questionNumber: number;
totalQuestions: number;
}

export const MULTI_ANSWER_DELIMITER = "---";

export default function DemographicsSurvey() {
const [currentQuestion, setCurrentQuestion] = useState(0);
const [surveyCompleted, setSurveyCompleted] = useState(false);
Expand Down Expand Up @@ -86,8 +93,8 @@ export default function DemographicsSurvey() {
}
const questionId = surveyData[currentQuestion]?.questionId;
let answers = [answer];
if (answer?.includes(";")) {
answers = answer.split(";");
if (answer?.includes(MULTI_ANSWER_DELIMITER)) {
answers = answer.split(MULTI_ANSWER_DELIMITER).filter((a) => a !== "");
}

// TODO: Fix these conditionals
Expand Down
95 changes: 95 additions & 0 deletions src/components/survey/dropdownAnswers.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import { useEffect, useMemo, useState } from "react";
import type { SurveyAnswer } from "./demographicssurvey";

interface DropdownAnswersProps {
answers: SurveyAnswer[];
updateCurrentAnswer: (val: string) => void;
setDisabled: (val: boolean) => void;
}

export default function DropdownAnswers({
answers,
updateCurrentAnswer,
setDisabled,
}: DropdownAnswersProps) {
const uniqueCounties = useMemo(() => {
const counties = answers.map((a) => a.answer.split("-")[0]);
return ["--", ...new Set(counties)] as string[];
}, [answers]);

const [county, setCounty] = useState(uniqueCounties[0] ?? "");

const getFilteredWorkshopsByCounty = (val: string) => {
const filteredWorkshops: string[] = answers
.map((a) => a.answer)
.filter((c) => c.includes(val));
return [
"--",
...(filteredWorkshops.map((c) => c.split("-")[1]) as string[]),
];
};

const [countyWorkshops, setCountyWorkshops] = useState<string[]>(
getFilteredWorkshopsByCounty(uniqueCounties[0] ?? "")
);

useEffect(() => {
if (county === "--") {
setDisabled(true);
}
setCountyWorkshops(getFilteredWorkshopsByCounty(county));
}, [county, answers]);

const handleChange = (val: string) => {
updateCurrentAnswer(`${county}-${val}`);
if (county !== "--" && val !== "--") {
setDisabled(false);
}
if (county === "--" || val === "--") {
setDisabled(true);
}
};

return (
<>
<div className="flex flex-col">
<>
<label htmlFor="county" className="text-med mb-5 mr-2">
Select your county
</label>
<select
id="county"
className="form-select mb-5 w-64 rounded"
onChange={(e) => setCounty((e.target as HTMLSelectElement).value)}
>
{uniqueCounties.map((c, index) => (
<option key={index} value={c}>
{c}
</option>
))}
</select>
</>
{countyWorkshops.length > 0 && (
<>
<label htmlFor="workshop" className="mb-5 mr-2 text-base">
Select your workshop
</label>
<select
id="workshop"
className="form-select mb-5 w-64 rounded"
onChange={(e) =>
handleChange((e.target as HTMLSelectElement).value)
}
>
{countyWorkshops.map((w, index) => (
<option key={index} value={w}>
{w}
</option>
))}
</select>
</>
)}
</div>
</>
);
}
28 changes: 22 additions & 6 deletions src/components/survey/multiSelectAnswers.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { useRef, useState } from "react";
import type { SurveyAnswer } from "./demographicssurvey";
import {
MULTI_ANSWER_DELIMITER,
type SurveyAnswer,
} from "./demographicssurvey";

interface MultiSelectAnswersProps {
answers: SurveyAnswer[];
Expand All @@ -26,9 +29,13 @@ export default function MultiSelectAnswers({
const checkedTextAnswers = Array.from(checkboxesText).filter(
(cb) => (cb as HTMLInputElement).checked
);
const uncheckedTextAnswers = Array.from(checkboxesText).filter(
(cb) => !(cb as HTMLInputElement).checked
);
const values = checkedAnswers
.map((ca) => (ca as HTMLInputElement).value)
.join(";");
.join(MULTI_ANSWER_DELIMITER);

const textValues = checkedTextAnswers.map(
(ca) =>
(ca as HTMLInputElement).value +
Expand All @@ -38,13 +45,23 @@ export default function MultiSelectAnswers({
(ca) => (ca as HTMLInputElement).value
);
setDisabledInput(answerValues);
const textValuesConcat = textValues.join(";");
const textValuesConcat = textValues.join(MULTI_ANSWER_DELIMITER);
if (values.length > 0) {
updateCurrentAnswer(values + ";" + textValuesConcat);
updateCurrentAnswer(values + MULTI_ANSWER_DELIMITER + textValuesConcat);
} else {
updateCurrentAnswer(textValuesConcat);
}

if (textValuesConcat.length === 0) {
// clear text boxes
uncheckedTextAnswers.forEach((ca) => {
(ca.nextElementSibling?.children[0] as HTMLInputElement).value = "";
return;
});
}
if (values.length === 0 && textValuesConcat.length === 0) {
setDisabled(true);
return;
}
setDisabled(false);
};

Expand All @@ -64,7 +81,6 @@ export default function MultiSelectAnswers({
</>
);
};
console.log("Disabled inputs are:", disabledInput);
const checkBoxText = (a: SurveyAnswer, index: number) => {
return (
<>
Expand Down
9 changes: 9 additions & 0 deletions src/components/survey/surveyquestion.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import type {
import RadioButtonAnswers from "./radioButtonAnswers";
import MultiSelectAnswers from "./multiSelectAnswers";
import TextAnswer from "./textAnswers";
import DropdownAnswers from "./dropdownAnswers";

interface SurveyQuestionProps {
surveyInfo: DemographicSurveyInfo;
Expand Down Expand Up @@ -57,6 +58,14 @@ export default function SurveyQuestion({
number={questionType === "number"}
/>
);
case "dropdown":
return (
<DropdownAnswers
setDisabled={setDisabled}
updateCurrentAnswer={setSelectedAnswer}
answers={answers}
/>
);
default:
return null;
}
Expand Down

1 comment on commit dca747b

@vercel
Copy link

@vercel vercel bot commented on dca747b May 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

hierr – ./

hierr-codeforhawaii.vercel.app
hierr.vercel.app
hierr-git-main-codeforhawaii.vercel.app

Please sign in to comment.