Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using the linear key in Quiz type to jumble questions received from A… #174

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 42 additions & 22 deletions src/services/API/Question.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,56 @@ import { apiClient } from "./RootClient";
import { questionsEndpoint } from "./Endpoints";
import { Question } from "@/types";

/**
* Shuffles the order of questions in an array
* @param {Array} array - The array to be shuffled
* @returns {Array} shuffled array
*/
const shuffleArray = (array: any) => {
return array.sort(() => Math.random() - 0.5); // Simple random shuffle
};

export default {
/**
* returns the list of questions belonging to a question set
* Returns the list of questions belonging to a question set, with optional shuffling
* @param {string} questionSetId - uuid of the question set
* @param {number | undefined} skip - skip a number of records
* @param {number | undefined} limit - limit the returned number of records
* @returns {Promise<QuizResponse>} a list of questions belonging to the given question set
* @param {string} linear - Linear key from Quiz JSON (can be 'jumble' or other value)
* @returns {Promise<QuizResponse>} a list of questions
*/
async getQuestions(
questionSetId: string,
skip: number | undefined = undefined,
limit: number | undefined = undefined
limit: number | undefined = undefined,
linear: string = "linear" // Default is "linear", or can be passed as 'jumble'
): Promise<Question[]> {
type queryParamsType = {
question_set_id: string,
skip?: number,
limit?: number,
}

const queryParams: queryParamsType = {
question_set_id: questionSetId,
}

if (skip != undefined) queryParams.skip = skip
if (limit != undefined) queryParams.limit = limit

const response = await apiClient().get(
questionsEndpoint,
{ params: queryParams }
);
return response.data;
type queryParamsType = {
question_set_id: string;
skip?: number;
limit?: number;
linear: string;
};

const queryParams: queryParamsType = {
question_set_id: questionSetId,
linear: linear, // Pass linear key for jumbling or not
};

if (skip != undefined) queryParams.skip = skip;
if (limit != undefined) queryParams.limit = limit;

const response = await apiClient().get(questionsEndpoint, {
params: queryParams,
});

let questions = response.data;

// If 'linear' is 'jumble', shuffle the questions
if (linear === "jumble") {
questions = shuffleArray(questions); // Shuffle questions if linear is "jumble"
}

return questions;
},
};
};