profile.js 4.65 KB
import './comment/frCommenter';
import './vendor/jquery.timeago';
import teamTypeahead from './common/directives/teamTypeahead';

angular.module('footyroom').requires.push('fr.profile');

angular.module('fr.profile', [teamTypeahead])
    .controller('ProfileSettingsController', ['$scope', '$http', function ($scope, $http) {
        $scope.submitForm = function (event) {
            if ($scope.userForm.$invalid) {
                event.preventDefault();
            }
        };

        $scope.$watch('userForm', function () {
            $scope.userForm.verifyPassword.$validators.verify = function (modelValue, viewValue) {
                return viewValue == $scope.userForm.newPassword.$viewValue;
            };
        });

        $scope.fbConnect = function () {
            $scope.fbLoginError = null;

            FB.login(fbStatusChangeCallback, { scope: 'public_profile, email' });
        };

        $scope.fbDisconnect = function () {
            $scope.fbLoginError = null;

            $http.post('/profile/settings/fbdisconnect')

                .then(handleFbApiResponse);
        };

        /**
         *  Private methods.
         */

        function fbStatusChangeCallback(response) {
            var token = null;

            if (response.status === 'connected') {
                token = response.authResponse.accessToken;

                $http.post('/profile/settings/fbconnect', { token: token })

                    .then(handleFbApiResponse);
            }
        }

        function handleFbApiResponse(response) {
            if (response.data === 'success') {
                window.location = '/profile/settings';
            } else {
                $scope.fbLoginError = response.data;
            }
        }
    }])

    .controller('ProfileCtrl', function () {
    })

    .controller('FriendsPaginationCtrl', ['$scope', '$rootScope', '$http', function ($scope, $rootScope, $http) {
        $scope.currentPage = 1;
        $scope.itemsPerPage = 20;

        $scope.$on('loadFriends', function () {
            loadFriends();
        });

        $scope.init = function (type) {
            $scope.type = type;
            loadFriends();
        };

        $scope.pageChanged = function () {
            loadFriends();
        };

        $scope.removeFriend = function (userID) {
            $http.post('/profile/friends/unfriend', { userId: userID })

                .then(handleFriendApiResponse);
        };

        $scope.ignoreFriend = function (userID) {
            $http.post('/profile/friends/delete-request', { fromUserId: userID })

                .then(handleFriendApiResponse);
        };

        $scope.acceptFriend = function (userID) {
            $http.post('/profile/friends/accept-request', { fromUserId: userID })

                .then(function (response) {
                    handleFriendApiResponse(response, true);
                });
        };

        function handleFriendApiResponse(response, broadcast) {
            if (response.data === 'OK') {
                if (broadcast) {
                    $rootScope.$broadcast('loadFriends');
                } else {
                    loadFriends();
                }
            }
        }

        function loadFriends(response) {
            $http({
                method: 'GET',
                url: '/profile/' + $scope.username + '/friends.json',
                params: {
                    type: $scope.type,
                    page: $scope.currentPage,
                    perPage: $scope.itemsPerPage,
                },
            })

                .then(function (response) {
                    $scope.totalItems = parseInt(response.data.total);
                    $scope.friends = response.data.data;
                });
        }
    }]);

$(document).ready(function () {
    $('#changePhoto_label').click(function () {
        $('#inputChangePhoto').click();
    });

    $('#filter_all').click(function () {
        $('#all_friends').show();
        $('#pending_friends').hide();
    });

    $('#filter_pending').click(function () {
        $('#all_friends').hide();
        $('#pending_friends').show();
    });

    $('#inputChangePhoto').change(function () {
        $('#changePhotoForm').submit();
    });

    $.profile = {
        userID: $('#profile_userID').val(),
    };

    $('#add_as_friend').click(function () {
        const _this = this;

        $.post('/profile/friends/request', { userId: $.profile.userID }, function (result) {
            if (result.success == true) {
                $('#profile_connect_text').removeClass('displayno');
                $(_this).hide();
            } else {
                alert('Error in communication');
            }
        }, 'json');
    });
});