Skip to content

Commit

Permalink
Added time class.
Browse files Browse the repository at this point in the history
  • Loading branch information
pgebert committed Jul 6, 2023
1 parent b415ddd commit e4f805a
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 21 deletions.
5 changes: 3 additions & 2 deletions src/bcsClient.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import puppeteer, {Page} from "puppeteer";
import {Task} from "./task";

interface BcsClientInterface {
add(tasks: Task[]): void;
Expand Down Expand Up @@ -53,10 +54,10 @@ export class BcsClient implements BcsClientInterface {
const lastRowForProjectId = rowsForProjectId[rowsForProjectId.length - 1]

const hourInput = lastRowForProjectId.querySelector(`td[name="effortExpense"] > span > input:nth-child(1)`) as HTMLInputElement;
hourInput.value = task.hours;
hourInput.value = task.time.getHours().toString();

const minuteInput = lastRowForProjectId.querySelector(`td[name="effortExpense"] > span > input:nth-child(3)`) as HTMLInputElement;
minuteInput.value = task.minutes;
minuteInput.value = task.time.minutes.toString();

const descriptionInput = lastRowForProjectId.querySelector(`td[name="description"] > textarea`) as HTMLInputElement;
descriptionInput.value = task.description;
Expand Down
29 changes: 27 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ colors.theme({

const validateInput = (input: string): boolean => /\d{1,2}:\d{1,2}/i.test(input);

const handleAddCommand = async () => {

const main = async () => {

console.log("Handle add command")
const tasks: Task[] = [];

await prompt([
Expand Down Expand Up @@ -88,6 +88,31 @@ const main = async () => {
moreTasks = input.more === "true";
});
}
}


const main = async () => {


await prompt({
type: 'select',
name: 'command',
message: 'What do you want to do?',
choices: ['login', 'add', 'get', 'check']
}).then((response) => {
console.log('Answer:', response)

switch (response.command) {
case 'add':
handleAddCommand();
break;
default:
console.log("Not implemented yet.");
break;
}
}).catch(console.error);


};

main().catch(console.error);
12 changes: 6 additions & 6 deletions src/task.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {toMinutes, toTimeString} from "./utils";
import {Time} from "./time";

interface ITask {
ticket: string
Expand All @@ -16,13 +16,13 @@ interface IPattern {
export class Task {
ticket: string
description: string
minutes: number
time: Time
projectId: string

constructor({ticket, description, time}: ITask) {
this.ticket = ticket;
this.description = description;
this.minutes = toMinutes(time);
this.time = Time.fromString(time);
this.projectId = this.getProjectId();

}
Expand All @@ -36,15 +36,15 @@ export class Task {
];

for (let pattern of patterns) {
if (pattern.regex.test(this[pattern.source])) {
if (pattern.regex.test(<string>this[pattern.source])) {
return pattern.value;
}
}
return null;
}

toString(): string {
const {description, ticket, minutes} = this;
return `Task(description: ${description}, ticket: ${ticket}, time: ${toTimeString(minutes)})`;
const {description, ticket, time} = this;
return `Task(description: ${description}, ticket: ${ticket}, time: ${time})`;
}
}
36 changes: 36 additions & 0 deletions src/time.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
export class Time {
minutes: number;

constructor(minutes: number) {
this.minutes = minutes;
}

public static fromString(time: string): Time {
return new Time(this.toMinutes(time));
}

private static toMinutes(time: string): number {
const [hours, minutes] = time.split(":").map(t => parseInt(t, 10) || 0);
return hours * 60 + minutes;
}

plus(other: Time): Time {
return new Time(this.minutes + other.minutes);
}

minus(other: Time): Time {
return new Time(this.minutes - other.minutes);
}

getHours(): number {
return Math.floor(this.minutes / 60);
}

getRemainingMinutes(): number {
return this.minutes % 60;
}

toString(): string {
return `${this.getHours().toString().padStart(2, '0')}:${this.getRemainingMinutes().toString().padStart(2, '0')}`;
}
}
11 changes: 0 additions & 11 deletions src/utils.ts

This file was deleted.

0 comments on commit e4f805a

Please sign in to comment.