[FIX] batch write errors and vocab not deleted
This commit is contained in:
@@ -170,7 +170,6 @@ 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,
|
||||||
@@ -178,35 +177,32 @@ export default withRouter(class CreateSet extends React.Component {
|
|||||||
owner: this.state.user.uid,
|
owner: this.state.user.uid,
|
||||||
groups: [],
|
groups: [],
|
||||||
})
|
})
|
||||||
// setDoc(setDocRef, {
|
|
||||||
// title: this.state.inputs.title,
|
|
||||||
// public: this.state.inputs.public,
|
|
||||||
// owner: this.state.user.uid,
|
|
||||||
// groups: [],
|
|
||||||
// })
|
|
||||||
.then(() => {
|
.then(() => {
|
||||||
const batch = db.batch();
|
let promises = [];
|
||||||
// const batch = writeBatch(this.state.db);
|
let batches = [db.batch()];
|
||||||
|
|
||||||
this.state.inputContents.map((contents) => {
|
this.state.inputContents.map((contents, index) => {
|
||||||
|
if (index % 248 === 0) {
|
||||||
|
promises.push(batches[batches.length - 1].commit());
|
||||||
|
batches.push(db.batch());
|
||||||
|
}
|
||||||
const vocabDocRef = setDocRef.collection("vocab").doc();
|
const vocabDocRef = setDocRef.collection("vocab").doc();
|
||||||
// const vocabDocRef = doc(collection(setDocRef, "vocab"));
|
return batches[batches.length - 1].set(vocabDocRef, {
|
||||||
return batch.set(vocabDocRef, {
|
|
||||||
term: contents.term,
|
term: contents.term,
|
||||||
definition: contents.definition,
|
definition: contents.definition,
|
||||||
sound: false,
|
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();
|
||||||
})
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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) => {
|
||||||
|
if (index % 248 === 0) {
|
||||||
|
promises.push(batches[batches.length - 1].commit());
|
||||||
|
batches.push(db.batch());
|
||||||
|
}
|
||||||
const vocabDocRef = setDocRef.collection("vocab").doc(contents.vocabId);
|
const vocabDocRef = setDocRef.collection("vocab").doc(contents.vocabId);
|
||||||
return batch.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
|
||||||
|
|
||||||
batch.commit().then(() => {
|
if (this.state.inputContents.length < this.state.originalInputContents.length) {
|
||||||
|
let batchItems = this.state.inputContents.length % 248;
|
||||||
|
for (let i = this.state.inputContents.length; i < this.state.originalInputContents.length; i++) {
|
||||||
|
if (batchItems + i % 248 === 0) {
|
||||||
|
if (batchItems !== 0) batchItems = 0;
|
||||||
|
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.stopLoading();
|
||||||
this.props.history.push("/sets/" + setDocRef.id);
|
this.props.history.push("/sets/" + setDocRef.id);
|
||||||
}).catch((e) => {
|
}).catch((error) => {
|
||||||
console.log("Couldn't update set. Batch commit error: " + e);
|
console.log("Couldn't update set: " + error);
|
||||||
this.stopLoading();
|
this.stopLoading();
|
||||||
})
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user