Allow any vocab to be removed from set
This commit is contained in:
@@ -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
|
||||||
if (index % 248 === 0) {
|
.filter(contents =>
|
||||||
promises.push(batches[batches.length - 1].commit());
|
this.cleanseVocabString(contents.term) !== "" &&
|
||||||
batches.push(db.batch());
|
this.cleanseVocabString(contents.definition) !== ""
|
||||||
}
|
)
|
||||||
const vocabDocRef = setDocRef.collection("vocab").doc();
|
.map((contents, index) => {
|
||||||
return batches[batches.length - 1].set(vocabDocRef, {
|
if (index % 248 === 0) {
|
||||||
term: contents.term,
|
promises.push(batches[batches.length - 1].commit());
|
||||||
definition: contents.definition,
|
batches.push(db.batch());
|
||||||
sound: false,
|
}
|
||||||
|
const vocabDocRef = setDocRef.collection("vocab").doc();
|
||||||
|
if (contents.term === "") {
|
||||||
|
return batches[batches.length - 1].delete(vocabDocRef);
|
||||||
|
} else {
|
||||||
|
return batches[batches.length - 1].set(vocabDocRef, {
|
||||||
|
term: contents.term,
|
||||||
|
definition: contents.definition,
|
||||||
|
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();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -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);
|
||||||
return batches[batches.length - 1].set(vocabDocRef, {
|
if (contents.term === "") {
|
||||||
term: contents.term,
|
return batches[batches.length - 1].delete(vocabDocRef);
|
||||||
definition: contents.definition,
|
} else {
|
||||||
sound: contents.sound,
|
return batches[batches.length - 1].set(vocabDocRef, {
|
||||||
});
|
term: contents.term,
|
||||||
|
definition: contents.definition,
|
||||||
|
sound: contents.sound,
|
||||||
|
});
|
||||||
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
// TODO: sound files
|
// TODO: sound files
|
||||||
|
|||||||
Reference in New Issue
Block a user