| 1 | <template> |
| 2 | <div class="contents gap-3"> |
| 3 | <div class="flex items-center gap-3"> |
| 4 | <n-button |
| 5 | :size |
| 6 | :type="job.enabled ? 'warning' : 'success'" |
| 7 | secondary |
| 8 | :loading="loadingAction" |
| 9 | class="grow" |
| 10 | @click="toggleState()" |
| 11 | > |
| 12 | <template #icon> |
| 13 | <Icon :name="job.enabled ? PauseIcon : StartIcon" /> |
| 14 | </template> |
| 15 | {{ job.enabled ? "Pause" : "Start" }} |
| 16 | </n-button> |
| 17 | </div> |
| 18 | <div class="flex items-center gap-3"> |
| 19 | <n-button :size type="success" secondary :loading="loadingRun" @click="run()"> |
| 20 | <template #icon> |
| 21 | <Icon :name="RunIcon" /> |
| 22 | </template> |
| 23 | Run once |
| 24 | </n-button> |
| 25 | <n-button :size secondary :loading="loadingUpdate" @click="showForm = true"> |
| 26 | <template #icon> |
| 27 | <Icon :name="UpdatedIcon" /> |
| 28 | </template> |
| 29 | </n-button> |
| 30 | </div> |
| 31 | </div> |
| 32 | |
| 33 | <n-modal |
| 34 | v-model:show="showForm" |
| 35 | display-directive="show" |
| 36 | preset="card" |
| 37 | :style="{ maxWidth: 'min(450px, 90vw)', overflow: 'hidden' }" |
| 38 | :title="`Update ${job.name}`" |
| 39 | :bordered="false" |
| 40 | segmented |
| 41 | > |
| 42 | <JobForm :job @updated="update($event)" /> |
| 43 | </n-modal> |
| 44 | </template> |
| 45 | |
| 46 | <script setup lang="ts"> |
| 47 | import type { ButtonSize } from "naive-ui" |
| 48 | import type { UpdateJobPayload } from "@/api/endpoints/scheduler" |
| 49 | import type { Job } from "@/types/scheduler.d" |
| 50 | import { NButton, NModal, useMessage } from "naive-ui" |
| 51 | import { ref, toRefs } from "vue" |
| 52 | import Api from "@/api" |
| 53 | import Icon from "@/components/common/Icon.vue" |
| 54 | import JobForm from "./JobForm.vue" |
| 55 | |
| 56 | const props = defineProps<{ job: Job; size?: ButtonSize; inline?: boolean }>() |
| 57 | const { job, size } = toRefs(props) |
| 58 | |
| 59 | const StartIcon = "material-symbols:autoplay" |
| 60 | const PauseIcon = "carbon:pause-filled" |
| 61 | const RunIcon = "carbon:play" |
| 62 | const UpdatedIcon = "carbon:settings-adjust" |
| 63 | |
| 64 | const message = useMessage() |
| 65 | const showForm = ref(false) |
| 66 | const loadingRun = ref(false) |
| 67 | const loadingAction = ref(false) |
| 68 | const loadingUpdate = ref(false) |
| 69 | |
| 70 | function toggleState() { |
| 71 | loadingAction.value = true |
| 72 | |
| 73 | const action = job.value.enabled ? "pause" : "start" |
| 74 | |
| 75 | Api.scheduler |
| 76 | .jobAction(job.value.id, action) |
| 77 | .then(res => { |
| 78 | if (res.data.success) { |
| 79 | job.value.enabled = action === "start" |
| 80 | message.success(res.data?.message || "Job updated successfully.") |
| 81 | } else { |
| 82 | message.warning(res.data?.message || "An error occurred. Please try again later.") |
| 83 | } |
| 84 | }) |
| 85 | .catch(err => { |
| 86 | message.error(err.response?.data?.message || "An error occurred. Please try again later.") |
| 87 | }) |
| 88 | .finally(() => { |
| 89 | loadingAction.value = false |
| 90 | }) |
| 91 | } |
| 92 | |
| 93 | function run() { |
| 94 | loadingRun.value = true |
| 95 | |
| 96 | Api.scheduler |
| 97 | .jobAction(job.value.id, "run") |
| 98 | .then(res => { |
| 99 | if (res.data.success) { |
| 100 | // TODO-FE: check timezone with Taylor |
| 101 | job.value.last_success = new Date() |
| 102 | message.success(res.data?.message || "Job executed successfully.") |
| 103 | } else { |
| 104 | message.warning(res.data?.message || "An error occurred. Please try again later.") |
| 105 | } |
| 106 | }) |
| 107 | .catch(err => { |
| 108 | message.error(err.response?.data?.message || "An error occurred. Please try again later.") |
| 109 | }) |
| 110 | .finally(() => { |
| 111 | loadingRun.value = false |
| 112 | }) |
| 113 | } |
| 114 | |
| 115 | function update(payload: UpdateJobPayload) { |
| 116 | showForm.value = false |
| 117 | job.value.time_interval = payload.time_interval |
| 118 | } |
| 119 | </script> |