Listening preview and some more patches
This commit is contained in:
@@ -3,15 +3,14 @@ import { ModuleState } from "../types";
|
||||
import ReorderResult from "./types";
|
||||
|
||||
const reorderFillBlanks = (exercise: FillBlanksExercise, startId: number): ReorderResult<FillBlanksExercise> => {
|
||||
console.log();
|
||||
const newSolutions = exercise.solutions
|
||||
let newSolutions = exercise.solutions
|
||||
.sort((a, b) => parseInt(a.id) - parseInt(b.id))
|
||||
.map((solution, index) => ({
|
||||
...solution,
|
||||
id: (startId + index).toString()
|
||||
}));
|
||||
|
||||
const idMapping = exercise.solutions
|
||||
let idMapping = exercise.solutions
|
||||
.sort((a, b) => parseInt(a.id) - parseInt(b.id))
|
||||
.reduce((acc, solution, index) => {
|
||||
acc[solution.id] = (startId + index).toString();
|
||||
@@ -25,7 +24,7 @@ const reorderFillBlanks = (exercise: FillBlanksExercise, startId: number): Reord
|
||||
});
|
||||
|
||||
|
||||
const newWords = exercise.words.map(word => {
|
||||
let newWords = exercise.words.map(word => {
|
||||
if (typeof word === 'string') {
|
||||
return word;
|
||||
} else if ('letter' in word && 'word' in word) {
|
||||
@@ -36,7 +35,7 @@ const reorderFillBlanks = (exercise: FillBlanksExercise, startId: number): Reord
|
||||
return word;
|
||||
});
|
||||
|
||||
const newUserSolutions = exercise.userSolutions?.map(solution => ({
|
||||
let newUserSolutions = exercise.userSolutions?.map(solution => ({
|
||||
...solution,
|
||||
id: idMapping[solution.id] || solution.id
|
||||
}));
|
||||
@@ -55,36 +54,36 @@ const reorderFillBlanks = (exercise: FillBlanksExercise, startId: number): Reord
|
||||
};
|
||||
|
||||
const reorderWriteBlanks = (exercise: WriteBlanksExercise, startId: number): ReorderResult<WriteBlanksExercise> => {
|
||||
const oldIds = exercise.solutions.map(s => s.id);
|
||||
const newIds = oldIds.map((_, index) => (startId + index).toString());
|
||||
|
||||
const newSolutions = exercise.solutions.map((solution, index) => ({
|
||||
id: newIds[index],
|
||||
solution: [...solution.solution]
|
||||
let oldIds = exercise.solutions.map(s => s.id);
|
||||
let newIds = oldIds.map((_, index) => (startId + index).toString());
|
||||
|
||||
let newSolutions = exercise.solutions.map((solution, index) => ({
|
||||
id: newIds[index],
|
||||
solution: [...solution.solution]
|
||||
}));
|
||||
|
||||
|
||||
let newText = exercise.text;
|
||||
oldIds.forEach((oldId, index) => {
|
||||
newText = newText.replace(
|
||||
`{{${oldId}}}`,
|
||||
`{{${newIds[index]}}}`
|
||||
);
|
||||
newText = newText.replace(
|
||||
`{{${oldId}}}`,
|
||||
`{{${newIds[index]}}}`
|
||||
);
|
||||
});
|
||||
|
||||
const result = {
|
||||
exercise: {
|
||||
...exercise,
|
||||
solutions: newSolutions,
|
||||
text: newText
|
||||
},
|
||||
lastId: startId + newSolutions.length
|
||||
|
||||
let result = {
|
||||
exercise: {
|
||||
...exercise,
|
||||
solutions: newSolutions,
|
||||
text: newText
|
||||
},
|
||||
lastId: startId + newSolutions.length
|
||||
};
|
||||
|
||||
|
||||
return result;
|
||||
};
|
||||
};
|
||||
|
||||
const reorderTrueFalse = (exercise: TrueFalseExercise, startId: number): ReorderResult<TrueFalseExercise> => {
|
||||
const newQuestions = exercise.questions
|
||||
let newQuestions = exercise.questions
|
||||
.sort((a, b) => parseInt(a.id) - parseInt(b.id))
|
||||
.map((question, index) => ({
|
||||
...question,
|
||||
@@ -101,7 +100,7 @@ const reorderTrueFalse = (exercise: TrueFalseExercise, startId: number): Reorder
|
||||
};
|
||||
|
||||
const reorderMatchSentences = (exercise: MatchSentencesExercise, startId: number): ReorderResult<MatchSentencesExercise> => {
|
||||
const newSentences = exercise.sentences
|
||||
let newSentences = exercise.sentences
|
||||
.sort((a, b) => parseInt(a.id) - parseInt(b.id))
|
||||
.map((sentence, index) => ({
|
||||
...sentence,
|
||||
@@ -119,7 +118,7 @@ const reorderMatchSentences = (exercise: MatchSentencesExercise, startId: number
|
||||
|
||||
|
||||
const reorderMultipleChoice = (exercise: MultipleChoiceExercise, startId: number): ReorderResult<MultipleChoiceExercise> => {
|
||||
const newQuestions = exercise.questions
|
||||
let newQuestions = exercise.questions
|
||||
.sort((a, b) => parseInt(a.id) - parseInt(b.id))
|
||||
.map((question, index) => ({
|
||||
...question,
|
||||
@@ -138,7 +137,7 @@ const reorderMultipleChoice = (exercise: MultipleChoiceExercise, startId: number
|
||||
|
||||
const reorderSection = (exercises: Exercise[], startId: number): { exercises: Exercise[], lastId: number } => {
|
||||
let currentId = startId;
|
||||
const reorderedExercises = exercises.map(exercise => {
|
||||
let reorderedExercises = exercises.map(exercise => {
|
||||
let result;
|
||||
|
||||
switch (exercise.type) {
|
||||
@@ -182,26 +181,25 @@ const reorderSection = (exercises: Exercise[], startId: number): { exercises: Ex
|
||||
|
||||
const reorderModule = (moduleState: ModuleState) => {
|
||||
let currentId = 1;
|
||||
const reorderedSections = moduleState.sections.map(section => {
|
||||
const currentSection = section.state as ReadingPart | ListeningPart | LevelPart;
|
||||
console.log(currentSection.exercises);
|
||||
const result = reorderSection(currentSection.exercises, currentId);
|
||||
currentId = result.lastId;
|
||||
console.log(result);
|
||||
return {
|
||||
...section,
|
||||
state: {
|
||||
...currentSection,
|
||||
exercises: result.exercises
|
||||
}
|
||||
};
|
||||
let reorderedSections = moduleState.sections.map(section => {
|
||||
let currentSection = section.state as ReadingPart | ListeningPart | LevelPart;
|
||||
console.log(currentSection.exercises);
|
||||
let result = reorderSection(currentSection.exercises, currentId);
|
||||
currentId = result.lastId;
|
||||
return {
|
||||
...section,
|
||||
state: {
|
||||
...currentSection,
|
||||
exercises: result.exercises
|
||||
}
|
||||
};
|
||||
});
|
||||
|
||||
|
||||
return {
|
||||
...moduleState,
|
||||
sections: reorderedSections
|
||||
...moduleState,
|
||||
sections: reorderedSections
|
||||
};
|
||||
};
|
||||
};
|
||||
|
||||
export {
|
||||
reorderFillBlanks,
|
||||
|
||||
Reference in New Issue
Block a user