guesstimate/src/routes/project/tasks-table.tsx

201 lines
9.4 KiB
TypeScript
Raw Normal View History

2020-04-20 11:14:46 +02:00
import { FunctionalComponent, h } from "preact";
import { useState, useEffect } from "preact/hooks";
2020-04-22 22:07:52 +02:00
import style from "./style.module.css";
2020-04-20 11:14:46 +02:00
import { Project } from "../../models/project";
import { newTask, Task, TaskID, EstimationConfidence } from "../../models/task";
2020-04-21 09:24:39 +02:00
import EditableText from "../../components/editable-text";
2020-04-21 14:10:50 +02:00
import { usePrintMediaQuery } from "../../hooks/use-media-query";
import { defaults, getTimeUnit } from "../../models/params";
import ProjectTimeUnit from "../../components/project-time-unit";
2020-04-20 11:14:46 +02:00
export interface TaskTableProps {
project: Project
onTaskAdd: (task: Task) => void
onTaskRemove: (taskId: TaskID) => void
onEstimationChange: (taskId: TaskID, confidence: EstimationConfidence, value: number) => void
2020-04-21 09:24:39 +02:00
onTaskLabelUpdate: (taskId: TaskID, label: string) => void
2020-04-20 11:14:46 +02:00
}
export type EstimationTotals = { [confidence in EstimationConfidence]: number }
2020-04-21 09:24:39 +02:00
const TaskTable: FunctionalComponent<TaskTableProps> = ({ project, onTaskAdd, onEstimationChange, onTaskRemove, onTaskLabelUpdate }) => {
2020-04-20 11:14:46 +02:00
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);
2020-04-21 14:10:50 +02:00
const isPrint = usePrintMediaQuery();
2020-04-20 11:14:46 +02:00
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]);
2020-04-21 09:24:39 +02:00
const onNewTaskLabelChange = (evt: Event) => {
2020-04-20 11:14:46 +02:00
const value = (evt.currentTarget as HTMLInputElement).value;
setTask({...task, label: value});
};
2020-04-21 09:24:39 +02:00
const onNewTaskCategoryChange = (evt: Event) => {
2020-04-20 11:14:46 +02:00
const value = (evt.currentTarget as HTMLInputElement).value;
setTask({...task, category: value});
};
2020-04-21 09:24:39 +02:00
const onTaskLabelChange = (taskId: TaskID, value: string) => {
onTaskLabelUpdate(taskId, value);
};
2020-04-20 11:14:46 +02:00
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 (
<div class="table-container">
2020-04-21 14:10:50 +02:00
<table class={`table is-bordered is-striped is-hoverable is-fullwidth ${style.middleTable}`}>
2020-04-20 11:14:46 +02:00
<thead>
<tr>
2020-04-21 14:10:50 +02:00
<th class={`${style.noBorder} noPrint`} rowSpan={2}></th>
2020-04-20 11:14:46 +02:00
<th class={style.mainColumn} rowSpan={2}>Tâche</th>
<th rowSpan={2}>Catégorie</th>
2020-04-21 20:45:47 +02:00
<th colSpan={3}>Estimation (en <ProjectTimeUnit project={project} />)</th>
2020-04-20 11:14:46 +02:00
</tr>
<tr>
<th>Optimiste</th>
<th>Probable</th>
<th>Pessimiste</th>
</tr>
</thead>
<tbody>
{
Object.values(project.tasks).map(t => {
const category = project.params.taskCategories[t.category];
const categoryLabel = category ? category.label : '???';
return (
<tr key={`taks-${t.id}`}>
2020-04-21 14:10:50 +02:00
<td class={`is-narrow noPrint`}>
2020-04-20 11:14:46 +02:00
<button
onClick={onTaskRemoveClick.bind(null, t.id)}
class="button is-danger is-small is-outlined">
🗑
</button>
</td>
2020-04-21 09:24:39 +02:00
<td class={style.mainColumn}>
<EditableText
render={(value) => (<span>{value}</span>)}
onChange={onTaskLabelChange.bind(null, t.id)}
value={t.label} />
</td>
2020-04-20 11:14:46 +02:00
<td>{ categoryLabel }</td>
<td>
2020-04-21 14:10:50 +02:00
{
isPrint ?
<span>{t.estimations.optimistic}</span> :
<input class="input" type="number" value={t.estimations.optimistic}
min={0}
onChange={onOptimisticChange.bind(null, t.id)} />
}
2020-04-20 11:14:46 +02:00
</td>
<td>
2020-04-21 14:10:50 +02:00
{
isPrint ?
<span>{t.estimations.likely}</span> :
<input class="input" type="number" value={t.estimations.likely}
min={0}
onChange={onLikelyChange.bind(null, t.id)} />
}
2020-04-20 11:14:46 +02:00
</td>
<td>
2020-04-21 14:10:50 +02:00
{
isPrint ?
<span>{t.estimations.pessimistic}</span> :
<input class="input" type="number" value={t.estimations.pessimistic}
min={0}
onChange={onPessimisticChange.bind(null, t.id)} />
}
2020-04-20 11:14:46 +02:00
</td>
</tr>
)
})
}
{
Object.keys(project.tasks).length === 0 ?
<tr>
2020-04-21 14:10:50 +02:00
<td class={`${style.noBorder} noPrint`}></td>
2020-04-20 11:14:46 +02:00
<td class={style.noTasks} colSpan={5}>Aucune tâche pour l'instant.</td>
</tr> :
null
}
</tbody>
<tfoot>
<tr>
2020-04-21 14:10:50 +02:00
<td class={`${style.noBorder} noPrint`}></td>
<td colSpan={2} class={isPrint ? style.noBorder : ''}>
<div class="field has-addons noPrint">
2020-04-20 11:14:46 +02:00
<p class="control is-expanded">
<input class="input" type="text" placeholder="Nouvelle tâche"
2020-04-21 09:24:39 +02:00
value={task.label} onChange={onNewTaskLabelChange} />
2020-04-20 11:14:46 +02:00
</p>
<p class="control">
<span class="select">
2020-04-21 09:24:39 +02:00
<select onChange={onNewTaskCategoryChange} value={task.category}>
2020-04-20 11:14:46 +02:00
{
Object.values(project.params.taskCategories).map(tc => {
return (
<option key={`task-category-${tc.id}`} value={tc.id}>{tc.label}</option>
);
})
}
</select>
</span>
</p>
<p class="control">
<a class="button is-primary" onClick={onAddTaskClick}>
Ajouter
</a>
</p>
</div>
</td>
<th colSpan={3}>Total</th>
</tr>
<tr>
2020-04-21 14:10:50 +02:00
<td colSpan={isPrint ? 2 : 3} class={style.noBorder}></td>
<td>{totals.optimistic} <ProjectTimeUnit project={project} /></td>
<td>{totals.likely} <ProjectTimeUnit project={project} /></td>
<td>{totals.pessimistic} <ProjectTimeUnit project={project} /></td>
2020-04-20 11:14:46 +02:00
</tr>
</tfoot>
</table>
</div>
);
};
export default TaskTable;