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 267
/
articlesStore.js
123 lines (106 loc) · 3.41 KB
/
articlesStore.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
import { observable, action, computed } from 'mobx';
import agent from '../agent';
const LIMIT = 10;
export class ArticlesStore {
@observable isLoading = false;
@observable page = 0;
@observable totalPagesCount = 0;
@observable articlesRegistry = observable.map();
@observable predicate = {};
@computed get articles() {
return this.articlesRegistry.values();
};
clear() {
this.articlesRegistry.clear();
this.page = 0;
}
getArticle(slug) {
return this.articlesRegistry.get(slug);
}
@action setPage(page) {
this.page = page;
}
@action setPredicate(predicate) {
if (JSON.stringify(predicate) === JSON.stringify(this.predicate)) return;
this.clear();
this.predicate = predicate;
}
$req() {
if (this.predicate.myFeed) return agent.Articles.feed(this.page, LIMIT);
if (this.predicate.favoritedBy) return agent.Articles.favoritedBy(this.predicate.favoritedBy, this.page, LIMIT);
if (this.predicate.tag) return agent.Articles.byTag(this.predicate.tag, this.page, LIMIT);
if (this.predicate.author) return agent.Articles.byAuthor(this.predicate.author, this.page, LIMIT);
return agent.Articles.all(this.page, LIMIT, this.predicate);
}
@action loadArticles() {
this.isLoading = true;
return this.$req()
.then(action(({ articles, articlesCount }) => {
this.articlesRegistry.clear();
articles.forEach(article => this.articlesRegistry.set(article.slug, article));
this.totalPagesCount = Math.ceil(articlesCount / LIMIT);
}))
.finally(action(() => { this.isLoading = false; }));
}
@action loadArticle(slug, { acceptCached = false } = {}) {
if (acceptCached) {
const article = this.getArticle(slug);
if (article) return Promise.resolve(article);
}
this.isLoading = true;
return agent.Articles.get(slug)
.then(action(({ article }) => {
this.articlesRegistry.set(article.slug, article);
return article;
}))
.finally(action(() => { this.isLoading = false; }));
}
@action makeFavorite(slug) {
const article = this.getArticle(slug);
if (article && !article.favorited) {
article.favorited = true;
article.favoritesCount++;
return agent.Articles.favorite(slug)
.catch(action(err => {
article.favorited = false;
article.favoritesCount--;
throw err;
}));
}
return Promise.resolve();
}
@action unmakeFavorite(slug) {
const article = this.getArticle(slug);
if (article && article.favorited) {
article.favorited = false;
article.favoritesCount--;
return agent.Articles.unfavorite(slug)
.catch(action(err => {
article.favorited = true;
article.favoritesCount++;
throw err;
}));
}
return Promise.resolve();
}
@action createArticle(article) {
return agent.Articles.create(article)
.then(({ article }) => {
this.articlesRegistry.set(article.slug, article);
return article;
})
}
@action updateArticle(data) {
return agent.Articles.update(data)
.then(({ article }) => {
this.articlesRegistry.set(article.slug, article);
return article;
})
}
@action deleteArticle(slug) {
this.articlesRegistry.delete(slug);
return agent.Articles.del(slug)
.catch(action(err => { this.loadArticles(); throw err; }));
}
}
export default new ArticlesStore();