forked from gpbl/isomorphic500
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPhotoStore.js
55 lines (43 loc) · 1.23 KB
/
PhotoStore.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
import { BaseStore } from "fluxible/addons";
import Actions from "../constants/Actions";
import _ from "lodash";
/*
This is a "resource store", holding the photo objects loaded by the app.
Photo objects can come either loading a single photo (`LOAD_PHOTO_SUCCESS`)
or after loading featured photos (`LOAD_FEATURED_PHOTOS_SUCCESS`).
*/
export default class PhotoStore extends BaseStore {
static storeName = "PhotoStore"
static handlers = {
[Actions.LOAD_FEATURED_PHOTOS_SUCCESS]: "handleLoadFeaturedSuccess",
[Actions.LOAD_PHOTO_SUCCESS]: "handleLoadSuccess"
}
constructor(dispatcher) {
super(dispatcher);
this.photos = {};
}
handleLoadSuccess(photo) {
this.photos[photo.id] = _.merge({}, this.photos[photo.id], photo);
this.emitChange();
}
handleLoadFeaturedSuccess({ photos }) {
this.photos = _(photos).keyBy("id").merge(this.photos).value();
this.emitChange();
}
get(id, minSize=0) {
return _.find(this.photos, photo =>
photo.id === parseInt(id) && photo.images[0].size >= minSize
);
}
getMultiple(ids) {
return ids.map(id => this.photos[id]);
}
dehydrate() {
return {
photos: this.photos
};
}
rehydrate(state) {
this.photos = state.photos;
}
}