Skip to content
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

Child allergy selector component #12

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
124 changes: 104 additions & 20 deletions src/app/page.tsx
Original file line number Diff line number Diff line change
@@ -1,36 +1,120 @@
'use client'
'use client';
import LanguagePicker from '@/components/LanguagePicker';
import Typography from '@mui/material/Typography';
import Box from '@mui/material/Box';
import { useState } from 'react';
import { LanguageCodes, PrimaryNeeds } from '@/const';
import Typography from '@mui/material/Typography';
import Box from '@mui/material/Box';
import {
LanguageCodes,
PrimaryNeeds,
foodAllergies,
foodRestrictions,
medicalConditions,
medicineAllergies,
phobias,
} from '@/const';
import PrimaryNeedsCheckboxes from '@/components/PrimaryNeedsCheckboxes';
import { useState } from 'react';
import AllergySelector from '@/components/AllergySelector';

export default function Home() {
const [primaryLanguage, setPrimaryLanguage] = useState<keyof typeof LanguageCodes | ''>('');
const [primaryLanguage, setPrimaryLanguage] = useState<
keyof typeof LanguageCodes | ''
>('');
const [secondaryLanguage, setSecondaryLanguage] = useState('');
const [primaryNeeds, setPrimaryNeeds] = useState<( keyof typeof PrimaryNeeds )[]>([]);
const handlePrimaryNeedsChanged = (primaryNeeds: (keyof typeof PrimaryNeeds)[]) => {
const [primaryNeeds, setPrimaryNeeds] = useState<
(keyof typeof PrimaryNeeds)[]
>([]);
const [childAllergies, setChildAllergies] = useState<{
foodAllergies: (keyof typeof foodAllergies)[];
foodRestrictions: (keyof typeof foodRestrictions)[];
medicineAllergies: (keyof typeof medicineAllergies)[];
medicalConditions: (keyof typeof medicalConditions)[];
phobias: (keyof typeof phobias)[];
}>({
foodAllergies: [],
foodRestrictions: [],
medicineAllergies: [],
medicalConditions: [],
phobias: [],
});

const handlePrimaryNeedsChanged = (
primaryNeeds: (keyof typeof PrimaryNeeds)[]
) => {
setPrimaryNeeds(primaryNeeds);
}
};

const handleAllergiesChanged = ({
selectedFoodAllergies,
selectedFoodRestrictions,
selectedMedicineAllergies,
selectedMedicalConditions,
selectedPhobias,
}: {
selectedFoodAllergies: (keyof typeof foodAllergies)[];
selectedFoodRestrictions: (keyof typeof foodRestrictions)[];
selectedMedicineAllergies: (keyof typeof medicineAllergies)[];
selectedMedicalConditions: (keyof typeof medicalConditions)[];
selectedPhobias: (keyof typeof phobias)[];
}) => {
setChildAllergies({
foodAllergies: selectedFoodAllergies,
foodRestrictions: selectedFoodRestrictions,
medicineAllergies: selectedMedicineAllergies,
medicalConditions: selectedMedicalConditions,
phobias: selectedPhobias,
});
};

return (
<main className="min-h-screen p-24">
<Box className="flex flex-row justify-between">
<Box sx={{width: '50%' }}>
<Typography variant="h6" gutterBottom>I speak...</Typography>
<LanguagePicker onChange={(val => setPrimaryLanguage(val || '') )} id="primary-language" label="Native Language" />
<Box sx={{ width: '50%' }}>
<Typography variant="h6" gutterBottom>
I speak...
</Typography>
<LanguagePicker
onChange={(val) => setPrimaryLanguage(val || '')}
id="primary-language"
label="Native Language"
/>
</Box>
<Box sx={{ width: '50%' }}>
<Typography variant="h6" gutterBottom>I am traveling somewhere that speaks...</Typography>
<LanguagePicker onChange={(val => setSecondaryLanguage(val || ''))} id="secondary-language" label="Foreign Language" />
<Typography variant="h6" gutterBottom>
I am traveling somewhere that speaks...
</Typography>
<LanguagePicker
onChange={(val) => setSecondaryLanguage(val || '')}
id="secondary-language"
label="Foreign Language"
/>
</Box>
</Box>
{primaryLanguage && secondaryLanguage && <Box className="flex flex-row justify-between">
<Box sx={{ width: '100%', mt: 3}} >
<Typography variant="h6" gutterBottom>I have the following needs I need to communicate:</Typography>
<PrimaryNeedsCheckboxes langCode={primaryLanguage} handlePrimaryNeedsChanged={handlePrimaryNeedsChanged} />
{primaryLanguage && secondaryLanguage && (
<Box className="flex flex-row justify-between">
<Box sx={{ width: '100%', mt: 3 }}>
<Typography variant="h6" gutterBottom>
I have the following needs I need to communicate:
</Typography>
<PrimaryNeedsCheckboxes
langCode={primaryLanguage}
handlePrimaryNeedsChanged={handlePrimaryNeedsChanged}
/>
</Box>
</Box>
)}

{primaryLanguage && secondaryLanguage && primaryNeeds && (
<Box className="flex flex-row justify-between">
<Box sx={{ width: '100%', mt: 3 }}>
<AllergySelector
onChange={handleAllergiesChanged}
id="child-allergy-selector"
langCode={primaryLanguage}
primaryNeeds={primaryNeeds}
/>
</Box>
</Box>
</Box>}
)}
</main>
)
);
}
168 changes: 168 additions & 0 deletions src/components/AllergySelector.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
import React, { useState } from 'react';
import Autocomplete from '@mui/material/Autocomplete';
import TextField from '@mui/material/TextField';
import Typography from '@mui/material/Typography';
import {
foodAllergies,
foodAllergiesByLanguage,
foodRestrictions,
foodRestrictionsByLanguage,
LanguageCodes,
medicalConditions,
medicalConditionsByLanguage,
medicineAllergies,
medicineAllergiesByLanguage,
phobias,
phobiasByLanguage,
PrimaryNeeds,
primaryNeedsByLanguage,
} from '@/const';

