extract phone settings loader

This commit is contained in:
m5r 2022-05-22 13:09:42 +02:00
parent 1b9de34387
commit a94d3b5e05
3 changed files with 30 additions and 26 deletions

View File

@ -4,7 +4,7 @@ import Button from "../button";
import SettingsSection from "../settings-section"; import SettingsSection from "../settings-section";
import Alert from "~/features/core/components/alert"; import Alert from "~/features/core/components/alert";
import useSession from "~/features/core/hooks/use-session"; 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"; import type { SetPhoneNumberActionData } from "~/features/settings/actions/phone";
export default function PhoneNumberForm() { export default function PhoneNumberForm() {

View File

@ -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<PhoneNumber, "id" | "number" | "isCurrent">[];
};
const loader: LoaderFunction = async ({ request }) => {
const { organization, twilioAccount } = await requireLoggedIn(request);
if (!twilioAccount) {
logger.warn("Twilio account is not connected");
return json<PhoneSettingsLoaderData>({ 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<PhoneSettingsLoaderData>({ phoneNumbers });
};
export default loader;

View File

@ -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 settingsPhoneAction from "~/features/settings/actions/phone";
import settingsPhoneLoader from "~/features/settings/loaders/phone";
import TwilioConnect from "~/features/settings/components/phone/twilio-connect"; import TwilioConnect from "~/features/settings/components/phone/twilio-connect";
import PhoneNumberForm from "~/features/settings/components/phone/phone-number-form"; 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 = { export const loader = settingsPhoneLoader;
phoneNumbers: Pick<PhoneNumber, "id" | "number" | "isCurrent">[];
};
export const loader: LoaderFunction = async ({ request }) => {
const { organization, twilioAccount } = await requireLoggedIn(request);
if (!twilioAccount) {
logger.warn("Twilio account is not connected");
return json<PhoneSettingsLoaderData>({ 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<PhoneSettingsLoaderData>({ phoneNumbers });
};
export const action = settingsPhoneAction; export const action = settingsPhoneAction;