Skip to content

Commit

Permalink
feat(timesheet): ✨ added week navigation allowing the user to view pr…
Browse files Browse the repository at this point in the history
…evious weeks (#11)
  • Loading branch information
davids-ensemble committed May 11, 2024
1 parent 472e5af commit 82bf941
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 19 deletions.
1 change: 1 addition & 0 deletions src/components/tj-jira-panel/tj-jira-panel.css
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
font-size: 14px;
box-sizing: border-box;
--primary-color: #005273;
--accent-color: #2479aa;
}

#tj-panel {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ button {
text-align: left;
padding: 0;
margin: 0;
color: #2479aa;
color: var(--accent-color);
}
2 changes: 1 addition & 1 deletion src/components/tj-settings/tj-settings.css
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ p {
text-align: left;
padding: 0;
margin: 0;
color: #2479aa;
color: var(--accent-color);
}

.server {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ th div {
}

&.currentDay {
color: #2479aa;
color: var(--accent-color);
font-weight: 900;
}

Expand All @@ -77,3 +77,21 @@ input {
input[disabled] {
cursor: not-allowed;
}

.week-navigation__container {
display: flex;
justify-content: space-between;
align-items: center;
margin: 0 0 10px 0;
}

.week-navigation__button {
border: none;
background: none;
cursor: pointer;
font-size: 14px;
text-align: right;
padding: 0;
margin: 4px 0 0 0;
color: var(--accent-color);
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Component, Event, EventEmitter, Host, Prop, h } from '@stencil/core';
import { Component, Event, EventEmitter, Host, Prop, State, Watch, h } from '@stencil/core';

import { Task } from '@utils/tj';
import { Task, User } from '@utils/tj';

import type { Notification } from '../../../notifications-provider/types';

Expand Down Expand Up @@ -35,11 +35,8 @@ export class TJNewTaskForm {
*/
@Prop() task!: Task;

days: Day[] = [];

componentWillLoad() {
this.days = this.getWeekDays(new Date());
}
@State() recordedHours = this.task.recordedHours;
@State() days: Day[] = this.getWeekDays(new Date());

/**
* Returns an array of Day objects representing the week days of the current week.
Expand Down Expand Up @@ -85,6 +82,22 @@ export class TJNewTaskForm {
}
}

onWeekChange = (e: Event) => {
const target = e.target as HTMLButtonElement;
const type = target.dataset.type;
const newDate = new Date(this.days[0].date.getTime());
const offset = type === 'next' ? 7 : -7;
newDate.setDate(newDate.getDate() + offset);
this.days = this.getWeekDays(newDate);
};

@Watch('days')
async getTimeSheet(newDays: Day[]) {
const day = newDays[0].date;
const task = await User.getTaskById(this.task.id, day);
this.recordedHours = task.recordedHours;
}

render() {
return (
<Host>
Expand Down Expand Up @@ -117,7 +130,7 @@ export class TJNewTaskForm {
type="text"
aria-label={`Hours recorded on ${longWeekdayFormatter.format(day.date)}`}
disabled={(this.task?.startDate ?? new Date()) > day.date}
value={this.task.recordedHours[day.iso]}
value={this.recordedHours[day.iso]}
onFocus={(e: FocusEvent) => {
(e.target as HTMLInputElement).select();
}}
Expand All @@ -135,6 +148,19 @@ export class TJNewTaskForm {
</tr>
</tbody>
</table>
<div class="week-navigation__container">
{this.days[0].date > this.task.startDate && (
<button data-type="previous" class="week-navigation__button" onClick={this.onWeekChange}>
&lt; Previous week
</button>
)}
<span class="week-navigation__spacer"></span>
{this.days[6].date < new Date() && (
<button data-type="next" class="week-navigation__button" onClick={this.onWeekChange}>
Next week &gt;
</button>
)}
</div>
</Host>
);
}
Expand Down
14 changes: 6 additions & 8 deletions src/utils/tj/User.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ interface AuthObject {
}

export class User {
private static authObject: AuthObject = JSON.parse(
localStorage.getItem('tj_user') ?? '{}',
);
private static authObject: AuthObject = JSON.parse(localStorage.getItem('tj_user') ?? '{}');

public static get username(): string {
return User.authObject.username;
Expand Down Expand Up @@ -76,9 +74,9 @@ export class User {
return dom.querySelector('sessionUuid')?.textContent === sessionUuid;
}

public static async getTimesheet() {
const date = new Date().toISOString().split('T')[0];
const body = `<getTimesheet><forUser>${this.userId}</forUser><containingDay>${date}</containingDay></getTimesheet>`;
public static async getTimesheet(date = new Date()) {
const dateString = date.toISOString().split('T')[0];
const body = `<getTimesheet><forUser>${this.userId}</forUser><containingDay>${dateString}</containingDay></getTimesheet>`;
const response = await fetch(Server.url, {
method: 'POST',
headers: {
Expand Down Expand Up @@ -130,8 +128,8 @@ export class User {
return null;
}

public static async getTaskById(id: string) {
const timesheet = await this.getTimesheet();
public static async getTaskById(id: string, timesheetDate?: Date) {
const timesheet = await this.getTimesheet(timesheetDate);
const xpath = `//task[@id="${id}"]`;
const task = timesheet.evaluate(xpath, timesheet).iterateNext();
if (task) {
Expand Down

0 comments on commit 82bf941

Please sign in to comment.