-
Notifications
You must be signed in to change notification settings - Fork 92
/
index.js
260 lines (241 loc) · 7.71 KB
/
index.js
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
import React, { useEffect, memo } from 'react';
import PropTypes from 'prop-types';
import { connect } from 'react-redux';
import { createStructuredSelector } from 'reselect';
import { compose } from 'redux';
import get from 'lodash/get';
import debounce from 'lodash/debounce';
import isEmpty from 'lodash/isEmpty';
import styled from '@emotion/styled';
import { injectSaga } from 'redux-injectors';
import { Card, IconButton, Skeleton, InputAdornment, OutlinedInput, CardHeader, Divider } from '@mui/material';
import { Search as SearchIcon } from '@mui/icons-material';
import { useHistory } from 'react-router-dom';
import T from '@components/T';
import { If } from '@components/If';
import { For } from '@components/For';
import { RepoCard } from '@components/RepoCard';
import colors from '@app/themes/colors';
import { selectLoading, selectReposData, selectReposError, selectRepoName } from './selectors';
import { homeContainerCreators } from './reducer';
import homeContainerSaga from './saga';
import { translate } from '@app/utils';
const CustomCard = styled(Card)`
&& {
margin: 1.25rem 0;
padding: 1rem;
max-width: ${(props) => props.maxwidth};
color: ${(props) => props.color};
${(props) => props.color && `color: ${props.color}`};
}
`;
const CustomCardHeader = styled(CardHeader)`
&& {
padding: 0;
}
`;
const Container = styled.div`
&& {
display: flex;
flex-direction: column;
max-width: ${(props) => props.maxwidth}px;
width: 100%;
margin: 0 auto;
padding: ${(props) => props.padding}px;
}
`;
const RightContent = styled.div`
display: flex;
align-self: flex-end;
`;
const StyledT = styled(T)`
&& {
color: ${colors.gotoStories};
}
`;
const StyledOutlinedInput = styled(OutlinedInput)`
legend {
display: none;
}
> fieldset {
top: 0;
}
`;
/**
* HomeContainer component that handles the logic for searching and displaying GitHub repositories.
* It includes input handling, loading state management, and rendering of the repository list or error state.
*
* @date 01/03/2024 - 14:47:28
*
* @param {Object} props - The component props.
* @param {Function} props.dispatchGithubRepos - Function to dispatch the action for fetching GitHub repositories.
* @param {Function} props.dispatchClearGithubRepos - Function to dispatch the action for clearing the GitHub repositories data.
* @param {Object} props.reposData - The data of the repositories fetched.
* @param {Object} props.reposError - The error object if there is an error fetching the repositories.
* @param {string} props.repoName - The name of the repository to search for.
* @param {string} props.maxwidth - The maximum width of the component.
* @param {string} props.padding - The padding of the component.
* @returns {JSX.Element} The HomeContainer component.
*/
export function HomeContainer({
dispatchGithubRepos,
dispatchClearGithubRepos,
reposData,
reposError,
repoName,
maxwidth,
padding,
loading
}) {
const history = useHistory();
useEffect(() => {
if (repoName && !reposData?.items?.length) {
dispatchGithubRepos(repoName);
}
}, []);
const searchRepos = (rName) => {
dispatchGithubRepos(rName);
};
const handleOnChange = (rName) => {
if (!isEmpty(rName)) {
searchRepos(rName);
} else {
dispatchClearGithubRepos();
}
};
const debouncedHandleOnChange = debounce(handleOnChange, 200);
const handleStoriesClick = () => {
history.push('/stories');
window.location.reload();
};
return (
<Container maxwidth={maxwidth} padding={padding}>
<RightContent>
<StyledT onClick={handleStoriesClick} data-testid="redirect" id="stories" />
</RightContent>
<CustomCard maxwidth={maxwidth}>
<CustomCardHeader title={translate('repo_search')} />
<Divider sx={{ mb: 1.25 }} light />
<T marginBottom={10} id="get_repo_details" />
<StyledOutlinedInput
inputProps={{ 'data-testid': 'search-bar' }}
onChange={(event) => debouncedHandleOnChange(event.target.value)}
fullWidth
defaultValue={repoName}
placeholder={translate('default_template')}
endAdornment={
<InputAdornment position="end">
<IconButton
data-testid="search-icon"
aria-label="search repos"
type="button"
onClick={() => searchRepos(repoName)}
>
<SearchIcon />
</IconButton>
</InputAdornment>
}
/>
</CustomCard>
{renderRepoList(reposData, loading, repoName)}
{renderErrorState(repoName, loading, reposError)}
</Container>
);
}
const renderSkeleton = () => {
return (
<>
<Skeleton data-testid="skeleton" animation="wave" variant="text" height={40} />
<Skeleton data-testid="skeleton" animation="wave" variant="text" height={40} />
<Skeleton data-testid="skeleton" animation="wave" variant="text" height={40} />
</>
);
};
const renderRepoList = (reposData, loading, repoName) => {
const items = get(reposData, 'items', []);
const totalCount = get(reposData, 'totalCount', 0);
return (
<If condition={!isEmpty(items) || loading}>
<CustomCard>
<If condition={!loading} otherwise={renderSkeleton()}>
<>
<If condition={!isEmpty(repoName)}>
<div>
<T id="search_query" values={{ repoName }} />
</div>
</If>
<If condition={totalCount !== 0}>
<div>
<T id="matching_repos" values={{ totalCount }} />
</div>
</If>
<For
of={items}
ParentComponent={Container}
renderItem={(item, index) => <RepoCard key={index} {...item} />}
/>
</>
</If>
</CustomCard>
</If>
);
};
const renderErrorState = (repoName, loading, reposError) => {
let repoError;
let messageId;
if (reposError) {
repoError = reposError;
messageId = 'error-message';
} else if (isEmpty(repoName)) {
repoError = 'repo_search_default';
messageId = 'default-message';
}
return (
<If condition={!loading && repoError}>
<CustomCard color={reposError ? 'red' : 'grey'}>
<CustomCardHeader title={translate('repo_list')} />
<Divider sx={{ mb: 1.25 }} light />
<T data-testid={messageId} id={repoError} text={repoError} />
</CustomCard>
</If>
);
};
HomeContainer.propTypes = {
dispatchGithubRepos: PropTypes.func,
dispatchClearGithubRepos: PropTypes.func,
intl: PropTypes.object,
reposData: PropTypes.shape({
totalCount: PropTypes.number,
incompleteResults: PropTypes.bool,
items: PropTypes.array
}),
reposError: PropTypes.string,
repoName: PropTypes.string,
history: PropTypes.object,
maxwidth: PropTypes.number,
padding: PropTypes.number,
loading: PropTypes.bool
};
HomeContainer.defaultProps = {
maxwidth: 500,
padding: 20,
reposData: {},
reposError: null
};
const mapStateToProps = createStructuredSelector({
loading: selectLoading(),
reposData: selectReposData(),
reposError: selectReposError(),
repoName: selectRepoName()
});
// eslint-disable-next-line require-jsdoc
export function mapDispatchToProps(dispatch) {
const { requestGetGithubRepos, clearGithubRepos } = homeContainerCreators;
return {
dispatchGithubRepos: (repoName) => dispatch(requestGetGithubRepos(repoName)),
dispatchClearGithubRepos: () => dispatch(clearGithubRepos())
};
}
const withConnect = connect(mapStateToProps, mapDispatchToProps);
export default compose(withConnect, memo, injectSaga({ key: 'homeContainer', saga: homeContainerSaga }))(HomeContainer);
export const HomeContainerTest = compose()(HomeContainer);