119 lines
3 KiB
JavaScript
119 lines
3 KiB
JavaScript
export const SELECT_POST = 'SELECT_POST';
|
|
export const UNSELECT_POST = 'UNSELECT_POST';
|
|
|
|
export const RECEIVE_POSTS = 'RECEIVE_POSTS';
|
|
export const RECEIVE_POST = 'RECEIVE_POST';
|
|
export const REQUEST_POSTS = 'REQUEST_POSTS';
|
|
|
|
export const MARK_POST_READ = 'MARK_POST_READ';
|
|
|
|
export const selectPost = post => ({
|
|
type: SELECT_POST,
|
|
post,
|
|
});
|
|
|
|
export const unSelectPost = () => ({
|
|
type: UNSELECT_POST,
|
|
});
|
|
|
|
export const postRead = (post, rule, category) => ({
|
|
type: MARK_POST_READ,
|
|
category: category,
|
|
post: post,
|
|
rule: rule,
|
|
});
|
|
|
|
export const markPostRead = (post, token) => {
|
|
return (dispatch, getState) => {
|
|
const { rules } = getState();
|
|
const { categories } = getState();
|
|
|
|
const url = `/api/posts/${post.id}/`;
|
|
const options = {
|
|
method: 'PATCH',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'X-CSRFToken': token,
|
|
},
|
|
body: JSON.stringify({ read: true }),
|
|
};
|
|
|
|
const rule = rules.items[post.rule];
|
|
const category = categories.items[rule.category];
|
|
|
|
return fetch(url, options)
|
|
.then(response => response.json())
|
|
.then(updatedPost => {
|
|
const updatedRule = { ...rule, unread: rule.unread - 1 };
|
|
const updatedCategory = { ...category, unread: category.unread - 1 };
|
|
|
|
dispatch(receivePost({ ...updatedPost }));
|
|
dispatch(postRead({ ...updatedPost }, updatedRule, updatedCategory));
|
|
});
|
|
};
|
|
};
|
|
|
|
export const receivePosts = json => ({
|
|
type: RECEIVE_POSTS,
|
|
posts: json.items,
|
|
next: json.next,
|
|
});
|
|
|
|
export const receivePost = post => ({
|
|
type: RECEIVE_POST,
|
|
post,
|
|
});
|
|
|
|
export const requestPosts = () => ({ type: REQUEST_POSTS });
|
|
|
|
export const fetchPostsByCategory = (category, page = false) => {
|
|
return dispatch => {
|
|
dispatch(requestPosts());
|
|
|
|
const url = page ? page : `/api/categories/${category.id}/posts/?read=false`;
|
|
return fetch(url)
|
|
.then(response => response.json())
|
|
.then(json => {
|
|
const posts = {};
|
|
|
|
json.results.forEach(post => {
|
|
posts[post.id] = post;
|
|
});
|
|
|
|
dispatch(receivePosts({ items: posts, next: json.next }));
|
|
})
|
|
.catch(error => {
|
|
if (error instanceof TypeError) {
|
|
console.log(`Unable to parse posts from request: ${error}`);
|
|
}
|
|
|
|
dispatch(receivePosts({ items: {}, next: null }));
|
|
});
|
|
};
|
|
};
|
|
|
|
export const fetchPostsByRule = (rule, page = false) => {
|
|
return dispatch => {
|
|
dispatch(requestPosts());
|
|
|
|
const url = page ? page : `/api/rules/${rule.id}/posts/?read=false`;
|
|
return fetch(url)
|
|
.then(response => response.json())
|
|
.then(json => {
|
|
const posts = {};
|
|
|
|
json.results.forEach(post => {
|
|
posts[post.id] = post;
|
|
});
|
|
|
|
dispatch(receivePosts({ items: posts, next: json.next }));
|
|
})
|
|
.catch(error => {
|
|
if (error instanceof TypeError) {
|
|
console.log(`Unable to parse posts from request: ${error}`);
|
|
}
|
|
|
|
dispatch(receivePosts({ items: {}, next: null }));
|
|
});
|
|
};
|
|
};
|