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.

109 lines
2.5KB

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