import React, { FunctionComponent, useState, MouseEvent, ChangeEvent } from "react"; import { Project } from "../../models/project"; import style from './style.module.css'; import { ProjectReducerActions, updateTaskCategoryCost, updateTaskCategoryLabel, removeTaskCategory, addTaskCategory } from "../../hooks/use-project-reducer"; import EditableText from "../../components/editable-text"; import { TaskCategoryID, createTaskCategory } from "../../models/task"; import { getCurrency, getTaskCategoryCost } from "../../models/params"; export interface TaskCategoriesTableProps { project: Project dispatch: (action: ProjectReducerActions) => void } const TaskCategoriesTable: FunctionComponent = ({ project, dispatch }) => { 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 onNewTaskCategoryCostChange = (evt: ChangeEvent) => { const costPerTimeUnit = parseFloat((evt.currentTarget as HTMLInputElement).value); setNewTaskCategory(newTaskCategory => ({ ...newTaskCategory, costPerTimeUnit })); }; const onNewTaskCategoryLabelChange = (evt: ChangeEvent) => { const label = (evt.currentTarget as HTMLInputElement).value; setNewTaskCategory(newTaskCategory => ({ ...newTaskCategory, label })); }; const onNewTaskCategoryAddClick = (evt: MouseEvent) => { dispatch(addTaskCategory(newTaskCategory)); setNewTaskCategory(createTaskCategory()); }; return (
{ Object.values(project.params.taskCategories).map(tc => { return ( ); }) }
Catégorie Coût par unité de temps
({value} {getCurrency(project)})} onChange={onTaskCategoryCostChange.bind(null, tc.id)} />
); }; export default TaskCategoriesTable;