-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathSearchPage.tsx
More file actions
232 lines (219 loc) · 7.23 KB
/
Copy pathSearchPage.tsx
File metadata and controls
232 lines (219 loc) · 7.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
import { useConfig } from '@city-of-helsinki/react-helsinki-headless-cms';
import type { QueryEventListArgs } from '@events-helsinki/components';
import {
LoadingSpinner,
SrOnly,
useIsSmallScreen,
useSearchTranslation,
getLargeEventCardId,
useEventListQuery,
BasicMeta,
MAIN_CONTENT_ID,
useCmsRoutedAppHelper,
EventsOrderBySelect,
DEFAULT_EVENT_SORT_OPTION,
isEventSortOption,
EventList,
useClearClosedEventsFromApolloCache,
HELSINKI_OCD_DIVISION_ID,
EVENT_SEARCH_FILTERS,
} from '@events-helsinki/components';
import type { SelectCustomTheme } from 'hds-react';
import { ButtonVariant } from 'hds-react';
import { useRouter } from 'next/router';
import queryString from 'query-string';
import React from 'react';
import { scroller } from 'react-scroll';
import { toast } from 'react-toastify';
import { ROUTES } from '../../../constants';
import AppConfig from '../../app/AppConfig';
import styles from './eventSearchPage.module.scss';
import SearchResultsContainer from './searchResultList/SearchResultsContainer';
import { getEventSearchVariables, getEventUrl, getNextPage } from './utils';
const useSearchQuery = () => {
const router = useRouter();
const eventFilters = React.useMemo(() => {
const searchParams = new URLSearchParams(
queryString.stringify(router.query)
);
const sortParam = searchParams.get('sort');
const variables: QueryEventListArgs = getEventSearchVariables({
include: AppConfig.eventSearchQueryIncludeParamValue,
pageSize: AppConfig.pageSize,
params: searchParams,
sortOrder: isEventSortOption(sortParam)
? sortParam
: DEFAULT_EVENT_SORT_OPTION,
// Always filter with HELSINKI_OCD_DIVISION_ID to limit the results to city of Helsinki events.
// NOTE: This is not needed if using any `*Ongoing` -filter as
// they automatically limit the results to city of Helsinki events.
[EVENT_SEARCH_FILTERS.DIVISIONS]: [HELSINKI_OCD_DIVISION_ID],
// Don't use superEventType when experimenting
// LIIKUNTA-512 (https://helsinkisolutionoffice.atlassian.net/browse/LIIKUNTA-512)
// superEventType: ['umbrella', 'none']
// Only the course type search should use this param;
// LIIKUNTA-512 (https://helsinkisolutionoffice.atlassian.net/browse/LIIKUNTA-512)
// superEvent: 'none',
});
return variables;
}, [router.query]);
return useEventListQuery({
ssr: false,
variables: eventFilters,
});
};
const SearchPage: React.FC<{
SearchComponent: React.FC<{
scrollToResultList: () => void;
'data-testid'?: string;
}>;
}> = ({ SearchComponent }) => {
const { t } = useSearchTranslation();
const router = useRouter();
const routerHelper = useCmsRoutedAppHelper();
const [isFetchingMore, setIsFetchingMore] = React.useState(false);
const isSmallScreen = useIsSmallScreen();
const { meta } = useConfig();
const {
data: eventsData,
fetchMore,
loading: isLoadingEvents,
} = useSearchQuery();
const eventsList = eventsData?.eventList;
// Clear the cache from the events of the past
useClearClosedEventsFromApolloCache(eventsData);
const handleLoadMore = async () => {
const page = eventsData?.eventList.meta
? getNextPage(eventsData.eventList.meta)
: null;
setIsFetchingMore(true);
if (page) {
try {
await fetchMore({
variables: {
page,
},
});
} catch (e) {
// eslint-disable-next-line no-console
console.error('handleLoadMore', 'error in fetchMore', e);
toast.error(t('search:errorLoadMore'));
}
}
setIsFetchingMore(false);
};
const scrollToResultList = () => {
if (isSmallScreen) {
scroller.scrollTo('resultList', {
delay: 0,
duration: 1000,
offset: -50,
smooth: true,
});
}
};
const scrollToEventCard = (id: string) => {
scroller.scrollTo(id, {
delay: 0,
duration: 300,
offset: -50,
smooth: true,
});
};
React.useEffect(() => {
if (router.asPath && router.query?.scrollToResults) {
scrollToResultList();
} else if (router.query?.eventId) {
scrollToEventCard(
getLargeEventCardId(
Array.isArray(router.query.eventId)
? router.query.eventId[0]
: router.query.eventId
)
);
routerHelper.removeQueryParamsFromRouter(
router,
['eventId'],
ROUTES.SEARCH
);
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
return (
<div>
<BasicMeta
appleTouchIconUrl={meta?.appleTouchIconUrl}
favIconUrl={meta?.favIconUrl}
favIconSvgUrl={meta?.favIconSvgUrl}
manifestUrl={meta?.manifestUrl}
/>
<SearchComponent
scrollToResultList={scrollToResultList}
data-testid="searchContainer"
/>
<main id={MAIN_CONTENT_ID}>
<div
className={styles.resultList}
id="resultList"
data-testid="resultList"
>
<SrOnly aria-live="polite" aria-atomic={true}>
{isLoadingEvents || isFetchingMore
? t('search:ariaLiveLoading')
: t('search:ariaLiveSearchReady', {
totalCount: eventsList?.meta.count,
shownCount: eventsList?.data.length,
})}
</SrOnly>
<LoadingSpinner
className={styles.spinner}
isLoading={!isFetchingMore && isLoadingEvents}
>
{eventsList && (
<SearchResultsContainer
eventsCount={eventsList.meta.count}
loading={isLoadingEvents}
eventList={
<EventList
cardSize="large"
events={eventsList.data}
hasNext={!!eventsList.meta.next}
count={eventsList.meta.count}
loading={isFetchingMore}
onLoadMore={handleLoadMore}
getEventUrl={(event, router, locale) =>
getEventUrl(event, router, locale)
}
showEnrolmentStatusInCardDetails={
AppConfig.showEnrolmentStatusInCardDetails
}
loadMoreButtonVariant={ButtonVariant.Success}
/>
}
orderBySelectComponent={
<EventsOrderBySelect
theme={
{
'--menu-item-background-color':
'var(--color-input-dark)',
'--menu-item-background-color-hover':
'var(--color-input-light)',
'--menu-item-background-color-selected':
'var(--color-input-dark)',
'--menu-item-background-color-selected-hover':
'var(--color-input-dark)',
'--menu-item-color-selected-hover':
'var(--color-white)',
} as SelectCustomTheme
}
/>
}
/>
)}
</LoadingSpinner>
</div>
</main>
</div>
);
};
export default SearchPage;