Skip to content

Commit

Permalink
#68 Renamed field to common format. Added cancel catching.
Browse files Browse the repository at this point in the history
  • Loading branch information
artzub committed Dec 29, 2020
1 parent 4485b0a commit 68a99b5
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 18 deletions.
6 changes: 3 additions & 3 deletions src/components/Header/components/UserStep/Body.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ const Body = () => {
const inputRef = useRef();
const [search, setSearch] = useState('');
const [neverChange, setNeverChange] = useState(true);
const { isFetching, searched, top } = useSelector(slice.selectors.getState);
const { isFetching, items, top } = useSelector(slice.selectors.getState);
const [bodyOpen, setBodyOpen] = useUIProperty('bodyOpen');

const onChange = useCallback(
Expand Down Expand Up @@ -126,8 +126,8 @@ const Body = () => {
dense
subheader={SearchHeader}
>
{!searched.length && <NotData />}
{(searched || []).map((user) => (
{!items.length && <NotData />}
{(items || []).map((user) => (
<ListItem
alignItems="center"
key={user.login}
Expand Down
22 changes: 13 additions & 9 deletions src/components/Header/components/UserStep/Header.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -80,34 +80,38 @@ const PropertyValue = styled.div`
`;

const Header = (props) => {
const { profile } = useSelector(slice.selectors.getState);
const { selected } = useSelector(slice.selectors.getState);
const {
avatar_url, name, login,
html_url, public_repos, blog,
} = selected || {};

return (
<Container {...props}>
<Avatar src={profile?.avatar_url} />
<Avatar src={avatar_url} />
<InfoContainer>
{!profile && <div>Find a user</div>}
{profile && (
{!selected && <div>Find a user</div>}
{selected && (
<React.Fragment>
<Title>{profile.name || profile.login}</Title>
<Title>{name || login}</Title>
<Properties>
<Property>
<GithubIcon size={16} />
<PropertyValue>
<Link href={profile.html_url}>{profile.login}</Link>
<Link target="_blank" href={html_url}>{login}</Link>
</PropertyValue>
</Property>
</Properties>
<Properties>
<Property title="Amount of repositories">
<SourceRepositoriesIcon size={16} />
<PropertyValue>{profile.public_repos}</PropertyValue>
<PropertyValue>{public_repos}</PropertyValue>
</Property>
{profile.blog && (
{blog && (
<Property title="Web site">
<LinkVariantIcon size={16} />
<PropertyValue>
<Link href={profile.blog}>blog</Link>
<Link target="_blank" href={blog}>blog</Link>
</PropertyValue>
</Property>
)}
Expand Down
23 changes: 17 additions & 6 deletions src/redux/modules/profiles.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import { getProfile, searchAccount } from '@/redux/api/github';
import { createSlice, startFetching, stopFetching } from "@/redux/utils";
import { call, put } from 'redux-saga/effects';
import { call, cancelled, put } from 'redux-saga/effects';

const initialState = {
isFetching: false,
profile: null,
searched: [],
selected: null,
items: [],
top: [],
error: null,
};

const setProfile = (state, profile) => {
state.profile = profile;
const setProfile = (state, selected) => {
state.selected = selected;
};

export default createSlice({
Expand All @@ -31,14 +31,16 @@ export default createSlice({
search: startFetching,
searchSuccess: (state, { payload }) => {
stopFetching(state);
state.searched = Array.isArray(payload) ? payload : [];
state.items = Array.isArray(payload) ? payload : [];
},
fetchTop: startFetching,
fetchTopSuccess: (state, { payload }) => {
stopFetching(state);
state.top = Array.isArray(payload) ? payload : [];
},

stopFetching,

fail: (state, { payload: { message } }) => {
stopFetching(state);
state.error = message;
Expand All @@ -53,6 +55,9 @@ export default createSlice({
yield put(actions.fetchProfileSuccess(data));
} catch (error) {
yield put(actions.fail(error));
if (yield cancelled()) {
yield put(actions.stopFetching());
}
}
},
},
Expand All @@ -64,6 +69,9 @@ export default createSlice({
yield put(actions.searchSuccess(data));
} catch (error) {
yield put(actions.fail(error));
if (yield cancelled()) {
yield put(actions.stopFetching());
}
}
},
},
Expand All @@ -75,6 +83,9 @@ export default createSlice({
yield put(actions.fetchTopSuccess(data));
} catch (error) {
yield put(actions.fail(error));
if (yield cancelled()) {
yield put(actions.stopFetching());
}
}
},
},
Expand Down

0 comments on commit 68a99b5

Please sign in to comment.