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.

235 lines
6.2KB

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