<commit_before>var Vue = require('vue');
Vue.use(require('vue-resource'));

var moment    = require('moment');
var URL       = require('url-parse');
var nprogress = require('nprogress');
var cache     = require('./local_storage');

var ITEMS_CACHE_KEY    = 'pocket-items';
var USERNAME_CACHE_KEY = 'pocket-username';

new Vue({
    el: '#pocketApp',

    data: {
        username      : '',
        items         : {},
        count         : 8,
        current_offset: 0,
    },

    created: function () {

        this.username = cache.getString(USERNAME_CACHE_KEY);

        if (this.username) {

            // Populate items list from local storage before retrieving updated list.
            if (cache.keyExists(ITEMS_CACHE_KEY)) {
                this.items = cache.getJson(ITEMS_CACHE_KEY);
            }

            this.getItems(this.count, 0);

        } else {
            this.getItemsIfLoggedIn();
        }
    },

    methods: {

        authorize: function() {

            this.startProgress();

            this.$http.get('/auth/request', function(data, status, request) {

                this.endProgress();

                if (data.redirect_to) {
                    window.location = data.redirect_to;
                }

            });
        },

        getItemsIfLoggedIn: function() {

            this.$http.get('/auth/user', function(data, status, request) {

                if (! data.username) {
                    return;
                }

                this.username = data.username;

                cache.store(USERNAME_CACHE_KEY, this.username);

                this.getItems(this.count, 0);

            });
        },

        // Replaces the existing list of items.
        getItems: function(num, offset) {

            var postData = {
                count : num,
                offset: offset
            };

            this.current_offset = offset;

            this.sendPostRequest(postData, this.replaceItems);
        },

        getItemsCount: function() {

            this.startProgress();

            this.$http.get('/items/all', function(data, status, request) {

                this.endProgress();

                alert('You currently have ' + Object.keys(data.list).length + ' items');
            });
        },

        // Callback function for sendPostRequest
        addItems: function(newItems) {

            // Add the downloaded items to the items list.
            Object.keys(newItems).forEach(function(key) {
                Vue.set(this.items, key, newItems[key]);
            }.bind(this));
        },

        // Callback function for sendPostRequest
        replaceItems: function(newItems) {

            // Remove all existing items.
            this.itemsKeys.forEach(function(key) {
                Vue.delete(this.items, key);
            }.bind(this));

            this.addItems(newItems);
        },

        doAction: function(action, item_id) {

            // Remove item from the list immediately, then make POST call
            Vue.delete(this.items, item_id);

            this.startProgress();

            this.$http.post('/item/' + item_id + '/' + action, function(data, status, request) {<commit_msg>Updated cache as items are removed.<commit_after>var Vue = require('vue');
Vue.use(require('vue-resource'));

var moment    = require('moment');
var URL       = require('url-parse');
var nprogress = require('nprogress');
var cache     = require('./local_storage');

var ITEMS_CACHE_KEY    = 'pocket-items';
var USERNAME_CACHE_KEY = 'pocket-username';

new Vue({
    el: '#pocketApp',

    data: {
        username      : '',
        items         : {},
        count         : 8,
        current_offset: 0,
    },

    created: function () {

        this.username = cache.getString(USERNAME_CACHE_KEY);

        if (this.username) {

            // Populate items list from local storage before retrieving updated list.
            if (cache.keyExists(ITEMS_CACHE_KEY)) {
                this.items = cache.getJson(ITEMS_CACHE_KEY);
            }

            this.getItems(this.count, 0);

        } else {
            this.getItemsIfLoggedIn();
        }
    },

    methods: {

        authorize: function() {

            this.startProgress();

            this.$http.get('/auth/request', function(data, status, request) {

                this.endProgress();

                if (data.redirect_to) {
                    window.location = data.redirect_to;
                }

            });
        },

        getItemsIfLoggedIn: function() {

            this.$http.get('/auth/user', function(data, status, request) {

                if (! data.username) {
                    return;
                }

                this.username = data.username;

                cache.store(USERNAME_CACHE_KEY, this.username);

                this.getItems(this.count, 0);

            });
        },

        // Replaces the existing list of items.
        getItems: function(num, offset) {

            var postData = {
                count : num,
                offset: offset
            };

            this.current_offset = offset;

            this.sendPostRequest(postData, this.replaceItems);
        },

        getItemsCount: function() {

            this.startProgress();

            this.$http.get('/items/all', function(data, status, request) {

                this.endProgress();

                alert('You currently have ' + Object.keys(data.list).length + ' items');
            });
        },

        // Callback function for sendPostRequest
        addItems: function(newItems) {

            // Add the downloaded items to the items list.
            Object.keys(newItems).forEach(function(key) {
                Vue.set(this.items, key, newItems[key]);
            }.bind(this));
        },

        // Callback function for sendPostRequest
        replaceItems: function(newItems) {

            // Remove all existing items.
            this.itemsKeys.forEach(function(key) {
                Vue.delete(this.items, key);
            }.bind(this));

            this.addItems(newItems);
        },

        doAction: function(action, item_id) {

            // Remove item from the list immediately, then make POST call
            Vue.delete(this.items, item_id);

            // Update cache with new list
            cache.store(ITEMS_CACHE_KEY, this.items);

            this.startProgress();

            this.$http.post('/item/' + item_id + '/' + action, function(data, status, request) {
{'fraction_non_alphanumeric': 0.12, 'fraction_numerical': 0.0012260536398467432, 'mean_word_length': 1.5794466403162055, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4696946', 'n_tokens_mistral': 1818, 'n_tokens_neox': 1678, 'n_words': 358}