Me learning react like an absolute chungus
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

230 lines
6.6KB

  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.state = {
  18. taskIterator: initState.taskIterator,
  19. columnIterator: initState.columnIterator,
  20. columns: initState.columns as Column[],
  21. };
  22. }
  23. /* Deletes a task from a specific column with a specific ID
  24. @param columnID: The ID of the column in which the task to be deleted resides
  25. @param taskID: The ID of the task which is to be deleted
  26. @return void
  27. !!!Bugs: TBA
  28. Last Edited 2020/04/29 15:20
  29. */
  30. deleteTask = (columnID: number, taskID: number) => {
  31. // Find Column
  32. let columnsNew = this.state.columns;
  33. const correctColunm = columnsNew.find((column) => column.id === columnID);
  34. if (!correctColunm) {
  35. return;
  36. }
  37. const corrIndex = columnsNew.indexOf(correctColunm);
  38. // Find and Remove Task from Found Column
  39. let alteredColumn = correctColunm;
  40. alteredColumn.tasks = alteredColumn.tasks.filter(
  41. (task) => task.id !== taskID
  42. );
  43. columnsNew[corrIndex] = alteredColumn;
  44. this.setState({ columns: columnsNew });
  45. };
  46. /* Deletes a column with a specific ID
  47. @param id: The ID of the column that is to be deleted
  48. @return void
  49. Bugs: None that i know of
  50. Last Edited 2020/04/27 17:30
  51. */
  52. deleteColumn = (id: number) => {
  53. const { columns } = { ...this.state };
  54. const filteredColumns = columns.filter((column) => column.id !== id);
  55. this.setState({ columns: filteredColumns });
  56. console.log(this.state.columns);
  57. };
  58. /* Adds a column to the panel
  59. @param columnInstance: Data massed back from the modal which contains all the information to create a column
  60. @return void
  61. Bugs: None that I am aware of
  62. Last Edited 2020/04/28 16:00
  63. */
  64. addColumn = (columnInstance?: ColumnPassback) => {
  65. let columnId = this.state.columnIterator;
  66. this.setState({ columnIterator: columnId + 1 });
  67. if (columnInstance) {
  68. this.state.columns.push({
  69. id: columnId,
  70. name: columnInstance?.title,
  71. color: columnInstance?.color,
  72. tasks: [],
  73. });
  74. }
  75. };
  76. /* Progresses the task to the column immediately to the right
  77. @param columnId: The column which the task is originally hosted at
  78. @param taskId: The UID for the task
  79. @return void
  80. Bugs: None that I am aware of
  81. Last Edited: 2020/04/30 10:30
  82. */
  83. progressTask = (columnId: number, taskId: number) => {
  84. let columns = this.state.columns;
  85. let fromColumn = columns.find((column) => column.id === columnId);
  86. if (!fromColumn) {
  87. alert("Initial Column not found. State bad");
  88. return;
  89. }
  90. let fromColumnIndex = columns.indexOf(fromColumn);
  91. if (!columns[fromColumnIndex + 1]) {
  92. alert("There is no column to progress to. Voiding action");
  93. return;
  94. }
  95. let toColumn = columns[fromColumnIndex + 1];
  96. let movingTask = fromColumn.tasks.find((task) => task.id === taskId);
  97. if (!movingTask) {
  98. alert("Moving task not found. Voiding action");
  99. return;
  100. }
  101. toColumn.tasks.push(movingTask);
  102. this.deleteTask(columnId, taskId);
  103. columns[fromColumnIndex + 1] = toColumn;
  104. this.setState({ columns: columns });
  105. };
  106. /* Regresses the task to the column immediately to the left
  107. @param columnId: The column which the task is originally hosted at
  108. @param taskId: The UID for the task
  109. @return void
  110. Bugs: None that I am aware of
  111. Last Edited: 2020/04/30 10:50
  112. */
  113. regressTask = (columnId: number, taskId: number) => {
  114. let columns = this.state.columns;
  115. let fromColumn = columns.find((column) => column.id === columnId);
  116. if (!fromColumn) {
  117. alert("Initial Column not found. State bad");
  118. return;
  119. }
  120. let fromColumnIndex = columns.indexOf(fromColumn);
  121. if (!columns[fromColumnIndex - 1]) {
  122. alert("There is no column to regress to. Voiding action");
  123. return;
  124. }
  125. let toColumn = columns[fromColumnIndex - 1];
  126. let movingTask = fromColumn.tasks.find((task) => task.id === taskId);
  127. if (!movingTask) {
  128. alert("Moving task not found. Voiding action");
  129. return;
  130. }
  131. toColumn.tasks.push(movingTask);
  132. this.deleteTask(columnId, taskId);
  133. columns[fromColumnIndex - 1] = toColumn;
  134. this.setState({ columns: columns });
  135. };
  136. /* Adds a task to a specified column
  137. @param taskInstance: Data passed back from the modal which contains the necessary information to create the task
  138. @return void
  139. Bugs: None that I am aware of
  140. Last Edited 2020/04/29 15:00
  141. */
  142. addTask = (taskInstance?: TaskPassback) => {
  143. if (this.state.columns.length === 0) {
  144. alert("You might want to consider adding a column!");
  145. return;
  146. }
  147. let taskId = this.state.taskIterator;
  148. let columns = this.state.columns;
  149. let index = -2;
  150. if (taskInstance) {
  151. this.setState({ taskIterator: taskId + 1 });
  152. let indexObj = columns.find((o) => o.id === taskInstance.column);
  153. if (indexObj) {
  154. index = columns.indexOf(indexObj);
  155. }
  156. if (index >= 0) {
  157. columns[index].tasks.push({
  158. id: taskId,
  159. title: taskInstance.title,
  160. description: taskInstance.description,
  161. priority: taskInstance.priority,
  162. });
  163. } else {
  164. alert(
  165. "Error finding that column. Check it hasn't already been deleted!" +
  166. index
  167. );
  168. }
  169. }
  170. };
  171. render() {
  172. return (
  173. <div className="App">
  174. <Header
  175. addColumn={this.addColumn}
  176. addTask={this.addTask}
  177. columns={this.state.columns}
  178. />
  179. <div className="panel">
  180. {this.state.columns.map((column, index) => (
  181. <KBColumn
  182. color={column.color}
  183. title={column.name}
  184. key={index}
  185. onDelete={this.deleteColumn}
  186. deleteTask={this.deleteTask}
  187. progressTask={this.progressTask}
  188. regressTask={this.regressTask}
  189. id={column.id}
  190. tasks={column.tasks}
  191. />
  192. ))}
  193. </div>
  194. <div className="footer">
  195. Not to be taken seriously. Done as a practice React + TS + MobX
  196. project. Form over function... That's what I always say
  197. </div>
  198. </div>
  199. );
  200. }
  201. }
  202. export default App;