/src/utils/fetch.js

https://bitbucket.org/LorenK/sdconnect · JavaScript · 146 lines · 126 code · 16 blank · 4 comment · 5 complexity · c7c74902f591c00ac4007d115428495b MD5 · raw file

  1. import React from 'react'
  2. import { Card, ScheduleCard } from '../components/card'
  3. import TimeFormatter from './time'
  4. import { Firebase } from './firebase'
  5. import { contextualFirebase } from '../utils/store'
  6. export default class Fetch {
  7. static getCards(cardClick) {
  8. return Fetch.getFormattedPosts().then(data => Fetch.getCardsFromData(data, cardClick))
  9. }
  10. static getCardsFromData(data, cardClick) {
  11. const postCards = []
  12. const scheduleCards = []
  13. for (var p = 0; p < data.postCards.length; p++) {
  14. const postCard = data.postCards[p]
  15. postCards.push(
  16. <Card
  17. key={p}
  18. onCardClick={() => cardClick(postCard.classId, postCard.postId)}
  19. classId={postCard.classId}
  20. postId={postCard.postId}
  21. profileImage={postCard.profileImage}
  22. username={postCard.username}
  23. className={postCard.className}
  24. dateTime={postCard.dateTime}
  25. body={postCard.body}
  26. didLike={postCard.didLike}
  27. likeCount={postCard.likeCount}
  28. comments={postCard.comments}
  29. email={postCard.email} />
  30. )
  31. }
  32. for (var s = 0; s < data.scheduleCards.length; s++) {
  33. const scheduleCard = data.scheduleCards[s];
  34. scheduleCards.push(
  35. <ScheduleCard
  36. key={s}
  37. classId={scheduleCard.classId}
  38. postId={scheduleCard.postId}
  39. startTime={scheduleCard.startTime}
  40. endTime={scheduleCard.endTime}
  41. room={scheduleCard.room}
  42. profileImage={scheduleCard.profileImage}
  43. username={scheduleCard.username}
  44. className={scheduleCard.className}
  45. dateTime={scheduleCard.dateTime}
  46. body={scheduleCard.body} />
  47. )
  48. }
  49. return { postCards, scheduleCards }
  50. }
  51. static formattedPost(post, classObj, teacherData) {
  52. const me = contextualFirebase.getState().me
  53. const name = `${teacherData.meta.firstName} ${teacherData.meta.lastName}`
  54. post.comments.sort((x, y) => {
  55. return y.dateTime - x.dateTime
  56. })
  57. for (let c of post.comments) {
  58. c.isMine = c.user.id === me.uid
  59. }
  60. return {
  61. classId: classObj.id,
  62. isMine: teacherData.id === me.uid,
  63. postId: post.id,
  64. comments: post.comments,
  65. didLike: post.didLike,
  66. likeCount: post.likeCount,
  67. profileImage: teacherData.profileImage,
  68. username: name,
  69. className: classObj.name,
  70. timestamp: post.dateTime,
  71. dateTime: TimeFormatter.getFormattedDate(post.dateTime),
  72. body: post.body,
  73. email: teacherData.meta.email
  74. }
  75. }
  76. static getFormattedPost() {
  77. return contextualFirebase.getState().post().then(post => {
  78. return Firebase.getClass(post.classRef).then(classObj => {
  79. return Firebase.getUserData(classObj.teacher).then(teacherData => {
  80. teacherData.id = classObj.teacher.id
  81. return Promise.resolve(Fetch.formattedPost(post, classObj, teacherData))
  82. })
  83. })
  84. })
  85. }
  86. static dateSort = (x, y) => {
  87. return y.timestamp - x.timestamp
  88. }
  89. static getFormattedPosts() {
  90. // Get classes and posts
  91. return contextualFirebase.getState().classes()
  92. .then(classRefs => {
  93. let postCards = []
  94. const scheduleCards = []
  95. for (var i = 0; i < classRefs.length; i++) {
  96. const classRef = classRefs[i]
  97. return Firebase.getClass(classRef).then(classObj => {
  98. return Firebase.getUserData(classObj.teacher).then(teacherData => {
  99. teacherData.id = classObj.teacher.id
  100. return contextualFirebase.getState().posts(classRef).then(posts => {
  101. // Iterate through each post in class
  102. for (var p = 0; p < posts.length; p++) {
  103. const post = posts[p]
  104. postCards.push(Fetch.formattedPost(post, classObj, teacherData))
  105. }
  106. // Sort postCards
  107. postCards.sort(Fetch.dateSort)
  108. // Push schedule card
  109. const firstPost = postCards[0]
  110. const name = `${teacherData.meta.firstName} ${teacherData.meta.lastName}`
  111. scheduleCards.push({
  112. classId: classObj.id,
  113. postId: firstPost.id,
  114. startTime: TimeFormatter.getTime(classObj.startTime),
  115. endTime: TimeFormatter.getTime(classObj.endTime),
  116. room: classObj.room,
  117. profileImage: teacherData.profileImage,
  118. username: name,
  119. className: classObj.name,
  120. dateTime: firstPost.dateTime,
  121. body: firstPost.body
  122. })
  123. return Promise.resolve({postCards, scheduleCards})
  124. })
  125. })
  126. })
  127. }
  128. })
  129. }
  130. }