Advertisement
Guest User

Untitled

a guest
Feb 18th, 2020
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.15 KB | None | 0 0
  1. import React, { useState, useEffect } from 'react'
  2. import blogService from '../services/blogs'
  3. import userService from '../services/users'
  4. import Togglable from './Togglable'
  5. import BlogForm from './BlogForm'
  6. import Notification from './Notification'
  7.  
  8. const BlogDisplay = (props) => {
  9. const [blogs, setBlogs] = useState([])
  10. const [notification, setNotification] = useState(null)
  11. const [errorMessage, setErrorMessage] = useState(null)
  12. const blogFormRef = React.createRef()
  13.  
  14. useEffect(() => {
  15. blogService.getAll().then(blogs =>
  16. setBlogs(blogs)
  17. )
  18. }, [])
  19.  
  20.  
  21. const blogStyle = {
  22. paddingTop: 10,
  23. paddingLeft: 2,
  24. border: 'solid',
  25. borderWidth: 1,
  26. marginBottom: 5,
  27. }
  28. const showBlogs = () => {
  29.  
  30. return (
  31. blogs.sort((b1, b2) => b2.likes - b1.likes).map(b =>
  32. contentVisibility(b)
  33. )
  34. )
  35.  
  36. }
  37. const addLike = async (blog) => {
  38. let returnedBlog = await blogService.update({
  39. user: blog.user.id,
  40. title: blog.title,
  41. author: blog.author,
  42. url: blog.url,
  43. likes: blog.likes + 1,
  44. visible: blog.visible,
  45. }, blog.id)
  46. setBlogs(blogs.map(b => b.id !== blog.id ? b : returnedBlog))
  47. }
  48.  
  49. const contentVisibility = (blog) => {
  50. console.log('LISÄÄJÄ' + blog.user.username)
  51.  
  52. if (blog.visible === false) {
  53. return (
  54. <div key={blog.id} style={blogStyle}>
  55. <div>{blog.title} {blog.author} <button onClick={() =>
  56. handleToggleVisibility(blog)}>view</button>
  57. </div>
  58. </div>
  59. )
  60. } else {
  61. return (
  62. <div key={blog.id} style={blogStyle}>
  63. <div>{blog.title} {blog.author} <button onClick={() =>
  64. handleToggleVisibility(blog)}>hide</button></div>
  65. <div>{blog.url} </div>
  66. <div>likes {blog.likes} <button onClick={() => addLike(blog)}>like</button> </div>
  67. <div>added by {blog.user.username} </div>
  68. <div>{props.user.username === blog.user.username && <button onClick={() =>
  69. deleteBlog(blog)}>delete</button>} <br /></div>
  70. </div>
  71. )
  72. }
  73.  
  74. }
  75. const handleToggleVisibility = async (blog) => {
  76. console.log(blog)
  77. let returnedBlog = await blogService.update({
  78. user: blog.user.id,
  79. title: blog.title,
  80. author: blog.author,
  81. url: blog.url,
  82. likes: blog.likes,
  83. visible: !blog.visible,
  84. }, blog.id)
  85. console.log('TÄMÄ' + returnedBlog.user)
  86. setBlogs(blogs.map(b => b.id !== blog.id ? b : returnedBlog))
  87.  
  88. }
  89. const addBlog = async (blogObject) => {
  90. let users = await userService.getAll()
  91. let user = users.filter(u => u.username === props.user.username)
  92. console.log(user)
  93. blogFormRef.current.toggleVisibility()
  94. try {
  95. let b = await blogService.create({
  96. title: blogObject.title,
  97. author: blogObject.author,
  98. url: blogObject.url,
  99. visible: false,
  100. })
  101. setBlogs(blogs.concat(b))
  102. setNotification(`a new blog ${b.title} by ${b.author} was added`)
  103. setTimeout(() => {
  104. setNotification(null)
  105. }, 5000)
  106. } catch (exception) {
  107. console.log(blogObject)
  108. setErrorMessage('something went wrong')
  109. setTimeout(() => {
  110. setErrorMessage(null)
  111. }, 5000)
  112. }
  113. }
  114. const deleteBlog = async (blogObject) => {
  115. let result = window.confirm("Are you sure you want to delete this blog?");
  116. if (result) {
  117. try {
  118. let b = await blogService.remove(blogObject.id)
  119. setBlogs(blogs.filter(b => b.id !== blogObject.id))
  120. setErrorMessage(`a new blog ${b.title} by ${b.author} was deleted`)
  121. setTimeout(() => {
  122. setErrorMessage(null)
  123. }, 5000)
  124. } catch (exception) {
  125. console.log(blogObject)
  126. setErrorMessage('something went wrong')
  127. setTimeout(() => {
  128. setErrorMessage(null)
  129. }, 5000)
  130. }
  131. }
  132.  
  133. }
  134. const showBlogForm = () => {
  135. return (
  136. <div>
  137. <h2>Add new</h2>
  138. <Togglable buttonLabel="new blog" ref={blogFormRef}>
  139. {props.user !== null && <BlogForm createNewBlog={addBlog} />}
  140. </Togglable>
  141. </div>
  142. )
  143. }
  144. return (
  145. <>
  146. <Notification message={errorMessage} type="warning" />
  147. <Notification message={notification} type="success" />
  148. {props.user !== null && showBlogForm()}
  149. {props.user !== null && showBlogs()}
  150. </>
  151.  
  152. )
  153.  
  154.  
  155. }
  156. export default BlogDisplay
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement