[FIX] batch write errors and vocab not deleted

This commit is contained in:
2021-10-26 17:05:25 +01:00
parent 3207d0ee37
commit 3ad415370d
2 changed files with 77 additions and 60 deletions

View File

@@ -170,44 +170,40 @@ export default withRouter(class CreateSet extends React.Component {
const db = this.state.db; const db = this.state.db;
const setDocRef = db.collection("sets").doc(); const setDocRef = db.collection("sets").doc();
// const setDocRef = doc(collection(this.state.db, "sets"));
setDocRef.set({ setDocRef.set({
title: this.state.inputs.title, title: this.state.inputs.title,
public: this.state.inputs.public, public: this.state.inputs.public,
owner: this.state.user.uid, owner: this.state.user.uid,
groups: [], groups: [],
}) })
// setDoc(setDocRef, { .then(() => {
// title: this.state.inputs.title, let promises = [];
// public: this.state.inputs.public, let batches = [db.batch()];
// owner: this.state.user.uid,
// groups: [],
// })
.then(() => {
const batch = db.batch();
// const batch = writeBatch(this.state.db);
this.state.inputContents.map((contents) => { this.state.inputContents.map((contents, index) => {
const vocabDocRef = setDocRef.collection("vocab").doc(); if (index % 248 === 0) {
// const vocabDocRef = doc(collection(setDocRef, "vocab")); promises.push(batches[batches.length - 1].commit());
return batch.set(vocabDocRef, { batches.push(db.batch());
term: contents.term, }
definition: contents.definition, const vocabDocRef = setDocRef.collection("vocab").doc();
sound: false, return batches[batches.length - 1].set(vocabDocRef, {
term: contents.term,
definition: contents.definition,
sound: false,
});
}) })
})
// TODO: sound files if (!batches[batches.length - 1]._delegate._committed) promises.push(batches[batches.length - 1].commit().catch(() => null));
batch.commit().then(() => { Promise.all(promises).then(() => {
this.stopLoading(); this.stopLoading();
this.props.history.push("/sets/" + setDocRef.id); this.props.history.push("/sets/" + setDocRef.id);
}).catch((e) => { }).catch((error) => {
console.log("Couldn't create set. Batch commit error: " + e); console.log("Couldn't update set: " + error);
this.stopLoading(); this.stopLoading();
}) });
}); });
} }
} }

View File

@@ -20,6 +20,7 @@ export default withRouter(class EditSet extends Component {
public: false, public: false,
}, },
inputContents: [], inputContents: [],
originalInputContents: [],
setInaccessible: false, setInaccessible: false,
navbarItems: [ navbarItems: [
{ {
@@ -91,6 +92,7 @@ export default withRouter(class EditSet extends Component {
public: setDoc.data().public, public: setDoc.data().public,
}, },
inputContents: vocab, inputContents: vocab,
originalInputContents: JSON.parse(JSON.stringify(vocab)),
canMakeSetNonPublic: !(setDoc.data().groups && setDoc.data().groups.length > 0), canMakeSetNonPublic: !(setDoc.data().groups && setDoc.data().groups.length > 0),
}; };
@@ -229,7 +231,7 @@ export default withRouter(class EditSet extends Component {
}, this.handleSetDataChange()); }, this.handleSetDataChange());
} }
saveSet = () => { saveSet = async () => {
if (this.state.canSaveSet) { if (this.state.canSaveSet) {
this.setState({ this.setState({
loading: true, loading: true,
@@ -240,30 +242,49 @@ export default withRouter(class EditSet extends Component {
const setId = this.props.match.params.setId; const setId = this.props.match.params.setId;
const setDocRef = db.collection("sets").doc(setId); const setDocRef = db.collection("sets").doc(setId);
setDocRef.update({ let promises = [];
title: this.state.inputs.title, let batches = [db.batch()];
public: this.state.inputs.public,
}).then(() => {
const batch = db.batch();
this.state.inputContents.map((contents) => { this.state.inputContents.map((contents, index) => {
const vocabDocRef = setDocRef.collection("vocab").doc(contents.vocabId); if (index % 248 === 0) {
return batch.set(vocabDocRef, { promises.push(batches[batches.length - 1].commit());
term: contents.term, batches.push(db.batch());
definition: contents.definition, }
sound: contents.sound, const vocabDocRef = setDocRef.collection("vocab").doc(contents.vocabId);
}) return batches[batches.length - 1].set(vocabDocRef, {
}) term: contents.term,
definition: contents.definition,
sound: contents.sound,
});
})
// TODO: sound files // TODO: sound files
batch.commit().then(() => { if (this.state.inputContents.length < this.state.originalInputContents.length) {
this.stopLoading(); let batchItems = this.state.inputContents.length % 248;
this.props.history.push("/sets/" + setDocRef.id); for (let i = this.state.inputContents.length; i < this.state.originalInputContents.length; i++) {
}).catch((e) => { if (batchItems + i % 248 === 0) {
console.log("Couldn't update set. Batch commit error: " + e); if (batchItems !== 0) batchItems = 0;
this.stopLoading(); promises.push(batches[batches.length - 1].commit());
}) batches.push(db.batch());
}
const vocabDocRef = setDocRef
.collection("vocab")
.doc(this.state.originalInputContents[i].vocabId);
batches[batches.length - 1].delete(vocabDocRef);
}
}
if (!batches[batches.length - 1]._delegate._committed) promises.push(batches[batches.length - 1].commit().catch(() => null));
Promise.all(promises).then(() => {
this.stopLoading();
this.props.history.push("/sets/" + setDocRef.id);
}).catch((error) => {
console.log("Couldn't update set: " + error);
this.stopLoading();
}); });
} }
} }