New route to launch action individually

This commit is contained in:
nemunaire 2022-12-08 20:15:00 +01:00
parent d202cdfee8
commit 05c87ac7b3
3 changed files with 62 additions and 0 deletions

View File

@ -2,6 +2,7 @@ package api
import (
"fmt"
"log"
"net/http"
"github.com/gin-gonic/gin"
@ -90,4 +91,23 @@ func declareActionsRoutes(cfg *config.Config, router *gin.RouterGroup) {
c.JSON(http.StatusOK, nil)
})
actionsRoutes.POST("/run", func(c *gin.Context) {
action := c.MustGet("action").(*reveil.Action)
cmd, err := action.Launch()
if err != nil {
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": fmt.Sprintf("Unable to run the action: %s", err.Error())})
return
}
go func() {
err := cmd.Wait()
if err != nil {
log.Printf("%q: %s", action.Name, err.Error())
}
}()
c.JSON(http.StatusOK, true)
})
}

View File

@ -25,6 +25,18 @@ export class Action {
}
}
async launch() {
const res = await fetch(`api/actions/${this.id}/run`, {
method: 'POST',
headers: {'Accept': 'application/json'}
});
if (res.status == 200) {
return true;
} else {
throw new Error((await res.json()).errmsg);
}
}
toggleEnable() {
this.enabled = !this.enabled;
this.save();

View File

@ -3,6 +3,7 @@
import {
Container,
Icon,
Input,
ListGroup,
ListGroupItem,
@ -10,6 +11,14 @@
} from 'sveltestrap';
import { getAction } from '$lib/action';
import { actions } from '$lib/stores/actions';
function deleteThis(action) {
action.delete().then(() => {
actions.refresh();
goto('routines/actions/');
})
}
</script>
{#await getAction($page.params.aid)}
@ -34,5 +43,26 @@
<Input type="switch" on:change={() => action.toggleEnable()} checked={action.enabled} />
</ListGroupItem>
</ListGroup>
<ListGroup class="my-2 text-center">
<ListGroupItem
action
tag="button"
class="text-success fw-bold"
on:click={() => action.launch()}
>
<Icon name="play-fill" />
Lancer cette action
</ListGroupItem>
<ListGroupItem
action
tag="button"
class="text-danger fw-bold"
on:click={() => deleteThis(action)}
>
<Icon name="trash" />
Supprimer cette action
</ListGroupItem>
</ListGroup>
</Container>
{/await}