Advertisement
Guest User

Untitled

a guest
Apr 25th, 2019
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.51 KB | None | 0 0
  1. import React, { Component } from "react";
  2. import { AppConsumer } from "./app-context";
  3. import { Grid, Button, Input } from "semantic-ui-react";
  4.  
  5. const TodoInput = props => {
  6. let todo;
  7. return (
  8. <AppConsumer>
  9. {({ addTodo }) => (
  10. <form
  11. onSubmit={e => {
  12. e.preventDefault();
  13. }}
  14. >
  15. <Input
  16. type="text"
  17. id="todoInput"
  18. onChange={e => {
  19. todo = e.target.value;
  20. }}
  21. placeholder="type todo here"
  22. />
  23. <Button
  24. type="submit"
  25. onClick={() => {
  26. if (todo) {
  27. addTodo(todo);
  28. const inputEl = document.getElementById("todoInput");
  29. inputEl.value = "";
  30. }
  31. }}
  32. >
  33. Add Todo
  34. </Button>
  35. </form>
  36. )}
  37. </AppConsumer>
  38. );
  39. };
  40.  
  41. const DisplayTodos = props => {
  42. return (
  43. <AppConsumer>
  44. {({ todos, completed }) => {
  45. const notCompleted = todos.filter(todo => {
  46. if (todo.completed === false) {
  47. return todo;
  48. }
  49. });
  50.  
  51. return (
  52. <div>
  53. <p>All your todos: {notCompleted.length}</p>
  54. <ol>
  55. {todos.length > 0
  56. ? todos.map(todo => (
  57. <li
  58. className={
  59. todo.completed
  60. ? "completed todo align-left"
  61. : "todo align-left"
  62. }
  63. key={todo.id}
  64. onClick={() => completed(todo.id)}
  65. >
  66. {todo.todo}
  67. {" "}
  68. <span className={todo.completed ? "date" : "date"}>
  69. {new Date(todo.createdAt).toString()}
  70. </span>
  71. </li>
  72. ))
  73. : ""}
  74. </ol>
  75. </div>
  76. );
  77. }}
  78. </AppConsumer>
  79. );
  80. };
  81.  
  82. class TodoApp extends Component {
  83. render() {
  84. return (
  85. <div className="todos">
  86. <Grid columns={1} className="ui center aligned">
  87. <Grid.Row>
  88. <Grid.Column className="todo-body" width={12}>
  89. <h2>Simple Todo</h2>
  90. <TodoInput />
  91. <div id="display-todos">
  92. <DisplayTodos className="align-left" />
  93. </div>
  94. </Grid.Column>
  95. </Grid.Row>
  96. </Grid>
  97. </div>
  98. );
  99. }
  100. }
  101.  
  102. export default TodoApp;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement