[FIX] orphaned data causing errors

Set titles now stored in progress & incorrect answer records
This commit is contained in:
2021-10-23 16:42:52 +01:00
parent 87a0f612ae
commit c80905dba9
4 changed files with 108 additions and 82 deletions

View File

@@ -56,7 +56,6 @@ export default withRouter(class GroupStats extends Component {
}
async componentDidMount() {
let promises = [];
let newState = {
sets: {},
setsWithHistory: {},
@@ -77,40 +76,39 @@ export default withRouter(class GroupStats extends Component {
});
if (newState.role === "owner") {
promises.push(
this.state.db
.collection("groups")
.doc(this.props.match.params.groupId)
.get()
.then(async (groupDoc) => {
document.title = `Stats | ${groupDoc.data().display_name} | Parandum`;
newState.groupName = groupDoc.data().display_name;
return Promise.all(groupDoc.data().sets.map((setId) => {
return this.state.db.collection("sets")
.doc(setId)
.get()
.then((doc) => {
newState.sets[setId] = {
title: doc.data().title,
};
});
}));
}).catch((error) => {
console.log(`Can't access group: ${error}`);
newState.groupName = "";
document.title = "Stats | Parandum";
})
);
await this.state.db
.collection("groups")
.doc(this.props.match.params.groupId)
.get()
.then(async (groupDoc) => {
document.title = `Stats | ${groupDoc.data().display_name} | Parandum`;
newState.groupName = groupDoc.data().display_name;
return Promise.all(groupDoc.data().sets.map((setId) => {
return this.state.db.collection("sets")
.doc(setId)
.get()
.then((doc) => {
newState.sets[setId] = {
title: doc.data().title,
};
});
}));
}).catch((error) => {
console.log(`Can't access group: ${error}`);
newState.groupName = "";
document.title = "Stats | Parandum";
});
promises.push(
this.state.db.collection("incorrect_answers")
.where("groups", "array-contains", this.props.match.params.groupId)
.orderBy("term", "asc")
.get()
.then((querySnapshot) => {
let incorrectAnswers = [];
querySnapshot.docs.map((doc, index, array) => {
const groupSetIds = Object.keys(newState.sets);
if (groupSetIds.length > 0) await this.state.db.collection("incorrect_answers")
.where("groups", "array-contains", this.props.match.params.groupId)
.orderBy("term", "asc")
.get()
.then((querySnapshot) => {
let incorrectAnswers = [];
querySnapshot.docs.map((doc, index, array) => {
if (doc.data().setIds.some(item => groupSetIds.includes(item))) {
if (index === 0 || doc.data().term !== array[index - 1].data().term || doc.data().definition !== array[index - 1].data().definition) {
incorrectAnswers.push({
term: doc.data().term,
@@ -145,19 +143,18 @@ export default withRouter(class GroupStats extends Component {
doc.data().setIds.map((setId) => newState.setsWithHistory[setId] = true);
return true;
});
newState.incorrectAnswers = incorrectAnswers.sort((a, b) => b.count + b.switchedCount - a.count - a.switchedCount);
newState.filteredIncorrectAnswers = newState.incorrectAnswers;
newState.totalIncorrect = querySnapshot.docs.length;
})
.catch((error) => {
newState.incorrectAnswers = [];
newState.totalIncorrect = 0;
console.log(`Couldn't get group progress: ${error}`);
})
);
await Promise.all(promises);
}
return false;
});
newState.incorrectAnswers = incorrectAnswers.sort((a, b) => b.count + b.switchedCount - a.count - a.switchedCount);
newState.filteredIncorrectAnswers = newState.incorrectAnswers;
newState.totalIncorrect = querySnapshot.docs.length;
})
.catch((error) => {
newState.incorrectAnswers = [];
newState.totalIncorrect = 0;
console.log(`Couldn't get group progress: ${error}`);
});
}
this.setState(newState);

View File

@@ -132,14 +132,17 @@ export default class IncorrectHistory extends Component {
}
}
doc.data().setIds.map((setId) => subPromises.push(
this.state.db.collection("sets")
.doc(setId)
.get()
.then((doc) =>
newState.setsWithHistory[setId] = doc.data().title
)
));
// doc.data().setIds.map((setId) => subPromises.push(
// this.state.db.collection("sets")
// .doc(setId)
// .get()
// .then((doc) =>
// newState.setsWithHistory[setId] = doc.data().title
// )
// ));
doc.data().setIds.map((setId, setIndex) =>
newState.setsWithHistory[setId] = doc.data().set_titles[setIndex]
);
return true;
});
@@ -166,7 +169,6 @@ export default class IncorrectHistory extends Component {
setIds: doc.data().setIds,
})
);
console.log(newState.progressHistory);
newState.totalTests = newState.progressHistory.length;
})
);
@@ -230,14 +232,6 @@ export default class IncorrectHistory extends Component {
return newVocabItem;
});
console.log(this.state.progressHistory
.filter((progressItem) =>
this.arraysHaveSameMembers(progressItem.setIds, [selectedSet.value]) ||
(
this.state.includeCompoundTests &&
progressItem.setIds.includes(selectedSet.value)
)
));
this.setState({
filteredIncorrectAnswers: filteredIncorrectAnswers,
selectedSet: selectedSet,