Skip to content

Commit 0f261b1

Browse files
committed
12-Implementing the Issues Feature: Client-Side Filter
1 parent 6781b48 commit 0f261b1

File tree

1 file changed

+79
-23
lines changed

1 file changed

+79
-23
lines changed

src/Issue/IssueList/index.js

Lines changed: 79 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import gql from 'graphql-tag';
55
import IssueItem from '../IssueItem';
66
import Loading from '../../Loading';
77
import ErrorMessage from '../../Error';
8+
import { ButtonUnobtrusive } from '../../Button';
89

910
import './style.css';
1011

@@ -27,33 +28,88 @@ const GET_ISSUES_OF_REPOSITORY = gql`
2728
}
2829
`;
2930

30-
const Issues = ({ repositoryOwner, repositoryName }) => (
31-
<Query
32-
query={GET_ISSUES_OF_REPOSITORY}
33-
variables={{
34-
repositoryOwner,
35-
repositoryName,
36-
}}
37-
>
38-
{({ data, loading, error }) => {
39-
if (error) {
40-
return <ErrorMessage error={error} />;
41-
}
31+
const ISSUE_STATES = {
32+
NONE: 'NONE',
33+
OPEN: 'OPEN',
34+
CLOSED: 'CLOSED',
35+
};
4236

43-
const { repository } = data;
37+
const TRANSITION_LABELS = {
38+
[ISSUE_STATES.NONE]: 'Show Open Issues',
39+
[ISSUE_STATES.OPEN]: 'Show Closed Issues',
40+
[ISSUE_STATES.CLOSED]: 'Hide Issues',
41+
};
4442

45-
if (loading && !repository) {
46-
return <Loading />;
47-
}
43+
const TRANSITION_STATE = {
44+
[ISSUE_STATES.NONE]: ISSUE_STATES.OPEN,
45+
[ISSUE_STATES.OPEN]: ISSUE_STATES.CLOSED,
46+
[ISSUE_STATES.CLOSED]: ISSUE_STATES.NONE,
47+
};
4848

49-
if (!repository.issues.edges.length) {
50-
return <div className="IssueList">No issues ...</div>;
51-
}
49+
const isShow = issueState => issueState !== ISSUE_STATES.NONE;
5250

53-
return <IssueList issues={repository.issues} />;
54-
}}
55-
</Query>
56-
);
51+
class Issues extends React.Component {
52+
state = {
53+
issueState: ISSUE_STATES.NONE,
54+
};
55+
56+
onChangeIssueState = nextIssueState => {
57+
this.setState({ issueState: nextIssueState });
58+
};
59+
60+
render() {
61+
const { issueState } = this.state;
62+
const { repositoryOwner, repositoryName } = this.props;
63+
64+
return (
65+
<div className="Issues">
66+
<ButtonUnobtrusive
67+
onClick={() =>
68+
this.onChangeIssueState(TRANSITION_STATE[issueState])
69+
}
70+
>
71+
{TRANSITION_LABELS[issueState]}
72+
</ButtonUnobtrusive>
73+
74+
{isShow(issueState) && (
75+
<Query
76+
query={GET_ISSUES_OF_REPOSITORY}
77+
variables={{
78+
repositoryOwner,
79+
repositoryName,
80+
}}
81+
>
82+
{({ data, loading, error }) => {
83+
if (error) {
84+
return <ErrorMessage error={error} />;
85+
}
86+
87+
const { repository } = data;
88+
89+
if (loading && !repository) {
90+
return <Loading />;
91+
}
92+
93+
const filteredRepository = {
94+
issues: {
95+
edges: repository.issues.edges.filter(
96+
issue => issue.node.state === issueState,
97+
),
98+
},
99+
};
100+
101+
if (!filteredRepository.issues.edges.length) {
102+
return <div className="IssueList">No issues ...</div>;
103+
}
104+
105+
return <IssueList issues={filteredRepository.issues} />;
106+
}}
107+
</Query>
108+
)}
109+
</div>
110+
);
111+
}
112+
}
57113

58114
const IssueList = ({ issues }) => (
59115
<div className="IssueList">

0 commit comments

Comments
 (0)