newsreader/src/newsreader/js/homepage/actions/selected.js
sonny 858f84aaad Refactor endpoint tests
Replace force_login calls with login call from client class in setUp
2019-10-28 21:35:19 +01:00

65 lines
1.7 KiB
JavaScript

import { receiveCategory, requestCategory } from './categories.js';
import { receiveRule, requestRule } from './rules.js';
export const MARK_SECTION_READ = 'MARK_SECTION_READ';
export const markSectionRead = (category, rule = {}) => ({
category: category,
rule: rule,
type: MARK_SECTION_READ,
});
const markCategoryRead = (category, token) => {
return dispatch => {
dispatch(requestCategory(category));
const url = `/api/categories/${category.id}/read/`;
const options = {
method: 'POST',
headers: {
'X-CSRFToken': token,
},
};
return fetch(url, options)
.then(response => response.json())
.then(updatedCategory => {
dispatch(receiveCategory({ ...updatedCategory }));
dispatch(markSectionRead({ ...category, ...updatedCategory }));
});
};
};
const markRuleRead = (rule, token) => {
return (dispatch, getState) => {
const { categories } = getState();
const category = categories.items[rule.category];
dispatch(requestRule(rule));
const url = `/api/rules/${rule.id}/read/`;
const options = {
method: 'POST',
headers: {
'X-CSRFToken': token,
},
};
return fetch(url, options)
.then(response => response.json())
.then(updatedRule => {
dispatch(receiveRule({ ...updatedRule }));
// Use the old rule to decrement category with old unread count
dispatch(markSectionRead({ ...category }, { ...rule }));
});
};
};
export const markRead = (selected, token) => {
if ('category' in selected) {
return markRuleRead(selected, token);
} else {
return markCategoryRead(selected, token);
}
};