import { FunctionalComponent, h } from "preact"; import { useState, useEffect } from "preact/hooks"; import * as style from "./style.css"; import { Project } from "../../models/project"; import { newTask, Task, TaskID, EstimationConfidence } from "../../models/task"; export interface TaskTableProps { project: Project onTaskAdd: (task: Task) => void onTaskRemove: (taskId: TaskID) => void onEstimationChange: (taskId: TaskID, confidence: EstimationConfidence, value: number) => void } export type EstimationTotals = { [confidence in EstimationConfidence]: number } const TaskTable: FunctionalComponent = ({ project, onTaskAdd, onEstimationChange, onTaskRemove }) => { const defaultTaskCategory = Object.keys(project.params.taskCategories)[0]; const [ task, setTask ] = useState(newTask("", defaultTaskCategory)); const [ totals, setTotals ] = useState({ [EstimationConfidence.Optimistic]: 0, [EstimationConfidence.Likely]: 0, [EstimationConfidence.Pessimistic]: 0, } as EstimationTotals); useEffect(() => { let optimistic = 0; let likely = 0; let pessimistic = 0; Object.values(project.tasks).forEach(t => { optimistic += t.estimations.optimistic; likely += t.estimations.likely; pessimistic += t.estimations.pessimistic; }); setTotals({ optimistic, likely, pessimistic }); }, [project.tasks]); const onTaskLabelChange = (evt: Event) => { const value = (evt.currentTarget as HTMLInputElement).value; setTask({...task, label: value}); }; const onTaskCategoryChange = (evt: Event) => { const value = (evt.currentTarget as HTMLInputElement).value; setTask({...task, category: value}); }; const onAddTaskClick = (evt: Event) => { onTaskAdd(task); setTask(newTask("", defaultTaskCategory)); }; const onTaskRemoveClick = (taskId: TaskID, evt: Event) => { onTaskRemove(taskId); }; const withEstimationChange = (confidence: EstimationConfidence, taskID: TaskID, evt: Event) => { const textValue = (evt.currentTarget as HTMLInputElement).value; const value = parseFloat(textValue); onEstimationChange(taskID, confidence, value); }; const onOptimisticChange = withEstimationChange.bind(null, EstimationConfidence.Optimistic); const onLikelyChange = withEstimationChange.bind(null, EstimationConfidence.Likely); const onPessimisticChange = withEstimationChange.bind(null, EstimationConfidence.Pessimistic); return (
{ Object.values(project.tasks).map(t => { const category = project.params.taskCategories[t.category]; const categoryLabel = category ? category.label : '???'; return ( ) }) } { Object.keys(project.tasks).length === 0 ? : null }
Tâche Catégorie Estimation
Optimiste Probable Pessimiste
{t.label} { categoryLabel }
Aucune tâche pour l'instant.

Ajouter

Total
{totals.optimistic} {totals.likely} {totals.pessimistic}
); }; export default TaskTable;