Skip to content
Snippets Groups Projects
Commit f4528ab3 authored by Felipe D'Abrantes's avatar Felipe D'Abrantes
Browse files

Add use hook for Questions

parent 708876f8
No related branches found
No related tags found
1 merge request!25Add a question management page
import { useState, useEffect } from 'react';
export type QuestionRecord = {
id: string;
content: string;
used: number;
createdAt: string;
updatedAt: string;
};
export const useQuestions = () => {
const [rehydrateQuestions, setRehydrateQuestions] = useState(false);
const [questions, setQuestions] = useState<undefined | QuestionRecord[]>(undefined);
useEffect(() => {
if (questions !== undefined) return;
setRehydrateQuestions(true);
}, [questions]);
const fetchQuestions = async () => {
const endpoint = `${process.env.NEXT_PUBLIC_FEED_SERVICE_URL}questions`;
const headers = { Authorization: `Bearer ${sessionStorage.getItem('token')}` };
const response = await fetch(endpoint, { headers });
if (response.ok) {
const data = await response.json();
return data;
}
};
useEffect(() => {
if (!rehydrateQuestions) return;
fetchQuestions().then((res: QuestionRecord[]) => {
setQuestions(res);
setRehydrateQuestions(false);
});
}, [rehydrateQuestions]);
return { questions, setRehydrateQuestions };
};
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment