BansOverviewController.js 1.63 KB
angular.module('footyroom')

.controller('BansOverviewController', ['$http', '$uibModal', function ($http, $uibModal) {
    const ctrl = this;
    var offset = 0;

    ctrl.sortBy = 'latestBan';
    ctrl.setSort = setSort;
    ctrl.loadMore = loadMore;
    ctrl.getBans = getBans;
    ctrl.searchUser = searchUser;
    ctrl.getSummaries = getSummaries;

    getSummaries();

    function getSummaries() {
        $http.get('/bans/summaries.json', {
            params: {
                sortBy: ctrl.sortBy,
                offset: offset,
                username: ctrl.username,
            },
        })
            .success(function (response) {
                if (offset > 0) {
                    ctrl.summaries = ctrl.summaries.concat(response.data);
                } else {
                    ctrl.summaries = response.data;
                }
            });
    }

    function setSort(newSortBy) {
        if (ctrl.sortBy === newSortBy) {
            return;
        }

        offset = 0;
        ctrl.sortBy = newSortBy;

        getSummaries();
    }

    function loadMore() {
        offset += 20;
        getSummaries();
    }

    function getBans(userId) {
        $uibModal.open({
            templateUrl: '/views/ng/modal/userBanList.html',
            controllerAs: 'ctrl',
            bindToController: true,
            controller: function () {
                $http.get('/bans.json', { params: { userId } })
                    .success((response) => {
                        this.bans = response.data;
                    });
            },
        });
    }

    function searchUser() {
        offset = 0;
        getSummaries();
    }
}]);