tipEditorComponent.js 4.48 KB
angular.module('footyroom').component('tipEditor', {
    template: /* html */ `
            <div fr-litelay="$ctrl.isSaving || $ctrl.isLoading">
                <div class="modal-body">
                    <div class="form-group">
                        <label>Match ID</label>
                        <input class="form-control" type="text" ng-model="$ctrl.tip.matchId" placeholder="Example: 123456789">
                        <span class="help-block">
                            You can get match ID from the URL of a match to which this tip belongs.
                            Checkout Live Scores to get a list of upcoming matches and their URLs.
                        </span>
                    </div>
                    <div class="form-group">
                        <label>Prediction</label>
                        <input class="form-control" type="text" ng-model="$ctrl.tip.prediction" placeholder="Example: Both teams to score - Yes">
                    </div>
                    <div class="form-group">
                        <label>Odds</label>
                        <input class="form-control" type="text" ng-model="$ctrl.tip.odds" placeholder="Example: 1.99">
                    </div>
                    <div class="form-group">
                        <label>Status</label>
                        <select class="form-control" ng-model="$ctrl.tip.status">
                            <option value="pending">Pending</option>
                            <option value="correct">Correct</option>
                            <option value="wrong">Wrong</option>
                            <option value="neutral">Neutral</option>
                        </select>
                        <span class="help-block">
                            All tips start as "Pending", but when it's settled you will have to update its outcome here.
                        </span>
                    </div>
                    <div class="form-group">
                        <label>Actual Result (optional)</label>
                        <input class="form-control" type="text" ng-model="$ctrl.tip.result" placeholder="Examples: 1 : 2, Yes, Neymar">
                        <span class="help-block">
                            This is usually the final score of the game or some other outcome.
                            If this field is left empty final score will be shown automatically.
                        </span>
                    </div>
                </div>
                <div class="modal-footer">
                    <div class="pull-left">
                        <button ng-click="$ctrl.remove()" ng-if="$ctrl.tip.id" class="btn btn-default">Delete</button>
                    </div>
                    <div class="pull-right">
                        <button class="btn btn-default" ng-click="$ctrl.dismiss()">Cancel</button>
                        <button class="btn btn-primary" ng-click="$ctrl.save()">Save</button>
                    </div>
                </div>
            </div>
        `,
    bindings: {
        id: '@',
        dismiss: '&',
    },
    controller: [
        '$http',
        function ($http) {
            const vm = this;

            vm.$onInit = init;
            vm.save = save;
            vm.remove = remove;

            function init() {
                if (vm.id) {
                    vm.isLoading = true;

                    $http.get(`/tips/${this.id}.json`).then(function (response) {
                        vm.tip = response.data;
                        vm.isLoading = false;
                    });
                } else {
                    vm.tip = {
                        status: 'pending',
                    };
                }
            }

            function save() {
                vm.isSaving = true;

                const persist = vm.tip.id
                    ? $http.put(`/tips/${vm.tip.id}`, vm.tip)
                    : $http.post('/tips', vm.tip);

                persist
                    .then(() => {
                        window.location.reload();
                    })
                    .catch(() => {
                        vm.isSaving = false;
                    });
            }

            function remove() {
                vm.isSaving = true;

                $http.delete(`/tips/${this.id}`)
                    .then(() => {
                        window.location.reload();
                    })
                    .catch(() => {
                        vm.isSaving = false;
                    });
            }
        },
    ],
});