Skip to content

Commit

Permalink
New progress bar, bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
alinnert committed Dec 10, 2023
1 parent f57df07 commit 74f4344
Show file tree
Hide file tree
Showing 10 changed files with 68 additions and 37 deletions.
2 changes: 1 addition & 1 deletion .prettierrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@
"tabWidth": 2,
"singleQuote": true,
"printWidth": 100,
"trailingComma": "none"
"trailingComma": "all"
}
2 changes: 1 addition & 1 deletion src/assets/main.css
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
@tailwind base;
@tailwind components;
@tailwind utilities;
@tailwind utilities;
27 changes: 7 additions & 20 deletions src/components/DaySummary.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ import { format, formatDuration } from 'date-fns'
import { de } from 'date-fns/locale'
import { computed } from 'vue'
import { getRatio } from '../lib/date-fns/getRatio'
import HoursProgress from './HoursProgress.vue'
const timesStore = useTimesStore()
const lessThan8Hours = computed(() => (timesStore.workTimeOfSelectedDay.hours ?? 0) < 8)
const remainingLabel = computed(() =>
lessThan8Hours.value ? '8 Stunden erreicht in' : 'Überstunden'
lessThan8Hours.value ? '8 Stunden erreicht in' : 'Überstunden',
)
</script>

Expand All @@ -22,7 +23,7 @@ const remainingLabel = computed(() =>
{{
formatDuration(timesStore.workTimeOfSelectedDay, {
format: ['hours', 'minutes'],
locale: de
locale: de,
}) || '-'
}}
</div>
Expand All @@ -34,7 +35,7 @@ const remainingLabel = computed(() =>
{{
formatDuration(timesStore.remainingWorkTimeOfSelectedDay, {
format: ['hours', 'minutes'],
locale: de
locale: de,
})
}}
</div>
Expand All @@ -51,22 +52,8 @@ const remainingLabel = computed(() =>
</template>
</div>

<div class="flex flex-col col-span-2">
<div class="flex justify-between items-end">
<div class="w-0.5 h-3 bg-black"></div>
<template v-for="i in 8" :key="i">
<div class="w-px h-1 bg-gray-400"></div>
<div class="w-px h-2 bg-gray-500"></div>
<div class="w-px h-1 bg-gray-400"></div>
<div class="w-0.5 h-3 bg-black"></div>
</template>
</div>

<progress
class="w-full appearance-none [&::-webkit-progress-bar]:h-4 [&::-webkit-progress-bar]:bg-white [&::-webkit-progress-bar]:border-solid [&::-webkit-progress-bar]:border-2 [&::-webkit-progress-bar]:border-black [&::-webkit-progress-value]:bg-emerald-600"
:value="getRatio(timesStore.workTimeOfSelectedDay, { hours: 8 })"
max="1"
></progress>
<div class="col-span-2">
<HoursProgress :value="timesStore.workTimeOfSelectedDay"></HoursProgress>
</div>

<div>
Expand All @@ -75,7 +62,7 @@ const remainingLabel = computed(() =>
{{
formatDuration(timesStore.breakTimeOfSelectedDay, {
format: ['hours', 'minutes'],
locale: de
locale: de,
}) || '-'
}}
</div>
Expand Down
2 changes: 1 addition & 1 deletion src/components/DaysListItem.vue
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const timesStore = useTimesStore()
class="px-3 py-2 cursor-default text-center rounded select-none"
:class="{
'hover:bg-gray-200': !isSameDay(timesStore.selectedDay, day),
'bg-gray-300': isSameDay(timesStore.selectedDay, day)
'bg-gray-300': isSameDay(timesStore.selectedDay, day),
}"
@click="timesStore.setSelectedDay(day)"
>
Expand Down
32 changes: 32 additions & 0 deletions src/components/HoursProgress.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<script setup lang="ts">
const props = defineProps<{ value: Duration }>()
function getSegmentWidth(index: number): string {
if (index <= (props.value.hours ?? 0)) {
return '100%'
}
if (index > (props.value.hours ?? 0) + 1) {
return '0%'
}
return `${(100 / 60) * (props.value.minutes ?? 0)}%`
}
</script>

<template>
<div class="flex gap-1">
<template v-for="i in 11" :key="i">
<div
class="flex h-3 rounded-full flex-grow overflow-hidden"
:class="{ 'bg-sky-200': i <= 8, 'bg-yellow-200': i > 8 && i <= 10, 'bg-rose-200': i > 10 }"
>
<div
class="h-full"
:class="{ 'bg-sky-800': i <= 8, 'bg-yellow-800': i > 8 && i <= 10, 'bg-rose-800': i > 10 }"
:style="{ width: getSegmentWidth(i) }"
></div>
</div>
</template>
</div>
</template>
6 changes: 4 additions & 2 deletions src/components/ToolbarButton.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<template>
<button class="px-3 py-1 bg-gray-100 hover:bg-gray-200 active:bg-gray-300 rounded border border-gray-300 hover:border-gray-400 active:border-gray-400 cursor-default select-none">
<button
class="px-3 py-1 bg-gray-100 hover:bg-gray-200 active:bg-gray-300 rounded border border-gray-300 hover:border-gray-400 active:border-gray-400 cursor-default select-none"
>
<slot></slot>
</button>
</template>
</template>
2 changes: 1 addition & 1 deletion src/components/UiStack.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
<div class="flex flex-col gap-4">
<slot></slot>
</div>
</template>
</template>
2 changes: 1 addition & 1 deletion src/lib/date-fns/getRatio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ export function getRatio(duration: Duration, compareDuration: Duration): number
const baseDate = new Date(0)
const compareDate = add(baseDate, compareDuration)
const date = add(baseDate, duration)
return 1 / compareDate.getTime() * date.getTime()
return (1 / compareDate.getTime()) * date.getTime()
}
11 changes: 8 additions & 3 deletions src/lib/date/sortDates.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
export function sortDates(dateA: Date, dateB: Date): number {
return dateB.getTime() - dateA.getTime()
}
export function sortDates(reverse = false): (dateA: Date, dateB: Date) => number {
return (dateA, dateB) => {
if (reverse) {
return dateA.getTime() - dateB.getTime()
}
return dateB.getTime() - dateA.getTime()
}
}
19 changes: 12 additions & 7 deletions src/stores/times.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export const useTimesStore = defineStore('times', () => {

watchEffect(() => {
const serializedTimestamps = JSON.stringify(
timestamps.value.map((timestamp) => timestamp.getTime())
timestamps.value.map((timestamp) => timestamp.getTime()),
)
localStorage.setItem(timestampsStorageKey, serializedTimestamps)
})
Expand All @@ -34,9 +34,13 @@ export const useTimesStore = defineStore('times', () => {
}
}

days.sort(sortDates)
days.sort(sortDates())

const today = new Date(currentTime.value.getFullYear(), currentTime.value.getMonth(), currentTime.value.getDate())
const today = new Date(
currentTime.value.getFullYear(),
currentTime.value.getMonth(),
currentTime.value.getDate(),
)

if (!isSameDay(days[0], today)) {
days.unshift(today)
Expand All @@ -52,7 +56,9 @@ export const useTimesStore = defineStore('times', () => {
})

const timestampsOfSelectedDay = computed((): Date[] => {
return timestamps.value.filter((timestamp) => isSameDay(timestamp, selectedDay.value))
const result = timestamps.value.filter((timestamp) => isSameDay(timestamp, selectedDay.value))
result.sort(sortDates(true))
return result
})

const workTimeOfSelectedDay = computed((): Duration => {
Expand All @@ -63,7 +69,7 @@ export const useTimesStore = defineStore('times', () => {
list.forEach((timestamp, index) => {
if (index % 2 === 0) return
workTimeDurations.push(
intervalToDuration({ start: timestampsOfSelectedDay.value[index - 1], end: timestamp })
intervalToDuration({ start: timestampsOfSelectedDay.value[index - 1], end: timestamp }),
)
})

Expand All @@ -81,7 +87,7 @@ export const useTimesStore = defineStore('times', () => {
if (index % 2 === 1 || index === 0) return

breakTimeDurations.push(
intervalToDuration({ start: timestampsOfSelectedDay.value[index - 1], end: timestamp })
intervalToDuration({ start: timestampsOfSelectedDay.value[index - 1], end: timestamp }),
)
})

Expand Down Expand Up @@ -112,7 +118,6 @@ export const useTimesStore = defineStore('times', () => {
},
addTime(time: Date) {
timestamps.value.push(time)
timestamps.value.sort(sortDates)
},
addCurrentTime() {
this.addTime(new Date())
Expand Down

0 comments on commit 74f4344

Please sign in to comment.