Add new global pinia webcron store to handle webcron requests. The old implementation would add additional intervals for each page request.
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
import { Head, usePage } from '@inertiajs/vue3'
|
import { Head, usePage, } from '@inertiajs/vue3'
|
||||||
import AppSidebar from '@/components/AppSidebar.vue';
|
import AppSidebar from '@/components/AppSidebar.vue';
|
||||||
import { onMounted } from 'vue';
|
import { onMounted } from 'vue';
|
||||||
import 'vue-sonner/style.css'
|
import 'vue-sonner/style.css'
|
||||||
@@ -9,36 +9,27 @@ import { Button } from '@/components/ui/crm-button'
|
|||||||
import { SidebarProvider } from '@/components/ui/sidebar';
|
import { SidebarProvider } from '@/components/ui/sidebar';
|
||||||
import { AlertDialog, AlertDialogContent, AlertDialogDescription, AlertDialogFooter, AlertDialogHeader, AlertDialogTitle, } from '@/components/ui/alert-dialog'
|
import { AlertDialog, AlertDialogContent, AlertDialogDescription, AlertDialogFooter, AlertDialogHeader, AlertDialogTitle, } from '@/components/ui/alert-dialog'
|
||||||
import { alertStore } from '@/stores/alertStore';
|
import { alertStore } from '@/stores/alertStore';
|
||||||
import axios from 'axios';
|
import { webcronStore } from '@/stores/webcronStore';
|
||||||
|
|
||||||
const alert = alertStore()
|
const alert = alertStore()
|
||||||
|
const webcron = webcronStore()
|
||||||
|
|
||||||
const props = defineProps<{
|
const props = defineProps<{
|
||||||
title: string
|
title: string
|
||||||
}>();
|
}>();
|
||||||
const isOpen = usePage().props.sidebarOpen;
|
const isOpen = usePage().props.sidebarOpen;
|
||||||
const cron = usePage().props.cron
|
const cron = usePage().props.cron
|
||||||
|
|
||||||
|
|
||||||
onMounted(() => {
|
onMounted(() => {
|
||||||
if (navigator.platform.toUpperCase().indexOf('MAC') >= 0) {
|
if (navigator.platform.toUpperCase().indexOf('MAC') >= 0) {
|
||||||
document.body.classList.add('is-mac')
|
document.body.classList.add('is-mac')
|
||||||
}
|
}
|
||||||
|
|
||||||
if (cron) {
|
if (cron) {
|
||||||
triggerWebcron()
|
webcron.start()
|
||||||
setInterval(triggerWebcron, 30000)
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
const triggerWebcron = async function () {
|
|
||||||
await axios.get('/webcron')
|
|
||||||
.catch(function (response) {
|
|
||||||
if (response.status >= 400) {
|
|
||||||
console.error(response.message)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<template>
|
<template>
|
||||||
|
|||||||
@@ -1,4 +1,3 @@
|
|||||||
import { options } from '@coders-tm/vue-number-format';
|
|
||||||
import { defineStore } from 'pinia'
|
import { defineStore } from 'pinia'
|
||||||
|
|
||||||
export interface AlertOptions {
|
export interface AlertOptions {
|
||||||
|
|||||||
@@ -0,0 +1,32 @@
|
|||||||
|
import { defineStore } from 'pinia'
|
||||||
|
import axios from 'axios';
|
||||||
|
|
||||||
|
export const webcronStore = defineStore('webcron', {
|
||||||
|
state: () => {
|
||||||
|
return {
|
||||||
|
timeout: 1000 * 30,
|
||||||
|
interval: 0,
|
||||||
|
url: '/webcron'
|
||||||
|
}
|
||||||
|
},
|
||||||
|
actions: {
|
||||||
|
start() {
|
||||||
|
if (this.interval) return
|
||||||
|
this.triggerWebcron(this.url)
|
||||||
|
this.interval = setInterval(this.triggerWebcron, this.timeout, this.url)
|
||||||
|
},
|
||||||
|
|
||||||
|
stop() {
|
||||||
|
clearInterval(this.interval)
|
||||||
|
this.interval = 0
|
||||||
|
},
|
||||||
|
triggerWebcron: async (url: string) => {
|
||||||
|
await axios.get(url)
|
||||||
|
.catch(function (response) {
|
||||||
|
if (response.status >= 400) {
|
||||||
|
console.error(response.message)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
Reference in New Issue
Block a user