guesstimate/client/src/routes/project/task-categories-table.tsx

114 lines
4.2 KiB
TypeScript
Raw Normal View History

2020-04-21 20:45:47 +02:00
import { FunctionalComponent, h } from "preact";
import { Project } from "../../models/project";
2020-04-22 22:07:52 +02:00
import style from './style.module.css';
2020-04-22 22:48:47 +02:00
import { ProjectReducerActions, updateTaskCategoryCost, updateTaskCategoryLabel, removeTaskCategory, addTaskCategory } from "../../hooks/use-project-reducer";
2020-04-21 20:45:47 +02:00
import EditableText from "../../components/editable-text";
2020-04-22 22:48:47 +02:00
import { TaskCategoryID, createTaskCategory } from "../../models/task";
2020-04-21 20:45:47 +02:00
import { getCurrency, getTaskCategoryCost } from "../../models/params";
2020-04-22 22:48:47 +02:00
import { useState } from "preact/hooks";
2020-04-21 20:45:47 +02:00
export interface TaskCategoriesTableProps {
2020-04-22 22:48:47 +02:00
project: Project
dispatch: (action: ProjectReducerActions) => void
2020-04-21 20:45:47 +02:00
}
const TaskCategoriesTable: FunctionalComponent<TaskCategoriesTableProps> = ({ project, dispatch }) => {
2020-04-22 22:48:47 +02:00
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));
};
2020-04-21 20:45:47 +02:00
2020-04-22 22:48:47 +02:00
const onNewTaskCategoryCostChange = (evt: Event) => {
const costPerTimeUnit = parseFloat((evt.currentTarget as HTMLInputElement).value);
setNewTaskCategory(newTaskCategory => ({ ...newTaskCategory, costPerTimeUnit }));
};
2020-04-21 20:45:47 +02:00
2020-04-22 22:48:47 +02:00
const onNewTaskCategoryLabelChange = (evt: Event) => {
const label = (evt.currentTarget as HTMLInputElement).value;
setNewTaskCategory(newTaskCategory => ({ ...newTaskCategory, label }));
};
2020-04-21 20:45:47 +02:00
2020-04-22 22:48:47 +02:00
const onNewTaskCategoryAddClick = (evt: Event) => {
dispatch(addTaskCategory(newTaskCategory));
setNewTaskCategory(createTaskCategory());
};
return (
<div class="table-container">
2020-04-22 23:18:18 +02:00
<label class="label is-size-5">Catégories de tâche</label>
2020-04-22 22:48:47 +02:00
<table class={`table is-bordered is-striped" ${style.middleTable}`}>
<thead>
<tr>
<th class={`${style.noBorder} is-narrow`}></th>
<th>Catégorie</th>
<th>Coût par unité de temps</th>
</tr>
</thead>
<tbody>
{
Object.values(project.params.taskCategories).map(tc => {
return (
<tr key={`task-category-${tc.id}`}>
<td>
<button
onClick={onTaskCategoryRemove.bind(null, tc.id)}
class="button is-danger is-small is-outlined">
🗑
</button>
</td>
<td>
<EditableText value={tc.label}
onChange={onTaskCategoryLabelChange.bind(null, tc.id)} />
</td>
<td>
<EditableText value={`${getTaskCategoryCost(tc)}`}
render={value=> (<span>{value} {getCurrency(project)}</span>)}
onChange={onTaskCategoryCostChange.bind(null, tc.id)} />
</td>
</tr>
);
})
}
</tbody>
<tfoot>
<tr>
<td class={`${style.noBorder}`}></td>
<td colSpan={2}>
<div class="field has-addons">
<p class="control is-expanded">
<input class="input" type="text" placeholder="Nouvelle catégorie"
value={newTaskCategory.label} onChange={onNewTaskCategoryLabelChange} />
</p>
<p class="control">
<input class="input" type="number"
value={newTaskCategory.costPerTimeUnit} onChange={onNewTaskCategoryCostChange} />
</p>
<p class="control">
<a class="button is-static">{getCurrency(project)}</a>
</p>
<p class="control">
<a class="button is-primary" onClick={onNewTaskCategoryAddClick}>
Ajouter
</a>
</p>
</div>
</td>
</tr>
</tfoot>
</table>
</div>
);
2020-04-21 20:45:47 +02:00
};
2020-04-22 22:48:47 +02:00
2020-04-21 20:45:47 +02:00
export default TaskCategoriesTable;