-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #812 from samuel-coutinho/feb-17-add-useAssessment…
…Answers Create useAssessmentAnswers to fetch Assessment questions and answers.
- Loading branch information
Showing
13 changed files
with
576 additions
and
489 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
import { useEffect, useState, useCallback } from "react"; | ||
import { getAllEntities } from "../repository/entity.repository"; | ||
|
||
interface AssessmentProps { | ||
assessmentId: string | null | undefined; | ||
} | ||
|
||
export interface Question { | ||
id: number; | ||
subtopicId: string; | ||
questionText: string; | ||
answerType: string; | ||
evidenceFileRequired: boolean; | ||
hint: string; | ||
isRequired: boolean; | ||
priorityLevel: "high priority" | "medium priority" | "low priority"; | ||
evidenceFiles?: File[]; | ||
answer: string; | ||
} | ||
|
||
export interface Subtopic { | ||
id: number; | ||
topicId: number; | ||
name: string; | ||
questions: Question[]; | ||
} | ||
|
||
export interface Topic { | ||
id: number; | ||
assessmentId: string; | ||
title: string; | ||
subtopics: Subtopic[]; | ||
} | ||
|
||
interface ApiQuestion { | ||
id: number; | ||
subtopic_id: string; | ||
question: string; | ||
answer_type: string; | ||
evidence_file_required: boolean; | ||
hint: string; | ||
is_required: boolean; | ||
priority_level: "high priority" | "medium priority" | "low priority"; | ||
evidence_files?: File[]; | ||
answer: string; | ||
} | ||
|
||
interface ApiSubtopic { | ||
id: number; | ||
topic_id: number; | ||
name: string; | ||
questions: ApiQuestion[]; | ||
} | ||
|
||
interface ApiTopic { | ||
id: number; | ||
assessment_id: string; | ||
title: string; | ||
subTopics: ApiSubtopic[]; | ||
} | ||
|
||
interface ApiResponse { | ||
data: { | ||
message: { | ||
topics: ApiTopic[]; | ||
} | ||
} | ||
} | ||
|
||
const convertResponseAttributes = (response: ApiResponse): Topic[] => { | ||
const responseTopics = response?.data?.message?.topics; | ||
|
||
const topics = responseTopics.map((topic: ApiTopic) => { | ||
return { | ||
id: topic.id, | ||
assessmentId: topic.assessment_id, | ||
title: topic.title, | ||
subtopics: topic.subTopics.map((subtopic: ApiSubtopic) => { | ||
return { | ||
id: subtopic.id, | ||
topicId: subtopic.topic_id, | ||
name: subtopic.name, | ||
questions: subtopic.questions.map((question: ApiQuestion) => { | ||
return { | ||
id: question.id, | ||
subtopicId: question.subtopic_id, | ||
questionText: question.question, | ||
answerType: question.answer_type, | ||
evidenceFileRequired: question.evidence_file_required, | ||
hint: question.hint, | ||
isRequired: question.is_required, | ||
priorityLevel: question.priority_level, | ||
evidenceFiles: question.evidence_files || [], | ||
answer: question.answer, | ||
}; | ||
}), | ||
}; | ||
}), | ||
}; | ||
}); | ||
|
||
return topics | ||
}; | ||
|
||
const useAssessmentAnswers = ({assessmentId}: AssessmentProps) => { | ||
const [topics, setTopics] = useState<Topic[]>([]); | ||
const [isLoading, setIsLoading] = useState(true); | ||
const [error, setError] = useState<string | null>(null); | ||
|
||
const fetchAssessmentAnswers = useCallback(async ({controller}: { controller: AbortController }) => { | ||
if (!controller) return; | ||
|
||
setIsLoading(true); | ||
setError(null); | ||
|
||
try { | ||
const response = await getAllEntities({routeUrl: `/assessments/getAnswers/${assessmentId}`}) | ||
if (response?.data?.message?.topics?.length > 0) { | ||
const topics = convertResponseAttributes(response); | ||
setTopics(topics); | ||
} else { | ||
setError("No assessment answers found for this project.") | ||
} | ||
} catch (error) { | ||
console.error('An error occurred:', error); | ||
if (error instanceof Error) { | ||
setError(error.message); | ||
} else { | ||
setError(String(error)); | ||
} | ||
} finally { | ||
setIsLoading(false); | ||
} | ||
}, [assessmentId]); | ||
|
||
useEffect(() => { | ||
const controller = new AbortController(); | ||
// Fetch assessment answers only if assessmentId is provided | ||
if (assessmentId) { | ||
fetchAssessmentAnswers({ controller }); | ||
} | ||
return () => controller.abort(); | ||
}, [assessmentId, fetchAssessmentAnswers]); | ||
|
||
|
||
return {topics, isLoading, error} | ||
} | ||
|
||
export default useAssessmentAnswers |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/** | ||
* Handles the display of an alert by setting the alert properties and | ||
* automatically clearing the alert after a specified timeout. | ||
* | ||
* @param {HandleAlertProps} props - The properties for handling the alert. | ||
* @param {string} props.variant - The variant of the alert (e.g., success, error). | ||
* @param {string} props.body - The body message of the alert. | ||
* @param {string} props.title - The title of the alert. | ||
* @param {React.Dispatch<React.SetStateAction<AlertProps | null>>} props.setAlert - The function to set the alert state. | ||
* @returns {() => void} A function to clear the timeout for the alert. | ||
*/ | ||
import { AlertProps } from "../../presentation/components/Alert"; | ||
|
||
interface HandleAlertProps extends AlertProps { | ||
setAlert: React.Dispatch<React.SetStateAction<AlertProps | null>>; | ||
} | ||
|
||
const ALERT_TIMEOUT = 2500 | ||
|
||
const handleAlert = ({ variant, body, title, setAlert }: HandleAlertProps) => { | ||
setAlert({ | ||
variant, | ||
title, | ||
body, | ||
}); | ||
const timeoutId = setTimeout(() => { | ||
setAlert(null) | ||
}, ALERT_TIMEOUT) | ||
return () => clearTimeout(timeoutId) | ||
}; | ||
|
||
|
||
export { handleAlert } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.