Skip to content

Commit

Permalink
close #222: db link changes as query changes
Browse files Browse the repository at this point in the history
  • Loading branch information
geoffrey-wu committed Apr 8, 2024
1 parent 6ac64d9 commit 4a52680
Show file tree
Hide file tree
Showing 2 changed files with 133 additions and 28 deletions.
86 changes: 71 additions & 15 deletions client/database/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -716,7 +716,7 @@ function QueryForm() {
setTossupPaginationNumber(tossupPaginationNumber);
setBonusPaginationNumber(bonusPaginationNumber);
}
fetch('/api/query?' + new URLSearchParams({
const params = new URLSearchParams({
queryString,
alternateSubcategories: validAlternateSubcategories,
categories: validCategories,
Expand All @@ -735,7 +735,8 @@ function QueryForm() {
bonusPagination: bonusPaginationNumber,
minYear,
maxYear
})).then(response => {
}).toString();
fetch(`/api/query?${params}`).then(response => {
if (response.status === 400) {
throw new Error('Invalid query');
}
Expand All @@ -752,11 +753,14 @@ function QueryForm() {
count: tossupCount,
questionArray: tossupArray
} = tossups;
setTossupCount(tossupCount);
setTossups(tossupArray);
const {
count: bonusCount,
questionArray: bonusArray
} = bonuses;
const highlightedTossupArray = JSON.parse(JSON.stringify(tossupArray));
const highlightedBonusArray = JSON.parse(JSON.stringify(bonusArray));

// create deep copy to highlight
const highlightedTossupArray = JSON.parse(JSON.stringify(tossupArray));
if (queryString !== '') {
for (let i = 0; i < highlightedTossupArray.length; i++) {
highlightedTossupArray[i] = highlightTossupQuery({
Expand All @@ -767,16 +771,6 @@ function QueryForm() {
queryString
});
}
}
setHighlightedTossups(highlightedTossupArray);
const {
count: bonusCount,
questionArray: bonusArray
} = bonuses;
setBonusCount(bonusCount);
setBonuses(bonusArray);
const highlightedBonusArray = JSON.parse(JSON.stringify(bonusArray));
if (queryString !== '') {
for (let i = 0; i < highlightedBonusArray.length; i++) {
highlightedBonusArray[i] = highlightBonusQuery({
bonus: highlightedBonusArray[i],
Expand All @@ -787,6 +781,11 @@ function QueryForm() {
});
}
}
setTossupCount(tossupCount);
setTossups(tossupArray);
setHighlightedTossups(highlightedTossupArray);
setBonusCount(bonusCount);
setBonuses(bonusArray);
setHighlightedBonuses(highlightedBonusArray);
if (randomize) {
setTossupPaginationLength(1);
Expand All @@ -800,6 +799,15 @@ function QueryForm() {
const endTime = performance.now();
const timeElapsed = ((endTime - startTime) / 1000).toFixed(2);
setQueryTime(timeElapsed);
history.pushState({
tossups,
highlightedTossupArray,
bonuses,
highlightedBonusArray,
timeElapsed,
workingMaxReturnLength,
randomize
}, '', '?' + params);
}).catch(error => {
console.error('Error:', error);
alert('Invalid query. Please check your search parameters and try again.');
Expand Down Expand Up @@ -850,6 +858,54 @@ function QueryForm() {
});
setDifficulties(tempDifficulties);
});
window.addEventListener('popstate', event => {
if (event.state === null) {
setTossupCount(0);
setTossups([]);
setHighlightedTossups([]);
setBonusCount(0);
setBonuses([]);
setHighlightedBonuses([]);
setTossupPaginationLength(1);
setBonusPaginationLength(1);
setTossupPaginationShift(0);
setBonusPaginationShift(0);
return;
}
const {
tossups,
highlightedTossupArray,
bonuses,
highlightedBonusArray,
timeElapsed,
workingMaxReturnLength,
randomize
} = event.state;
const {
count: tossupCount,
questionArray: tossupArray
} = tossups;
const {
count: bonusCount,
questionArray: bonusArray
} = bonuses;
setTossupCount(tossupCount);
setTossups(tossupArray);
setHighlightedTossups(highlightedTossupArray);
setBonusCount(bonusCount);
setBonuses(bonusArray);
setHighlightedBonuses(highlightedBonusArray);
if (randomize) {
setTossupPaginationLength(1);
setBonusPaginationLength(1);
} else {
setTossupPaginationLength(Math.ceil(tossupCount / workingMaxReturnLength));
setBonusPaginationLength(Math.ceil(bonusCount / workingMaxReturnLength));
}
setTossupPaginationShift(paginationShiftLength * Math.floor((tossupPaginationNumber - 1) / paginationShiftLength));
setBonusPaginationShift(paginationShiftLength * Math.floor((bonusPaginationNumber - 1) / paginationShiftLength));
setQueryTime(timeElapsed);
});
}, []);
return /*#__PURE__*/React.createElement("div", null, /*#__PURE__*/React.createElement(CategoryModal, null), /*#__PURE__*/React.createElement("form", {
className: "mt-3",
Expand Down
75 changes: 62 additions & 13 deletions client/database/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -819,7 +819,7 @@ function QueryForm() {
setBonusPaginationNumber(bonusPaginationNumber);
}

fetch('/api/query?' + new URLSearchParams({
const params = new URLSearchParams({
queryString,
alternateSubcategories: validAlternateSubcategories,
categories: validCategories,
Expand All @@ -838,7 +838,9 @@ function QueryForm() {
bonusPagination: bonusPaginationNumber,
minYear,
maxYear,
}))
}).toString();

fetch(`/api/query?${params}`)
.then(response => {
if (response.status === 400) {
throw new Error('Invalid query');
Expand All @@ -852,28 +854,28 @@ function QueryForm() {
const workingMaxReturnLength = Math.max(1, maxReturnLength || 25);

const { count: tossupCount, questionArray: tossupArray } = tossups;
setTossupCount(tossupCount);
setTossups(tossupArray);
const { count: bonusCount, questionArray: bonusArray } = bonuses;

// create deep copy to highlight
const highlightedTossupArray = JSON.parse(JSON.stringify(tossupArray));
const highlightedBonusArray = JSON.parse(JSON.stringify(bonusArray));

// create deep copy to highlight
if (queryString !== '') {
for (let i = 0; i < highlightedTossupArray.length; i++) {
highlightedTossupArray[i] = highlightTossupQuery({ tossup: highlightedTossupArray[i], regExp, searchType, ignoreWordOrder, queryString } );
}
}
setHighlightedTossups(highlightedTossupArray);

const { count: bonusCount, questionArray: bonusArray } = bonuses;
setBonusCount(bonusCount);
setBonuses(bonusArray);

const highlightedBonusArray = JSON.parse(JSON.stringify(bonusArray));
if (queryString !== '') {
for (let i = 0; i < highlightedBonusArray.length; i++) {
highlightedBonusArray[i] = highlightBonusQuery({ bonus: highlightedBonusArray[i], regExp, searchType, ignoreWordOrder, queryString });
}
}

setTossupCount(tossupCount);
setTossups(tossupArray);
setHighlightedTossups(highlightedTossupArray);

setBonusCount(bonusCount);
setBonuses(bonusArray);
setHighlightedBonuses(highlightedBonusArray);

if (randomize) {
Expand All @@ -890,6 +892,8 @@ function QueryForm() {
const endTime = performance.now();
const timeElapsed = ((endTime - startTime) / 1000).toFixed(2);
setQueryTime(timeElapsed);

history.pushState({ tossups, highlightedTossupArray, bonuses, highlightedBonusArray, timeElapsed, workingMaxReturnLength, randomize }, '', '?' + params);
})
.catch(error => {
console.error('Error:', error);
Expand Down Expand Up @@ -936,6 +940,51 @@ function QueryForm() {
});
setDifficulties(tempDifficulties);
});

window.addEventListener('popstate', event => {
if (event.state === null) {
setTossupCount(0);
setTossups([]);
setHighlightedTossups([]);

setBonusCount(0);
setBonuses([]);
setHighlightedBonuses([]);

setTossupPaginationLength(1);
setBonusPaginationLength(1);

setTossupPaginationShift(0);
setBonusPaginationShift(0);

return;
}

const { tossups, highlightedTossupArray, bonuses, highlightedBonusArray, timeElapsed, workingMaxReturnLength, randomize } = event.state;
const { count: tossupCount, questionArray: tossupArray } = tossups;
const { count: bonusCount, questionArray: bonusArray } = bonuses;

setTossupCount(tossupCount);
setTossups(tossupArray);
setHighlightedTossups(highlightedTossupArray);

setBonusCount(bonusCount);
setBonuses(bonusArray);
setHighlightedBonuses(highlightedBonusArray);

if (randomize) {
setTossupPaginationLength(1);
setBonusPaginationLength(1);
} else {
setTossupPaginationLength(Math.ceil(tossupCount / workingMaxReturnLength));
setBonusPaginationLength(Math.ceil(bonusCount / workingMaxReturnLength));
}

setTossupPaginationShift(paginationShiftLength * Math.floor((tossupPaginationNumber - 1) / paginationShiftLength));
setBonusPaginationShift(paginationShiftLength * Math.floor((bonusPaginationNumber - 1) / paginationShiftLength));

setQueryTime(timeElapsed);
});
}, []);

return (
Expand Down

0 comments on commit 4a52680

Please sign in to comment.