Skip to content
Snippets Groups Projects

refactor: updated days left in challenge component

2 files
+ 34
20
Compare changes
  • Side-by-side
  • Inline
Files
2
<script setup>
<script setup>
import { ref } from 'vue'
import {onBeforeMount, ref} from 'vue'
import {Card} from "@/components/ui/card/index.js";
import {Card} from "@/components/ui/card/index.js";
/**
/**
* The category of the saving challenge
* The category of the saving challenge
* @type {Ref<UnwrapRef<string>>}
*/
*/
const category = ref('Kaffe')
const category = ref('Kaffe')
/**
/**
* The amount of money the user wants to spend
* The amount of money the user wants to spend
* @type {Ref<UnwrapRef<number>>}
*/
*/
const amount = ref(200)
const amount = ref(200)
/**
/**
* The goal amount the user wants to spend
* The goal amount the user wants to spend
* @type {Ref<UnwrapRef<number>>}
*/
*/
const goal = ref(400)
const goal = ref(400)
/**
/**
* The deadline for the saving challenge
* The deadline for the saving challenge
* @type {Ref<UnwrapRef<string>>}
*/
*/
const deadline = ref('2024-04-30')
const deadline = ref('2024-05-29')
 
 
/**
 
* The days left for the saving challenge
 
*/
 
const daysLeft = ref(0)
/**
/**
* The number of days left until the deadline
* The hours left for the saving challenge
* @type {Ref<UnwrapRef<number>>}
*/
*/
const daysLeft = updateDaysLeft()
const hoursLeft = ref(0)
/**
/**
* Updates the days left until the given deadline
* Set the time remaining for the saving challenge
* @returns {number} The number of days left
*/
*/
function updateDaysLeft() {
function setTimeRemaining(){
//TODO: Feilhåndtering ugyldig dato?
const endDate = new Date(deadline.value);
const today = new Date()
const offset = endDate.getTimezoneOffset();
const deadlineDate = new Date(deadline.value)
const now = new Date();
const diffTime = deadlineDate - today
now.setMinutes(now.getMinutes() - offset);
const diffDays = Math.ceil(diffTime / (1000 * 60 * 60 * 24))
const total = endDate - now;
return diffDays
 
if (total < 0) {
 
daysLeft.value = 0;
 
hoursLeft.value = 0;
 
return;
 
}
 
 
const hours = Math.floor( (total/(1000*60*60)) % 24 );
 
const days = Math.floor( total/(1000*60*60*24) );
 
hoursLeft.value = hours;
 
daysLeft.value = days;
}
}
 
/**
 
* Set the time remaining when the component is mounted
 
*/
 
onBeforeMount(() => {
 
setTimeRemaining()
 
})
</script>
</script>
@@ -48,7 +63,7 @@ function updateDaysLeft() {
@@ -48,7 +63,7 @@ function updateDaysLeft() {
<card>
<card>
<div class="flex justify-between px-2.5">
<div class="flex justify-between px-2.5">
<p class="text-left">{{category}} </p>
<p class="text-left">{{category}} </p>
<p class="text-right" @reset="updateDaysLeft">Innen {{daysLeft}} dager</p>
<p class="text-right" @reset="setTimeRemaining">Innen {{ daysLeft }}d {{ hoursLeft }}t</p>
</div>
</div>
<div class="flex justify-center">
<div class="flex justify-center">
<p class="">Bruke mindre enn: </p>
<p class="">Bruke mindre enn: </p>
Loading