-
Notifications
You must be signed in to change notification settings - Fork 0
/
epic_posts.js
90 lines (82 loc) · 3.77 KB
/
epic_posts.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
import * as ActionTypes from "../ActionTypes";
import { createPostFulfilled, fetchPosts, fetchPostsFulfilled, fetchPostsWithIdFulfilled, changeRoute } from "../actions";
import {store, history} from "../configureStore";
import { Observable } from "rxjs/Observable";
import "rxjs/add/observable/combineLatest";
import "rxjs/add/operator/debounceTime";
import { concat as concat$ } from "rxjs/observable/concat";
import { from as from$ } from "rxjs/observable/from";
import { of as of$ } from "rxjs/observable/of";
import "rxjs/add/operator/map";
import "rxjs/add/operator/mergeMap";
import "rxjs/add/operator/startWith";
import "rxjs/add/operator/filter";
import "rxjs/add/operator/switchMap";
import "rxjs/add/operator/concatMap";
import "rxjs/add/operator/catch";
import "rxjs/add/observable/dom/ajax";
import {push} from "react-router-redux";
const ROOT_URL = "http://reduxblog.herokuapp.com/api";
const API_KEY = "key=davide123";
export const fetchPostsEpic = (action$) => {
return action$.filter((action$)=> action$.type === ActionTypes.FETCH_POSTS)
.mergeMap(action$ => {
return Observable.ajax.getJSON(`${ROOT_URL}/posts/?${API_KEY}`)
.map(response => fetchPostsFulfilled(response), (err) => {console.log(err);});
});
};
export const fetchPostsWithIdEpic = (action$) => {
return action$.filter((action$)=> action$.type === ActionTypes.FETCH_POSTS_WITH_ID)
.mergeMap(action$ => {
return Observable.ajax.getJSON(`${ROOT_URL}/posts/?${action$.payload}&${API_KEY}`)
.map(response => fetchPostsWithIdFulfilled(response), (err) => {console.log(err);});
});
};
export const createPostEpic = (action$) => {
const actionVectorizer = ((action$) => {
if (action$)
return action$.isArray() ? [].concat(...action$.mergeMap(x => actionVectorizer(x))) : action$;
})();
return action$.filter((action$)=> action$.type === ActionTypes.CREATE_POST)
.mergeMap(action$ => {
console.log("action$ is...");
console.log(action$);
let { values, history } = action$.payload;
return Observable.ajax.post(`${ROOT_URL}/posts/?${API_KEY}`, values)
.map(
(data) => {
if (data.status === 201) {
console.log("history after data.status 201...");
console.log(history);
console.log("Success status", data.status);
history.push("/");
return createPostFulfilled(data.status);
}
else {console.log("Server error is", data.status);}
},
(err) => {console.log(err);}
);
});
};
/* eslint-disable */
// export const changeRouteEpic = (action$) => {
// return action$.filter((action$)=> action$.type === ActionTypes.CHANGE_ROUTE)
// .switchMap(
// (action$) => concat$(
// from$(store.dispatch(fetchPosts())),
// of$(store.dispatch(history.replace(action$.payload)),
// )
// ));
// };
//
// export const changeRouteEpic = (action$) => {
// return action$
// .filter((action$)=> action$.type === ActionTypes.CHANGE_ROUTE)
// .mergeMap(action$ => {
// // window.location.href = action$.payload;
// console.log("final store.....");
// console.log(store.getState());
// store.dispatch(push(action$.payload));
// });
// };
/* eslint-enable */