Advertisement
Iv555

Untitled

Dec 17th, 2023
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.89 KB | None | 0 0
  1. import { useContext, useState } from "react";
  2. import { useLocation, Link } from "react-router-dom";
  3.  
  4. import { CourseContext } from "../../../contexts/Courses/CourseContext";
  5. import { AuthContext } from "../../../contexts/Users/AuthContext";
  6. import Paths from "../../../utils/Paths";
  7. import * as courseService from '../../../services/courseService'
  8. import toastrNotificationsService from "../../../services/toastrNotificationsService";
  9. import * as iconHelper from "../../../utils/getCourseIconByKey";
  10.  
  11. import "../../../assets/styles/services.css";
  12.  
  13. export const CourseDetails = ({ handleLinkClick, ...course }) => {
  14. const location = useLocation();
  15. const { updateCourseFunc } = useContext(CourseContext);
  16. const { auth } = useContext(AuthContext);
  17. const isAuthenticated = !!auth.username;
  18. const [canLike, setCanLike] = useState(true);
  19. const [canDisLike, setCanDisLike] = useState(true);
  20.  
  21. const likesHandler = () => {
  22. if (isAuthenticated) {
  23. if (!course.likes) {
  24. course.likes = [];
  25. }
  26. if (!course.dislikes) {
  27. course.dislikes = [];
  28. }
  29. let courseWithUpdatedLikes;
  30. if (course.likes.includes(auth._id) || course.dislikes.includes(auth._id)) {
  31. courseWithUpdatedLikes = {
  32. ...course
  33. }
  34. // setCanLike(false);
  35. toastrNotificationsService.showError('You have already voted for this course')
  36. }
  37. else {
  38. courseWithUpdatedLikes = {
  39. ...course,
  40. likes: [...course.likes, auth._id]
  41. }
  42. setCanLike(false);
  43. setCanDisLike(false);
  44. toastrNotificationsService.showSuccess('You have successfully voted for this course!')
  45. }
  46.  
  47.  
  48. const editedCourse = courseService.edit(course._id, courseWithUpdatedLikes);
  49. updateCourseFunc(editedCourse);
  50. }
  51. else {
  52. toastrNotificationsService.showError('Only registered users can vote. Please log in your account')
  53. }
  54. }
  55.  
  56. const dislikesHandler = () => {
  57. if (isAuthenticated) {
  58. if (!course.likes) {
  59. course.likes = [];
  60. }
  61. if (!course.dislikes) {
  62. course.dislikes = [];
  63. }
  64. let courseWithUpdatedDislikes;
  65.  
  66. if (course.likes.includes(auth._id) || course.dislikes.includes(auth._id)) {
  67. courseWithUpdatedDislikes = {
  68. ...course
  69. }
  70. toastrNotificationsService.showError('You have already voted for this course')
  71. }
  72. else {
  73. courseWithUpdatedDislikes = {
  74. ...course,
  75. dislikes: [...course.dislikes, auth._id]
  76. }
  77. setCanLike(false);
  78. setCanDisLike(false);
  79. toastrNotificationsService.showSuccess('You have successfully voted for this course!')
  80. }
  81.  
  82.  
  83. const editedCourse = courseService.edit(course._id, courseWithUpdatedDislikes);
  84. updateCourseFunc(editedCourse);
  85. } else {
  86. toastrNotificationsService.showError('Only registered users can vote. Please log in your account')
  87. }
  88. }
  89.  
  90. return (
  91. <div className="col-xl-4 col-md-6 service_col">
  92. <div className="service">
  93. <div className="service_title_container d-flex flex-row align-items-center justify-content-start">
  94. <div>
  95. <div className="service_icon">
  96. <img
  97. src={
  98. iconHelper.getIcon(course.name) !== undefined
  99. ? iconHelper.getIcon(course.name)
  100. : course.imageUrl
  101. }
  102. alt={course.name}
  103. />
  104. </div>
  105. </div>
  106. <div className="service_title">{course.name} Class</div>
  107. </div>
  108. {location.pathname === "/" ? (
  109. <div className="service_text_part">
  110. <p>{course.description}</p>
  111. </div>
  112. ) : (
  113. <>
  114. <div className="service_text">
  115. <p>{course.description}</p>
  116. </div>
  117. <div className="container">
  118. <div className="row justify-content-center">
  119. <button
  120. onClick={() => likesHandler()}
  121. className="like_dislike_btn"><i className="fa fa-3x fa-thumbs-up" aria-hidden="true"></i></button>
  122. <span className="likes_dislikes">{course.likes.length === 0 ? '' : course.likes.length}</span>
  123. <button
  124. onClick={() => dislikesHandler()}
  125. className="like_dislike_btn"><i className="fa fa-3x fa-thumbs-down" aria-hidden="true"></i></button>
  126. <span className="likes_dislikes">{course.dislikes.length === 0 ? '' : course.dislikes.length}</span>
  127. </div>
  128. </div>
  129. </>
  130. )}
  131. {location.pathname === "/" && (
  132. <div className="course_link">
  133. <Link to={Paths.Courses} onClick={handleLinkClick}>Read More</Link>
  134. </div>
  135. )}
  136. </div>
  137. </div>
  138. );
  139. };
  140.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement