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.

230 lines
5.9KB

  1. import React from "react";
  2. import Modal from "./Modal";
  3. import Column from "../types/Column";
  4. import TaskPassback from "../types/TaskPassback";
  5. import Priority from "../types/Priority";
  6. import ColumnPassback from "../types/ColumnPassback";
  7. type HeaderProps = {
  8. addColumn: (columnInstance?: ColumnPassback) => void;
  9. addTask: (taskInstance?: TaskPassback) => void;
  10. columns: Column[];
  11. };
  12. type State = {
  13. showTask: boolean;
  14. taskTitle: string;
  15. taskDesc: string;
  16. taskPrio: Priority;
  17. taskColumnNumber: number;
  18. showColumn: boolean;
  19. columnTitle: string;
  20. columnColor: string;
  21. };
  22. class Header extends React.Component<HeaderProps, State> {
  23. constructor(props: HeaderProps) {
  24. super(props);
  25. this.state = {
  26. showTask: false,
  27. showColumn: false,
  28. taskTitle: "",
  29. taskDesc: "",
  30. taskPrio: "normal",
  31. taskColumnNumber: -1,
  32. columnTitle: "",
  33. columnColor: "#00CC00",
  34. };
  35. }
  36. showTaskModal = () => {
  37. if (this.props.columns.length > 0) {
  38. let { showTask, taskColumnNumber } = { ...this.state };
  39. showTask = true;
  40. taskColumnNumber = this.props.columns[0].id;
  41. this.setState({ showTask, taskColumnNumber });
  42. } else {
  43. alert("Add a column please!");
  44. }
  45. };
  46. showColumnModal = () => {
  47. let { showColumn } = { ...this.state };
  48. showColumn = true;
  49. this.setState({ showColumn });
  50. };
  51. hideModal = () => {
  52. this.setState({ showTask: false });
  53. };
  54. hideColumnModal = () => {
  55. this.setState({ showColumn: false });
  56. };
  57. handleInputChange = (event: React.ChangeEvent<HTMLInputElement>) => {
  58. const target = event.target.id;
  59. if (target === "titleInput") {
  60. this.setState({ taskTitle: event.target.value });
  61. }
  62. if (target === "descInput") {
  63. this.setState({ taskDesc: event.target.value });
  64. }
  65. if (target === "columnTitle") {
  66. this.setState({ columnTitle: event.target.value });
  67. }
  68. if (target === "columnColor") {
  69. this.setState({ columnColor: event.target.value });
  70. }
  71. };
  72. handleSelectChange = (event: React.ChangeEvent<HTMLSelectElement>) => {
  73. if (event.target.id === "prioritySelect") {
  74. const selectedValue = event.target.value as Priority;
  75. this.setState({ taskPrio: selectedValue });
  76. }
  77. if (event.target.id === "columnSelect") {
  78. const selectedValue = parseInt(event.target.value);
  79. this.setState({ taskColumnNumber: selectedValue });
  80. }
  81. };
  82. render() {
  83. return (
  84. <div className="header" style={{ height: 60 }}>
  85. <h1
  86. style={{
  87. float: "left",
  88. margin: 0,
  89. padding: 0,
  90. verticalAlign: "center",
  91. }}
  92. >
  93. Kango Bango
  94. </h1>
  95. <Modal show={this.state.showTask} handleClose={this.hideModal}>
  96. <h3> Add Task</h3>
  97. <p>{this.props.columns[0] && this.props.columns[0].id}</p>
  98. <select
  99. value={this.state.taskColumnNumber}
  100. onChange={this.handleSelectChange}
  101. id="columnSelect"
  102. >
  103. {this.props.columns.map((column) => (
  104. <option value={column.id}>{column.name}</option>
  105. ))}
  106. ;
  107. </select>
  108. <label>
  109. {" "}
  110. Title:
  111. <input
  112. type="text"
  113. id="titleInput"
  114. value={this.state.taskTitle}
  115. onChange={this.handleInputChange}
  116. />
  117. </label>
  118. <br />
  119. <label>
  120. {" "}
  121. Description:
  122. <input
  123. type="textarea"
  124. id="descInput"
  125. value={this.state.taskDesc}
  126. onChange={this.handleInputChange}
  127. />
  128. </label>
  129. <br />
  130. <label>
  131. {" "}
  132. Priority:
  133. <select
  134. value={this.state.taskPrio}
  135. onChange={this.handleSelectChange}
  136. id="prioritySelect"
  137. >
  138. <option value="normal">Normal</option>
  139. <option value="important">Important</option>
  140. <option value="urgent">Urgent</option>
  141. </select>
  142. </label>
  143. <br />
  144. <button
  145. onClick={() =>
  146. this.props.addTask({
  147. title: this.state.taskTitle,
  148. description: this.state.taskDesc,
  149. priority: this.state.taskPrio,
  150. column: this.state.taskColumnNumber,
  151. })
  152. }
  153. >
  154. Add Task
  155. </button>
  156. <button
  157. onClick={() => {
  158. this.hideModal();
  159. }}
  160. >
  161. Close
  162. </button>
  163. </Modal>
  164. <Modal show={this.state.showColumn} handleClose={this.hideModal}>
  165. <h3>Lets add a column! 🤔</h3>
  166. <label>
  167. {" "}
  168. Title:
  169. <input
  170. type="text"
  171. id="columnTitle"
  172. value={this.state.columnTitle}
  173. onChange={this.handleInputChange}
  174. />
  175. </label>
  176. <br />
  177. <label>
  178. {" "}
  179. Color:
  180. <input
  181. type="text"
  182. id="columnColor"
  183. value={this.state.columnColor}
  184. onChange={this.handleInputChange}
  185. />
  186. </label>
  187. <br />
  188. <button
  189. onClick={() =>
  190. this.props.addColumn({
  191. title: this.state.columnTitle,
  192. color: this.state.columnColor,
  193. })
  194. }
  195. >
  196. Add Column
  197. </button>
  198. <button
  199. onClick={() => {
  200. this.hideColumnModal();
  201. }}
  202. >
  203. Close
  204. </button>
  205. </Modal>
  206. <button onClick={this.showTaskModal}> Add task </button>
  207. <button onClick={this.showColumnModal}> Add column </button>
  208. </div>
  209. );
  210. }
  211. }
  212. export default Header;