refacto to useTimeUntil hook
This commit is contained in:
@@ -1,10 +1,12 @@
|
|||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
import { computed, onUnmounted, ref } from "vue"
|
import { computed, ref } from "vue"
|
||||||
import { timeUntil } from "../services/time-until"
|
|
||||||
import { useTitle } from "@vueuse/core"
|
import { useTitle } from "@vueuse/core"
|
||||||
|
import { useTimeUntil } from "../hooks/useTimeUntil.hooks"
|
||||||
|
|
||||||
const props = defineProps<{ project?: string; target?: string }>()
|
const props = defineProps<{ project?: string; target?: string }>()
|
||||||
|
|
||||||
|
const target = computed(() => props.target)
|
||||||
|
|
||||||
if (props.project) {
|
if (props.project) {
|
||||||
useTitle(props.project)
|
useTitle(props.project)
|
||||||
}
|
}
|
||||||
@@ -17,47 +19,21 @@ const targetDate = computed(() =>
|
|||||||
: null
|
: null
|
||||||
)
|
)
|
||||||
|
|
||||||
const timeUntilTarget = props.target ? timeUntil(props.target) : null
|
const {
|
||||||
|
timeUntilTarget,
|
||||||
const yearsUntil = ref(timeUntilTarget?.years ?? 0)
|
isYearsDisplayed,
|
||||||
const monthsUntil = ref(timeUntilTarget?.months ?? 0)
|
isMonthsDisplayed,
|
||||||
const daysUntil = ref(timeUntilTarget?.days ?? 0)
|
isDaysDisplayed,
|
||||||
const hoursUntil = ref(timeUntilTarget?.hours ?? 0)
|
isHoursDisplayed,
|
||||||
const minutesUntil = ref(timeUntilTarget?.minutes ?? 0)
|
isMinutesDisplayed,
|
||||||
const secondsUntil = ref(timeUntilTarget?.seconds ?? 0)
|
isSecondsDisplayed,
|
||||||
|
yearsUntil,
|
||||||
const isYearsDisplayed = computed(() => yearsUntil.value > 0)
|
monthsUntil,
|
||||||
const isMonthsDisplayed = computed(
|
daysUntil,
|
||||||
() => isYearsDisplayed.value || monthsUntil.value > 0
|
hoursUntil,
|
||||||
)
|
minutesUntil,
|
||||||
const isDaysDisplayed = computed(
|
secondsUntil,
|
||||||
() => isMonthsDisplayed.value || daysUntil.value > 0
|
} = useTimeUntil(target)
|
||||||
)
|
|
||||||
const isHoursDisplayed = computed(
|
|
||||||
() => isDaysDisplayed.value || hoursUntil.value > 0
|
|
||||||
)
|
|
||||||
const isMinutesDisplayed = computed(
|
|
||||||
() => isHoursDisplayed.value || minutesUntil.value > 0
|
|
||||||
)
|
|
||||||
const isSecondsDisplayed = computed(
|
|
||||||
() => isMinutesDisplayed.value || secondsUntil.value > 0
|
|
||||||
)
|
|
||||||
|
|
||||||
const id = setInterval(() => {
|
|
||||||
if (!props.target) {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
const timeUntilTarget = timeUntil(props.target)
|
|
||||||
|
|
||||||
yearsUntil.value = timeUntilTarget.years
|
|
||||||
monthsUntil.value = timeUntilTarget.months
|
|
||||||
daysUntil.value = timeUntilTarget.days
|
|
||||||
hoursUntil.value = timeUntilTarget.hours
|
|
||||||
minutesUntil.value = timeUntilTarget.minutes
|
|
||||||
secondsUntil.value = timeUntilTarget.seconds
|
|
||||||
}, 1000)
|
|
||||||
|
|
||||||
onUnmounted(() => clearInterval(id))
|
|
||||||
|
|
||||||
const projectTitle = ref(props.project)
|
const projectTitle = ref(props.project)
|
||||||
const targetInput = ref(props.target)
|
const targetInput = ref(props.target)
|
||||||
|
|||||||
62
src/hooks/useTimeUntil.hooks.ts
Normal file
62
src/hooks/useTimeUntil.hooks.ts
Normal file
@@ -0,0 +1,62 @@
|
|||||||
|
import { ref, computed, onUnmounted, ComputedRef } from "vue"
|
||||||
|
import { timeUntil } from "../services/time-until"
|
||||||
|
|
||||||
|
export const useTimeUntil = (target: ComputedRef<string | undefined>) => {
|
||||||
|
const timeUntilTarget = target.value ? timeUntil(target.value) : null
|
||||||
|
|
||||||
|
const yearsUntil = ref(timeUntilTarget?.years ?? 0)
|
||||||
|
const monthsUntil = ref(timeUntilTarget?.months ?? 0)
|
||||||
|
const daysUntil = ref(timeUntilTarget?.days ?? 0)
|
||||||
|
const hoursUntil = ref(timeUntilTarget?.hours ?? 0)
|
||||||
|
const minutesUntil = ref(timeUntilTarget?.minutes ?? 0)
|
||||||
|
const secondsUntil = ref(timeUntilTarget?.seconds ?? 0)
|
||||||
|
|
||||||
|
const isYearsDisplayed = computed(() => yearsUntil.value > 0)
|
||||||
|
const isMonthsDisplayed = computed(
|
||||||
|
() => isYearsDisplayed.value || monthsUntil.value > 0
|
||||||
|
)
|
||||||
|
const isDaysDisplayed = computed(
|
||||||
|
() => isMonthsDisplayed.value || daysUntil.value > 0
|
||||||
|
)
|
||||||
|
const isHoursDisplayed = computed(
|
||||||
|
() => isDaysDisplayed.value || hoursUntil.value > 0
|
||||||
|
)
|
||||||
|
const isMinutesDisplayed = computed(
|
||||||
|
() => isHoursDisplayed.value || minutesUntil.value > 0
|
||||||
|
)
|
||||||
|
const isSecondsDisplayed = computed(
|
||||||
|
() => isMinutesDisplayed.value || secondsUntil.value > 0
|
||||||
|
)
|
||||||
|
|
||||||
|
const id = setInterval(() => {
|
||||||
|
if (!target.value) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
const timeUntilTarget = timeUntil(target.value)
|
||||||
|
|
||||||
|
yearsUntil.value = timeUntilTarget.years
|
||||||
|
monthsUntil.value = timeUntilTarget.months
|
||||||
|
daysUntil.value = timeUntilTarget.days
|
||||||
|
hoursUntil.value = timeUntilTarget.hours
|
||||||
|
minutesUntil.value = timeUntilTarget.minutes
|
||||||
|
secondsUntil.value = timeUntilTarget.seconds
|
||||||
|
}, 1000)
|
||||||
|
|
||||||
|
onUnmounted(() => clearInterval(id))
|
||||||
|
|
||||||
|
return {
|
||||||
|
timeUntilTarget,
|
||||||
|
isYearsDisplayed,
|
||||||
|
isMonthsDisplayed,
|
||||||
|
isDaysDisplayed,
|
||||||
|
isHoursDisplayed,
|
||||||
|
isMinutesDisplayed,
|
||||||
|
isSecondsDisplayed,
|
||||||
|
yearsUntil,
|
||||||
|
monthsUntil,
|
||||||
|
daysUntil,
|
||||||
|
hoursUntil,
|
||||||
|
minutesUntil,
|
||||||
|
secondsUntil,
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user