From 0f96e741d69a404123930e56b18c2cf883f448b6 Mon Sep 17 00:00:00 2001 From: Ashley Date: Sun, 17 Sep 2023 06:13:01 +0100 Subject: [PATCH] feat: Add default dashboard layout - Added a default dashboard layout consisting of several widgets. - Each widget has a unique ID, type, and grid configuration. - The `useDashboardStore` state now initializes with the default dashboard layout. fix: Update runJob function in tasks store - Updated the `runJob` function in the `tasks` store to properly handle the response from the API. - If the job is null, the function will return early. - The updated job is now properly stored in the store. feat: Update toMinutes filter - Updated the `toMinutes` filter to include a new `utc` parameter. - The `utc` parameter determines whether to consider UTC time or local time. - Moment objects are created using the respective time zone option. - The difference in minutes is calculated between the input value and the current time. --- frontend/src/stores/dashboard.ts | 44 ++++++++++++++++++++-------- frontend/src/stores/tasks.ts | 18 ++++++++++++ frontend/src/ts/filters/toMinutes.ts | 12 ++++++-- 3 files changed, 59 insertions(+), 15 deletions(-) diff --git a/frontend/src/stores/dashboard.ts b/frontend/src/stores/dashboard.ts index 8d8d0f087..b35e83027 100644 --- a/frontend/src/stores/dashboard.ts +++ b/frontend/src/stores/dashboard.ts @@ -7,22 +7,42 @@ interface DashboardStoreState { dashboard: WidgetOptions[]; } +const defaultDashboard: WidgetOptions[] = [ + { + id: nanoid(), + type: "InvitesTotal", + grid: { w: 2, h: 2 }, + }, + { + id: nanoid(), + type: "UsersTotal", + grid: { w: 2, h: 2 }, + }, + { + id: nanoid(), + type: "TasksTotal", + grid: { w: 2, h: 2 }, + }, + { + id: nanoid(), + type: "UsersGraph", + grid: { w: 3, h: 4 }, + }, + { + id: nanoid(), + type: "InvitesGraph", + grid: { w: 3, h: 4 }, + }, +]; + export const useDashboardStore = defineStore("dashboard", { state: (): DashboardStoreState => ({ - dashboard: [ - { - id: nanoid(), - type: "InvitesTotal", - grid: { w: 2, h: 2 }, - }, - { - id: nanoid(), - type: "UsersTotal", - grid: { w: 2, h: 2 }, - }, - ], + dashboard: defaultDashboard, }), actions: { + resetDashboard() { + this.dashboard = defaultDashboard; + }, updateWidget(widget: WidgetOptions) { const index = this.dashboard.findIndex((w) => w.id === widget.id); this.dashboard[index] = widget; diff --git a/frontend/src/stores/tasks.ts b/frontend/src/stores/tasks.ts index 7d7424be0..ce7f46e48 100644 --- a/frontend/src/stores/tasks.ts +++ b/frontend/src/stores/tasks.ts @@ -57,6 +57,24 @@ export const useTasksStore = defineStore("tasks", { // Return the job return job.data as Job; }, + async runJob(id: string) { + // Run the job + const job = await this.$axios.post(`/api/scheduler/jobs/${id}/run`).catch((err) => { + this.$toast.error("Could not run job"); + console.error(err); + return null; + }); + + // If the job is null, return + if (job === null) return; + + // Update the job in the store + const index = this.jobs.findIndex((job: Job) => job.id === id); + if (index !== -1) this.jobs[index] = job.data; + + // Return the job + return job.data as Job; + }, async pauseJob(id: string) { // Pause the job const job = await this.$axios.post(`/api/scheduler/jobs/${id}/pause`).catch((err) => { diff --git a/frontend/src/ts/filters/toMinutes.ts b/frontend/src/ts/filters/toMinutes.ts index 68b975df1..284759a88 100644 --- a/frontend/src/ts/filters/toMinutes.ts +++ b/frontend/src/ts/filters/toMinutes.ts @@ -3,11 +3,17 @@ import moment from "moment"; /** * toMinutes - Convert a string date or date object to minutes from now * - * @param {string | Date} value - The date or date string to convert to minutes from now + * @param value - The date or date string to convert to minutes from now + * @param utc - Whether to use UTC or not * @returns The minutes from now */ -function toMinutes(value: string | Date): number { - return moment(value).diff(moment(), "minutes"); +function toMinutes(value: string | Date, utc: boolean = true): number { + // Define different moments for the local times + const localDateTime = utc ? moment.utc(value) : moment(value); + const localNow = moment(); + + // Calculate the difference in minutes between the times + return localDateTime.diff(localNow, "minutes"); } export default toMinutes;