Me learning react like an absolute chungus
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

112 line
2.7KB

  1. import React from "react";
  2. import KBColumn from "./components/KBColumn";
  3. import "./App.css";
  4. import Header from "./components/Header";
  5. import Column from "./types/Column";
  6. import TaskPassback from "./types/TaskPassback";
  7. type State = {
  8. columns: Column[];
  9. columnIterator: number;
  10. taskIterator: number;
  11. };
  12. class App extends React.Component<{}, State> {
  13. constructor(props: any) {
  14. super(props);
  15. this.deleteColumn = this.deleteColumn.bind(this);
  16. this.addColumn = this.addColumn.bind(this);
  17. this.addTask = this.addTask.bind(this);
  18. this.state = {
  19. taskIterator: 1,
  20. columnIterator: 3,
  21. columns: [
  22. {
  23. id: 1,
  24. name: "Harry",
  25. color: "green",
  26. tasks: [],
  27. },
  28. {
  29. id: 2,
  30. name: "Potter",
  31. color: "red",
  32. tasks: [],
  33. },
  34. ],
  35. };
  36. }
  37. deleteColumn(id: number) {
  38. const { columns } = { ...this.state };
  39. const filteredColumns = columns.filter((column) => column.id !== id);
  40. this.setState({ columns: filteredColumns });
  41. console.log(this.state.columns);
  42. }
  43. addColumn() {
  44. let columnId = this.state.columnIterator;
  45. this.setState({ columnIterator: columnId + 1 });
  46. this.state.columns.push({
  47. id: columnId,
  48. name: "tempName" + columnId,
  49. color: "#404552",
  50. tasks: [],
  51. });
  52. }
  53. addTask(taskInstance?: TaskPassback) {
  54. console.log(taskInstance);
  55. if (this.state.columns.length > 0) {
  56. let taskId = this.state.taskIterator;
  57. let index = -1;
  58. if (taskInstance !== undefined) {
  59. this.setState({ taskIterator: taskId + 1 });
  60. let indexObj = this.state.columns.find(
  61. (o) => o.id === taskInstance.column
  62. );
  63. if (indexObj) {
  64. index = this.state.columns.indexOf(indexObj);
  65. }
  66. if (index >= 0) {
  67. this.state.columns[index].tasks.push({
  68. id: taskId,
  69. title: taskInstance.title,
  70. description: taskInstance.description,
  71. priority: taskInstance.priority,
  72. });
  73. }
  74. }
  75. } else {
  76. alert("You might want to consider adding a column!");
  77. }
  78. }
  79. render() {
  80. return (
  81. <div className="App">
  82. <Header
  83. addColumn={this.addColumn}
  84. addTask={this.addTask}
  85. columns={this.state.columns}
  86. />
  87. <div className="panel">
  88. {this.state.columns.map((column, index) => (
  89. <KBColumn
  90. color={column.color}
  91. title={column.name}
  92. key={index}
  93. onDelete={this.deleteColumn}
  94. id={column.id}
  95. tasks={column.tasks}
  96. />
  97. ))}
  98. </div>
  99. </div>
  100. );
  101. }
  102. }
  103. export default App;