PageRenderTime 29ms CodeModel.GetById 14ms app.highlight 11ms RepoModel.GetById 2ms app.codeStats 0ms

/src/utils/fetch.js

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