aboutsummaryrefslogtreecommitdiff
path: root/src/web/js/actions/MiddlePanelActions.js
blob: 159ba91bf82bda0828286ada8b4230a338b6f969 (plain)
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
var JarrDispatcher = require('../dispatcher/JarrDispatcher');
var MiddlePanelActionTypes = require('../constants/JarrConstants').MiddlePanelActionTypes;
var jquery = require('jquery');
var MiddlePanelStore = require('../stores/MiddlePanelStore');

var _last_fetched_with = {};
var shouldFetch = function(filters) {
    if(filters.filter != null // undefined means unchanged
            && (_last_fetched_with.filter != 'all'
                || _last_fetched_with.filter != filters.filter)) {
        return true;
    }
    if(_last_fetched_with.filter_type != null) {
        if(_last_fetched_with.filter_type != filters.filter_type) {
            return true;
        }
        if(_last_fetched_with.filter_id != filters.filter_id) {
            return true;
        }
    }
    return false;
}
var reloadIfNecessaryAndDispatch = function(dispath_payload) {
    if(shouldFetch(dispath_payload)) {
        filters = MiddlePanelStore.getRequestFilter();
        for (var key in filters) {
            if(dispath_payload[key] != null) {
                filters[key] = dispath_payload[key];
            }
        }
        jquery.getJSON('/middle_panel', dispath_payload, function(payload) {
            dispath_payload.articles = payload.articles;
            JarrDispatcher.dispatch(dispath_payload);
            _last_fetched_with = MiddlePanelStore.getRequestFilter();
        });
    } else {
        JarrDispatcher.dispatch(dispath_payload);
    }
}


var MiddlePanelActions = {
    reload: function() {
        filters = MiddlePanelStore.getRequestFilter();
        jquery.getJSON('/middle_panel', filters, function(payload) {
            _last_fetched_with = filters;
            JarrDispatcher.dispatch({
                type: MiddlePanelActionTypes.RELOAD_MIDDLE_PANEL,
                articles: payload.articles,
            });
        });
    },
    removeParentFilter: function(filter_type, filter_id) {
        reloadIfNecessaryAndDispatch({
            type: MiddlePanelActionTypes.MIDDLE_PANEL_PARENT_FILTER,
            filter_type: null,
            filter_id: null,
        });
    },
    setCategoryFilter: function(category_id) {
        reloadIfNecessaryAndDispatch({
            type: MiddlePanelActionTypes.MIDDLE_PANEL_PARENT_FILTER,
            filter_type: 'category',
            filter_id: category_id,
        });
    },
    setFeedFilter: function(feed_id) {
        reloadIfNecessaryAndDispatch({
            type: MiddlePanelActionTypes.MIDDLE_PANEL_PARENT_FILTER,
            filter_type: 'feed',
            filter_id: feed_id,
        });
    },
    setFilterAll: function() {
        reloadIfNecessaryAndDispatch({
            type: MiddlePanelActionTypes.MIDDLE_PANEL_FILTER,
            filter: 'all',
        });
    },
    setFilterUnread: function() {
        reloadIfNecessaryAndDispatch({
            type: MiddlePanelActionTypes.MIDDLE_PANEL_FILTER,
            filter: 'unread',
        });
    },
    setFilterLiked: function() {
        reloadIfNecessaryAndDispatch({
            type: MiddlePanelActionTypes.MIDDLE_PANEL_FILTER,
            filter: 'liked',
        });
    },
};

module.exports = MiddlePanelActions;
bgstack15