New route to delete the next alarm
This commit is contained in:
parent
4e0450faca
commit
fa13484718
@ -22,6 +22,16 @@ func declareAlarmsRoutes(cfg *config.Config, db *reveil.LevelDBStorage, resetTim
|
|||||||
c.JSON(http.StatusOK, alarm)
|
c.JSON(http.StatusOK, alarm)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
router.DELETE("/alarms/next", func(c *gin.Context) {
|
||||||
|
err := reveil.DropNextAlarm(cfg, db)
|
||||||
|
if err != nil {
|
||||||
|
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": err.Error()})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
c.JSON(http.StatusOK, true)
|
||||||
|
})
|
||||||
|
|
||||||
router.GET("/alarms/single", func(c *gin.Context) {
|
router.GET("/alarms/single", func(c *gin.Context) {
|
||||||
alarms, err := reveil.GetAlarmsSingle(db)
|
alarms, err := reveil.GetAlarmsSingle(db)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -69,6 +69,45 @@ func GetNextAlarm(cfg *config.Config, db *LevelDBStorage) (*time.Time, error) {
|
|||||||
return closestAlarm, nil
|
return closestAlarm, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func DropNextAlarm(cfg *config.Config, db *LevelDBStorage) error {
|
||||||
|
timenext, err := GetNextAlarm(cfg, db)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
alarmsRepeated, err := GetAlarmsRepeated(db)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, alarm := range alarmsRepeated {
|
||||||
|
next := alarm.GetNextOccurence(cfg, db)
|
||||||
|
if next != nil && *next == *timenext {
|
||||||
|
start := Date(*next)
|
||||||
|
stop := Date((*next).Add(time.Second))
|
||||||
|
|
||||||
|
return PutAlarmException(db, &AlarmException{
|
||||||
|
Start: &start,
|
||||||
|
End: &stop,
|
||||||
|
Comment: fmt.Sprintf("Automatic exception to cancel recurrent alarm %s", next.Format("Mon at 15:05")),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
alarmsSingle, err := GetAlarmsSingle(db)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, alarm := range alarmsSingle {
|
||||||
|
if alarm.Time == *timenext {
|
||||||
|
return DeleteAlarmSingle(db, alarm)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return fmt.Errorf("Unable to find the next alarm")
|
||||||
|
}
|
||||||
|
|
||||||
type Exceptions []time.Time
|
type Exceptions []time.Time
|
||||||
|
|
||||||
func (e Exceptions) Len() int {
|
func (e Exceptions) Len() int {
|
||||||
|
@ -33,6 +33,18 @@ export async function alarmNextTrack() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export async function deleteNextAlarm() {
|
||||||
|
const res = await fetch('api/alarms/next', {
|
||||||
|
method: 'DELETE',
|
||||||
|
headers: {'Accept': 'application/json'},
|
||||||
|
});
|
||||||
|
if (res.status == 200) {
|
||||||
|
return await res.json();
|
||||||
|
} else {
|
||||||
|
throw new Error((await res.json()).errmsg);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export async function alarmStop() {
|
export async function alarmStop() {
|
||||||
const res = await fetch('api/alarm', {
|
const res = await fetch('api/alarm', {
|
||||||
method: 'DELETE',
|
method: 'DELETE',
|
||||||
|
@ -7,8 +7,9 @@
|
|||||||
|
|
||||||
import CycleCounter from '$lib/components/CycleCounter.svelte';
|
import CycleCounter from '$lib/components/CycleCounter.svelte';
|
||||||
import DateFormat from '$lib/components/DateFormat.svelte';
|
import DateFormat from '$lib/components/DateFormat.svelte';
|
||||||
import { isAlarmActive, alarmNextTrack, runAlarm, alarmStop } from '$lib/alarm';
|
import { isAlarmActive, alarmNextTrack, runAlarm, alarmStop, deleteNextAlarm } from '$lib/alarm';
|
||||||
import { getNextAlarm, newNCyclesAlarm } from '$lib/alarmsingle';
|
import { getNextAlarm, newNCyclesAlarm } from '$lib/alarmsingle';
|
||||||
|
import { alarmsExceptions } from '$lib/stores/alarmexceptions';
|
||||||
import { alarmsSingle } from '$lib/stores/alarmsingle';
|
import { alarmsSingle } from '$lib/stores/alarmsingle';
|
||||||
import { quotes } from '$lib/stores/quotes';
|
import { quotes } from '$lib/stores/quotes';
|
||||||
|
|
||||||
@ -43,6 +44,14 @@
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function dropNextAlarm() {
|
||||||
|
deleteNextAlarm().then(() => {
|
||||||
|
alarmsExceptions.clear();
|
||||||
|
alarmsSingle.clear();
|
||||||
|
reloadNextAlarm();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
let extinctionInProgress = false;
|
let extinctionInProgress = false;
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
@ -90,6 +99,13 @@
|
|||||||
{:else}
|
{:else}
|
||||||
<DateFormat date={nextalarm} dateStyle="short" timeStyle="long" />
|
<DateFormat date={nextalarm} dateStyle="short" timeStyle="long" />
|
||||||
{/if}
|
{/if}
|
||||||
|
<button
|
||||||
|
class="btn btn-lg btn-link"
|
||||||
|
title="Supprimer ce prochain réveil"
|
||||||
|
on:click={dropNextAlarm}
|
||||||
|
>
|
||||||
|
<Icon name="x-circle-fill" />
|
||||||
|
</button>
|
||||||
{/if}
|
{/if}
|
||||||
{/await}
|
{/await}
|
||||||
</div>
|
</div>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user