- added settings page and function
- add Role Ring for avatar and display role for user nav
This commit is contained in:
17
src/service/profile.api.ts
Normal file
17
src/service/profile.api.ts
Normal file
@@ -0,0 +1,17 @@
|
||||
import { authMiddleware } from '@/lib/middleware';
|
||||
import { saveFile } from '@/utils/disk-storage';
|
||||
import { createServerFn } from '@tanstack/react-start';
|
||||
import z from 'zod';
|
||||
|
||||
export const uploadProfileImage = createServerFn({ method: 'POST' })
|
||||
.middleware([authMiddleware])
|
||||
.inputValidator(z.instanceof(FormData))
|
||||
.handler(async ({ data: formData }) => {
|
||||
const uuid = crypto.randomUUID();
|
||||
const file = formData.get('file') as File;
|
||||
if (!(file instanceof File)) throw new Error('File not found');
|
||||
const imageKey = `${uuid}.${file.type.split('/')[1]}`;
|
||||
const buffer = Buffer.from(await file.arrayBuffer());
|
||||
await saveFile(imageKey, buffer);
|
||||
return { imageKey };
|
||||
});
|
||||
13
src/service/profile.schema.ts
Normal file
13
src/service/profile.schema.ts
Normal file
@@ -0,0 +1,13 @@
|
||||
import i18n from '@/lib/i18n';
|
||||
import z from 'zod';
|
||||
|
||||
export const profileUpdateSchema = z.object({
|
||||
name: z.string().nonempty(
|
||||
i18n.t('profile.messages.is_required', {
|
||||
field: i18n.t('profile.form.name'),
|
||||
}),
|
||||
),
|
||||
image: z.instanceof(File).optional(),
|
||||
});
|
||||
|
||||
export type ProfileInput = z.infer<typeof profileUpdateSchema>;
|
||||
25
src/service/queries.ts
Normal file
25
src/service/queries.ts
Normal file
@@ -0,0 +1,25 @@
|
||||
import { getSession } from '@/lib/auth/session';
|
||||
// import { sessionPush } from '@/lib/auth/session';
|
||||
import { queryOptions } from '@tanstack/react-query';
|
||||
import { getSettings } from './setting.api';
|
||||
|
||||
export const sessionQueries = {
|
||||
all: ['auth'],
|
||||
user: () =>
|
||||
queryOptions({
|
||||
queryKey: [...sessionQueries.all, 'session'],
|
||||
queryFn: () => getSession(),
|
||||
// queryFn: () => sessionPush(),
|
||||
staleTime: 1000 * 60 * 20,
|
||||
retry: false,
|
||||
}),
|
||||
};
|
||||
|
||||
export const settingQueries = {
|
||||
all: ['setting'],
|
||||
list: () =>
|
||||
queryOptions({
|
||||
queryKey: [...settingQueries.all, 'list'],
|
||||
queryFn: () => getSettings(),
|
||||
}),
|
||||
};
|
||||
47
src/service/setting.api.ts
Normal file
47
src/service/setting.api.ts
Normal file
@@ -0,0 +1,47 @@
|
||||
import { prisma } from '@/db';
|
||||
import { Setting } from '@/generated/prisma/client';
|
||||
import { authMiddleware } from '@/lib/middleware';
|
||||
import { createServerFn } from '@tanstack/react-start';
|
||||
import { settingSchema } from './setting.schema';
|
||||
// import { settingSchema } from './setting.schema';
|
||||
|
||||
export type SettingReturn = {
|
||||
[key: string]: Setting;
|
||||
};
|
||||
|
||||
export const getSettings = createServerFn({ method: 'GET' })
|
||||
.middleware([authMiddleware])
|
||||
.handler(async () => {
|
||||
const settings = await prisma.setting.findMany();
|
||||
|
||||
const results: SettingReturn = {};
|
||||
|
||||
settings.forEach((setting) => {
|
||||
results[setting.key] = setting;
|
||||
});
|
||||
|
||||
return results;
|
||||
});
|
||||
|
||||
export const updateSettings = createServerFn({ method: 'POST' })
|
||||
.inputValidator(settingSchema)
|
||||
.middleware([authMiddleware])
|
||||
.handler(async ({ data }) => {
|
||||
// Update each setting
|
||||
const updates = Object.entries(data).map(([key, value]) =>
|
||||
prisma.setting.upsert({
|
||||
where: { key },
|
||||
update: { value },
|
||||
create: {
|
||||
key,
|
||||
value,
|
||||
description: key, // or provide proper descriptions
|
||||
relation: 'admin',
|
||||
},
|
||||
}),
|
||||
);
|
||||
|
||||
await prisma.$transaction(updates);
|
||||
|
||||
return { success: true };
|
||||
});
|
||||
27
src/service/setting.schema.ts
Normal file
27
src/service/setting.schema.ts
Normal file
@@ -0,0 +1,27 @@
|
||||
import i18n from '@/lib/i18n';
|
||||
import z from 'zod';
|
||||
|
||||
export const settingSchema = z.object({
|
||||
site_name: z.string().nonempty(
|
||||
i18n.t('settings.messages.is_required', {
|
||||
field: i18n.t('settings.form.name'),
|
||||
}),
|
||||
),
|
||||
site_description: z.string().nonempty(
|
||||
i18n.t('settings.messages.is_required', {
|
||||
field: i18n.t('settings.form.description'),
|
||||
}),
|
||||
),
|
||||
site_keywords: z.string().nonempty(
|
||||
i18n.t('settings.messages.is_required', {
|
||||
field: i18n.t('settings.form.keywords'),
|
||||
}),
|
||||
),
|
||||
site_language: z.string().nonempty(
|
||||
i18n.t('settings.messages.is_required', {
|
||||
field: i18n.t('settings.form.language'),
|
||||
}),
|
||||
),
|
||||
});
|
||||
|
||||
export type SettingsInput = z.infer<typeof settingSchema>;
|
||||
Reference in New Issue
Block a user