From a94d3b5e053b2a5214f487326b1ffe14f7ddd70f Mon Sep 17 00:00:00 2001 From: m5r Date: Sun, 22 May 2022 13:09:42 +0200 Subject: [PATCH] extract phone settings loader --- .../components/phone/phone-number-form.tsx | 2 +- app/features/settings/loaders/phone.ts | 27 +++++++++++++++++++ app/routes/__app/settings/phone.tsx | 27 ++----------------- 3 files changed, 30 insertions(+), 26 deletions(-) create mode 100644 app/features/settings/loaders/phone.ts diff --git a/app/features/settings/components/phone/phone-number-form.tsx b/app/features/settings/components/phone/phone-number-form.tsx index cc72a7d..2adf4b4 100644 --- a/app/features/settings/components/phone/phone-number-form.tsx +++ b/app/features/settings/components/phone/phone-number-form.tsx @@ -4,7 +4,7 @@ import Button from "../button"; import SettingsSection from "../settings-section"; import Alert from "~/features/core/components/alert"; import useSession from "~/features/core/hooks/use-session"; -import type { PhoneSettingsLoaderData } from "~/routes/__app/settings/phone"; +import type { PhoneSettingsLoaderData } from "~/features/settings/loaders/phone"; import type { SetPhoneNumberActionData } from "~/features/settings/actions/phone"; export default function PhoneNumberForm() { diff --git a/app/features/settings/loaders/phone.ts b/app/features/settings/loaders/phone.ts new file mode 100644 index 0000000..1f63290 --- /dev/null +++ b/app/features/settings/loaders/phone.ts @@ -0,0 +1,27 @@ +import { type LoaderFunction, json } from "@remix-run/node"; +import { type PhoneNumber, Prisma } from "@prisma/client"; + +import db from "~/utils/db.server"; +import { requireLoggedIn } from "~/utils/auth.server"; +import logger from "~/utils/logger.server"; + +export type PhoneSettingsLoaderData = { + phoneNumbers: Pick[]; +}; + +const loader: LoaderFunction = async ({ request }) => { + const { organization, twilioAccount } = await requireLoggedIn(request); + if (!twilioAccount) { + logger.warn("Twilio account is not connected"); + return json({ phoneNumbers: [] }); + } + + const phoneNumbers = await db.phoneNumber.findMany({ + where: { organizationId: organization.id }, + select: { id: true, number: true, isCurrent: true }, + orderBy: { id: Prisma.SortOrder.desc }, + }); + + return json({ phoneNumbers }); +}; +export default loader; diff --git a/app/routes/__app/settings/phone.tsx b/app/routes/__app/settings/phone.tsx index e231120..67d737e 100644 --- a/app/routes/__app/settings/phone.tsx +++ b/app/routes/__app/settings/phone.tsx @@ -1,32 +1,9 @@ -import { type LoaderFunction, json } from "@remix-run/node"; -import { type PhoneNumber, Prisma } from "@prisma/client"; - -import { requireLoggedIn } from "~/utils/auth.server"; import settingsPhoneAction from "~/features/settings/actions/phone"; +import settingsPhoneLoader from "~/features/settings/loaders/phone"; import TwilioConnect from "~/features/settings/components/phone/twilio-connect"; import PhoneNumberForm from "~/features/settings/components/phone/phone-number-form"; -import logger from "~/utils/logger.server"; -import db from "~/utils/db.server"; -export type PhoneSettingsLoaderData = { - phoneNumbers: Pick[]; -}; - -export const loader: LoaderFunction = async ({ request }) => { - const { organization, twilioAccount } = await requireLoggedIn(request); - if (!twilioAccount) { - logger.warn("Twilio account is not connected"); - return json({ phoneNumbers: [] }); - } - - const phoneNumbers = await db.phoneNumber.findMany({ - where: { organizationId: organization.id }, - select: { id: true, number: true, isCurrent: true }, - orderBy: { id: Prisma.SortOrder.desc }, - }); - - return json({ phoneNumbers }); -}; +export const loader = settingsPhoneLoader; export const action = settingsPhoneAction;