export type AllergySelectorProps = {
onChange: (selectedAllergies: {
selectedFoodAllergies: (keyof typeof foodAllergies)[];
selectedFoodRestrictions: (keyof typeof foodRestrictions)[];
selectedMedicineAllergies: (keyof typeof medicineAllergies)[];
selectedMedicalConditions: (keyof typeof medicalConditions)[];
selectedPhobias: (keyof typeof phobias)[];
}) => void;
id: string;
langCode: keyof typeof LanguageCodes;
primaryNeeds: (keyof typeof PrimaryNeeds)[];
};

export default function AllergySelector({
onChange,
id,
langCode,
primaryNeeds,
}: AllergySelectorProps) {
const [selectedFoodAllergies, setSelectedFoodAllergies] = useState<
{ code: string; label: string }[]
>([]);
const [selectedFoodRestrictions, setSelectedFoodRestrictions] = useState<
{ code: string; label: string }[]
>([]);
const [selectedMedicineAllergies, setSelectedMedicineAllergies] = useState<
{ code: string; label: string }[]
>([]);
const [selectedMedicalConditions, setSelectedMedicalConditions] = useState<
{ code: string; label: string }[]
>([]);
const [selectedPhobias, setSelectedPhobias] = useState<
{ code: string; label: string }[]
>([]);

const selectedAllergies = {
selectedFoodAllergies: selectedFoodAllergies.map(
(option) => option.code
) as (keyof typeof foodAllergies)[],
selectedFoodRestrictions: selectedFoodRestrictions.map(
(option) => option.code
) as (keyof typeof foodRestrictions)[],
selectedMedicineAllergies: selectedMedicineAllergies.map(
(option) => option.code
) as (keyof typeof medicineAllergies)[],
selectedMedicalConditions: selectedMedicalConditions.map(
(option) => option.code
) as (keyof typeof medicalConditions)[],
selectedPhobias: selectedPhobias.map(
(option) => option.code
) as (keyof typeof phobias)[],
};

const needsLabel = primaryNeedsByLanguage[langCode];

const getAutocompleteSelector = (
text: string,
allergyType: string,
label: string,
options: { [key: string]: string },
selectedValues: { code: string; label: string }[],
setSelectedValues: React.Dispatch<
React.SetStateAction<{ code: string; label: string }[]>
>
) => (
<div key={allergyType}>
<Typography variant="h6" gutterBottom>
{text}
</Typography>
<Autocomplete
multiple
id={`${id}-${allergyType}`}
options={Object.keys(options).map((code) => ({
label: options[code as keyof typeof options],
code,
}))}
getOptionLabel={(option) => option.label}
value={selectedValues}
onChange={(_, newValue) => {
setSelectedValues(newValue);
onChange(selectedAllergies);
}}
renderInput={(params) => (
<TextField
{...params}
variant="standard"
label={label}
placeholder="Type your needs"
/>
)}
/>
</div>
);

return (
<div>
{primaryNeeds.map((need) => (
<div key={need} style={{ marginBottom: '20px' }}>
{need === 'foodAllergies' &&
getAutocompleteSelector(
'I have the following ' + needsLabel.foodAllergies + ':',
need,
needsLabel.foodAllergies,
foodAllergiesByLanguage[langCode],
selectedFoodAllergies,
setSelectedFoodAllergies
)}
{need === 'foodRestrictions' &&
getAutocompleteSelector(
'I have the following ' + needsLabel.foodRestrictions + ':',
need,
needsLabel.foodRestrictions,
foodRestrictionsByLanguage[langCode],
selectedFoodRestrictions,
setSelectedFoodRestrictions
)}
{need === 'medicineAllergies' &&
getAutocompleteSelector(
'I have the following ' + needsLabel.medicineAllergies + ':',
need,
needsLabel.medicineAllergies,
medicineAllergiesByLanguage[langCode],
selectedMedicineAllergies,
setSelectedMedicineAllergies
)}
{need === 'medicalConditions' &&
getAutocompleteSelector(
'I have the following ' + needsLabel.medicalConditions + ':',
need,
needsLabel.medicalConditions,
medicalConditionsByLanguage[langCode],
selectedMedicalConditions,
setSelectedMedicalConditions
)}
{need === 'phobias' &&
getAutocompleteSelector(
'I have the following ' + needsLabel.phobias + ':',
need,
needsLabel.phobias,
phobiasByLanguage[langCode],
selectedPhobias,
setSelectedPhobias
)}
</div>
))}
</div>
);
}
Loading