PredictorController.js 1.51 KB
angular.module('footyroom')

.controller('PredictorController', ['$scope', '$route', '$sce', 'predictService', '$location', function ($scope, $route, $sce, predictService, $location) {
    $scope.$route = $route;

    $scope.predictor = DataStore.predictor;

    $scope.predictor.prizeHtml = $sce.trustAsHtml($scope.predictor.prizeHtml);

    $scope.loadPlayer = () => {
        predictService.getPlayer().then((player) => {
            $scope.player = player;
        });
    };

    /**
     * Request to load player only if we are not a guest.
     */
    if (!DataStore.isGuest) {
        $scope.loadPlayer();
    }

    /**
     * We will ask playing guests to sign up.
     */
    if (DataStore.isGuest) {
        $scope.showSignUp = true;
    }

    /**
     * Handle loading of player for "viewAs" situations.
     */
    $scope.$on('$routeChangeStart', function (event, next) {
        if (next) {
            predictService.loadViewAs(parseInt($location.search().viewAs, 10)).then(() => {
                $scope.viewAs = predictService.getViewAs();
            }).catch(() => {
                /**
                 * In case we can't fetch a viewAs player then redirect to predictor home page.
                 * TODO: What we really need is a proper 404 and a general error handler within
                 * angular. This is important for SEO also.
                 *
                 * https://stackoverflow.com/questions/14779190
                 */
                $location.url('/predictor');
            });
        }
    });
}]);