I have been working on a scheduler to track tasks completed within a specific timeframe.
Whenever I click "STOP," an input field appears where I can enter details about the task I just finished. The start time, end time, and duration of the task are then saved accordingly.
Currently, the main function I am focusing on fixing is "addTask()." I am attempting to store these tasks using LocalStorage, but each new entry overwrites the previous one instead of creating a list.
Below is the full code implementation:
HTML:
<div class="modalbox" [class.active]="modalboxActive">
<div class="modal">
<p>What task did you complete?</p>
<input type="text" [(ngModel)]="activity.name" />
<button (click)="addTask()" [disabled]="activity.name === ''">OK</button>
</div>
</div>
<div class="boxSuper">
<div class="boxLeft">
<div class="containerUp">
<button id="start" (click)="startTimer()">START</button>
<button id="pause" (click)="pauseTimer()">PAUSE</button>
<button id="stop" (click)="stopTimer()">STOP</button>
</div>
<div class="containerDown">
<p>{{ display }}</p>
</div>
</div>
<div class="boxRight">
<div class="containerLeft">
<ul class="listElementLeft" *ngFor="let item of tasks">
<li>
<span id="writings">Start Time:</span>
<span>{{ item.start }}</span>
</li>
<li>
<span id="writings">End Time:</span>
<span>{{ item.end }}</span>
</li>
<li>
<span id="writings">Duration:</span>
<span>{{ item.length }}</span>
</li>
</ul>
</div>
<div class="containerRight">
<ul class="listElement" *ngFor="let item of tasks">
<li>
<span id="writings">Task:</span>
<span>{{ item.name }}</span>
</li>
</ul>
</div>
</div>
</div>
TS:
import { importExpr } from '@angular/compiler/src/output/output_ast';
import { Component, OnInit } from '@angular/core';
import { timer } from 'rxjs';
import { Activity } from '../activity';
import { Result } from '../result';
@Component({
selector: 'app-timer',
templateUrl: './timer.component.html',
styleUrls: ['./timer.component.css'],
})
export class TimerComponent implements OnInit {
ngOnInit() {}
time: number = 0;
display: string | undefined;
interval: any;
modalboxActive = false;
startTime: string | undefined;
endTime: string | undefined;
activity: Activity = {
name: '',
};
tasks: Result[] = [];
startFunc() {
this.startTime = new Date().toString().split(' ')[4];
}
endFunc() {
this.endTime = new Date().toString().split(' ')[4];
}
addTask() {
var el: Result = {
name: this.activity.name,
end: this.endTime,
start: this.startTime,
length: this.display,
};
localStorage.setItem('taskList', JSON.stringify(el));
window.localStorage.getItem('taskList');
this.tasks.push(el);
this.activity.name = '';
this.modalboxActive = false;
this.resetTimer();
}
resetTimer() {
console.log('resetting timer...');
this.time = 0;
}
startTimer() {
console.log('starting timer...');
this.interval = setInterval(() => {
if (this.time === 0) {
this.time++;
} else {
this.time++;
}
this.display = this.transform(this.time);
}, 1000);
this.startFunc();
}
transform(value: number): string {
var sec_num = value;
var hours = Math.floor(sec_num / 3600);
var minutes = Math.floor((sec_num - hours * 3600) / 60);
var seconds = sec_num - hours * 3600 - minutes * 60;
return hours + ':' + minutes + ':' + seconds;
}
pauseTimer() {
clearInterval(this.interval);
}
stopTimer() {
console.log('stopping timer...');
this.modalboxActive = true;
clearInterval(this.interval);
this.endFunc();
}
}