Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 30 additions & 11 deletions src/store/modules/questions.store.js
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,9 @@ const actions = {
let request = null;
const ref = db.collection("questions").orderBy("name");

if (type === "next") {
if (!state.lastQuestionDocument) {
request = ref.limit(itemsPerPage).get();
} else if (type === "next") {
request = ref
.startAfter(state.lastQuestionDocument[1])
.limit(itemsPerPage)
Expand All @@ -378,7 +380,7 @@ const actions = {

request
.then(snapshot => {
if (snapshot.docs.length > 0) {
if (!snapshot.empty) {
first = snapshot.docs[0].data().name;
last = snapshot.docs[snapshot.docs.length - 1].data().name;
}
Expand Down Expand Up @@ -435,7 +437,8 @@ const actions = {

const pages = Object.keys(state.questions);

const questionAmount = this.getters.getDataSize.questions.general;
const dataSize = this.getters.getDataSize;
const questionAmount = dataSize?.questions?.general ?? 0;
const amount = questionAmount % 8;

if (!pages.includes("p" + page)) {
Expand All @@ -453,7 +456,7 @@ const actions = {

request
.then(snapshot => {
if (snapshot.docs.length > 0) {
if (!snapshot.empty) {
first = snapshot.docs[0].data().name;
last = snapshot.docs[snapshot.docs.length - 1].data().name;
}
Expand Down Expand Up @@ -623,6 +626,10 @@ const actions = {
.where("name", "==", name)
.get()
.then(snapshot => {
if (snapshot.empty) {
commit("setLoading", false);
return;
}
const doc = snapshot.docs[0];

const toDelete = {
Expand All @@ -648,6 +655,7 @@ const actions = {
.where("name", "==", doc.data().subject)
.get()
.then(snap => {
if(snap.empty) return;
const document = snap.docs[0];
const questions = document.data().questions;
const index = questions.findIndex(q => q.name === name);
Expand Down Expand Up @@ -700,6 +708,10 @@ const actions = {
.where("name", "==", name)
.get();

if (snapshot.empty) {
commit("setLoading", false);
return;
}
const doc = snapshot.docs[0];
const data = doc.data();

Expand Down Expand Up @@ -743,6 +755,7 @@ const actions = {
.where("name", "==", question.subject)
.get();

if(subSnap.empty) return;
const document = subSnap.docs[0];

if (!document.data().questions.find(q => q.name === question.name)) {
Expand Down Expand Up @@ -984,6 +997,7 @@ const actions = {
.where("name", "==", doc.data().subject)
.get()
.then(snap => {
if(snap.empty) return;
const document = snap.docs[0];
const questions = document.data().questions;
const index = questions.findIndex(
Expand All @@ -1010,8 +1024,9 @@ const actions = {
db.collection("data-size")
.get()
.then(snap => {
if (snap.empty) return;
const document = snap.docs[0];
const general = document.data().questions.general;
const general = document.data()?.questions?.general ?? 0;

const questions = {
general: general - snapshot.docs.length,
Expand Down Expand Up @@ -1127,7 +1142,7 @@ const actions = {

if (oldSubject) {
const sizeSnap = await db.collection("data-size").get();

if (sizeSnap.empty) return;
const document = sizeSnap.docs[0];
const general = document.data().questions.general;
const sub = question.subject;
Expand Down Expand Up @@ -1293,7 +1308,7 @@ const actions = {
* @param {string} payload - The question name.
* @returns {Promise<boolean>} Whether the question exists or not.
*/
async questionExists(_, payload) {
async questionExists({commit}, payload) {
return new Promise((resolve, reject) => {
try {
db.collection("questions")
Expand Down Expand Up @@ -1346,8 +1361,8 @@ const actions = {
...payload.question
};

let questionAmount = this.getters.getDataSize.questions.general;
questionAmount = questionAmount <= 0 ? 0 : questionAmount;
const dataSize = this.getters.getDataSize;
let questionAmount = dataSize?.questions?.general ?? 0;

const pageAmount = Math.ceil(questionAmount / 8);
const amount = questionAmount % 8;
Expand All @@ -1367,7 +1382,10 @@ const actions = {
});

const sizeSnap = await db.collection("data-size").get();

if(sizeSnap.empty){
commit("setLoading", false);
return;
}
const document = sizeSnap.docs[0];
const general = document.data().questions.general;
const sub = question.subject;
Expand All @@ -1393,6 +1411,7 @@ const actions = {
.where("name", "==", question.subject)
.get();

if(subSnap.empty) return;
const doc = subSnap.docs[0];

if (!doc.data().questions.find(q => q.name === question.name)) {
Expand Down Expand Up @@ -1440,7 +1459,7 @@ const actions = {
* @param {string} payload the question name.
* @returns {Question} an object that represents the question data.
*/
async getQuestionByName(_, payload) {
async getQuestionByName({ commit }, payload) {
try {
const snapshot = await db
.collection("questions")
Expand Down