-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add dialogs for starting and stopping sessions at specific time…
…s; enhance time handling in session logic
- Loading branch information
Showing
6 changed files
with
248 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
<script setup lang="ts"> | ||
import { ref, watch, computed } from 'vue'; | ||
import { kDialog, kDialogButton, kBlock } from 'konsta/vue'; | ||
import { useTime } from '~/composables/useTime'; | ||
const props = defineProps<{ | ||
opened: boolean, | ||
dayStartAt: string | ||
}>(); | ||
const emit = defineEmits<{ | ||
close: [], | ||
confirm: [date: Date] | ||
}>(); | ||
const { getStartOfDay } = useTime(computed(() => props.dayStartAt)); | ||
const selectedTime = ref(getStartOfDay(new Date())); | ||
const formatDate = (date: Date) => { | ||
try { | ||
return date.toISOString().slice(0, 16); | ||
} catch (e) { | ||
return new Date().toISOString().slice(0, 16); | ||
} | ||
}; | ||
const formattedTime = computed({ | ||
get: () => formatDate(selectedTime.value), | ||
set: (value) => { | ||
const date = new Date(value); | ||
if (!isNaN(date.getTime())) { | ||
selectedTime.value = date; | ||
} | ||
} | ||
}); | ||
watch(() => props.opened, (newVal) => { | ||
if (newVal) { | ||
const now = new Date(); | ||
selectedTime.value = getStartOfDay(now) || now; | ||
} | ||
}); | ||
const handleConfirm = () => { | ||
emit('confirm', selectedTime.value); | ||
emit('close'); | ||
}; | ||
</script> | ||
|
||
<template> | ||
<k-dialog :opened="opened" @backdropclick="emit('close')"> | ||
<template #title>Démarrer à une heure spécifique</template> | ||
|
||
<k-block> | ||
<input type="datetime-local" v-model="formattedTime" class="w-full p-2 rounded-lg"> | ||
</k-block> | ||
|
||
<template #buttons> | ||
<k-dialog-button @click="emit('close')">Annuler</k-dialog-button> | ||
<k-dialog-button strong @click="handleConfirm">Confirmer</k-dialog-button> | ||
</template> | ||
</k-dialog> | ||
</template> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
<script setup lang="ts"> | ||
import { ref, watch, computed } from 'vue'; | ||
import { kDialog, kDialogButton, kBlock, kButton } from 'konsta/vue'; | ||
import { useTime } from '~/composables/useTime'; | ||
const props = defineProps<{ | ||
opened: boolean, | ||
dayStartAt: string | ||
}>(); | ||
const emit = defineEmits<{ | ||
close: [], | ||
confirm: [date: Date] | ||
}>(); | ||
const { getStartOfDay } = useTime(computed(() => props.dayStartAt)); | ||
const selectedTime = ref(getStartOfDay(new Date())); | ||
const formatDate = (date: Date) => { | ||
try { | ||
return date.toISOString().slice(0, 16); | ||
} catch (e) { | ||
return new Date().toISOString().slice(0, 16); | ||
} | ||
}; | ||
const formattedTime = computed({ | ||
get: () => formatDate(selectedTime.value), | ||
set: (value) => { | ||
const date = new Date(value); | ||
if (!isNaN(date.getTime())) { | ||
selectedTime.value = date; | ||
} | ||
} | ||
}); | ||
watch(() => props.opened, (newVal) => { | ||
if (newVal) { | ||
const now = new Date(); | ||
selectedTime.value = getStartOfDay(now) || now; | ||
} | ||
}); | ||
const handleConfirm = () => { | ||
emit('confirm', selectedTime.value); | ||
emit('close'); | ||
}; | ||
const adjustTime = (minutes: number) => { | ||
const newTime = new Date(); | ||
newTime.setMinutes(newTime.getMinutes() + minutes); | ||
if (!isNaN(newTime.getTime())) { | ||
selectedTime.value = newTime; | ||
} | ||
}; | ||
</script> | ||
|
||
<template> | ||
<k-dialog :opened="opened" @backdropclick="emit('close')"> | ||
<template #title>Arrêter à une heure spécifique</template> | ||
|
||
<k-block> | ||
<div class="flex gap-2 mb-2"> | ||
<k-button small @click="adjustTime(-10)" class="flex-1">-10min</k-button> | ||
<k-button small @click="adjustTime(-30)" class="flex-1">-30min</k-button> | ||
<k-button small @click="adjustTime(-60)" class="flex-1">-1h</k-button> | ||
</div> | ||
<input type="datetime-local" v-model="formattedTime" class="w-full p-2 rounded-lg"> | ||
</k-block> | ||
|
||
<template #buttons> | ||
<k-dialog-button @click="emit('close')">Annuler</k-dialog-button> | ||
<k-dialog-button strong @click="handleConfirm">Confirmer</k-dialog-button> | ||
</template> | ||
</k-dialog> | ||
</template> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,23 @@ | ||
export function useTime(dayStartAt) { | ||
const setToStartOfDay = d => { | ||
const [h, m] = dayStartAt.value.split(':').map(Number); | ||
d.setHours(h, m, 0, 0); | ||
const [hours, minutes] = typeof dayStartAt.value === 'string' | ||
? dayStartAt.value.split(':').map(Number) | ||
: [Math.floor(dayStartAt.value / 100), dayStartAt.value % 100]; | ||
d.setHours(hours, minutes, 0, 0); | ||
}; | ||
|
||
const getStartOfDay = (d = new Date()) => { | ||
const s = new Date(d); | ||
setToStartOfDay(s); | ||
if (d.getHours() < 5) s.setDate(s.getDate() - 1); | ||
if (s > d) s.setDate(s.getDate() - 1); | ||
return s; | ||
}; | ||
|
||
const getSessionDay = d => { | ||
const sd = new Date(d); | ||
if (sd.getHours() < 5) sd.setDate(sd.getDate() - 1); | ||
const sd = getStartOfDay(d); | ||
return sd.toLocaleDateString('fr-FR', { day: '2-digit', month: 'long', year: 'numeric' }); | ||
}; | ||
|
||
|
||
return { setToStartOfDay, getStartOfDay, getSessionDay }; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters