2020-08-05 08:14:22 +03:00
|
|
|
import React from "react";
|
2020-08-23 22:33:25 +03:00
|
|
|
import { Task, ViewMode, Gantt } from "gantt-task-react";
|
2020-08-05 08:14:22 +03:00
|
|
|
import { ViewSwitcher } from "./components/view-switcher";
|
2021-03-27 21:05:38 +02:00
|
|
|
import { getStartEndDateForProject, initTasks } from "./helper";
|
2021-03-25 23:06:20 +02:00
|
|
|
import "gantt-task-react/dist/index.css";
|
2020-08-05 08:14:22 +03:00
|
|
|
|
2022-01-19 17:04:17 +01:00
|
|
|
// Init
|
2020-08-05 08:14:22 +03:00
|
|
|
const App = () => {
|
|
|
|
|
const [view, setView] = React.useState<ViewMode>(ViewMode.Day);
|
2021-03-01 21:55:27 +02:00
|
|
|
const [tasks, setTasks] = React.useState<Task[]>(initTasks());
|
2020-08-23 22:33:25 +03:00
|
|
|
const [isChecked, setIsChecked] = React.useState(true);
|
2022-02-06 21:52:49 +01:00
|
|
|
let columnWidth = 65;
|
2020-08-23 22:33:25 +03:00
|
|
|
if (view === ViewMode.Month) {
|
|
|
|
|
columnWidth = 300;
|
|
|
|
|
} else if (view === ViewMode.Week) {
|
|
|
|
|
columnWidth = 250;
|
|
|
|
|
}
|
2020-08-05 08:14:22 +03:00
|
|
|
|
2021-08-09 22:21:18 +03:00
|
|
|
const handleTaskChange = (task: Task) => {
|
2020-08-05 08:14:22 +03:00
|
|
|
console.log("On date change Id:" + task.id);
|
2021-03-27 21:05:38 +02:00
|
|
|
let newTasks = tasks.map(t => (t.id === task.id ? task : t));
|
|
|
|
|
if (task.project) {
|
|
|
|
|
const [start, end] = getStartEndDateForProject(newTasks, task.project);
|
|
|
|
|
const project = newTasks[newTasks.findIndex(t => t.id === task.project)];
|
|
|
|
|
if (
|
|
|
|
|
project.start.getTime() !== start.getTime() ||
|
|
|
|
|
project.end.getTime() !== end.getTime()
|
|
|
|
|
) {
|
|
|
|
|
const changedProject = { ...project, start, end };
|
|
|
|
|
newTasks = newTasks.map(t =>
|
|
|
|
|
t.id === task.project ? changedProject : t
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-03-01 21:55:27 +02:00
|
|
|
setTasks(newTasks);
|
2020-08-05 08:14:22 +03:00
|
|
|
};
|
|
|
|
|
|
2021-08-09 22:21:18 +03:00
|
|
|
const handleTaskDelete = (task: Task) => {
|
2020-09-09 17:08:16 +03:00
|
|
|
const conf = window.confirm("Are you sure about " + task.name + " ?");
|
2021-03-01 21:55:27 +02:00
|
|
|
if (conf) {
|
|
|
|
|
setTasks(tasks.filter(t => t.id !== task.id));
|
|
|
|
|
}
|
2020-09-09 17:08:16 +03:00
|
|
|
return conf;
|
2020-08-05 08:14:22 +03:00
|
|
|
};
|
|
|
|
|
|
2021-08-09 22:21:18 +03:00
|
|
|
const handleProgressChange = async (task: Task) => {
|
2021-03-01 21:55:27 +02:00
|
|
|
setTasks(tasks.map(t => (t.id === task.id ? task : t)));
|
2020-08-05 08:14:22 +03:00
|
|
|
console.log("On progress change Id:" + task.id);
|
|
|
|
|
};
|
|
|
|
|
|
2021-08-09 22:21:18 +03:00
|
|
|
const handleDblClick = (task: Task) => {
|
2020-08-05 08:14:22 +03:00
|
|
|
alert("On Double Click event Id:" + task.id);
|
|
|
|
|
};
|
|
|
|
|
|
2022-02-22 16:35:56 +01:00
|
|
|
const handleClick = (task: Task) => {
|
|
|
|
|
console.log("On Click event Id:" + task.id);
|
|
|
|
|
};
|
|
|
|
|
|
2021-08-09 22:21:18 +03:00
|
|
|
const handleSelect = (task: Task, isSelected: boolean) => {
|
2020-09-09 17:08:16 +03:00
|
|
|
console.log(task.name + " has " + (isSelected ? "selected" : "unselected"));
|
|
|
|
|
};
|
|
|
|
|
|
2021-08-09 22:21:18 +03:00
|
|
|
const handleExpanderClick = (task: Task) => {
|
|
|
|
|
setTasks(tasks.map(t => (t.id === task.id ? task : t)));
|
|
|
|
|
console.log("On expander click Id:" + task.id);
|
|
|
|
|
};
|
|
|
|
|
|
2020-08-05 08:14:22 +03:00
|
|
|
return (
|
2022-07-10 14:14:27 +02:00
|
|
|
<div className="Wrapper">
|
2020-08-23 22:33:25 +03:00
|
|
|
<ViewSwitcher
|
|
|
|
|
onViewModeChange={viewMode => setView(viewMode)}
|
|
|
|
|
onViewListChange={setIsChecked}
|
|
|
|
|
isChecked={isChecked}
|
|
|
|
|
/>
|
|
|
|
|
<h3>Gantt With Unlimited Height</h3>
|
2021-08-03 21:27:44 +03:00
|
|
|
<Gantt
|
|
|
|
|
tasks={tasks}
|
|
|
|
|
viewMode={view}
|
2021-08-09 22:21:18 +03:00
|
|
|
onDateChange={handleTaskChange}
|
|
|
|
|
onDelete={handleTaskDelete}
|
|
|
|
|
onProgressChange={handleProgressChange}
|
|
|
|
|
onDoubleClick={handleDblClick}
|
2022-02-22 16:35:56 +01:00
|
|
|
onClick={handleClick}
|
2021-08-09 22:21:18 +03:00
|
|
|
onSelect={handleSelect}
|
|
|
|
|
onExpanderClick={handleExpanderClick}
|
2021-08-03 21:27:44 +03:00
|
|
|
listCellWidth={isChecked ? "155px" : ""}
|
|
|
|
|
columnWidth={columnWidth}
|
|
|
|
|
/>
|
2020-08-23 22:33:25 +03:00
|
|
|
<h3>Gantt With Limited Height</h3>
|
|
|
|
|
<Gantt
|
2020-08-05 08:14:22 +03:00
|
|
|
tasks={tasks}
|
|
|
|
|
viewMode={view}
|
2021-08-09 22:21:18 +03:00
|
|
|
onDateChange={handleTaskChange}
|
|
|
|
|
onDelete={handleTaskDelete}
|
|
|
|
|
onProgressChange={handleProgressChange}
|
|
|
|
|
onDoubleClick={handleDblClick}
|
2022-02-22 16:35:56 +01:00
|
|
|
onClick={handleClick}
|
2021-08-09 22:21:18 +03:00
|
|
|
onSelect={handleSelect}
|
|
|
|
|
onExpanderClick={handleExpanderClick}
|
2020-09-01 23:08:15 +03:00
|
|
|
listCellWidth={isChecked ? "155px" : ""}
|
2020-08-23 22:33:25 +03:00
|
|
|
ganttHeight={300}
|
|
|
|
|
columnWidth={columnWidth}
|
2020-08-05 08:14:22 +03:00
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default App;
|