implemented file upload for attachments
This commit is contained in:
@@ -0,0 +1,5 @@
|
||||
|
||||
export type ActionResult<T> = {
|
||||
error?:string;
|
||||
result?:T;
|
||||
}
|
||||
@@ -9,6 +9,7 @@ import { Attribute, Attributes } from "@sequelize/core";
|
||||
import { User } from "@/model/User";
|
||||
import { AuthProps } from "@/providers/providers";
|
||||
import { Auth } from "@/model/Auth";
|
||||
import { ActionResult } from "./ActionResult";
|
||||
|
||||
type LoginReturn = {
|
||||
cookie?:unknown,
|
||||
@@ -80,10 +81,10 @@ export async function serverValidateSessionCookie(koek:string):Promise<boolean>
|
||||
return false
|
||||
}
|
||||
|
||||
export async function userIsAdmin(user:Partial<Attributes<User>>):Promise<boolean>
|
||||
export async function userIsAdmin():Promise<boolean>
|
||||
{
|
||||
const cookieAuthValue = await cookies().get('auth')?.value;
|
||||
const cookieAuthSanitized = decodeURIComponent(cookieAuthValue ? cookieAuthValue: "");
|
||||
const cookieAuthSanitized = cookieAuthValue? JSON.parse(JSON.stringify(cookieAuthValue)) : "";
|
||||
|
||||
if(!cookieAuthSanitized) return false;
|
||||
const parsedAuth = JSON.parse(cookieAuthSanitized);
|
||||
@@ -107,8 +108,9 @@ export async function userIsAdmin(user:Partial<Attributes<User>>):Promise<boolea
|
||||
export async function getCookieAuth():Promise<AuthProps>
|
||||
{
|
||||
const cookieAuthValue = await cookies().get('auth')?.value;
|
||||
const cookieAuthSanitized = decodeURIComponent(cookieAuthValue ? cookieAuthValue: "");
|
||||
|
||||
const cookieAuthSanitized = cookieAuthValue? JSON.parse(JSON.stringify(cookieAuthValue)) : "";
|
||||
console.log("kanker koek")
|
||||
|
||||
if(!cookieAuthSanitized) return {}
|
||||
|
||||
const kd = JSON.parse(cookieAuthSanitized);
|
||||
@@ -126,4 +128,3 @@ export async function getCookieAuth():Promise<AuthProps>
|
||||
}
|
||||
return authObject;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
'use client'
|
||||
|
||||
import toast from "react-hot-toast";
|
||||
import { ActionResult } from "./ActionResult";
|
||||
|
||||
|
||||
export function handleActionResult<T>(actionResult:ActionResult<T>): T | undefined
|
||||
{
|
||||
if(actionResult.error) toast.error(actionResult.error,{position:"top-center",duration:10000})
|
||||
else return actionResult.result;
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
'use server';
|
||||
|
||||
import { revalidatePath, revalidateTag } from 'next/cache'
|
||||
import { Post } from "@/model/Post";
|
||||
import { Attributes, where } from "@sequelize/core";
|
||||
import { cookies } from 'next/headers';
|
||||
import { getCookieAuth, userIsAdmin } from '../actions';
|
||||
import { User } from '@/model/User';
|
||||
import { ActionResult } from '../ActionResult';
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
export async function deletePost(postID: number): Promise<ActionResult<boolean>> {
|
||||
// revalidatePath("/admin/man-post","page")
|
||||
if(! await userIsAdmin()) return {error:"Unauthorized, not deleting Post", result: false}
|
||||
const destroy = await Post.destroy({ where: { id: postID } });
|
||||
return {result: true};
|
||||
}
|
||||
|
||||
|
||||
export async function getPosts(): Promise<ActionResult<Attributes<Post>[]>> {
|
||||
if(! await userIsAdmin()) return {error:"Unauthorized, not fetching Posts."}
|
||||
const posts = await Post.findAll();
|
||||
return {result:JSON.parse(JSON.stringify(posts))};
|
||||
}
|
||||
|
||||
export async function updatePost(postAttributes: Partial<Attributes<Post>>): Promise<ActionResult<Attributes<Post>[]>> {
|
||||
if(! await userIsAdmin()) return {error:"Unauthorized, not updating Post."}
|
||||
const post = await Post.update(postAttributes, {where:{id:postAttributes.id}});
|
||||
return {result:JSON.parse(JSON.stringify(post))};
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
'use server';
|
||||
import { Project } from "@/model/Project";
|
||||
import { ActionResult } from "../ActionResult";
|
||||
import { Attributes } from "@sequelize/core";
|
||||
import { userIsAdmin } from "../actions";
|
||||
|
||||
export async function getProjects(): Promise<ActionResult<Attributes<Project>[]>> {
|
||||
if (! await userIsAdmin()) return { error: 'Unauthorized, not fetching Projects' }
|
||||
const posts = await Project.findAll();
|
||||
return { result: JSON.parse(JSON.stringify(posts)) };
|
||||
}
|
||||
@@ -1,24 +0,0 @@
|
||||
'use server';
|
||||
|
||||
import { revalidatePath, revalidateTag } from 'next/cache'
|
||||
import { Post } from "@/model/Post";
|
||||
import { Attributes, where } from "@sequelize/core";
|
||||
|
||||
|
||||
export async function deletePost(postID: number): Promise<boolean> {
|
||||
revalidatePath("/admin/man-post","page")
|
||||
const destroy = await Post.destroy({ where: { id: postID } });
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
export async function getPosts(): Promise<string> {
|
||||
const posts = await Post.findAll();
|
||||
return JSON.stringify(posts);
|
||||
}
|
||||
|
||||
export async function updatePost(postAttributes: Partial<Attributes<Post>>): Promise<string> {
|
||||
revalidatePath("/admin/man-post","page")
|
||||
const post = await Post.update(postAttributes, {where:{id:postAttributes.id}});
|
||||
return JSON.stringify(post);
|
||||
}
|
||||
@@ -1,7 +0,0 @@
|
||||
'use server';
|
||||
import { Project } from "@/model/Project";
|
||||
|
||||
export async function getProjects(): Promise<string> {
|
||||
const posts = await Project.findAll();
|
||||
return JSON.stringify(posts);
|
||||
}
|
||||
Reference in New Issue
Block a user