mirror of
https://github.com/emailerfacu-spec/minix-front.git
synced 2026-04-17 15:47:31 -03:00
añadida pagina de posts
This commit is contained in:
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