separada la logica de publicar el post a la del manejo del front

This commit is contained in:
2025-12-08 13:37:23 -03:00
parent dc94fc309d
commit 9cc5831cfc
2 changed files with 36 additions and 29 deletions

View File

@@ -14,6 +14,7 @@
import { Tooltip } from './ui/tooltip';
import TooltipContent from './ui/tooltip/tooltip-content.svelte';
import TooltipTrigger from './ui/tooltip/tooltip-trigger.svelte';
import { publicarPost } from '@/hooks/publicarPost';
let mensaje = $state('');
@@ -22,35 +23,14 @@
async function handlePost(e: Event) {
e.preventDefault();
try {
const formData = new FormData();
formData.append('content', mensaje);
// formData.append('imageUrl', '');
// formData.append('parentPostId', '');
const req = fetch($apiBase + '/api/posts', {
method: 'POST',
//credentials: 'include',
headers: {
Authorization: `Bearer ${$sesionStore?.accessToken}`
},
body: formData
});
cargando = true;
const res = await req;
if (res.ok) {
mensaje = '';
const post = await res.json();
addPost(post);
return;
}
mostrarError = 'No se pudo crear el post.';
} catch {
mostrarError = 'Fallo al alcanzar el servidor';
} finally {
cargando = false;
}
cargando = true;
const formData = new FormData();
formData.append('content', mensaje);
// formData.append('imageUrl', '');
// formData.append('parentPostId', '');
mostrarError = await publicarPost(formData);
if (mostrarError == '') mensaje = '';
cargando = false;
}
function handleKeydown(e: KeyboardEvent) {

View 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';
}
}