Allow any vocab to be removed from set

This commit is contained in:
2021-11-26 14:43:23 +00:00
parent bbe04dccbc
commit 8810c5bcae
2 changed files with 58 additions and 27 deletions

View File

@@ -78,13 +78,22 @@ export default withRouter(class CreateSet extends React.Component {
} }
handleSetDataChange = () => { handleSetDataChange = () => {
const numberOfVocabPairs = this.state.inputContents.map(contents => let vocabWithTextExists = false;
this.cleanseVocabString(contents.term) !== "" && const noInvalidPairs = this.state.inputContents.map(contents => {
this.cleanseVocabString(contents.definition) !== "") const cleansedTerm = this.cleanseVocabString(contents.term);
const cleansedDefinition = this.cleanseVocabString(contents.definition);
const emptyVocabTermPresent = cleansedTerm === "" || cleansedDefinition === "";
if (emptyVocabTermPresent && cleansedTerm !== cleansedDefinition) {
return true;
} else if (!emptyVocabTermPresent) {
vocabWithTextExists = true;
}
return false;
})
.filter(x => x === true) .filter(x => x === true)
.length; .length === 0;
if (this.state.inputs.title !== "" && numberOfVocabPairs > 0 && numberOfVocabPairs === this.state.inputContents.length) { if (this.state.inputs.title !== "" && noInvalidPairs && vocabWithTextExists) {
this.setState({ this.setState({
canCreateSet: true, canCreateSet: true,
}) })
@@ -181,18 +190,27 @@ export default withRouter(class CreateSet extends React.Component {
let promises = []; let promises = [];
let batches = [db.batch()]; let batches = [db.batch()];
this.state.inputContents.map((contents, index) => { this.state.inputContents
.filter(contents =>
this.cleanseVocabString(contents.term) !== "" &&
this.cleanseVocabString(contents.definition) !== ""
)
.map((contents, index) => {
if (index % 248 === 0) { if (index % 248 === 0) {
promises.push(batches[batches.length - 1].commit()); promises.push(batches[batches.length - 1].commit());
batches.push(db.batch()); batches.push(db.batch());
} }
const vocabDocRef = setDocRef.collection("vocab").doc(); const vocabDocRef = setDocRef.collection("vocab").doc();
if (contents.term === "") {
return batches[batches.length - 1].delete(vocabDocRef);
} else {
return batches[batches.length - 1].set(vocabDocRef, { return batches[batches.length - 1].set(vocabDocRef, {
term: contents.term, term: contents.term,
definition: contents.definition, definition: contents.definition,
sound: false, sound: true,
});
}
}); });
})
if (!batches[batches.length - 1]._delegate._committed) promises.push(batches[batches.length - 1].commit().catch(() => null)); if (!batches[batches.length - 1]._delegate._committed) promises.push(batches[batches.length - 1].commit().catch(() => null));
@@ -200,7 +218,7 @@ export default withRouter(class CreateSet extends React.Component {
this.stopLoading(); this.stopLoading();
this.props.history.push("/sets/" + setDocRef.id); this.props.history.push("/sets/" + setDocRef.id);
}).catch((error) => { }).catch((error) => {
console.log("Couldn't update set: " + error); console.log("Couldn't save set: " + error);
this.stopLoading(); this.stopLoading();
}); });
}); });

View File

@@ -146,13 +146,22 @@ export default withRouter(class EditSet extends Component {
} }
handleSetDataChange = () => { handleSetDataChange = () => {
const numberOfVocabPairs = this.state.inputContents.map(contents => let vocabWithTextExists = false;
this.cleanseVocabString(contents.term) !== "" && const noInvalidPairs = this.state.inputContents.map(contents => {
this.cleanseVocabString(contents.definition) !== "") const cleansedTerm = this.cleanseVocabString(contents.term);
const cleansedDefinition = this.cleanseVocabString(contents.definition);
const emptyVocabTermPresent = cleansedTerm === "" || cleansedDefinition === "";
if (emptyVocabTermPresent && cleansedTerm !== cleansedDefinition) {
return true;
} else if (!emptyVocabTermPresent) {
vocabWithTextExists = true;
}
return false;
})
.filter(x => x === true) .filter(x => x === true)
.length; .length === 0;
if (this.state.inputs.title !== "" && numberOfVocabPairs > 0 && numberOfVocabPairs === this.state.inputContents.length) { if (this.state.inputs.title !== "" && noInvalidPairs && vocabWithTextExists) {
this.setState({ this.setState({
canSaveSet: true, canSaveSet: true,
}) })
@@ -251,11 +260,15 @@ export default withRouter(class EditSet extends Component {
batches.push(db.batch()); batches.push(db.batch());
} }
const vocabDocRef = setDocRef.collection("vocab").doc(contents.vocabId); const vocabDocRef = setDocRef.collection("vocab").doc(contents.vocabId);
if (contents.term === "") {
return batches[batches.length - 1].delete(vocabDocRef);
} else {
return batches[batches.length - 1].set(vocabDocRef, { return batches[batches.length - 1].set(vocabDocRef, {
term: contents.term, term: contents.term,
definition: contents.definition, definition: contents.definition,
sound: contents.sound, sound: contents.sound,
}); });
}
}) })
// TODO: sound files // TODO: sound files