generated from wednesday-solutions/react-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
200 lines (186 loc) · 5.43 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
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 { Card, Skeleton, Input } from 'antd';
import styled from 'styled-components';
import { injectIntl } from 'react-intl';
import { useHistory } from 'react-router-dom';
import T from '@components/T';
import Clickable from '@components/Clickable';
import { useInjectSaga } from 'utils/injectSaga';
import { selectHomeContainer, selectReposData, selectReposError, selectRepoName } from './selectors';
import { homeContainerCreators } from './reducer';
import saga from './saga';
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;
`;
export function HomeContainer({
dispatchGithubRepos,
dispatchClearGithubRepos,
intl,
reposData = {},
reposError = null,
repoName,
maxwidth,
padding
}) {
useInjectSaga({ key: 'homeContainer', saga });
const [loading, setLoading] = useState(false);
useEffect(() => {
const loaded = get(reposData, 'items', null) || reposError;
if (loading && loaded) {
setLoading(false);
}
}, [reposData]);
useEffect(() => {
if (repoName && !reposData?.items?.length) {
dispatchGithubRepos(repoName);
setLoading(true);
}
}, []);
const history = useHistory();
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 (
(items.length !== 0 || loading) && (
<CustomCard>
<Skeleton loading={loading} active>
{repoName && (
<div>
<T id="search_query" values={{ repoName }} />
</div>
)}
{totalCount !== 0 && (
<div>
<T id="matching_repos" values={{ totalCount }} />
</div>
)}
{items.map((item, index) => (
<CustomCard key={index}>
<T id="repository_name" values={{ name: item.name }} />
<T id="repository_full_name" values={{ fullName: item.fullName }} />
<T id="repository_stars" values={{ stars: item.stargazersCount }} />
</CustomCard>
))}
</Skeleton>
</CustomCard>
)
);
};
const renderErrorState = () => {
let repoError;
if (reposError) {
repoError = reposError;
} else if (!get(reposData, 'totalCount', 0)) {
repoError = 'respo_search_default';
}
return (
!loading &&
repoError && (
<CustomCard color={reposError ? 'red' : 'grey'} title={intl.formatMessage({ id: 'repo_list' })}>
<T id={repoError} />
</CustomCard>
)
);
};
const refreshPage = () => {
history.push('stories');
window.location.reload();
};
return (
<Container maxwidth={maxwidth} padding={padding}>
<RightContent>
<Clickable textId="stories" onClick={refreshPage} />
</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.object,
repoName: PropTypes.string,
history: PropTypes.object,
maxwidth: PropTypes.number,
padding: PropTypes.number
};
HomeContainer.defaultProps = {
maxwidth: 500,
padding: 20
};
const mapStateToProps = createStructuredSelector({
homeContainer: selectHomeContainer(),
reposData: selectReposData(),
reposError: selectReposError(),
repoName: selectRepoName()
});
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
)(HomeContainer);
export const HomeContainerTest = compose(injectIntl)(HomeContainer);