-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
208 lines (192 loc) · 5.64 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
import React, { useEffect, memo, useState } 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 'styled-components';
import { injectIntl } from 'react-intl';
import { injectSaga } from 'redux-injectors';
import { Card, Skeleton, Input } from 'antd';
import T from '@components/T';
import If from '@components/If';
import For from '@app/components/For';
import colors from '@app/themes/colors';
import RepoCard from '@app/components/RepoCard';
import { selectReposData, selectReposError, selectRepoName } from './selectors';
import { homeContainerCreators } from './reducer';
import homeContainerSaga from './saga';
import { useHistory } from 'react-router-dom';
const { Search } = Input;
const CustomCard = styled(Card)`
&& {
margin: 20px 0;
max-width: ${(props) => props.maxwidth};
color: ${(props) => props.color};
${(props) => props.color && `color: ${props.color}`};
}
`;
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};
}
`;
export function HomeContainer({
dispatchGithubRepos,
dispatchClearGithubRepos,
intl,
reposData,
reposError,
repoName,
maxwidth,
padding
}) {
const [loading, setLoading] = useState(false);
const history = useHistory();
useEffect(() => {
const loaded = get(reposData, 'items', null) || reposError;
if (loaded) {
setLoading(false);
}
}, [reposData]);
useEffect(() => {
if (repoName && !reposData?.items?.length) {
dispatchGithubRepos(repoName);
setLoading(true);
}
}, []);
const handleOnChange = (rName) => {
if (!isEmpty(rName)) {
dispatchGithubRepos(rName);
setLoading(true);
} else {
dispatchClearGithubRepos();
}
};
const debouncedHandleOnChange = debounce(handleOnChange, 200);
const renderRepoList = () => {
const items = get(reposData, 'items', []);
const totalCount = get(reposData, 'totalCount', 0);
return (
<If condition={!isEmpty(items) || loading}>
<CustomCard>
<Skeleton loading={loading} active>
<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} />}
/>
</Skeleton>
</CustomCard>
</If>
);
};
const renderErrorState = () => {
let repoError;
if (reposError) {
repoError = reposError;
} else if (isEmpty(repoName)) {
repoError = 'repo_search_default';
}
return (
!loading &&
repoError && (
<CustomCard color={reposError ? 'red' : 'grey'} title={intl.formatMessage({ id: 'repo_list' })}>
<If condition={reposError} otherwise={<T data-testid="default-message" id={repoError} />}>
<T data-testid="error-message" text={reposError} />
</If>
</CustomCard>
)
);
};
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 title={intl.formatMessage({ id: 'repo_search' })} maxwidth={maxwidth}>
<T marginBottom={10} id="get_repo_details" />
<Search
data-testid="search-bar"
defaultValue={repoName}
type="text"
onChange={(evt) => debouncedHandleOnChange(evt.target.value)}
onSearch={(searchText) => debouncedHandleOnChange(searchText)}
/>
</CustomCard>
{renderRepoList()}
{renderErrorState()}
</Container>
);
}
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
};
HomeContainer.defaultProps = {
maxwidth: 500,
padding: 20,
reposData: {},
reposError: null
};
const mapStateToProps = createStructuredSelector({
reposData: selectReposData(),
reposError: selectReposError(),
repoName: selectRepoName()
});
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(
injectIntl,
withConnect,
memo,
injectSaga({ key: 'homeContainer', saga: homeContainerSaga })
)(HomeContainer);
export const HomeContainerTest = compose(injectIntl)(HomeContainer);