This repository has been archived by the owner on Oct 1, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathgeotag.js
203 lines (175 loc) · 5.98 KB
/
geotag.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
/*
This file is a part of libertysoil.org website
Copyright (C) 2016 Loki Education (Social Enterprise)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import PropTypes from 'prop-types';
import React, { Component } from 'react';
import { bindActionCreators } from 'redux';
import { connect } from 'react-redux';
import Helmet from 'react-helmet';
import i from 'immutable';
import {
url as urlPropType,
mapOf as mapOfPropType
} from '../prop-types/common';
import { MapOfGeotags as MapOfGeotagsPropType } from '../prop-types/geotags';
import { MapOfSchools as MapOfSchoolsPropType } from '../prop-types/schools';
import {
ArrayOfPostsId as ArrayOfPostsIdPropType,
MapOfPosts as MapOfPostsPropType
} from '../prop-types/posts';
import { CommentsByCategory as CommentsByCategoryPropType } from '../prop-types/comments';
import {
CurrentUser as CurrentUserPropType,
MapOfUsers as MapOfUsersPropType
} from '../prop-types/users';
import ApiClient from '../api/client';
import { API_HOST } from '../config';
import { resetCreatePostForm, updateCreatePostForm } from '../actions/posts';
import { addGeotag, setGeotagPosts } from '../actions/geotags';
import { ActionsTrigger } from '../triggers';
import { createSelector, currentUserSelector } from '../selectors';
import { TAG_LOCATION } from '../consts/tags';
import ContinentNav from '../components/continent-nav';
import River from '../components/river_of_posts';
import NotFound from './not-found';
import BaseTagPage from './base/tag';
export class UnwrappedGeotagPage extends Component {
static displayName = 'UnwrappedGeotagPage';
static propTypes = {
comments: CommentsByCategoryPropType.isRequired,
current_user: CurrentUserPropType,
geotag_posts: mapOfPropType(urlPropType, ArrayOfPostsIdPropType).isRequired,
geotags: MapOfGeotagsPropType.isRequired,
is_logged_in: PropTypes.bool.isRequired,
params: PropTypes.shape({
url_name: PropTypes.string.isRequired
}),
posts: MapOfPostsPropType.isRequired,
schools: MapOfSchoolsPropType.isRequired,
users: MapOfUsersPropType.isRequired
};
static async fetchData(router, store, client) {
let geotag = client.getGeotag(router.params.url_name);
const geotagPosts = client.geotagPosts(router.params.url_name);
try {
geotag = await geotag;
} catch (e) {
store.dispatch(addGeotag({ url_name: router.params.url_name }));
return 404;
}
store.dispatch(addGeotag(geotag));
store.dispatch(setGeotagPosts(router.params.url_name, await geotagPosts));
const trigger = new ActionsTrigger(client, store.dispatch);
const promises = [trigger.loadUserRecentTags()];
if (geotag.type == 'Continent') {
promises.push(trigger.loadContinentNav(geotag.id));
}
await Promise.all(promises);
return 200;
}
render() {
const {
ui,
continent_nav,
comments,
create_post_form,
is_logged_in,
current_user,
posts,
resetCreatePostForm,
updateCreatePostForm,
geotags,
geotag_posts,
users,
schools
} = this.props;
const client = new ApiClient(API_HOST);
const triggers = new ActionsTrigger(client, this.props.dispatch);
const actions = { resetCreatePostForm, updateCreatePostForm };
const geotag = geotags.get(this.props.params.url_name);
const title = geotag ? geotag.get('name') : this.props.params.url_name;
if (!geotag) {
return null;
}
if (!geotag.get('id')) {
return <NotFound />;
}
const geotagPosts = geotag_posts.get(this.props.params.url_name) || i.List();
let sidebarAlt = null;
if (geotag.get('type') == 'Continent') {
sidebarAlt = (
<ContinentNav
continent={geotag}
continents={continent_nav.get('continents').map(name => geotags.get(name))}
countries={continent_nav.get('countries').map(name => geotags.get(name))}
/>
);
}
return (
<BaseTagPage
current_user={current_user}
is_logged_in={is_logged_in}
params={this.props.params}
tag={geotag}
type={TAG_LOCATION}
actions={actions}
triggers={triggers}
schools={schools.toList()}
postsAmount={geotagPosts.length}
create_post_form={create_post_form}
sidebarAlt={sidebarAlt}
>
<Helmet title={`${title} posts on `} />
<River
current_user={current_user}
posts={posts}
river={geotagPosts}
triggers={triggers}
comments={comments}
ui={ui}
users={users}
/>
</BaseTagPage>
);
}
}
const selector = createSelector(
currentUserSelector,
state => state.get('continent_nav'),
state => state.get('comments'),
state => state.get('create_post_form'),
state => state.get('geotags'),
state => state.get('geotag_posts'),
state => state.get('posts'),
state => state.get('schools'),
state => state.get('users'),
state => state.get('ui'),
(current_user, continent_nav, comments, create_post_form, geotags, geotag_posts, posts, schools, users, ui) => ({
continent_nav,
comments,
create_post_form,
geotags,
geotag_posts,
posts,
schools,
users,
ui,
...current_user
})
);
const GeotagPage = connect(selector, dispatch => ({
dispatch,
...bindActionCreators({ resetCreatePostForm, updateCreatePostForm }, dispatch)
}))(UnwrappedGeotagPage);
export default GeotagPage;