mirror of
https://github.com/lukevella/rallly.git
synced 2025-07-25 12:17:26 +02:00
♻️ Refactor instance settings to use cache (#1785)
This commit is contained in:
parent
6bacbd5833
commit
7ab25c68de
6 changed files with 68 additions and 35 deletions
|
@ -1,20 +0,0 @@
|
||||||
"use server";
|
|
||||||
|
|
||||||
import { requireAdmin } from "@/auth/queries";
|
|
||||||
import type { InstanceSettings } from "@/features/instance-settings/schema";
|
|
||||||
import { prisma } from "@rallly/database";
|
|
||||||
|
|
||||||
export async function updateInstanceSettings({
|
|
||||||
disableUserRegistration,
|
|
||||||
}: InstanceSettings) {
|
|
||||||
await requireAdmin();
|
|
||||||
|
|
||||||
await prisma.instanceSettings.update({
|
|
||||||
where: {
|
|
||||||
id: 1,
|
|
||||||
},
|
|
||||||
data: {
|
|
||||||
disableUserRegistration,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
}
|
|
|
@ -8,6 +8,7 @@ import {
|
||||||
SettingsGroupTitle,
|
SettingsGroupTitle,
|
||||||
} from "@/components/settings-group";
|
} from "@/components/settings-group";
|
||||||
import { Trans } from "@/components/trans";
|
import { Trans } from "@/components/trans";
|
||||||
|
import { updateInstanceSettings } from "@/features/instance-settings/mutations";
|
||||||
import {
|
import {
|
||||||
type InstanceSettings,
|
type InstanceSettings,
|
||||||
instanceSettingsSchema,
|
instanceSettingsSchema,
|
||||||
|
@ -31,7 +32,6 @@ import {
|
||||||
import { useToast } from "@rallly/ui/hooks/use-toast";
|
import { useToast } from "@rallly/ui/hooks/use-toast";
|
||||||
import { Switch } from "@rallly/ui/switch";
|
import { Switch } from "@rallly/ui/switch";
|
||||||
import { useForm } from "react-hook-form";
|
import { useForm } from "react-hook-form";
|
||||||
import { updateInstanceSettings } from "./actions";
|
|
||||||
|
|
||||||
export function InstanceSettingsForm({
|
export function InstanceSettingsForm({
|
||||||
defaultValue,
|
defaultValue,
|
||||||
|
|
25
apps/web/src/features/instance-settings/client.tsx
Normal file
25
apps/web/src/features/instance-settings/client.tsx
Normal file
|
@ -0,0 +1,25 @@
|
||||||
|
"use client";
|
||||||
|
|
||||||
|
import React from "react";
|
||||||
|
import type { InstanceSettings } from "./schema";
|
||||||
|
|
||||||
|
const InstanceSettingsContext = React.createContext<InstanceSettings>({
|
||||||
|
disableUserRegistration: false,
|
||||||
|
});
|
||||||
|
|
||||||
|
export function InstanceSettingsProvider({
|
||||||
|
value,
|
||||||
|
children,
|
||||||
|
}: {
|
||||||
|
value: InstanceSettings;
|
||||||
|
children: React.ReactNode;
|
||||||
|
}) {
|
||||||
|
return (
|
||||||
|
<InstanceSettingsContext.Provider value={value}>
|
||||||
|
{children}
|
||||||
|
</InstanceSettingsContext.Provider>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
export const useInstanceSettings = () =>
|
||||||
|
React.useContext(InstanceSettingsContext);
|
1
apps/web/src/features/instance-settings/constants.ts
Normal file
1
apps/web/src/features/instance-settings/constants.ts
Normal file
|
@ -0,0 +1 @@
|
||||||
|
export const instanceSettingsTag = "instance-settings";
|
19
apps/web/src/features/instance-settings/mutations.ts
Normal file
19
apps/web/src/features/instance-settings/mutations.ts
Normal file
|
@ -0,0 +1,19 @@
|
||||||
|
"use server";
|
||||||
|
|
||||||
|
import { requireAdmin } from "@/auth/queries";
|
||||||
|
import { type InstanceSettings, prisma } from "@rallly/database";
|
||||||
|
import { revalidateTag } from "next/cache";
|
||||||
|
import { instanceSettingsTag } from "./constants";
|
||||||
|
|
||||||
|
export async function updateInstanceSettings(data: Partial<InstanceSettings>) {
|
||||||
|
await requireAdmin();
|
||||||
|
|
||||||
|
await prisma.instanceSettings.update({
|
||||||
|
where: {
|
||||||
|
id: 1,
|
||||||
|
},
|
||||||
|
data,
|
||||||
|
});
|
||||||
|
|
||||||
|
revalidateTag(instanceSettingsTag);
|
||||||
|
}
|
|
@ -1,18 +1,26 @@
|
||||||
"server-only";
|
"server-only";
|
||||||
import { prisma } from "@rallly/database";
|
import { prisma } from "@rallly/database";
|
||||||
import { cache } from "react";
|
import { unstable_cache } from "next/cache";
|
||||||
|
import { instanceSettingsTag } from "./constants";
|
||||||
|
|
||||||
export const getInstanceSettings = cache(async () => {
|
export const getInstanceSettings = unstable_cache(
|
||||||
const instanceSettings = await prisma.instanceSettings.findUnique({
|
async () => {
|
||||||
where: {
|
const instanceSettings = await prisma.instanceSettings.findUnique({
|
||||||
id: 1,
|
where: {
|
||||||
},
|
id: 1,
|
||||||
select: {
|
},
|
||||||
disableUserRegistration: true,
|
select: {
|
||||||
},
|
disableUserRegistration: true,
|
||||||
});
|
},
|
||||||
|
});
|
||||||
|
|
||||||
return {
|
return {
|
||||||
disableUserRegistration: instanceSettings?.disableUserRegistration ?? false,
|
disableUserRegistration:
|
||||||
};
|
instanceSettings?.disableUserRegistration ?? false,
|
||||||
});
|
};
|
||||||
|
},
|
||||||
|
[],
|
||||||
|
{
|
||||||
|
tags: [instanceSettingsTag],
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue