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.

135 lines
4.4KB

  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. import ColumnPassback from "./types/ColumnPassback";
  8. import initState from "./state_examples/kbstate";
  9. type State = {
  10. columns: Column[];
  11. columnIterator: number;
  12. taskIterator: number;
  13. };
  14. class App extends React.Component<{}, State> {
  15. constructor(props: any) {
  16. super(props);
  17. this.deleteColumn = this.deleteColumn.bind(this);
  18. this.deleteTask = this.deleteTask.bind(this);
  19. this.addColumn = this.addColumn.bind(this);
  20. this.addTask = this.addTask.bind(this);
  21. this.state = {
  22. taskIterator: initState.taskIterator,
  23. columnIterator: initState.columnIterator,
  24. columns: initState.columns as Column[],
  25. };
  26. }
  27. deleteTask(cId: number, tId: number) {
  28. /* Find Column */
  29. const correctColunm = this.state.columns.filter(
  30. (column) => column.id === cId //Grabs the correct column according to the column ID
  31. );
  32. let columnsNew = this.state.columns; //Clones columns. Should be done earlier???????
  33. const corrIndex = columnsNew.indexOf(correctColunm[0]); //Grabs the index of the correct column. [0] is bad programming, but makes sense due to IDs being unique
  34. /* Find and Remove Task from Found Column */
  35. let alteredColumn = columnsNew[corrIndex]; //Collects a copt of the correct column. Again... unsure if timing is correct
  36. alteredColumn.tasks = alteredColumn.tasks.filter((task) => task.id !== tId); //Filter all the tasks to allow all except from the wanted. Maybe could be more efficient
  37. // Unsure how TS works under the hood
  38. columnsNew[corrIndex] = alteredColumn; //Input the altered column into the cloned list of columns
  39. this.setState({ columns: columnsNew }); //Push the cloned list with the altered column into the state
  40. alert("Deleted Task" + tId + "from Col" + cId); // Test for column numbers and IDs
  41. }
  42. deleteColumn(id: number) {
  43. const { columns } = { ...this.state };
  44. const filteredColumns = columns.filter((column) => column.id !== id);
  45. this.setState({ columns: filteredColumns });
  46. console.log(this.state.columns);
  47. }
  48. addColumn(columnInstance?: ColumnPassback) {
  49. let columnId = this.state.columnIterator;
  50. this.setState({ columnIterator: columnId + 1 });
  51. if (columnInstance !== undefined) {
  52. this.state.columns.push({
  53. id: columnId,
  54. name: columnInstance?.title,
  55. color: columnInstance?.color,
  56. tasks: [],
  57. });
  58. }
  59. }
  60. addTask(taskInstance?: TaskPassback) {
  61. console.log(taskInstance);
  62. if (this.state.columns.length > 0) {
  63. let taskId = this.state.taskIterator;
  64. let index = -2;
  65. if (taskInstance !== undefined) {
  66. this.setState({ taskIterator: taskId + 1 });
  67. console.log(this.state.columns);
  68. console.log(taskInstance.column);
  69. let indexObj = this.state.columns.find(
  70. (o) => o.id === taskInstance.column
  71. );
  72. if (indexObj) {
  73. index = this.state.columns.indexOf(indexObj);
  74. } else {
  75. alert("Clifford");
  76. }
  77. if (index >= 0) {
  78. this.state.columns[index].tasks.push({
  79. id: taskId,
  80. title: taskInstance.title,
  81. description: taskInstance.description,
  82. priority: taskInstance.priority,
  83. });
  84. } else {
  85. alert(
  86. "Error finding that column. Check it hasn't already been deleted!" +
  87. index
  88. );
  89. }
  90. } else {
  91. alert("Oh god, oh heck");
  92. }
  93. } else {
  94. alert("You might want to consider adding a column!");
  95. }
  96. console.log(this.state);
  97. }
  98. render() {
  99. return (
  100. <div className="App">
  101. <Header
  102. addColumn={this.addColumn}
  103. addTask={this.addTask}
  104. columns={this.state.columns}
  105. />
  106. <div className="panel">
  107. {this.state.columns.map((column, index) => (
  108. <KBColumn
  109. color={column.color}
  110. title={column.name}
  111. key={index}
  112. onDelete={this.deleteColumn}
  113. deleteTask={this.deleteTask}
  114. id={column.id}
  115. tasks={column.tasks}
  116. />
  117. ))}
  118. </div>
  119. </div>
  120. );
  121. }
  122. }
  123. export default App;