forked from wednesday-solutions/react-graphql-ts-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.tsx
203 lines (181 loc) · 5.74 KB
/
index.tsx
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
import React, { useEffect, memo, ChangeEvent } from 'react';
import PropTypes from 'prop-types';
import { connect } from 'react-redux';
import { createStructuredSelector } from 'reselect';
import { AnyAction, compose } from 'redux';
import debounce from 'lodash-es/debounce';
import isEmpty from 'lodash-es/isEmpty';
import { SearchOutlined } from '@ant-design/icons';
import styled from 'styled-components';
import { injectSaga } from 'redux-injectors';
import { Button, Input, Select } from 'antd';
import { selectLaunchData, selectLaunchListError, selectLoading } from './selectors';
import arrowUp from '@images/ArrowUp.svg';
import arrowDown from '@images/ArrowDown.svg';
import arrowUpDown from '@images/ArrowUpDown.svg';
import homeContainerSaga from './saga';
import { requestGetLaunchList } from './reducer';
import { LaunchList, ErrorHandler } from '@components';
import { colors, media } from '@app/themes';
import { injectIntl } from 'react-intl';
import useSort from './useSort';
import usePaginate from './usePaginate';
import { setQueryParam } from '@app/utils';
import history from '@app/utils/history';
import { RequestLaunchesActionPayload, HomeContainerProps } from './types';
const Container = styled.div`
&& {
display: flex;
flex-direction: column;
margin: 0 auto;
padding: 1rem;
background-color: ${colors.secondaryText};
}
`;
const CustomHeader = styled.div`
display: flex;
flex-direction: column;
gap: 0.5rem;
width: 100%;
${media.greaterThan('tablet')`
flex-direction: row;
`}
`;
const CustomSearch = styled(Input)`
&& {
.ant-input {
padding-left: 0.5rem;
}
}
`;
const ButtonBox = styled.div`
display: flex;
gap: 0.5rem;
`;
const SortSelect = styled(Select)`
&& {
width: 9.5rem;
background-color: #fff;
.ant-select-selection-item {
color: ${colors.secondaryText};
}
}
`;
const CustomFooter = styled.div`
display: flex;
justify-content: flex-end;
gap: 1rem;
`;
export function HomeContainer({ dispatchLaunchList, loading, launchData, intl, launchListError }: HomeContainerProps) {
const { order, handleClearSort, handleDateSort } = useSort();
const { page, hasNextPage, hasPrevPage, handleNext, handlePrev, resetPage } = usePaginate(launchData);
const missionName = new URLSearchParams(history.location.search).get('mission_name');
const setMissionName = (missionName: string) => setQueryParam({ param: 'mission_name', value: missionName });
const clearMissionName = () => setQueryParam({ param: 'mission_name', deleteParam: true });
useEffect(() => {
dispatchLaunchList({ missionName, order, page });
}, []);
useEffect(() => {
if (launchData.launches && !launchData.launches.length && page !== 1) {
resetPage();
}
}, [launchData]);
const handleSearch = debounce((e: ChangeEvent<HTMLInputElement>) => {
const missionSearch = e.target.value;
if (!isEmpty(missionSearch)) {
setMissionName(missionSearch);
} else {
clearMissionName();
}
}, 300);
const prefix = (
<SearchOutlined
style={{
fontSize: 22,
color: 'black'
}}
/>
);
return (
<Container>
<CustomHeader>
<CustomSearch
prefix={prefix}
data-testid="search-bar"
type="text"
placeholder={intl.formatMessage({ id: 'placeholder_text' })}
defaultValue={missionName || ''}
onChange={handleSearch}
autoFocus
/>
<ButtonBox>
<Button disabled={!order} onClick={handleClearSort} data-testid="clear-sort">
CLEAR SORT
</Button>
<SortSelect
data-testid="sort-select"
id="sort-select"
suffixIcon={
order === 'asc' ? (
<img src={arrowUp} alt="chevron-up" />
) : order === 'desc' ? (
<img src={arrowDown} alt="chevron-down" />
) : (
<img src={arrowUpDown} alt="chevron-up-down" />
)
}
value={order || 'default'}
onChange={handleDateSort as any}
>
<Select.Option value="default" disabled>
SORT BY DATE
</Select.Option>
<Select.Option value="desc">DESC</Select.Option>
<Select.Option value="asc">ASC</Select.Option>
</SortSelect>
</ButtonBox>
</CustomHeader>
<LaunchList launchData={launchData} loading={loading} />
<ErrorHandler loading={loading} launchListError={launchListError} />
<CustomFooter>
<Button data-testid="prev-btn" type="primary" onClick={handlePrev} disabled={loading || !hasPrevPage}>
PREV
</Button>
<Button data-testid="next-btn" type="primary" onClick={handleNext} disabled={loading || !hasNextPage}>
NEXT
</Button>
</CustomFooter>
</Container>
);
}
HomeContainer.propTypes = {
dispatchLaunchList: PropTypes.func,
launchData: PropTypes.shape({
launches: PropTypes.array
}),
launchListError: PropTypes.string,
history: PropTypes.object,
intl: PropTypes.object
};
HomeContainer.defaultProps = {
launchData: {},
launchListError: null
};
const mapStateToProps = createStructuredSelector({
launchData: selectLaunchData(),
launchListError: selectLaunchListError(),
loading: selectLoading()
});
export function mapDispatchToProps(dispatch: (arg0: AnyAction) => any) {
return {
dispatchLaunchList: (payload: RequestLaunchesActionPayload) => dispatch(requestGetLaunchList(payload))
};
}
const withConnect = connect(mapStateToProps, mapDispatchToProps);
export default compose(
withConnect,
memo,
injectSaga({ key: 'homeContainer', saga: homeContainerSaga }),
injectIntl
)(HomeContainer);
export const HomeContainerTest = HomeContainer;