mirror of
https://github.com/emailerfacu-spec/minix-front.git
synced 2026-04-01 13:10:44 -03:00
separada la logica de publicar el post a la del manejo del front
This commit is contained in:
@@ -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) {
|
||||
|
||||
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