Files
repo-home/apps/frontend/actions/article.actions.ts
T

94 lines
2.2 KiB
TypeScript

'use server'
import { revalidatePath } from 'next/cache'
import { Article, ArticleCreate, ArticleUpsert } from "@/apps/frontend/types/catalog";
import { sites } from '../_config/sites';
export async function articleCreate(data: ArticleCreate) {
console.log(data);
const result = await fetch('http://192.168.50.205/api/articles', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(data),
})
const resultData = await result.json();
if (!result.ok) {
return {
success: false,
errors: resultData.message, // kommt aus NestJS
};
}
revalidatePath(sites.article.new.url)
}
export async function articleUpdate(id: string, data: ArticleUpsert): Promise<ActionResult> {
const result = await fetch(`http://192.168.50.205/api/articles/${id}`, {
method: 'PATCH',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(data),
})
const resultData = await result.json();
if (!result.ok) {
return {
success: false,
errors: resultData.message, // kommt aus NestJS
};
}
revalidatePath(sites.article.update.url + '/' + id);
return { success: true };
}
export async function articleFindAll(): Promise<Article[]> {
const result = await fetch(`http://192.168.50.205/api/articles`, {
method: 'GET',
headers: { 'Content-Type': 'application/json' }
})
const data = await result.json() as Article[];
return data;
}
export async function articleFindById(id: string): Promise<any> {
const result = await fetch(`http://192.168.50.205/api/articles/${id}`, {
method: 'GET',
headers: { 'Content-Type': 'application/json' }
})
const data = await result.json() as Article
console.log('getArticle', data)
return data;
}
export async function articleDelete(id: string,revalidateUrl?:string): Promise<ActionResult> {
const result = await fetch(`http://192.168.50.205/api/articles/${id}`, {
method: 'DELETE',
headers: { 'Content-Type': 'application/json' }
})
const resultData = await result.json();
if (!result.ok) {
return {
success: false,
errors: resultData.message, // kommt aus NestJS
};
}
if(revalidateUrl) revalidatePath(revalidateUrl);
return { success: true };
}