65 lines
1.7 KiB
JavaScript
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);
|
|
}
|
|
};
|