From 38d926820cd65e1e7c3541e256f44a4e39f33a11 Mon Sep 17 00:00:00 2001 From: canove Date: Wed, 30 Sep 2020 20:00:17 -0300 Subject: [PATCH] improvement: trim search params on contacts and tickets filters --- .../services/AuthServices/RefreshTokenService.ts | 2 -- .../ContactServices/ListContactsService.ts | 4 ++-- .../services/TicketServices/ListTicketsService.ts | 10 ++++++---- frontend/src/components/NewTicketModal/index.js | 15 +++++++++++++-- frontend/src/translate/languages/en.js | 7 ++++--- frontend/src/translate/languages/pt.js | 1 + 6 files changed, 26 insertions(+), 13 deletions(-) diff --git a/backend/src/services/AuthServices/RefreshTokenService.ts b/backend/src/services/AuthServices/RefreshTokenService.ts index 216c459..8b96cfe 100644 --- a/backend/src/services/AuthServices/RefreshTokenService.ts +++ b/backend/src/services/AuthServices/RefreshTokenService.ts @@ -20,8 +20,6 @@ interface Response { export const RefreshTokenService = async (token: string): Promise => { let decoded; - console.log(token); - try { decoded = verify(token, authConfig.refreshSecret); } catch (err) { diff --git a/backend/src/services/ContactServices/ListContactsService.ts b/backend/src/services/ContactServices/ListContactsService.ts index 52c7f8a..eeb4128 100644 --- a/backend/src/services/ContactServices/ListContactsService.ts +++ b/backend/src/services/ContactServices/ListContactsService.ts @@ -22,10 +22,10 @@ const ListContactsService = async ({ name: Sequelize.where( Sequelize.fn("LOWER", Sequelize.col("name")), "LIKE", - `%${searchParam.toLowerCase()}%` + `%${searchParam.toLowerCase().trim()}%` ) }, - { number: { [Op.like]: `%${searchParam}%` } } + { number: { [Op.like]: `%${searchParam.toLowerCase().trim()}%` } } ] }; const limit = 20; diff --git a/backend/src/services/TicketServices/ListTicketsService.ts b/backend/src/services/TicketServices/ListTicketsService.ts index e474339..7351102 100644 --- a/backend/src/services/TicketServices/ListTicketsService.ts +++ b/backend/src/services/TicketServices/ListTicketsService.ts @@ -55,6 +55,8 @@ const ListTicketsService = async ({ } if (searchParam) { + const sanitizedSearchParam = searchParam.toLocaleLowerCase().trim(); + includeCondition = [ ...includeCondition, { @@ -65,7 +67,7 @@ const ListTicketsService = async ({ body: where( fn("LOWER", col("body")), "LIKE", - `%${searchParam.toLowerCase()}%` + `%${sanitizedSearchParam}%` ) }, required: false, @@ -79,15 +81,15 @@ const ListTicketsService = async ({ "$contact.name$": where( fn("LOWER", col("name")), "LIKE", - `%${searchParam.toLowerCase()}%` + `%${sanitizedSearchParam}%` ) }, - { "$contact.number$": { [Op.like]: `%${searchParam}%` } }, + { "$contact.number$": { [Op.like]: `%${sanitizedSearchParam}%` } }, { "$message.body$": where( fn("LOWER", col("body")), "LIKE", - `%${searchParam.toLowerCase()}%` + `%${sanitizedSearchParam}%` ) } ] diff --git a/frontend/src/components/NewTicketModal/index.js b/frontend/src/components/NewTicketModal/index.js index 1a6a4a8..77e6872 100644 --- a/frontend/src/components/NewTicketModal/index.js +++ b/frontend/src/components/NewTicketModal/index.js @@ -9,7 +9,9 @@ import Dialog from "@material-ui/core/Dialog"; import DialogActions from "@material-ui/core/DialogActions"; import DialogContent from "@material-ui/core/DialogContent"; import DialogTitle from "@material-ui/core/DialogTitle"; -import Autocomplete from "@material-ui/lab/Autocomplete"; +import Autocomplete, { + createFilterOptions, +} from "@material-ui/lab/Autocomplete"; import CircularProgress from "@material-ui/core/CircularProgress"; import { green } from "@material-ui/core/colors"; @@ -39,6 +41,12 @@ const useStyles = makeStyles(theme => ({ }, })); +const filterOptions = createFilterOptions({ + matchFrom: "start", + trim: true, + stringify: option => option.name, +}); + const NewTicketModal = ({ modalOpen, onClose }) => { const history = useHistory(); const classes = useStyles(); @@ -73,6 +81,8 @@ const NewTicketModal = ({ modalOpen, onClose }) => { return () => clearTimeout(delayDebounceFn); }, [searchParam, modalOpen]); + console.log(options); + const handleClose = () => { onClose(); setSearchParam(""); @@ -121,7 +131,8 @@ const NewTicketModal = ({ modalOpen, onClose }) => { setSelectedContact(newValue); }} options={options} - noOptionsText="No contacts found. Try another term." + filterOptions={filterOptions} + noOptionsText={i18n.t("newTicketModal.noOptions")} loading={loading} renderInput={params => (