Files
Plataforma-de-Tutoriais/frontend-plataforma-tutoriais/src/hooks/useVideoWatch.ts
2026-05-28 14:41:32 +01:00

30 lines
919 B
TypeScript

import API_URL from "../config/api";
import { useState, useRef } from "react";
export function useVideoWatch(videoId: number, initialWatched: boolean) {
const [watched, setWatched] = useState(initialWatched);
const alreadySent = useRef(false);
const markAsWatched = async () => {
if (alreadySent.current || watched ) return;
alreadySent.current = true;
try {
await fetch(`${API_URL}/api/video/${videoId}/watch`, {
method: "POST",
headers: {
Accept: "application/json",
Authorization: `Bearer ${localStorage.getItem("token")}`
},
});
setWatched(true);
} catch (error) {
console.error("Erro ao marcar o vídeo como assistido: ", error);
alreadySent.current = false;
}
}
return { watched, markAsWatched };
}