This repository has been archived by the owner on Nov 17, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 181
/
HtmlHeadStore.js
129 lines (100 loc) · 3.04 KB
/
HtmlHeadStore.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
import { BaseStore } from "fluxible/addons";
import Actions from "../constants/Actions";
import IntlMessageFormat from "intl-messageformat";
const SITE_NAME = "Isomorphic500";
const BASE_URL = "http://isomorphic500.herokuapp.com";
/*
This store listens to fluxible-router's actions and keep
the content for the <head> tag. Used in Html.js,
and Root.js (to change the document's title)
*/
export default class HtmlHeadStore extends BaseStore {
static storeName = "HtmlHeadStore"
static handlers = {
[Actions.NAVIGATE_START]: "handleNavigateStart",
[Actions.NAVIGATE_SUCCESS]: "handleNavigateSuccess",
[Actions.NAVIGATE_FAILURE]: "handleNavigateFailure"
}
constructor(dispatcher) {
super(dispatcher);
this.siteName = SITE_NAME;
this.currentUrl = null;
this.setInitialState();
}
setInitialState() {
this.title = this.formatMessage("meta.title");
this.description = this.formatMessage("meta.description");
this.images = [];
}
getTitle() {
return this.title;
}
getDescription() {
return this.description;
}
getSiteName() {
return this.siteName;
}
getCurrentUrl() {
const route = this.dispatcher.getStore("RouteStore").getCurrentRoute();
if (!route) {
return BASE_URL;
}
return `${BASE_URL}${route.url}`;
}
getImages() {
return this.images;
}
// Used to get internationalized messages, has access to the IntlStore
formatMessage(message, values={}) {
const store = this.dispatcher.getStore("IntlStore");
const msg = new IntlMessageFormat(store.getMessage(message), store.getLocales());
return msg.format(values);
}
handleNavigateStart() {
// Use a loading title when loading the route
this.title = this.formatMessage("meta.loadingTitle");
this.emitChange();
}
// Set the store content (images, description, title, etc.) according to the received route
// Remember: route is an immutable object!
handleNavigateSuccess(route) {
switch (route.name) {
case "photo":
const { id } = route.params;
const store = this.dispatcher.getStore("PhotoStore");
const photo = store.get(id);
this.title = this.formatMessage("photo.documentTitle", {
name: photo.name,
user: photo.user.fullname
});
this.description = this.formatMessage("photo.documentDescription", {
name: photo.name,
user: photo.user.fullname
});
this.images = [photo.image_url];
break;
case "featured":
const { feature } = route.params;
const featureName = this.formatMessage(`features.${feature}`);
this.title = this.formatMessage("featured.documentTitle", {
feature: featureName
});
break;
default:
// Just set the defaults
this.setInitialState();
break;
}
this.emitChange();
}
handleNavigateFailure(error) {
if (error.statusCode === 404) {
this.title = this.formatMessage("meta.notFoundTitle");
}
else {
this.title = this.formatMessage("meta.errorTitle");
}
this.emitChange();
}
}