diff --git a/app/routes/account/manage.tsx b/app/routes/account/manage.tsx index 75f9439..9a35ffd 100644 --- a/app/routes/account/manage.tsx +++ b/app/routes/account/manage.tsx @@ -61,6 +61,13 @@ function validateTeamId(teamId: unknown) { } } +function validateIncome(income: unknown) { + if (typeof income !== "number") { + console.log(income, typeof income); + return "Income must be a positive number or zero"; + } +} + type ActionData = { formError?: string; success?: string; @@ -69,12 +76,14 @@ type ActionData = { teamId: string | undefined; password: string | undefined; confirmPassword: string | undefined; + avgIncome: string | undefined; }; fields?: { password?: string; confirmPassword?: string; teamId?: string; icon?: string; + avgIncome?: number; }; }; @@ -91,6 +100,7 @@ export const action: ActionFunction = async ({ request }) => { const confirmPassword = form.get("confirmPassword"); const icon = form.get("icon") ?? (user ? user.username[0] : undefined); const teamId = form.get("teamId"); + const avgIncome = form.get("avgIncome"); const fields = { icon: typeof icon === "string" ? icon : undefined, @@ -98,6 +108,12 @@ export const action: ActionFunction = async ({ request }) => { confirmPassword: typeof confirmPassword === "string" ? confirmPassword : undefined, teamId: typeof teamId === "string" ? teamId : undefined, + avgIncome: + typeof avgIncome === "number" + ? Math.round(avgIncome) + : typeof avgIncome === "string" + ? parseInt(avgIncome, 10) + : undefined, }; const fieldErrors = { password: validatePassword(password), @@ -107,14 +123,19 @@ export const action: ActionFunction = async ({ request }) => { ), icon: validateIcon(icon), teamId: validateTeamId(teamId), + avgIncome: validateIncome(fields.avgIncome), }; if (Object.values(fieldErrors).some(Boolean)) return badRequest({ fieldErrors, fields }); const nonEmptyFields = Object.entries(fields).reduce((acc, [key, value]) => { - if (typeof value === "string" && key !== "confirmPassword") + if ( + (typeof value === "string" && key !== "confirmPassword") || + (typeof value === "number" && key === "avgIncome") + ) return { ...acc, [key]: value ?? undefined }; - else return acc; + + return acc; }, {}); const userUpdated = await updateUser({ id: user.id, @@ -299,6 +320,48 @@ export default function AccountPreferencesRoute() { )} +
+ To have an equal split based on everyone's income, you can select + this option. If of two people, one earns 1.5 times as much as the + other, then his or her share in the common expenses will be 1.5 + times as much as the other's. +
+ +