Skip to content

Commit

Permalink
feat: [BD-26] Create lib api service to interact with exam
Browse files Browse the repository at this point in the history
  • Loading branch information
UvgenGen authored and TamoshaytisV committed Apr 21, 2021
1 parent 32e1555 commit 4b0b7c7
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 10 deletions.
16 changes: 6 additions & 10 deletions src/SpecialExamWrapper/Sequence.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React, { useState, useEffect } from 'react';
import PropTypes from 'prop-types';
import { getAuthenticatedHttpClient } from '@edx/frontend-platform/auth';
import { getConfig } from '@edx/frontend-platform';
import { getExamData, getUserAtemptsData } from './data/api'

const ExamSequence = (props) => (
<div>
Expand All @@ -15,18 +16,13 @@ const SequenceExamWrapper = ({ children, ...props }) => {
const [isLoading, setIsLoading] = useState(true);
const { sequence, courseId, loader, userId } = props;

const getExamData = async () => {
const examUrl = new URL(
`${getConfig().LMS_BASE_URL}/api/edx_proctoring/v1/proctored_exam/exam/course_id/${courseId}/content_id/${sequence.id}`
);
const { data } = await getAuthenticatedHttpClient().get(examUrl.href);
const getExam = async () => {
const data = await getExamData(courseId, sequence.id);
setExamId(data.id);
};

const getAtemptData = async() => {
// TODO: find where to get user_id
const url = new URL(`${getConfig().LMS_BASE_URL}/api/edx_proctoring/v1/proctored_exam/active_exams_for_user?user_id=${userId}&course_id=${encodeURIComponent(courseId)}`);
const { data } = await getAuthenticatedHttpClient().get(url.href);
const getUserAtempt = async() => {
const data = await getUserAtemptsData(userId, courseId);
if (Object.keys(data).length > 0) {
const examData = data[0];
setExamStarted(examData.attempt.status === 'started');
Expand All @@ -35,7 +31,7 @@ const SequenceExamWrapper = ({ children, ...props }) => {
};

useEffect(() => {
getExamData().then(() => getAtemptData());
getExam().then(() => getUserAtempt());
}, []);

const startExam = async () => {
Expand Down
18 changes: 18 additions & 0 deletions src/SpecialExamWrapper/data/api.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { getAuthenticatedHttpClient } from '@edx/frontend-platform/auth';
import { getConfig } from '@edx/frontend-platform';

export async function getExamData(courseId, content_id){
const url = new URL(
`${getConfig().LMS_BASE_URL}/api/edx_proctoring/v1/proctored_exam/exam/course_id/${courseId}/content_id/${content_id}`
);
const { data } = await getAuthenticatedHttpClient().get(url.href);
return data
};

export async function getUserAtemptsData(userId, courseId){
const url = new URL(
`${getConfig().LMS_BASE_URL}/api/edx_proctoring/v1/proctored_exam/active_exams_for_user?user_id=${userId}&course_id=${encodeURIComponent(courseId)}`
);
const { data } = await getAuthenticatedHttpClient().get(url.href);
return data
};

0 comments on commit 4b0b7c7

Please sign in to comment.