From 787b0ef4ac6116c1c661c92419c03100046e0e27 Mon Sep 17 00:00:00 2001 From: m5r Date: Sun, 22 May 2022 12:39:54 +0200 Subject: [PATCH] simplify calls loader --- app/features/phone-calls/loaders/calls.ts | 33 +++++++++++++++-------- 1 file changed, 22 insertions(+), 11 deletions(-) diff --git a/app/features/phone-calls/loaders/calls.ts b/app/features/phone-calls/loaders/calls.ts index 6899cea..0cf1f6e 100644 --- a/app/features/phone-calls/loaders/calls.ts +++ b/app/features/phone-calls/loaders/calls.ts @@ -14,14 +14,27 @@ type PhoneCallMeta = { export type PhoneCallsLoaderData = { hasOngoingSubscription: boolean; hasPhoneNumber: boolean; - isFetchingCalls: boolean | null; - phoneCalls: (PhoneCall & { toMeta: PhoneCallMeta; fromMeta: PhoneCallMeta })[] | undefined; -}; +} & ( + | { + phoneCalls: (PhoneCall & { toMeta: PhoneCallMeta; fromMeta: PhoneCallMeta })[]; + isFetchingCalls?: never; + } + | { + phoneCalls?: never; + isFetchingCalls: boolean; + } +); const loader: LoaderFunction = async ({ request }) => { const sessionData = await requireLoggedIn(request); + const hasOngoingSubscription = true; // TODO + const hasPhoneNumber = Boolean(sessionData.phoneNumber); if (!sessionData.phoneNumber) { - throw new Error("unreachable"); + return json({ + hasOngoingSubscription, + hasPhoneNumber: false, + isFetchingCalls: false, + }); } const phoneNumber = await db.phoneNumber.findUnique({ @@ -29,10 +42,9 @@ const loader: LoaderFunction = async ({ request }) => { }); if (!phoneNumber || phoneNumber.isFetchingCalls) { return json({ - hasOngoingSubscription: true, // TODO - hasPhoneNumber: Boolean(phoneNumber), - isFetchingCalls: true, - phoneCalls: undefined, + hasOngoingSubscription, + hasPhoneNumber, + isFetchingCalls: phoneNumber?.isFetchingCalls ?? false, }); } @@ -41,9 +53,8 @@ const loader: LoaderFunction = async ({ request }) => { orderBy: { createdAt: Prisma.SortOrder.desc }, }); return json({ - hasOngoingSubscription: true, // TODO - hasPhoneNumber: Boolean(phoneNumber), - isFetchingCalls: null, + hasOngoingSubscription, + hasPhoneNumber, phoneCalls: phoneCalls.map((phoneCall) => ({ ...phoneCall, fromMeta: getPhoneNumberMeta(phoneCall.from),