From 926ba9a0394c295ff1ccae25152bc349933ba387 Mon Sep 17 00:00:00 2001 From: tongo Date: Mon, 7 Aug 2023 14:28:16 +0300 Subject: [PATCH] search donation wishes --- public/locales/bg/campaigns.json | 3 +- public/locales/en/campaigns.json | 3 +- .../client/campaigns/DonationWishes.tsx | 30 +++++++++++++++---- 3 files changed, 28 insertions(+), 8 deletions(-) diff --git a/public/locales/bg/campaigns.json b/public/locales/bg/campaigns.json index 130c93815..f5e6d863b 100644 --- a/public/locales/bg/campaigns.json +++ b/public/locales/bg/campaigns.json @@ -100,7 +100,8 @@ "title" : "Сортирай:", "date" : "Дата", "amount": "Дарение", - "search": "Търси..." + "search": "Търси...", + "noResults": "Не са открити резултати" }, "gallery": "Галерия", "report-irregularity": "Сигнализирай за злоупотреба", diff --git a/public/locales/en/campaigns.json b/public/locales/en/campaigns.json index 91e58f66e..7bd9d5b95 100644 --- a/public/locales/en/campaigns.json +++ b/public/locales/en/campaigns.json @@ -93,7 +93,8 @@ "title" : "Sort:", "date" : "Date", "amount": "Amount", - "search": "Search..." + "search": "Search...", + "noResults": "No messages found" }, "profile": "Profile:", "status": "Status:", diff --git a/src/components/client/campaigns/DonationWishes.tsx b/src/components/client/campaigns/DonationWishes.tsx index 1467be60b..c1b72d725 100644 --- a/src/components/client/campaigns/DonationWishes.tsx +++ b/src/components/client/campaigns/DonationWishes.tsx @@ -1,4 +1,4 @@ -import React, { useRef, useState } from 'react' +import React, { useMemo, useRef, useState } from 'react' import { useTranslation } from 'next-i18next' @@ -23,6 +23,7 @@ import { useDonationWishesList } from 'common/hooks/donationWish' import { getExactDate } from 'common/util/date' import theme from 'common/theme' import { SortData } from 'gql/types' +import { debounce } from 'lodash' type SortButton = { label: string @@ -39,16 +40,18 @@ export default function DonationWishes({ campaignId, pageSize = 5 }: Props) { const locale = i18n.language == 'bg' ? bg : enUS const titleRef = useRef(null) const [pageIndex, setPageIndex] = useState(0) + const [searchValue, setSearchValue] = useState('') const [sort, setSort] = useState({ sortBy: 'createdAt', sortOrder: 'desc', }) - const { data, isSuccess, refetch } = useDonationWishesList( + const { data, isSuccess } = useDonationWishesList( campaignId, { pageIndex, pageSize }, sort, + searchValue, ) const numOfPages = isSuccess && data ? Math.ceil(data.totalCount / pageSize) : 0 @@ -57,9 +60,14 @@ export default function DonationWishes({ campaignId, pageSize = 5 }: Props) { sortBy: value, sortOrder: sort.sortBy === value ? (sort.sortOrder === 'desc' ? 'asc' : 'desc') : 'desc', })) - refetch() } + const handleSearch = (event: React.ChangeEvent) => { + setSearchValue(event.target.value) + } + + const debounceSearch = useMemo(() => debounce(handleSearch, 300), []) + const handlePageChange = (_e: React.ChangeEvent, page: number) => { // 's impl is 1 index based // Our pagination apis are 0 index based @@ -94,13 +102,18 @@ export default function DonationWishes({ campaignId, pageSize = 5 }: Props) { }}> {t('campaign.messages')} - + {t('campaign.sort.title')} @@ -112,7 +125,6 @@ export default function DonationWishes({ campaignId, pageSize = 5 }: Props) { sx={{ fontSize: theme.typography.pxToRem(14), color: 'rgba(0, 0, 0, 0.54)', - '&:after': { content: '"|"', paddingX: '1rem' }, }}> {label} {sort.sortBy === value && @@ -130,6 +142,7 @@ export default function DonationWishes({ campaignId, pageSize = 5 }: Props) { ), }} variant="outlined" + onChange={debounceSearch} /> @@ -196,6 +209,11 @@ export default function DonationWishes({ campaignId, pageSize = 5 }: Props) { ))} + + {data?.items?.length === 0 && searchValue !== '' && ( + {t('campaign.sort.noResults')} + )} + {numOfPages > 1 && (