This repository has been archived by the owner on Sep 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 266
/
MainView.js
144 lines (127 loc) · 3.31 KB
/
MainView.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
import ArticleList from "components/ArticleList";
import React from "react";
import { inject, observer } from "mobx-react";
import { withRouter, NavLink } from "react-router-dom";
import { parse as qsParse } from "query-string";
const YourFeedTab = props => {
if (props.currentUser) {
return (
<li className="nav-item">
<NavLink
className="nav-link"
isActive={(match, location) => {
return location.search.match("tab=feed") ? 1 : 0;
}}
to={{
pathname: "/",
search: "?tab=feed"
}}
>
Your Feed
</NavLink>
</li>
);
}
return null;
};
const GlobalFeedTab = props => {
return (
<li className="nav-item">
<NavLink
className="nav-link"
isActive={(match, location) => {
return !location.search.match(/tab=(feed|tag)/) ? 1 : 0;
}}
to={{
pathname: "/",
search: "?tab=all"
}}
>
Global Feed
</NavLink>
</li>
);
};
const TagFilterTab = props => {
if (!props.tag) {
return null;
}
return (
<li className="nav-item">
<a href="" className="nav-link active">
<i className="ion-pound" /> {props.tag}
</a>
</li>
);
};
@inject("articlesStore", "commonStore", "userStore")
@withRouter
@observer
export default class MainView extends React.Component {
componentWillMount() {
this.props.articlesStore.setPredicate(this.getPredicate());
}
componentDidMount() {
this.props.articlesStore.loadArticles();
}
componentDidUpdate(previousProps) {
if (
this.getTab(this.props) !== this.getTab(previousProps) ||
this.getTag(this.props) !== this.getTag(previousProps)
) {
this.props.articlesStore.setPredicate(this.getPredicate());
this.props.articlesStore.loadArticles();
}
}
getTag(props = this.props) {
return qsParse(props.location.search).tag || "";
}
getTab(props = this.props) {
return qsParse(props.location.search).tab || "all";
}
getPredicate(props = this.props) {
switch (this.getTab(props)) {
case "feed":
return { myFeed: true };
case "tag":
return { tag: qsParse(props.location.search).tag };
default:
return {};
}
}
handleTabChange = tab => {
if (this.props.location.query.tab === tab) return;
this.props.router.push({ ...this.props.location, query: { tab } });
};
handleSetPage = page => {
this.props.articlesStore.setPage(page);
this.props.articlesStore.loadArticles();
};
render() {
const { currentUser } = this.props.userStore;
const {
articles,
isLoading,
page,
totalPagesCount
} = this.props.articlesStore;
return (
<div className="col-md-9">
<div className="feed-toggle">
<ul className="nav nav-pills outline-active">
<YourFeedTab currentUser={currentUser} tab={this.getTab()} />
<GlobalFeedTab tab={this.getTab()} />
<TagFilterTab tag={qsParse(this.props.location.search).tag} />
</ul>
</div>
<ArticleList
articles={articles}
loading={isLoading}
totalPagesCount={totalPagesCount}
currentPage={page}
onSetPage={this.handleSetPage}
/>
</div>
);
}
}