mirror of
https://github.com/emailerfacu-spec/minix-front.git
synced 2026-04-16 15:37:32 -03:00
añadida pagina de posts
This commit is contained in:
@@ -30,13 +30,16 @@
|
||||
import DialogDescription from './ui/dialog/dialog-description.svelte';
|
||||
import { sesionStore } from '@/stores/usuario';
|
||||
import { likePost } from '@/hooks/likePost';
|
||||
import { goto } from '$app/navigation';
|
||||
import { resolve } from '$app/paths';
|
||||
|
||||
interface postProp {
|
||||
post: Post;
|
||||
postAModificar: Post | null;
|
||||
update?: Function;
|
||||
}
|
||||
|
||||
let { post, postAModificar = $bindable() }: postProp = $props();
|
||||
let { post, postAModificar = $bindable(), update }: postProp = $props();
|
||||
|
||||
let cargandoBorrar = $state(false);
|
||||
let mensajeError = $state('');
|
||||
@@ -82,7 +85,7 @@
|
||||
likePost(post),
|
||||
new Promise((resolve) => setTimeout(resolve, 300))
|
||||
]);
|
||||
console.log(1);
|
||||
// console.log(1);
|
||||
if (ok) {
|
||||
if (post.isLiked) {
|
||||
post.likesCount--;
|
||||
@@ -94,9 +97,10 @@
|
||||
errorLike = true;
|
||||
mensajeError = message;
|
||||
}
|
||||
console.log(1);
|
||||
updatePostStore(post.id, post);
|
||||
console.log(1);
|
||||
// console.log(2);
|
||||
if (update) update();
|
||||
else updatePostStore(post.id, post);
|
||||
// console.log(3);
|
||||
cargandoLike = false;
|
||||
}
|
||||
</script>
|
||||
@@ -179,7 +183,12 @@
|
||||
<ThumbsUp />
|
||||
{/if}
|
||||
</Button>
|
||||
<Button variant="ghost" class="flex items-center gap-2 rounded-full bg-accent p-3 text-lg">
|
||||
|
||||
<Button
|
||||
variant="ghost"
|
||||
class="flex items-center gap-2 rounded-full bg-accent p-3 text-lg"
|
||||
onclick={() => goto(resolve('/post/[idpost]', { idpost: post.id }))}
|
||||
>
|
||||
<p>
|
||||
{post.repliesCount}
|
||||
</p>
|
||||
|
||||
@@ -13,6 +13,9 @@
|
||||
import TooltipTrigger from './ui/tooltip/tooltip-trigger.svelte';
|
||||
import { publicarPost } from '@/hooks/publicarPost';
|
||||
import { filtrarImagen } from '@/utils';
|
||||
import { invalidate } from '$app/navigation';
|
||||
|
||||
let { placeholder, parentPostId }: { placeholder?: string; parentPostId?: string } = $props();
|
||||
|
||||
let mensaje = $state('');
|
||||
let imagen: File | null = $state(null);
|
||||
@@ -29,13 +32,14 @@
|
||||
if (imagen) {
|
||||
formData.append('image', imagen);
|
||||
}
|
||||
// formData.append('parentPostId', '');
|
||||
if (parentPostId) formData.append('parentPostId', parentPostId);
|
||||
mostrarError = await publicarPost(formData);
|
||||
if (mostrarError == '') {
|
||||
mensaje = '';
|
||||
imagen = null;
|
||||
}
|
||||
cargando = false;
|
||||
if (parentPostId) invalidate('post:respuestas');
|
||||
}
|
||||
|
||||
function handleKeydown(e: KeyboardEvent) {
|
||||
@@ -73,7 +77,7 @@
|
||||
}}
|
||||
ondrop={handleDrop}
|
||||
maxlength={280}
|
||||
placeholder="Alguna novedad?"
|
||||
placeholder={placeholder ? placeholder : 'Alguna novedad?'}
|
||||
onkeydown={handleKeydown}
|
||||
></InputGroupTextarea>
|
||||
|
||||
|
||||
30
src/lib/hooks/obtenerPostPorId.ts
Normal file
30
src/lib/hooks/obtenerPostPorId.ts
Normal file
@@ -0,0 +1,30 @@
|
||||
import { apiBase } from '@/stores/url';
|
||||
import { sesionStore } from '@/stores/usuario';
|
||||
import { get } from 'svelte/store';
|
||||
import type { Post } from '../../types';
|
||||
|
||||
export async function obtenerPostPorId(
|
||||
idpost: string,
|
||||
fetch2?: Function,
|
||||
depends?: Function
|
||||
): Promise<null | Post | string> {
|
||||
if (idpost == '') return null;
|
||||
if (depends) depends('post:post');
|
||||
const fetchFn = fetch2 ? fetch2 : fetch;
|
||||
|
||||
try {
|
||||
const req = await fetchFn(`${get(apiBase)}/api/posts/${idpost}`, {
|
||||
method: 'GET',
|
||||
headers: {
|
||||
Authorization: `Bearer ${get(sesionStore)?.accessToken}`
|
||||
}
|
||||
});
|
||||
let data = await req.json();
|
||||
if (req.ok) {
|
||||
return data;
|
||||
}
|
||||
return data.message;
|
||||
} catch {
|
||||
return 'No se pudo alcanzar el servidor.';
|
||||
}
|
||||
}
|
||||
28
src/lib/hooks/obtenerRespuestasPorId.ts
Normal file
28
src/lib/hooks/obtenerRespuestasPorId.ts
Normal file
@@ -0,0 +1,28 @@
|
||||
import { apiBase } from '@/stores/url';
|
||||
import { sesionStore } from '@/stores/usuario';
|
||||
import { get } from 'svelte/store';
|
||||
import type { Post } from '../../types';
|
||||
|
||||
export async function obtenerRespuestasPorId(
|
||||
id: string,
|
||||
fetch2?: Function,
|
||||
depends?: Function
|
||||
): Promise<string | Post[] | null> {
|
||||
if (depends) depends('post:respuestas');
|
||||
const fetchFn = fetch2 ? fetch2 : fetch;
|
||||
try {
|
||||
const req = await fetchFn(`${get(apiBase)}/api/posts/${id}/replies`, {
|
||||
method: 'GET',
|
||||
headers: {
|
||||
Authorization: `Bearer ${get(sesionStore)?.accessToken}`
|
||||
}
|
||||
});
|
||||
if (req.ok) {
|
||||
const data = await req.json();
|
||||
return data;
|
||||
}
|
||||
return null;
|
||||
} catch {
|
||||
return 'No se pudo obtener del dato del servidor';
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user