simplify calls loader

This commit is contained in:
m5r 2022-05-22 12:39:54 +02:00
parent e8703a41f8
commit 787b0ef4ac

View File

@ -14,14 +14,27 @@ type PhoneCallMeta = {
export type PhoneCallsLoaderData = { export type PhoneCallsLoaderData = {
hasOngoingSubscription: boolean; hasOngoingSubscription: boolean;
hasPhoneNumber: 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 loader: LoaderFunction = async ({ request }) => {
const sessionData = await requireLoggedIn(request); const sessionData = await requireLoggedIn(request);
const hasOngoingSubscription = true; // TODO
const hasPhoneNumber = Boolean(sessionData.phoneNumber);
if (!sessionData.phoneNumber) { if (!sessionData.phoneNumber) {
throw new Error("unreachable"); return json<PhoneCallsLoaderData>({
hasOngoingSubscription,
hasPhoneNumber: false,
isFetchingCalls: false,
});
} }
const phoneNumber = await db.phoneNumber.findUnique({ const phoneNumber = await db.phoneNumber.findUnique({
@ -29,10 +42,9 @@ const loader: LoaderFunction = async ({ request }) => {
}); });
if (!phoneNumber || phoneNumber.isFetchingCalls) { if (!phoneNumber || phoneNumber.isFetchingCalls) {
return json<PhoneCallsLoaderData>({ return json<PhoneCallsLoaderData>({
hasOngoingSubscription: true, // TODO hasOngoingSubscription,
hasPhoneNumber: Boolean(phoneNumber), hasPhoneNumber,
isFetchingCalls: true, isFetchingCalls: phoneNumber?.isFetchingCalls ?? false,
phoneCalls: undefined,
}); });
} }
@ -41,9 +53,8 @@ const loader: LoaderFunction = async ({ request }) => {
orderBy: { createdAt: Prisma.SortOrder.desc }, orderBy: { createdAt: Prisma.SortOrder.desc },
}); });
return json<PhoneCallsLoaderData>({ return json<PhoneCallsLoaderData>({
hasOngoingSubscription: true, // TODO hasOngoingSubscription,
hasPhoneNumber: Boolean(phoneNumber), hasPhoneNumber,
isFetchingCalls: null,
phoneCalls: phoneCalls.map((phoneCall) => ({ phoneCalls: phoneCalls.map((phoneCall) => ({
...phoneCall, ...phoneCall,
fromMeta: getPhoneNumberMeta(phoneCall.from), fromMeta: getPhoneNumberMeta(phoneCall.from),