Skip to content

Commit

Permalink
refactor back to details array
Browse files Browse the repository at this point in the history
  • Loading branch information
SashankBalusu committed Dec 22, 2024
1 parent d6e07dd commit d78ac36
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 52 deletions.
58 changes: 17 additions & 41 deletions app/add-details/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,8 @@ export default function Home() {
router.push('/view-plants');
}
const [currentIndex, setCurrentIndex] = useState<number>(1);
const [details, setDetails] = useState<Record<string, Partial<UserPlant>>>(
plantsToAdd.reduce(
(acc, plant) => ({
...acc,
[plant.id]: { plant_id: plant.id, user_id: userId! },
}),
{},
),
const [details, setDetails] = useState<Partial<UserPlant>[]>(
plantsToAdd.map(plant => ({ plant_id: plant.id, user_id: userId! })),
);

const plantDictionary: Record<UUID, string> = {};
Expand All @@ -51,13 +45,9 @@ export default function Home() {
// Set curr date in details to default date if not on submission page

if (currentIndex <= plantsToAdd.length) {
const currentDetail = details[plantsToAdd[currentIndex - 1].id];
const currentDetail = details[currentIndex - 1];
if (!currentDetail || !currentDetail.date_added) {
updateInput(
'date_added',
getDefaultDate(),
plantsToAdd[currentIndex - 1].id,
);
updateInput('date_added', getDefaultDate(), currentIndex - 1);
}
}

Expand All @@ -74,22 +64,21 @@ export default function Home() {
// disable next if planting type not selected (undefined)
const disableNext =
currentIndex <= plantsToAdd.length &&
!details[plantsToAdd[currentIndex - 1].id].planting_type;
!details[currentIndex - 1].planting_type;

function updateInput(field: string, value: string, plant_id: UUID) {
setDetails(prevDetails => ({
...prevDetails,
[plant_id]: {
...prevDetails[plant_id],
[field]: value,
},
}));
function updateInput(field: string, value: string, index: number) {
const updatedDetails = [...details];
updatedDetails[index] = {
...updatedDetails[index],
[field]: value,
};
setDetails(updatedDetails);
}
const handleSubmit = async () => {
// TODO: elegantly handle not logged in case (e.g. when someonee clicks "Back")
// instead of doing userId!
try {
await insertUserPlants(userId!, Object.values(details));
await insertUserPlants(userId!, details);
router.push('/view-plants');
} catch (error) {
console.error('Error inserting user plants:', error);
Expand All @@ -109,26 +98,13 @@ export default function Home() {
</Flex>
<PlantDetails
plant={plantsToAdd[currentIndex - 1]}
date={
details[plantsToAdd[currentIndex - 1].id].date_added ??
getDefaultDate()
}
plantingType={
details[plantsToAdd[currentIndex - 1].id].planting_type ?? ''
}
date={details[currentIndex - 1].date_added ?? getDefaultDate()}
plantingType={details[currentIndex - 1].planting_type ?? ''}
onDateChange={date =>
updateInput(
'date_added',
date,
plantsToAdd[currentIndex - 1].id,
)
updateInput('date_added', date, currentIndex - 1)
}
onPlantingTypeChange={type =>
updateInput(
'planting_type',
type,
plantsToAdd[currentIndex - 1].id,
)
updateInput('planting_type', type, currentIndex - 1)
}
/>
</Flex>
Expand Down
20 changes: 9 additions & 11 deletions components/ReviewAddDetails/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ export default function ReviewAddDetails({
updateInput,
plantDictionary,
}: {
details: Record<string, Partial<UserPlant>>;
updateInput: (field: string, value: string, plant_id: UUID) => void;
details: Partial<UserPlant>[];
updateInput: (field: string, value: string, index: number) => void;
plantDictionary: Record<UUID, string>;
}) {
const plantingTypeOptions: DropdownOption<PlantingTypeEnum>[] = [
Expand All @@ -24,22 +24,20 @@ export default function ReviewAddDetails({

return (
<ReviewContainer>
{Object.entries(details).map(([plant_id, detail]) => (
<Flex $direction="column" $gap="8px" $mb="16px" key={plant_id}>
<P1 $color={COLORS.shrub}>{plantDictionary[plant_id as UUID]}</P1>
{details.map((detail, index) => (
<Flex $direction="column" $gap="8px" $mb="16px" key={detail.plant_id}>
<P1 $color={COLORS.shrub}>
{plantDictionary[detail.plant_id as UUID]}
</P1>
<CustomSelect
label="Planting Type"
value={detail.planting_type as string}
options={plantingTypeOptions}
onChange={value =>
updateInput('planting_type', value, plant_id as UUID)
}
onChange={value => updateInput('planting_type', value, index)}
/>
<DateInput
value={detail.date_added as string}
onChange={value =>
updateInput('date_added', value, plant_id as UUID)
}
onChange={value => updateInput('date_added', value, index)}
/>
</Flex>
))}
Expand Down

0 comments on commit d78ac36

Please sign in to comment.