From e179dafe70a91acc4bffb6558e9205ad630ad006 Mon Sep 17 00:00:00 2001 From: tejusk2 <92613406+tejusk2@users.noreply.github.com> Date: Thu, 28 Jul 2022 10:21:10 -0500 Subject: [PATCH 1/2] search fix --- client/package.json | 1 + client/src/containers/Buffet.jsx | 12 ++++++++---- client/src/containers/Search.jsx | 20 +++++++++++++++++--- client/src/services/fields.js | 9 ++++----- package-lock.json | 25 ++++++++++++++++++++++++- 5 files changed, 54 insertions(+), 13 deletions(-) diff --git a/client/package.json b/client/package.json index 8ca9794..c5869e6 100644 --- a/client/package.json +++ b/client/package.json @@ -8,6 +8,7 @@ "@emotion/styled": "^11.9.3", "@mui/icons-material": "^5.8.4", "@mui/material": "^5.9.1", + "acorn": "^8.8.0", "crypto-js": "^4.1.1", "dotenv": "^16.0.1", "formik": "^2.2.9", diff --git a/client/src/containers/Buffet.jsx b/client/src/containers/Buffet.jsx index 96179a6..d597ff7 100644 --- a/client/src/containers/Buffet.jsx +++ b/client/src/containers/Buffet.jsx @@ -15,12 +15,15 @@ import { searchQuestions } from 'services/questionsServices'; const recent = {}; const best = { sort: 'u' }; -const interesting = { - match: { answers: 0 }, + +const interesting = { match: JSON.stringify({ 'answers': 0 }), sort: 'uvc' } + +/*const interesting = { + match: { answers : 0 }, sort: 'uvc', -}; +};*/ const hot = { - match: { hasAcceptedAnswer: false }, + match: JSON.stringify({ hasAcceptedAnswer: false }), sort: 'uvac', }; const sortObjArr = [recent, best, interesting, hot]; @@ -31,6 +34,7 @@ export default function Buffet() { const [sort, setSort] = useState(0); const getData = async () => { + const { error, questions } = await searchQuestions(sortObjArr[sort]); if (error) { setError(error); diff --git a/client/src/containers/Search.jsx b/client/src/containers/Search.jsx index 38c43c6..e230d18 100644 --- a/client/src/containers/Search.jsx +++ b/client/src/containers/Search.jsx @@ -10,12 +10,26 @@ export default function Search() { const [searchParams] = useSearchParams() const getData = async () => { - const createdAt = searchParams.get('createdAt'); + let matchObj = {}; + + const newdate = new Date(searchParams.get('createdAt')); const creator = searchParams.get('creator'); const text = searchParams.get('text'); const title = searchParams.get('title'); - console.log(new Date(createdAt)) - const { questions } = await searchQuestions({ regexMatch: { creator, text, title } }) + + const time = {} + + time["$gte"] = parseInt(newdate.getTime()); + newdate.setDate(newdate.getDate() + 1) + time["$lte"] = newdate.getTime() + + const createdAt = time + + + + const { questions } = await searchQuestions({ regexMatch: { creator, text, title }, match: {createdAt} } ) + + return questions; } diff --git a/client/src/services/fields.js b/client/src/services/fields.js index 03ae0f3..4d43feb 100644 --- a/client/src/services/fields.js +++ b/client/src/services/fields.js @@ -118,11 +118,10 @@ const searchFields = [ id: 'text', label: 'Text', }, - // { - // id: 'createdAt', - // label: 'Date', - // type: 'date', - // }, + { + id: 'createdAt', + type: 'date', + }, { id: 'creator', label: 'Creator', diff --git a/package-lock.json b/package-lock.json index 4b4adda..f7ec7a8 100644 --- a/package-lock.json +++ b/package-lock.json @@ -21,6 +21,7 @@ "@emotion/styled": "^11.9.3", "@mui/icons-material": "^5.8.4", "@mui/material": "^5.9.1", + "acorn": "^8.8.0", "crypto-js": "^4.1.1", "dotenv": "^16.0.1", "formik": "^2.2.9", @@ -41,6 +42,17 @@ "yup": "^0.32.11" } }, + "client/node_modules/acorn": { + "version": "8.8.0", + "resolved": "https://registry.npmjs.org/acorn/-/acorn-8.8.0.tgz", + "integrity": "sha512-QOxyigPVrpZ2GXT+PFyZTl6TtOFc5egxHIP9IlQ+RbupQuX4RkT/Bee4/kQuC02Xkzg84JcT7oLYtDIQxp+v7w==", + "bin": { + "acorn": "bin/acorn" + }, + "engines": { + "node": ">=0.4.0" + } + }, "node_modules/@ampproject/remapping": { "version": "2.2.0", "license": "Apache-2.0", @@ -5968,7 +5980,8 @@ }, "node_modules/cors": { "version": "2.8.5", - "license": "MIT", + "resolved": "https://registry.npmjs.org/cors/-/cors-2.8.5.tgz", + "integrity": "sha512-KIHbLJqu73RGr/hnbrO9uBeixNGuvSQjul/jdFvS/KFSIH1hWVd1ng7zOHx+YrEfInLG7q4n6GHQ9cDtxv/P6g==", "dependencies": { "object-assign": "^4", "vary": "^1" @@ -23824,6 +23837,8 @@ }, "cors": { "version": "2.8.5", + "resolved": "https://registry.npmjs.org/cors/-/cors-2.8.5.tgz", + "integrity": "sha512-KIHbLJqu73RGr/hnbrO9uBeixNGuvSQjul/jdFvS/KFSIH1hWVd1ng7zOHx+YrEfInLG7q4n6GHQ9cDtxv/P6g==", "requires": { "object-assign": "^4", "vary": "^1" @@ -29446,6 +29461,7 @@ "@emotion/styled": "^11.9.3", "@mui/icons-material": "^5.8.4", "@mui/material": "^5.9.1", + "acorn": "*", "crypto-js": "^4.1.1", "dotenv": "^16.0.1", "formik": "^2.2.9", @@ -29464,6 +29480,13 @@ "superagent-throttle": "^1.0.1", "universal-cookie": "^4.0.4", "yup": "^0.32.11" + }, + "dependencies": { + "acorn": { + "version": "8.8.0", + "resolved": "https://registry.npmjs.org/acorn/-/acorn-8.8.0.tgz", + "integrity": "sha512-QOxyigPVrpZ2GXT+PFyZTl6TtOFc5egxHIP9IlQ+RbupQuX4RkT/Bee4/kQuC02Xkzg84JcT7oLYtDIQxp+v7w==" + } } }, "qs": { From ecb2e9e08160b8ff1a2f0f52c6851a45b855e3df Mon Sep 17 00:00:00 2001 From: tejusk2 <92613406+tejusk2@users.noreply.github.com> Date: Thu, 28 Jul 2022 19:42:16 -0500 Subject: [PATCH 2/2] search fully working --- client/src/containers/Search.jsx | 68 ++++++++++++++++++++++++++------ 1 file changed, 55 insertions(+), 13 deletions(-) diff --git a/client/src/containers/Search.jsx b/client/src/containers/Search.jsx index e230d18..ed304de 100644 --- a/client/src/containers/Search.jsx +++ b/client/src/containers/Search.jsx @@ -5,29 +5,71 @@ import { SearchForm } from 'controllers/FormControllers'; import { PaginatedList } from 'controllers'; import { ListQuestion } from 'components'; import { searchQuestions } from 'services/questionsServices'; +import { useEffect } from 'react'; export default function Search() { - const [searchParams] = useSearchParams() + const [searchParams, setSearchParams] = useSearchParams() + useEffect(()=>{ + setSearchParams() + }, []) + const getData = async () => { - let matchObj = {}; - - const newdate = new Date(searchParams.get('createdAt')); - const creator = searchParams.get('creator'); - const text = searchParams.get('text'); - const title = searchParams.get('title'); + let match = {}; + let regexMatch = {} + let time ={}; + let newdate = searchParams.get('createdAt'); + + if(newdate){ + newdate = new Date(newdate) + time["$gte"] = parseInt(newdate.getTime()); + newdate.setDate(newdate.getDate() + 1) + time["$lte"] = newdate.getTime() + match = JSON.stringify({"createdAt" : time}) + } - const time = {} - time["$gte"] = parseInt(newdate.getTime()); - newdate.setDate(newdate.getDate() + 1) - time["$lte"] = newdate.getTime() + - const createdAt = time + + + if(searchParams.get('creator')){regexMatch["creator"] = searchParams.get('creator')}; + if(searchParams.get('text')){ + const fields = searchParams.get('text'); + let arr = fields.split(" ") + let matchString = ""; + for(var i = 0; i< arr.length; i++){ + matchString += "(" + arr[i] + ")" + if(i < arr.length-1){ + matchString += " | " + } + } + + regexMatch["text"] = matchString + " /gmi"; + }; + if(searchParams.get('title')){ + const fields = searchParams.get('title'); - const { questions } = await searchQuestions({ regexMatch: { creator, text, title }, match: {createdAt} } ) + let arr = fields.split(" ") + let matchString = ""; + + for(var i = 0; i< arr.length; i++){ + matchString += "(" + arr[i] + ")" + if(i < arr.length-1){ + matchString += " | " + } + } + + regexMatch["title"] = matchString + " /gi"; + + }; + + + + + const { questions } = await searchQuestions({ regexMatch, match}) return questions;