trevorpan
1 week ago
120
4
Vue

Every page load calls a method that is supposed to only run once

Posted 1 week ago by trevorpan

Hi,

Was wondering if you could point out where this loads the updateBiddedStatus each time someone views the page.


    <div id="countdown">

        <slot :countdown="countdown">{{ until }}</slot>

        <div v-if="bidded" v-text="biddedText" v-bind:slug="slug">

        </div>

        <div v-else>
            <span>{{ remaining.days }} d, </span>
            <span>{{ remaining.hours }} h, </span>
            <span>{{ remaining.minutes }} m, </span>
            <span>{{ remaining.seconds }} s </span>
        </div>
    </div>

</template>


<script>

    import moment from 'moment';

    export default {

        props: {
            slug: '',
            until: String,
            biddedText: {
                default: 'This job has bidded.'
            },
        },

        data() {
            return {
                now: new Date(),
                countdown: '',
            }
        },

        created() {
            let interval = this.refreshEverySecond();

            this.$on('bidded', () => clearInterval(interval));
            this.$on('bidded', () => this.updateBiddedStatus());
        },

        computed: {
            bidded() {
                return this.remaining.total <= 0;
            },

            remaining() {
                let remaining = moment.duration(Date.parse(this.until) - this.now);

                if (remaining <= 0) {
                    this.$emit('bidded');
                }

                return {
                    total: remaining,
                    days: remaining.days(),
                    hours: remaining.hours(),
                    minutes: remaining.minutes(),
                    seconds: remaining.seconds()
                };
            },
        },

        methods: {

            refreshEverySecond() {
                return setInterval(() => {
                    this.now = new Date();
                }, 1000);
            },

            async updateBiddedStatus() {
                await window.axios.put(`/jobs/` + this.slug + `/updateBiddedStatus`);
            },
        }
    }
</script>

I added this condition in the following method but it seems to be called nonetheless as telescope says the DeadlineExpired event is fired and sends a mail. The goal is for the updateBiddedStatus to only run once - when the countdown timer gets to zero.

      public function updateBiddedStatus(Request $request, SlugJob $job)
        {
           // dd($job->complete);
            if ($job->complete === false) {
                DB::transaction(function () use($job) {

                    DB::table('jobs')
                        ->where([
                            ['id', $job->id],
                            ['bidded', '=', 0]
                        ])
                        ->update(array('bidded' => 1));
                });

                event(new DeadlineExpired($job));
            }
        }

Thank you ~

Please sign in or create an account to participate in this conversation.