chore: add all the code from original repo
This commit is contained in:
@@ -0,0 +1,53 @@
|
||||
'use server';
|
||||
|
||||
import { initializeUser } from '../../../../../data/initializeUser';
|
||||
import { CreateProfileFormSchema } from '../../../../../data/types';
|
||||
import prisma from '../../../../../prisma/prisma';
|
||||
|
||||
interface CreateItemActionProps {
|
||||
clear: boolean;
|
||||
error?: string;
|
||||
}
|
||||
|
||||
export async function CreateProfileAction(
|
||||
_: CreateItemActionProps,
|
||||
formData: FormData
|
||||
) {
|
||||
const formDataObj = Object.fromEntries(formData.entries());
|
||||
|
||||
const validatedBody = CreateProfileFormSchema.safeParse(formDataObj);
|
||||
|
||||
if (!validatedBody.success) {
|
||||
throw new Error('Bad request');
|
||||
}
|
||||
|
||||
const user = await initializeUser();
|
||||
|
||||
try {
|
||||
const existingProfile = await prisma.profile.findFirst({
|
||||
where: {
|
||||
userId: user.id,
|
||||
name: validatedBody.data.name
|
||||
}
|
||||
});
|
||||
|
||||
if (existingProfile) {
|
||||
return { clear: false };
|
||||
}
|
||||
} catch (error) {
|
||||
throw new Error(`Failed to find profile`);
|
||||
}
|
||||
|
||||
try {
|
||||
await prisma.profile.create({
|
||||
data: {
|
||||
userId: user.id,
|
||||
name: validatedBody.data.name
|
||||
}
|
||||
});
|
||||
|
||||
return { clear: true };
|
||||
} catch (error) {
|
||||
throw new Error(`Failed to create profile`);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
'use server';
|
||||
|
||||
import { z } from 'zod';
|
||||
import prisma from '../../../../../prisma/prisma';
|
||||
|
||||
export async function DeleteProfileAction(id: string) {
|
||||
const validatedBody = z.string().safeParse(id);
|
||||
|
||||
if (!validatedBody.success) {
|
||||
throw new Error('Bad request');
|
||||
}
|
||||
|
||||
try {
|
||||
await prisma.profile.delete({
|
||||
where: {
|
||||
id
|
||||
}
|
||||
});
|
||||
} catch (error) {
|
||||
throw new Error(`Failed to delete profile`);
|
||||
}
|
||||
}
|
||||
23
app/components/Settings/Profiles/actions/ProfilesAction.ts
Normal file
23
app/components/Settings/Profiles/actions/ProfilesAction.ts
Normal file
@@ -0,0 +1,23 @@
|
||||
'use server';
|
||||
|
||||
import { initializeUser } from '../../../../../data/initializeUser';
|
||||
import prisma from '../../../../../prisma/prisma';
|
||||
|
||||
export async function ProfilesAction() {
|
||||
const user = await initializeUser();
|
||||
|
||||
try {
|
||||
const profiles = await prisma.profile.findMany({
|
||||
where: {
|
||||
userId: user.id
|
||||
},
|
||||
orderBy: {
|
||||
createdAt: 'desc'
|
||||
}
|
||||
});
|
||||
|
||||
return profiles;
|
||||
} catch (error) {
|
||||
throw new Error('Failed to find profiles');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
'use server';
|
||||
|
||||
import { z } from 'zod';
|
||||
import prisma from '../../../../../prisma/prisma';
|
||||
|
||||
interface ProfileActionProps {
|
||||
id: string;
|
||||
name: string;
|
||||
error?: string;
|
||||
}
|
||||
|
||||
export async function UpdateProfileAction({ id, name }: ProfileActionProps) {
|
||||
const validatedBody = z.string().safeParse(name);
|
||||
|
||||
if (!validatedBody.success) {
|
||||
throw new Error('Bad request');
|
||||
}
|
||||
|
||||
try {
|
||||
await prisma.profile.update({
|
||||
where: {
|
||||
id
|
||||
},
|
||||
data: {
|
||||
name: validatedBody.data
|
||||
}
|
||||
});
|
||||
} catch (error) {
|
||||
throw new Error(`Failed to update profile`);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user