diff --git a/.gitignore b/.gitignore index f540086..6bc2de2 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ actions +alarms.db gongs reveil routines diff --git a/api/alarms.go b/api/alarms.go index bcb82db..36a6599 100644 --- a/api/alarms.go +++ b/api/alarms.go @@ -1,86 +1,266 @@ package api import ( + "fmt" "net/http" + "time" "github.com/gin-gonic/gin" "git.nemunai.re/nemunaire/reveil/config" + "git.nemunai.re/nemunaire/reveil/model" ) -func declareAlarmsRoutes(cfg *config.Config, router *gin.RouterGroup) { +func declareAlarmsRoutes(cfg *config.Config, db *reveil.LevelDBStorage, router *gin.RouterGroup) { router.GET("/alarms/next", func(c *gin.Context) { }) - router.POST("/alarms", func(c *gin.Context) { + router.GET("/alarms/single", func(c *gin.Context) { + alarms, err := reveil.GetAlarmsSingle(db) + if err != nil { + c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": err.Error()}) + return + } + + c.JSON(http.StatusOK, alarms) }) + router.POST("/alarms/single", func(c *gin.Context) { + var alarm reveil.AlarmSingle + if err := c.ShouldBindJSON(&alarm); err != nil { + c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"errmsg": err.Error()}) + return + } - router.GET("/alarms/manuals", func(c *gin.Context) { + if time.Now().After(alarm.Time) { + c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"errmsg": "This date is already passed."}) + return + } - }) - router.GET("/alarms/usuals", func(c *gin.Context) { - - }) - router.GET("/alarms/excepts", func(c *gin.Context) { + alarm.Id = nil + if err := reveil.PutAlarmSingle(db, &alarm); err != nil { + c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": err.Error()}) + return + } + c.JSON(http.StatusOK, alarm) }) - manualAlarmsRoutes := router.Group("/alarms/manuals/:aid") - manualAlarmsRoutes.Use(manualAlarmHandler) + router.GET("/alarms/repeated", func(c *gin.Context) { + alarms, err := reveil.GetAlarmsRepeated(db) + if err != nil { + c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": err.Error()}) + return + } - manualAlarmsRoutes.GET("", func(c *gin.Context) { + c.JSON(http.StatusOK, alarms) + }) + router.POST("/alarms/repeated", func(c *gin.Context) { + var alarm reveil.AlarmRepeated + if err := c.ShouldBindJSON(&alarm); err != nil { + c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"errmsg": err.Error()}) + return + } + + alarm.Id = nil + if err := reveil.PutAlarmRepeated(db, &alarm); err != nil { + c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": err.Error()}) + return + } + + c.JSON(http.StatusOK, alarm) + }) + + router.GET("/alarms/exceptions", func(c *gin.Context) { + exceptions, err := reveil.GetAlarmExceptions(db) + if err != nil { + c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": err.Error()}) + return + } + + c.JSON(http.StatusOK, exceptions) + }) + router.POST("/alarms/exceptions", func(c *gin.Context) { + var alarm reveil.AlarmException + if err := c.ShouldBindJSON(&alarm); err != nil { + c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"errmsg": err.Error()}) + return + } + + if alarm.Start == nil { + c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"errmsg": "Start not defined"}) + return + } + if alarm.End == nil { + c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"errmsg": "End not defined"}) + return + } + if time.Now().After(time.Time(*alarm.End)) { + c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"errmsg": "End date is already passed."}) + return + } + if !time.Time(*alarm.End).After(time.Time(*alarm.Start)) { + c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"errmsg": "Start is defined after End. Please verify your inputs."}) + return + } + + alarm.Id = nil + if err := reveil.PutAlarmException(db, &alarm); err != nil { + c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": err.Error()}) + return + } + + c.JSON(http.StatusOK, alarm) + }) + + singleAlarmsRoutes := router.Group("/alarms/single/:aid") + singleAlarmsRoutes.Use(func(c *gin.Context) { + id, err := reveil.NewIdentifierFromString(c.Param("aid")) + if err != nil { + c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"errmsg": fmt.Sprintf("Invalid alarm idenfifier: %s", err.Error())}) + return + } + + alarm, err := reveil.GetAlarmSingle(db, id) + if err != nil { + c.AbortWithStatusJSON(http.StatusNotFound, gin.H{"errmsg": err.Error()}) + return + } + + c.Set("alarm", alarm) + + c.Next() + }) + + singleAlarmsRoutes.GET("", func(c *gin.Context) { c.JSON(http.StatusOK, c.MustGet("alarm")) }) - manualAlarmsRoutes.PUT("", func(c *gin.Context) { - c.JSON(http.StatusOK, c.MustGet("alarm")) + singleAlarmsRoutes.PUT("", func(c *gin.Context) { + oldalarm := c.MustGet("alarm").(*reveil.AlarmSingle) + + var alarm reveil.AlarmSingle + if err := c.ShouldBindJSON(&alarm); err != nil { + c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"errmsg": err.Error()}) + return + } + + alarm.Id = oldalarm.Id + if err := reveil.PutAlarmSingle(db, &alarm); err != nil { + c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": err.Error()}) + return + } + + c.JSON(http.StatusOK, alarm) }) - manualAlarmsRoutes.DELETE("", func(c *gin.Context) { - c.JSON(http.StatusOK, c.MustGet("alarm")) + singleAlarmsRoutes.DELETE("", func(c *gin.Context) { + alarm := c.MustGet("alarm").(*reveil.AlarmSingle) + + if err := reveil.DeleteAlarmSingle(db, alarm); err != nil { + c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": err.Error()}) + return + } + + c.JSON(http.StatusOK, nil) }) - usualAlarmsRoutes := router.Group("/alarms/usuals/:aid") - usualAlarmsRoutes.Use(usualAlarmHandler) + repeatedAlarmsRoutes := router.Group("/alarms/repeated/:aid") + repeatedAlarmsRoutes.Use(func(c *gin.Context) { + id, err := reveil.NewIdentifierFromString(c.Param("aid")) + if err != nil { + c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"errmsg": fmt.Sprintf("Invalid alarm idenfifier: %s", err.Error())}) + return + } - usualAlarmsRoutes.GET("", func(c *gin.Context) { - c.JSON(http.StatusOK, c.MustGet("alarm")) - }) - usualAlarmsRoutes.PUT("", func(c *gin.Context) { - c.JSON(http.StatusOK, c.MustGet("alarm")) - }) - usualAlarmsRoutes.DELETE("", func(c *gin.Context) { - c.JSON(http.StatusOK, c.MustGet("alarm")) + alarm, err := reveil.GetAlarmRepeated(db, id) + if err != nil { + c.AbortWithStatusJSON(http.StatusNotFound, gin.H{"errmsg": err.Error()}) + return + } + + c.Set("alarm", alarm) + + c.Next() }) - exceptAlarmsRoutes := router.Group("/alarms/excepts/:aid") - exceptAlarmsRoutes.Use(exceptAlarmHandler) + repeatedAlarmsRoutes.GET("", func(c *gin.Context) { + c.JSON(http.StatusOK, c.MustGet("alarm")) + }) + repeatedAlarmsRoutes.PUT("", func(c *gin.Context) { + oldalarm := c.MustGet("alarm").(*reveil.AlarmRepeated) - exceptAlarmsRoutes.GET("", func(c *gin.Context) { + var alarm reveil.AlarmRepeated + if err := c.ShouldBindJSON(&alarm); err != nil { + c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"errmsg": err.Error()}) + return + } + + alarm.Id = oldalarm.Id + if err := reveil.PutAlarmRepeated(db, &alarm); err != nil { + c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": err.Error()}) + return + } + + c.JSON(http.StatusOK, alarm) + }) + repeatedAlarmsRoutes.DELETE("", func(c *gin.Context) { + alarm := c.MustGet("alarm").(*reveil.AlarmRepeated) + + if err := reveil.DeleteAlarmRepeated(db, alarm); err != nil { + c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": err.Error()}) + return + } + + c.JSON(http.StatusOK, nil) + }) + + exceptionAlarmsRoutes := router.Group("/alarms/exceptions/:aid") + exceptionAlarmsRoutes.Use(func(c *gin.Context) { + id, err := reveil.NewIdentifierFromString(c.Param("aid")) + if err != nil { + c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"errmsg": fmt.Sprintf("Invalid alarm idenfifier: %s", err.Error())}) + return + } + + alarm, err := reveil.GetAlarmException(db, id) + if err != nil { + c.AbortWithStatusJSON(http.StatusNotFound, gin.H{"errmsg": err.Error()}) + return + } + + c.Set("alarm", alarm) + + c.Next() + }) + + exceptionAlarmsRoutes.GET("", func(c *gin.Context) { c.JSON(http.StatusOK, c.MustGet("alarm")) }) - exceptAlarmsRoutes.PUT("", func(c *gin.Context) { - c.JSON(http.StatusOK, c.MustGet("alarm")) + exceptionAlarmsRoutes.PUT("", func(c *gin.Context) { + oldalarm := c.MustGet("alarm").(*reveil.AlarmException) + + var alarm reveil.AlarmException + if err := c.ShouldBindJSON(&alarm); err != nil { + c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"errmsg": err.Error()}) + return + } + + alarm.Id = oldalarm.Id + if err := reveil.PutAlarmException(db, &alarm); err != nil { + c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": err.Error()}) + return + } + + c.JSON(http.StatusOK, alarm) }) - exceptAlarmsRoutes.DELETE("", func(c *gin.Context) { - c.JSON(http.StatusOK, c.MustGet("alarm")) + exceptionAlarmsRoutes.DELETE("", func(c *gin.Context) { + alarm := c.MustGet("alarm").(*reveil.AlarmException) + + if err := reveil.DeleteAlarmException(db, alarm); err != nil { + c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": err.Error()}) + return + } + + c.JSON(http.StatusOK, nil) }) } - -func manualAlarmHandler(c *gin.Context) { - c.Set("alarm", nil) - - c.Next() -} - -func usualAlarmHandler(c *gin.Context) { - c.Set("alarm", nil) - - c.Next() -} - -func exceptAlarmHandler(c *gin.Context) { - c.Set("alarm", nil) - - c.Next() -} diff --git a/api/routes.go b/api/routes.go index 09d58e7..31e6ef7 100644 --- a/api/routes.go +++ b/api/routes.go @@ -11,7 +11,7 @@ func DeclareRoutes(router *gin.Engine, cfg *config.Config, db *reveil.LevelDBSto apiRoutes := router.Group("/api") declareActionsRoutes(cfg, apiRoutes) - declareAlarmsRoutes(cfg, apiRoutes) + declareAlarmsRoutes(cfg, db, apiRoutes) declareGongsRoutes(cfg, apiRoutes) declareHistoryRoutes(cfg, apiRoutes) declareQuotesRoutes(cfg, apiRoutes) diff --git a/model/alarm.go b/model/alarm.go new file mode 100644 index 0000000..4c35d7f --- /dev/null +++ b/model/alarm.go @@ -0,0 +1,193 @@ +package reveil + +import ( + "fmt" + "time" +) + +type Date time.Time + +func (d *Date) MarshalJSON() (dst []byte, err error) { + return []byte(fmt.Sprintf("\"%04d-%02d-%02d\"", time.Time(*d).Year(), time.Time(*d).Month(), time.Time(*d).Day())), nil +} + +func (d *Date) UnmarshalJSON(src []byte) error { + tmp, err := time.Parse("\"2006-01-02\"", string(src)) + if err != nil { + return err + } + *d = Date(tmp) + + return nil +} + +type Hour time.Time + +func (h *Hour) MarshalJSON() (dst []byte, err error) { + return []byte(fmt.Sprintf("\"%02d:%02d\"", time.Time(*h).Hour(), time.Time(*h).Minute())), nil +} + +func (h *Hour) UnmarshalJSON(src []byte) error { + tmp, err := time.Parse("\"15:04\"", string(src)) + if err != nil { + return err + } + *h = Hour(tmp) + + return nil +} + +type AlarmRepeated struct { + Id Identifier `json:"id"` + Weekday uint8 `json:"weekday"` + StartTime *Hour `json:"time"` + FollowingRoutines []Identifier `json:"routines"` + IgnoreExceptions bool `json:"ignore_exceptions"` + Comment string `json:"comment"` +} + +func GetAlarmRepeated(db *LevelDBStorage, id Identifier) (alarm *AlarmRepeated, err error) { + alarm = &AlarmRepeated{} + err = db.get(fmt.Sprintf("alarm-repeated-%s", id.ToString()), alarm) + return +} + +func GetAlarmsRepeated(db *LevelDBStorage) (alarms []*AlarmRepeated, err error) { + iter := db.search("alarm-repeated-") + defer iter.Release() + + for iter.Next() { + var a AlarmRepeated + + err = decodeData(iter.Value(), &a) + if err != nil { + return + } + + alarms = append(alarms, &a) + } + + return +} + +func PutAlarmRepeated(db *LevelDBStorage, alarm *AlarmRepeated) (err error) { + var key string + var id Identifier + + if alarm.Id.IsEmpty() { + key, id, err = db.findBytesKey("alarm-repeated-", IDENTIFIER_LEN) + if err != nil { + return err + } + } + + alarm.Id = id + + return db.put(key, alarm) +} + +func DeleteAlarmRepeated(db *LevelDBStorage, alarm *AlarmRepeated) (err error) { + return db.delete(fmt.Sprintf("alarm-repeated-%s", alarm.Id.ToString())) +} + +type AlarmSingle struct { + Id Identifier `json:"id"` + Time time.Time `json:"time"` + FollowingRoutines []Identifier `json:"routines"` + Comment string `json:"comment"` +} + +func GetAlarmSingle(db *LevelDBStorage, id Identifier) (alarm *AlarmSingle, err error) { + alarm = &AlarmSingle{} + err = db.get(fmt.Sprintf("alarm-single-%s", id.ToString()), alarm) + return +} + +func GetAlarmsSingle(db *LevelDBStorage) (alarms []*AlarmSingle, err error) { + iter := db.search("alarm-single-") + defer iter.Release() + + for iter.Next() { + var a AlarmSingle + + err = decodeData(iter.Value(), &a) + if err != nil { + return + } + + alarms = append(alarms, &a) + } + + return +} + +func PutAlarmSingle(db *LevelDBStorage, alarm *AlarmSingle) (err error) { + var key string + var id Identifier + + if alarm.Id.IsEmpty() { + key, id, err = db.findBytesKey("alarm-single-", IDENTIFIER_LEN) + if err != nil { + return err + } + } + + alarm.Id = id + + return db.put(key, alarm) +} + +func DeleteAlarmSingle(db *LevelDBStorage, alarm *AlarmSingle) (err error) { + return db.delete(fmt.Sprintf("alarm-single-%s", alarm.Id.ToString())) +} + +type AlarmException struct { + Id Identifier `json:"id"` + Start *Date `json:"start"` + End *Date `json:"end"` + Comment string `json:"comment"` +} + +func GetAlarmException(db *LevelDBStorage, id Identifier) (alarm *AlarmException, err error) { + alarm = &AlarmException{} + err = db.get(fmt.Sprintf("alarm-exception-%s", id.ToString()), alarm) + return +} + +func GetAlarmExceptions(db *LevelDBStorage) (alarms []*AlarmException, err error) { + iter := db.search("alarm-exception-") + defer iter.Release() + + for iter.Next() { + var a AlarmException + + err = decodeData(iter.Value(), &a) + if err != nil { + return + } + + alarms = append(alarms, &a) + } + + return +} + +func PutAlarmException(db *LevelDBStorage, alarm *AlarmException) (err error) { + var key string + var id Identifier + + if alarm.Id.IsEmpty() { + key, id, err = db.findBytesKey("alarm-exception-", IDENTIFIER_LEN) + if err != nil { + return err + } + } + + alarm.Id = id + + return db.put(key, alarm) +} + +func DeleteAlarmException(db *LevelDBStorage, alarm *AlarmException) (err error) { + return db.delete(fmt.Sprintf("alarm-exception-%s", alarm.Id.ToString())) +} diff --git a/ui/package.json b/ui/package.json index 528c5c8..2a9146b 100644 --- a/ui/package.json +++ b/ui/package.json @@ -33,6 +33,7 @@ }, "type": "module", "dependencies": { + "dayjs": "^1.11.5", "sass": "^1.49.7", "sass-loader": "^13.0.0", "sveltestrap": "^5.8.3", diff --git a/ui/src/components/ActionList.svelte b/ui/src/components/ActionList.svelte index 7eb287b..046a4a9 100644 --- a/ui/src/components/ActionList.svelte +++ b/ui/src/components/ActionList.svelte @@ -1,5 +1,4 @@ @@ -14,22 +18,36 @@ Exceptions -
- - - Du samedi 14 - au dimanche 26 février - +
+ {#if $alarmsExceptions.list !== null} + {#if $alarmsExceptions.list.length} +
+ {#each $alarmsExceptions.list as alarm (alarm.id)} + + Du + + {/each} +
+ {:else} +

Pas d'exception programmée

+ {/if} + {:else} + {#await alarmsExceptions.refresh()} +
+ Chargement en cours… +
+ {/await} + {/if}
diff --git a/ui/src/components/AlarmRepeatedList.svelte b/ui/src/components/AlarmRepeatedList.svelte index 960de5f..df7faa8 100644 --- a/ui/src/components/AlarmRepeatedList.svelte +++ b/ui/src/components/AlarmRepeatedList.svelte @@ -4,8 +4,12 @@ import { Button, Icon, + Spinner, } from 'sveltestrap'; + import { weekdayStr } from '../lib/alarmrepeated'; + import { alarmsRepeated } from '../stores/alarmrepeated'; + export let flush = false; @@ -14,27 +18,36 @@ Réveils habituels
-
- - Les lundis à 6h50 - - - Les mardis à 6h50 - +
+ {#if $alarmsRepeated.list !== null} + {#if $alarmsRepeated.list.length} +
+ {#each $alarmsRepeated.list as alarm (alarm.id)} + + Les {weekdayStr(alarm.weekday)}s à {alarm.time} + + {/each} +
+ {:else} +

Pas de réveil habituel programmé

+ {/if} + {:else} + {#await alarmsRepeated.refresh()} +
+ Chargement en cours… +
+ {/await} + {/if}
diff --git a/ui/src/components/AlarmSingleList.svelte b/ui/src/components/AlarmSingleList.svelte index 2e09240..095670b 100644 --- a/ui/src/components/AlarmSingleList.svelte +++ b/ui/src/components/AlarmSingleList.svelte @@ -4,8 +4,12 @@ import { Button, Icon, + Spinner, } from 'sveltestrap'; + import DateFormat from '../components/DateFormat.svelte'; + import { alarmsSingle } from '../stores/alarmsingle'; + export let flush = false; @@ -14,14 +18,36 @@ Réveils manuels
-

Pas de prochain réveil manuel programmé

+ {#if $alarmsSingle.list !== null} + {#if $alarmsSingle.list.length} +
+ {#each $alarmsSingle.list as alarm (alarm.id)} + + Le + + {/each} +
+ {:else} +

Pas de prochain réveil manuel programmé

+ {/if} + {:else} + {#await alarmsSingle.refresh()} +
+ Chargement en cours… +
+ {/await} + {/if}
diff --git a/ui/src/components/DateFormat.svelte b/ui/src/components/DateFormat.svelte new file mode 100644 index 0000000..f2ac452 --- /dev/null +++ b/ui/src/components/DateFormat.svelte @@ -0,0 +1,17 @@ + + +{formatDate(date, dateStyle, timeStyle)} diff --git a/ui/src/components/DateRangeFormat.svelte b/ui/src/components/DateRangeFormat.svelte new file mode 100644 index 0000000..0c5ab4e --- /dev/null +++ b/ui/src/components/DateRangeFormat.svelte @@ -0,0 +1,18 @@ + + +{formatRange(startDate, endDate, dateStyle, timeStyle)} diff --git a/ui/src/components/DateTimeInput.svelte b/ui/src/components/DateTimeInput.svelte new file mode 100644 index 0000000..19118ed --- /dev/null +++ b/ui/src/components/DateTimeInput.svelte @@ -0,0 +1,31 @@ + + + diff --git a/ui/src/components/GongsList.svelte b/ui/src/components/GongsList.svelte index ecc6115..3ba4e7c 100644 --- a/ui/src/components/GongsList.svelte +++ b/ui/src/components/GongsList.svelte @@ -91,7 +91,6 @@
Chargement en cours…
- {:then gongs} {/await} {/if} diff --git a/ui/src/lib/alarmexception.js b/ui/src/lib/alarmexception.js new file mode 100644 index 0000000..f9f2249 --- /dev/null +++ b/ui/src/lib/alarmexception.js @@ -0,0 +1,71 @@ +export class AlarmException { + constructor(res) { + if (res) { + this.update(res); + } + } + + update({ id, start, end, comment }) { + this.id = id; + this.start = start; + this.end = end; + this.comment = comment; + } + + _start() { + return new Date(this.start); + } + + _end() { + return new Date(this.end); + } + + async delete() { + const res = await fetch(`api/alarms/exceptions/${this.id}`, { + method: 'DELETE', + 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/alarms/exceptions/${this.id}`:'api/alarms/exceptions', { + method: this.id?'PUT':'POST', + headers: {'Accept': 'application/json'}, + body: JSON.stringify(this), + }); + if (res.status == 200) { + const data = await res.json(); + this.update(data); + return data; + } else { + throw new Error((await res.json()).errmsg); + } + } +} + +export async function getAlarmsException() { + const res = await fetch(`api/alarms/exceptions`, {headers: {'Accept': 'application/json'}}) + if (res.status == 200) { + const data = await res.json(); + if (data === null) + return []; + else + return data.map((t) => new AlarmException(t)); + } else { + throw new Error((await res.json()).errmsg); + } +} + +export async function getAlarmException(aid) { + const res = await fetch(`api/alarms/exceptions/${aid}`, {headers: {'Accept': 'application/json'}}) + if (res.status == 200) { + return new AlarmException(await res.json()); + } else { + throw new Error((await res.json()).errmsg); + } +} diff --git a/ui/src/lib/alarmrepeated.js b/ui/src/lib/alarmrepeated.js new file mode 100644 index 0000000..afbacda --- /dev/null +++ b/ui/src/lib/alarmrepeated.js @@ -0,0 +1,91 @@ +export class AlarmRepeated { + constructor(res) { + if (res) { + this.update(res); + } + } + + update({ id, weekday, time, routines, ignore_exceptions, comment }) { + this.id = id; + this.weekday = weekday; + this.time = time; + this.routines = routines; + this.ignore_exceptions = ignore_exceptions; + this.comment = comment; + } + + async delete() { + const res = await fetch(`api/alarms/repeated/${this.id}`, { + method: 'DELETE', + 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/alarms/repeated/${this.id}`:'api/alarms/repeated', { + method: this.id?'PUT':'POST', + headers: {'Accept': 'application/json'}, + body: JSON.stringify(this), + }); + if (res.status == 200) { + const data = await res.json(); + this.update(data); + return data; + } else { + throw new Error((await res.json()).errmsg); + } + } +} + +export async function getAlarmsRepeated() { + const res = await fetch(`api/alarms/repeated`, {headers: {'Accept': 'application/json'}}) + if (res.status == 200) { + const data = await res.json(); + if (data === null) + return []; + else + return data.map((t) => new AlarmRepeated(t)); + } else { + throw new Error((await res.json()).errmsg); + } +} + +export async function getAlarmRepeated(aid) { + const res = await fetch(`api/alarms/repeated/${aid}`, {headers: {'Accept': 'application/json'}}) + if (res.status == 200) { + return new AlarmRepeated(await res.json()); + } else { + throw new Error((await res.json()).errmsg); + } +} + +export function weekdayStr(weekday) { + switch (weekday) { + case 0: + case "0": + return "dimanche"; + case 1: + case "1": + return "lundi"; + case 2: + case "2": + return "mardi"; + case 3: + case "3": + return "mercredi"; + case 4: + case "4": + return "jeudi"; + case 5: + case "5": + return "vendredi"; + case 6: + case "6": + return "samedi"; + } +} diff --git a/ui/src/lib/alarmsingle.js b/ui/src/lib/alarmsingle.js new file mode 100644 index 0000000..4aecc56 --- /dev/null +++ b/ui/src/lib/alarmsingle.js @@ -0,0 +1,63 @@ +export class AlarmSingle { + constructor(res) { + if (res) { + this.update(res); + } + } + + update({ id, time, routines, comment }) { + this.id = id; + this.time = new Date(time); + this.routines = routines; + this.comment = comment; + } + + async delete() { + const res = await fetch(`api/alarms/single/${this.id}`, { + method: 'DELETE', + 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/alarms/single/${this.id}`:'api/alarms/single', { + method: this.id?'PUT':'POST', + headers: {'Accept': 'application/json'}, + body: JSON.stringify(this), + }); + if (res.status == 200) { + const data = await res.json(); + this.update(data); + return data; + } else { + throw new Error((await res.json()).errmsg); + } + } +} + +export async function getAlarmsSingle() { + const res = await fetch(`api/alarms/single`, {headers: {'Accept': 'application/json'}}) + if (res.status == 200) { + const data = await res.json(); + if (data === null) + return []; + else + return data.map((t) => new AlarmSingle(t)); + } else { + throw new Error((await res.json()).errmsg); + } +} + +export async function getAlarmSingle(aid) { + const res = await fetch(`api/alarms/single/${aid}`, {headers: {'Accept': 'application/json'}}) + if (res.status == 200) { + return new AlarmSingle(await res.json()); + } else { + throw new Error((await res.json()).errmsg); + } +} diff --git a/ui/src/routes/alarms/[kind]/+layout.svelte b/ui/src/routes/alarms/[kind]/+layout.svelte index 63f69a3..c322b14 100644 --- a/ui/src/routes/alarms/[kind]/+layout.svelte +++ b/ui/src/routes/alarms/[kind]/+layout.svelte @@ -15,11 +15,11 @@ function slugToComponent(slug) { switch(slug) { - case "singles": + case "single": return AlarmSingleList; - case "repeateds": + case "repeated": return AlarmRepeatedList; - case "excepts": + case "exceptions": return AlarmExceptionList; } } diff --git a/ui/src/routes/alarms/[kind]/+page.svelte b/ui/src/routes/alarms/[kind]/+page.svelte index 69bceee..6417ee2 100644 --- a/ui/src/routes/alarms/[kind]/+page.svelte +++ b/ui/src/routes/alarms/[kind]/+page.svelte @@ -11,20 +11,20 @@ function slugToComponent(slug) { switch(slug) { - case "singles": + case "single": return AlarmSingleList; - case "repeateds": + case "repeated": return AlarmRepeatedList; case "exceptions": - return AlarmExceptList; + return AlarmExceptionList; } } function slugToText(slug) { switch(slug) { - case "singles": + case "single": return "un réveil manuel"; - case "repeateds": + case "repeated": return "un réveil habituel"; case "exceptions": return "une exception"; diff --git a/ui/src/routes/alarms/[kind]/[aid]/+page.svelte b/ui/src/routes/alarms/[kind]/[aid]/+page.svelte index db09d58..9eed3a5 100644 --- a/ui/src/routes/alarms/[kind]/[aid]/+page.svelte +++ b/ui/src/routes/alarms/[kind]/[aid]/+page.svelte @@ -3,33 +3,150 @@ Button, Col, Container, - Row, Icon, + ListGroup, + ListGroupItem, + Row, + Spinner, } from 'sveltestrap'; + import { goto } from '$app/navigation'; import { page } from '$app/stores'; + import DateFormat from '../../../../components/DateFormat.svelte'; + import DateRangeFormat from '../../../../components/DateRangeFormat.svelte'; + import { getAlarmSingle } from '../../../../lib/alarmsingle'; + import { getAlarmRepeated, weekdayStr } from '../../../../lib/alarmrepeated'; + import { getAlarmException } from '../../../../lib/alarmexception'; + import { alarmsRepeated } from '../../../../stores/alarmrepeated'; + import { alarmsSingle } from '../../../../stores/alarmsingle'; + import { alarmsExceptions } from '../../../../stores/alarmexceptions'; + function slugToTitle(slug) { switch(slug) { - case "manuals": + case "single": return "Réveil manuel"; - case "usuals": + case "repeated": return "Réveil habituel"; - case "excepts": + case "exceptions": return "Exception"; } } + + let objP; + let obj; + + $: { + switch ($page.params["kind"]) { + case "single": + objP = getAlarmSingle($page.params["aid"]); + break; + case "repeated": + objP = getAlarmRepeated($page.params["aid"]); + break; + case "exceptions": + objP = getAlarmException($page.params["aid"]); + break; + } + objP.then((o) => obj = o); + } + + function editThis() { + goto('alarms/' + $page.params["kind"]); + } + + function deleteThis() { + obj.delete().then(() => { + switch($page.params["kind"]) { + case "single": + alarmsSingle.clear(); + break; + case "repeated": + alarmsRepeated.clear(); + break; + case "exceptions": + alarmsExceptions.clear(); + break; + } + + goto('alarms/' + $page.params["kind"]); + }) + } -

- {slugToTitle($page.params["kind"])} du ... -

- {#if $page.params["kind"] == "manuals"} - manuals - {:else if $page.params["kind"] == "usuals"} - ususlas - {:else if $page.params["kind"] == "excepts"} - excepts + {#if $page.params["kind"] == "single"} + {#await objP} +
+ Chargement en cours… +
+ {:then alarm} +

+ {slugToTitle($page.params["kind"])} du +

+ + + Date du réveil + + + Heure du réveil + + + {/await} + {:else if $page.params["kind"] == "repeated"} + {#await objP} +
+ Chargement en cours… +
+ {:then alarm} +

+ {slugToTitle($page.params["kind"])} des {weekdayStr(alarm.weekday)} à {alarm.time} +

+ + + Jour de la semaine {weekdayStr(alarm.weekday)} + + + Heure du réveil {alarm.time} + + + Ignorer les exceptions ? {alarm.ignore_exceptions?"oui":"non"} + + + {/await} + {:else if $page.params["kind"] == "exceptions"} + {#await objP} +
+ Chargement en cours… +
+ {:then exception} +

+ {slugToTitle($page.params["kind"])} du +

+ Entre le + {/await} {/if} + + {#await objP then alarm} + + + + Éditer ce {slugToTitle($page.params["kind"]).toLowerCase()} + + + + Supprimer ce {slugToTitle($page.params["kind"]).toLowerCase()} + + + {/await}
diff --git a/ui/src/routes/alarms/[kind]/new/+page.svelte b/ui/src/routes/alarms/[kind]/new/+page.svelte index 5185d6a..aadb1c7 100644 --- a/ui/src/routes/alarms/[kind]/new/+page.svelte +++ b/ui/src/routes/alarms/[kind]/new/+page.svelte @@ -1,100 +1,155 @@ -
+

{slugToTitle($page.params["kind"])}

- {#if $page.params["kind"] == "manuals"} + {#if $page.params["kind"] == "single"} - + - - - - - - - + {#if $routines.list} + + {#each $routines.list as routine (routine.id)} + + {/each} + + {:else} + {#await routines.refresh()} +
+ Chargement en cours… +
+ {/await} + {/if}
- {:else if $page.params["kind"] == "usuals"} + {:else if $page.params["kind"] == "repeated"} - - - - - - - - + + + + + + + + - + - - - - - - - + {#if $routines.list} + + {#each $routines.list as routine (routine.id)} + + {/each} + + {:else} + {#await routines.refresh()} +
+ Chargement en cours… +
+ {/await} + {/if}
- + - {:else if $page.params["kind"] == "excepts"} + {:else if $page.params["kind"] == "exceptions"} - + - + {/if} -
+
diff --git a/ui/src/stores/actions.js b/ui/src/stores/actions.js index 9133f93..3b9796d 100644 --- a/ui/src/stores/actions.js +++ b/ui/src/stores/actions.js @@ -23,10 +23,6 @@ function createActionsStore() { return list; }, - getActionByFilename: (fname) => { - - }, - update: (res_actions, cb=null) => { if (res_actions.status === 200) { res_actions.json().then((list) => { diff --git a/ui/src/stores/alarmexceptions.js b/ui/src/stores/alarmexceptions.js new file mode 100644 index 0000000..25a6e7c --- /dev/null +++ b/ui/src/stores/alarmexceptions.js @@ -0,0 +1,41 @@ +import { derived, writable } from 'svelte/store'; + +import { getAlarmsException } from '../lib/alarmexception' + +function createAlarmsExceptionStore() { + const { subscribe, set, update } = writable({list: null}); + + return { + subscribe, + + set: (v) => { + update((m) => Object.assign(m, v)); + }, + + clear: () => { + update((m) => m = {list: null}); + }, + + refresh: async () => { + const list = await getAlarmsException(); + + update((m) => (Object.assign(m, {list}))); + return list; + }, + + update: (res_AlarmsException, cb=null) => { + if (res_AlarmsException.status === 200) { + res_AlarmsException.json().then((list) => { + update((m) => (Object.assign(m, {list}))); + + if (cb) { + cb(list); + } + }); + } + }, + }; + +} + +export const alarmsExceptions = createAlarmsExceptionStore(); diff --git a/ui/src/stores/alarmrepeated.js b/ui/src/stores/alarmrepeated.js new file mode 100644 index 0000000..d355738 --- /dev/null +++ b/ui/src/stores/alarmrepeated.js @@ -0,0 +1,41 @@ +import { derived, writable } from 'svelte/store'; + +import { getAlarmsRepeated } from '../lib/alarmrepeated' + +function createAlarmsRepeatedStore() { + const { subscribe, set, update } = writable({list: null}); + + return { + subscribe, + + set: (v) => { + update((m) => Object.assign(m, v)); + }, + + clear: () => { + update((m) => m = {list: null}); + }, + + refresh: async () => { + const list = await getAlarmsRepeated(); + + update((m) => (Object.assign(m, {list}))); + return list; + }, + + update: (res_AlarmsRepeated, cb=null) => { + if (res_AlarmsRepeated.status === 200) { + res_AlarmsRepeated.json().then((list) => { + update((m) => (Object.assign(m, {list}))); + + if (cb) { + cb(list); + } + }); + } + }, + }; + +} + +export const alarmsRepeated = createAlarmsRepeatedStore(); diff --git a/ui/src/stores/alarmsingle.js b/ui/src/stores/alarmsingle.js new file mode 100644 index 0000000..ceeba17 --- /dev/null +++ b/ui/src/stores/alarmsingle.js @@ -0,0 +1,41 @@ +import { derived, writable } from 'svelte/store'; + +import { getAlarmsSingle } from '../lib/alarmsingle' + +function createAlarmsSingleStore() { + const { subscribe, set, update } = writable({list: null}); + + return { + subscribe, + + set: (v) => { + update((m) => Object.assign(m, v)); + }, + + clear: () => { + update((m) => m = {list: null}); + }, + + refresh: async () => { + const list = await getAlarmsSingle(); + + update((m) => (Object.assign(m, {list}))); + return list; + }, + + update: (res_AlarmsSingle, cb=null) => { + if (res_AlarmsSingle.status === 200) { + res_AlarmsSingle.json().then((list) => { + update((m) => (Object.assign(m, {list}))); + + if (cb) { + cb(list); + } + }); + } + }, + }; + +} + +export const alarmsSingle = createAlarmsSingleStore();