mirror of
https://github.com/emailerfacu-spec/minix-front.git
synced 2026-04-07 14:10:43 -03:00
separada la logica de publicar el post a la del manejo del front
This commit is contained in:
27
src/lib/hooks/publicarPost.ts
Normal file
27
src/lib/hooks/publicarPost.ts
Normal file
@@ -0,0 +1,27 @@
|
||||
import { addPost } from "@/stores/posts";
|
||||
import { apiBase } from "@/stores/url";
|
||||
import { sesionStore } from "@/stores/usuario";
|
||||
import { get } from "svelte/store";
|
||||
|
||||
export async function publicarPost(formData: FormData){
|
||||
try{
|
||||
const req = fetch(get(apiBase) + '/api/posts', {
|
||||
method: 'POST',
|
||||
//credentials: 'include',
|
||||
headers: {
|
||||
Authorization: `Bearer ${get(sesionStore)?.accessToken}`
|
||||
},
|
||||
body: formData
|
||||
});
|
||||
|
||||
const res = await req;
|
||||
if (res.ok) {
|
||||
const post = await res.json();
|
||||
addPost(post);
|
||||
return '';
|
||||
}
|
||||
return 'No se pudo crear el post.';
|
||||
} catch {
|
||||
return 'Fallo al alcanzar el servidor';
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user