mirror of
https://github.com/emailerfacu-spec/minix-front.git
synced 2026-04-01 13:10:44 -03:00
29 lines
824 B
TypeScript
29 lines
824 B
TypeScript
import { apiBase } from '@/stores/url';
|
|
import type { Post, PostResponseDto } from '../../types';
|
|
import { get } from 'svelte/store';
|
|
import { sesionStore } from '@/stores/usuario';
|
|
|
|
export async function updatePost(post: Post, callbackfn: Function, message: string) {
|
|
try {
|
|
const formData = new FormData();
|
|
formData.append('content', post.content);
|
|
formData.append('image', post.image || '');
|
|
|
|
const req = await fetch(get(apiBase) + `/api/posts/${post.id}`, {
|
|
method: 'PUT',
|
|
headers: {
|
|
Authorization: `Bearer ${get(sesionStore)?.accessToken}`
|
|
},
|
|
body: formData
|
|
});
|
|
if (req.ok) {
|
|
const newpost: PostResponseDto = await req.json();
|
|
callbackfn(newpost);
|
|
return;
|
|
}
|
|
message = 'Fallo al intentar modificar la publicación';
|
|
} catch {
|
|
message = 'No se pudo alcanzar el servidor';
|
|
}
|
|
}
|