From d202cdfee8c94a18ac02fa5a7d05fec32a6e5612 Mon Sep 17 00:00:00 2001 From: Pierre-Olivier Mercier Date: Thu, 8 Dec 2022 19:33:51 +0100 Subject: [PATCH] New route to test routine --- api/routines.go | 8 ++++++++ ui/src/lib/components/CardRoutine.svelte | 8 ++++++++ ui/src/lib/routine.js | 12 ++++++++++++ 3 files changed, 28 insertions(+) diff --git a/api/routines.go b/api/routines.go index 16d7c0a..82c5b53 100644 --- a/api/routines.go +++ b/api/routines.go @@ -78,4 +78,12 @@ func declareRoutinesRoutes(cfg *config.Config, router *gin.RouterGroup) { c.JSON(http.StatusOK, nil) }) + + routinesRoutes.POST("/run", func(c *gin.Context) { + routine := c.MustGet("routine").(*reveil.Routine) + + go routine.Launch(cfg) + + c.JSON(http.StatusOK, true) + }) } diff --git a/ui/src/lib/components/CardRoutine.svelte b/ui/src/lib/components/CardRoutine.svelte index cc63566..4ca9b1b 100644 --- a/ui/src/lib/components/CardRoutine.svelte +++ b/ui/src/lib/components/CardRoutine.svelte @@ -37,6 +37,14 @@ > + {routine.name} {#if routine.steps} diff --git a/ui/src/lib/routine.js b/ui/src/lib/routine.js index 95e6691..f08b2c2 100644 --- a/ui/src/lib/routine.js +++ b/ui/src/lib/routine.js @@ -26,6 +26,18 @@ export class Routine { } } + async launch() { + const res = await fetch(`api/routines/${this.id}/run`, { + method: 'POST', + headers: {'Accept': 'application/json'} + }); + if (res.status == 200) { + return true; + } else { + throw new Error((await res.json()).errmsg); + } + } + async save() { const res = await fetch(this.id?`api/routines/${this.id}`:'api/routines', { method: this.id?'PUT':'POST',