import type { LoaderFunction, ActionFunction } from "remix"; import { useLoaderData, useCatch, redirect, Link, Form } from "remix"; import type { Team, User } from "@prisma/client"; import Header from "~/components/Header"; import { db } from "~/utils/db.server"; import { requireUserId, getUser } from "~/utils/session.server"; type LoaderData = { user: User & { team: Team & { members: User[]; }; }; }; export const loader: LoaderFunction = async ({ request }) => { const user = await getUser(request); if (!user) { return redirect("/login"); } const data: LoaderData = { user }; return data; }; export const action: ActionFunction = async ({ request }) => { const form = await request.formData(); if (form.get("_method") === "patch") { const userId = await requireUserId(request); const user = await getUser(request); const balanceByIncomeField = form.get("balanceByIncome"); const balanceByIncome = typeof balanceByIncomeField === "boolean" ? balanceByIncomeField : typeof balanceByIncomeField === "string" ? Boolean(balanceByIncomeField) : false; const team = await db.team.findUnique({ where: { id: user?.teamId }, }); if (!team) { throw new Response("Can't update what does not exist", { status: 404 }); } if (user?.teamId !== team.id) { throw new Response("Pssh, nice try. That's not your expense", { status: 401, }); } await db.team.update({ where: { id: team.id }, data: { balanceByIncome } }); return redirect("/expenses"); } }; export default function JokesIndexRoute() { const data = useLoaderData(); const allUsersHaveSetAvgIncome = data.user.team.members?.every( (m) => m.avgIncome && m.avgIncome > 0 ); return ( <>

Team: {data.user.team.id}

{!data.user.team.members || data.user.team.members?.length === 0 ? (

No members

) : ( data.user.team.members.map((member) => (
{member.icon ?? member.username[0]}

{member.username}

)) )}

Balance based on income

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.

{!allUsersHaveSetAvgIncome ? (
Not all users from this team set their average income.
Check in the Account page
) : ( )}
); } export function CatchBoundary() { const caught = useCatch(); if (caught.status === 404) { return (
There are no users to display.
); } throw new Error(`Unexpected caught response with status: ${caught.status}`); } export function ErrorBoundary() { return
I did a whoopsies.
; }