From b0bffe6fb82a46996b747d6a63af7c924b9bf24a Mon Sep 17 00:00:00 2001 From: William Petit Date: Wed, 22 Apr 2020 22:48:47 +0200 Subject: [PATCH] Task categories edition --- src/models/task.ts | 29 ++-- src/routes/project/task-categories-table.tsx | 153 ++++++++++++------- 2 files changed, 117 insertions(+), 65 deletions(-) diff --git a/src/models/task.ts b/src/models/task.ts index 363ade2..76f3020 100644 --- a/src/models/task.ts +++ b/src/models/task.ts @@ -1,4 +1,5 @@ import { uuidV4 } from "../util/uuid" +import { defaults } from "./params"; export type TaskID = string @@ -24,14 +25,22 @@ export interface TaskCategory { } export function newTask(label: string, category: TaskCategoryID): Task { - return { - id: uuidV4(), - label, - category, - estimations: { - [EstimationConfidence.Optimistic]: 0, - [EstimationConfidence.Likely]: 0, - [EstimationConfidence.Pessimistic]: 0, - } - }; + return { + id: uuidV4(), + label, + category, + estimations: { + [EstimationConfidence.Optimistic]: 0, + [EstimationConfidence.Likely]: 0, + [EstimationConfidence.Pessimistic]: 0, + } + }; +} + +export function createTaskCategory(): TaskCategory { + return { + id: uuidV4(), + costPerTimeUnit: defaults.costPerTimeUnit, + label: "" + }; } \ No newline at end of file diff --git a/src/routes/project/task-categories-table.tsx b/src/routes/project/task-categories-table.tsx index a2589cb..82ec14a 100644 --- a/src/routes/project/task-categories-table.tsx +++ b/src/routes/project/task-categories-table.tsx @@ -1,70 +1,113 @@ import { FunctionalComponent, h } from "preact"; import { Project } from "../../models/project"; import style from './style.module.css'; -import { projectReducer, ProjectReducerActions, updateTaskCategoryCost, updateTaskCategoryLabel, removeTaskCategory } from "../../hooks/use-project-reducer"; +import { ProjectReducerActions, updateTaskCategoryCost, updateTaskCategoryLabel, removeTaskCategory, addTaskCategory } from "../../hooks/use-project-reducer"; import EditableText from "../../components/editable-text"; -import { TaskCategoryID } from "../../models/task"; -import ProjectTimeUnit from "../../components/project-time-unit"; +import { TaskCategoryID, createTaskCategory } from "../../models/task"; import { getCurrency, getTaskCategoryCost } from "../../models/params"; +import { useState } from "preact/hooks"; export interface TaskCategoriesTableProps { - project: Project - dispatch: (action: ProjectReducerActions) => void + project: Project + dispatch: (action: ProjectReducerActions) => void } const TaskCategoriesTable: FunctionalComponent = ({ project, dispatch }) => { - const onTaskCategoryRemove = (categoryId: TaskCategoryID) => { - dispatch(removeTaskCategory(categoryId)); - }; + const [ newTaskCategory, setNewTaskCategory ] = useState(createTaskCategory()); + + const onTaskCategoryRemove = (categoryId: TaskCategoryID) => { + dispatch(removeTaskCategory(categoryId)); + }; + + const onTaskCategoryLabelChange = (categoryId: TaskCategoryID, value: string) => { + dispatch(updateTaskCategoryLabel(categoryId, value)); + }; + + const onTaskCategoryCostChange = (categoryId: TaskCategoryID, value: string) => { + const cost = parseFloat(value); + dispatch(updateTaskCategoryCost(categoryId, cost)); + }; - const onTaskCategoryLabelChange = (categoryId: TaskCategoryID, value: string) => { - dispatch(updateTaskCategoryLabel(categoryId, value)); - }; + const onNewTaskCategoryCostChange = (evt: Event) => { + const costPerTimeUnit = parseFloat((evt.currentTarget as HTMLInputElement).value); + setNewTaskCategory(newTaskCategory => ({ ...newTaskCategory, costPerTimeUnit })); + }; - const onTaskCategoryCostChange = (categoryId: TaskCategoryID, value: string) => { - const cost = parseFloat(value); - dispatch(updateTaskCategoryCost(categoryId, cost)); - }; + const onNewTaskCategoryLabelChange = (evt: Event) => { + const label = (evt.currentTarget as HTMLInputElement).value; + setNewTaskCategory(newTaskCategory => ({ ...newTaskCategory, label })); + }; - return ( -
- - - - - - - - - - - { - Object.values(project.params.taskCategories).map(tc => { - return ( - - - - - - ); - }) - } - -
CatégorieCoût par unité de temps
- - - - ({value} {getCurrency(project)})} - onChange={onTaskCategoryCostChange.bind(null, tc.id)} /> -
-
- ); + const onNewTaskCategoryAddClick = (evt: Event) => { + dispatch(addTaskCategory(newTaskCategory)); + setNewTaskCategory(createTaskCategory()); + }; + + return ( +
+ + + + + + + + + + + { + Object.values(project.params.taskCategories).map(tc => { + return ( + + + + + + ); + }) + } + + + + + + + +
CatégorieCoût par unité de temps
+ + + + + ({value} {getCurrency(project)})} + onChange={onTaskCategoryCostChange.bind(null, tc.id)} /> +
+
+

+ +

+

+ +

+

+ {getCurrency(project)} +

+

+ + Ajouter + +

+
+
+
+ ); }; - + export default TaskCategoriesTable;