diff --git a/src/lib/components/PostCard.svelte b/src/lib/components/PostCard.svelte index be5f55e..2704294 100644 --- a/src/lib/components/PostCard.svelte +++ b/src/lib/components/PostCard.svelte @@ -1,6 +1,8 @@ @@ -61,10 +84,10 @@ - - - {post.authorDisplayName[0].toUpperCase()} - + + + {post.authorDisplayName[0].toUpperCase()} + {post.authorDisplayName} @@ -74,7 +97,7 @@ {#if post.authorName === $sesionStore?.username} - + @@ -103,16 +126,31 @@ - - {post.content} + + {post.content} {#if post.imageUrl} {/if} - - {post.likesCount} likes - {post.repliesCount} replies + + likeHandler()} + > + + {post.likesCount} + + + + + + {post.repliesCount} + + + + {post.createdAt.replace('T', ' ').split('.')[0]} @@ -122,7 +160,7 @@ -{#if mensajeError} +{#if mensajeError || errorLike} diff --git a/src/lib/hooks/likePublicacion.ts b/src/lib/hooks/likePublicacion.ts new file mode 100644 index 0000000..9c27855 --- /dev/null +++ b/src/lib/hooks/likePublicacion.ts @@ -0,0 +1,21 @@ +import { apiBase } from '@/stores/url'; +import { get } from 'svelte/store'; +import { sesionStore } from '@/stores/usuario'; +import type { Post } from '../../types'; + +export async function likePublicacion(post: Post) { + try { + const req = await fetch(get(apiBase) + `/api/posts/${post.id}/like`, { + method: post.isLiked ? 'DELETE' : 'POST', + headers: { + 'Content-Type': 'application/json', + Authorization: `Bearer ${get(sesionStore)?.accessToken}` + } + }); + + const data: { message: string } = await req.json(); + return { message: data.message, ok: req.ok }; + } catch { + return { message: 'No se pudo alcanzar el servidor', ok: false }; + } +} diff --git a/src/types.d.ts b/src/types.d.ts index 0fed13d..0f6f5f7 100644 --- a/src/types.d.ts +++ b/src/types.d.ts @@ -15,6 +15,7 @@ export interface Post { isEdited: boolean; visibility: string; hashtags?: string[]; + isLiked: boolean; } export interface User {
{post.content}
+ {post.likesCount} +
+ {post.repliesCount} +