-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDashboard.vue
154 lines (141 loc) · 3.84 KB
/
Dashboard.vue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
<template>
<div>
<div class="custom-offset"></div>
<h2>Dashboard</h2>
<p v-if="loading" class="white-text center">Fetching your classes...</p>
<template v-if="betaSubjects.length && !loading && hasFetchedUser">
<div class="responsive-grid">
<template v-for="(subject, i) in betaSubjects">
<div class="subject-card" :key="i">
<base-card>
<h4 class="black-text">{{ subject.id }}</h4>
<p class="black-text">{{ subject.enrolledUsers.length }} enrolled </p>
<!-- number of people online -->
<p class="black-text">{{ numberOfOnlineClassmates(subject )}} online</p>
<floating-button
color="pink"
iconName="slideshow"
@click="$router.push(subject.id)"/>
<template v-if="user.displayName == 'Elton Lin'">
<floating-button
iconName="delete"
color="black"
@click="deleteSubject(subject)"/>
</template>
</base-card>
</div>
</template>
</div>
</template>
<manage-classes/>
</div>
</template>
<script>
import db from '@/firebase/init.js'
import PulseButton from '@/components/reusables/PulseButton.vue'
import FloatingButton from '@/components/reusables/FloatingButton.vue'
import PopupModal from '@/components/reusables/PopupModal.vue'
import ManageClasses from '@/components/dashboard/ManageClasses.vue'
import firebase from 'firebase/app'
import 'firebase/firestore'
import { mapState } from 'vuex'
export default {
components: {
PulseButton,
FloatingButton,
PopupModal,
ManageClasses
},
computed: {
...mapState({
user: state => state.user.user,
hasFetchedUser: state => state.user.hasFetchedUser
}),
setOfOnlineUIDs() {
let arrayOfUIDs = []
this.onlineUsers.forEach(user => arrayOfUIDs.push(user.id))
return new Set(arrayOfUIDs)
}
},
data() {
return {
subjects: [],
loading: true,
feedback: '',
betaSubjects: [],
onlineUsers: []
}
},
watch: {
// fetch subject cards when user initially enters / enrolls a subject / delete a subject
user: {
handler: 'fetchSubjects'
}
},
created() {
const onlineUsersRef = db.collection('users').where('isOnline', '==', true)
this.$bind('onlineUsers', onlineUsersRef)
if (this.hasFetchedUser) {
this.fetchSubjects()
}
},
methods: {
async fetchSubjects() {
if (!this.hasFetchedUser) {
return
}
const enrolledSubjects = db
.collection('subjects')
.where('enrolledUsers', 'array-contains', this.user.uid)
this.$bind('betaSubjects', enrolledSubjects)
this.loading = false
},
async deleteSubject({ id }) {
const ref = db.collection('users').doc(this.user.uid)
// remove user's reference
await ref.update({
enrolledSubjects: firebase.firestore.FieldValue.arrayRemove(id)
})
// remove subject's reference
const subjectRef = db.collection('subjects').doc(id)
await subjectRef.update({
enrolledUsers: firebase.firestore.FieldValue.arrayRemove(this.user.uid)
})
},
numberOfOnlineClassmates({ enrolledUsers }) {
const enrolledUIDs = enrolledUsers
const onlineUIDs = this.setOfOnlineUIDs
const enrolledAndOnline = new Set(
[...enrolledUIDs].filter(UID => onlineUIDs.has(UID))
)
return enrolledAndOnline.size
}
}
}
</script>
<style lang="scss" scoped>
.responsive-grid {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(250px, 0.97fr));
grid-gap: 30px;
max-width: 90%;
margin: 0 auto 30px;
}
h2 {
@extend .center;
padding-bottom: 50px;
}
h4 {
@extend .black-text;
}
.custom-offset {
padding-top: 30px;
}
.new-subject {
margin: auto;
width: 65%;
}
.spinner-wrapper {
margin-left: 49%;
}
</style>