-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor quiz/page.tsx to use QuizService and quiz.d.ts
- Loading branch information
1 parent
b90d57a
commit 2c54c6c
Showing
3 changed files
with
95 additions
and
68 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
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,45 @@ | ||
import { Quiz } from '@/types'; | ||
|
||
export const QuizService = { | ||
fetchData: async (): Promise<Quiz.ApiResponse> => { | ||
try { | ||
console.log('Fetching data from API...'); | ||
const response = await fetch( | ||
'http://localhost:80/v1/quizzes/2?pageNumber=0&pageSize=60', | ||
{ | ||
headers: { | ||
'x-user-id': '12asd', | ||
}, | ||
} | ||
); | ||
const responseData: Quiz.ApiResponse = await response.json(); | ||
console.log('Data fetched successfully:', responseData); | ||
return responseData; | ||
} catch (error) { | ||
console.error('Error fetching data:', error); | ||
throw error; | ||
} | ||
}, | ||
|
||
submitAttempt: async (mcqId: number): Promise<void> => { | ||
try { | ||
await fetch( | ||
`http://localhost/v1/quizzes/2/mcqs/${mcqId}/attempt`, | ||
{ | ||
method: 'PUT', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
'x-user-id': '12asd', | ||
}, | ||
body: JSON.stringify({ | ||
attempt: 1, | ||
}), | ||
} | ||
); | ||
console.log(`Attempt submitted for MCQ ID: ${mcqId}`); | ||
} catch (error) { | ||
console.error(`Error submitting attempt for MCQ ID: ${mcqId}`, error); | ||
throw error; | ||
} | ||
}, | ||
}; |
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,45 @@ | ||
declare namespace Quiz { | ||
|
||
interface Option { | ||
no: number; | ||
text: string; | ||
explanation: string; | ||
isAnswer: boolean; | ||
} | ||
|
||
interface Topic { | ||
id: number; | ||
name: string; | ||
} | ||
|
||
interface Skill { | ||
id: number; | ||
name: string; | ||
topicId: number | null; | ||
} | ||
|
||
interface Mcq { | ||
id: number; | ||
stem: string; | ||
options: Option[]; | ||
topics: Topic[]; | ||
skills: Skill[]; | ||
status: string; | ||
publishedOn: number; | ||
publishedBy: string; | ||
closedOn: number | null; | ||
closedBy: string | null; | ||
createdOn: number; | ||
createdBy: string; | ||
} | ||
|
||
interface ApiResponse { | ||
id: number; | ||
mcqs: Mcq[]; | ||
pageNumber: number; | ||
pageSize: number; | ||
totalPages: number; | ||
totalRecords: number; | ||
} | ||
} | ||
|