-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathlesson-progress.js
More file actions
148 lines (138 loc) · 5.09 KB
/
Copy pathlesson-progress.js
File metadata and controls
148 lines (138 loc) · 5.09 KB
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
(function () {
const config = {
apiKey: 'AIzaSyDoXSwni65CuY1_32ZE8B1nwfQO_3VNpTw',
authDomain: 'contract-center-llc-10.firebaseapp.com',
projectId: 'contract-center-llc-10',
storageBucket: 'contract-center-llc-10.firebasestorage.app',
messagingSenderId: '323221512767',
appId: '1:323221512767:web:6421260f875997dbf64e8a'
};
const local = location.protocol === 'file:' || /^(localhost|127\.0\.0\.1|::1)$/.test(location.hostname);
let firebasePromise;
let initialAuthStatePromise;
function loadScript(src) {
return new Promise((resolve, reject) => {
const script = document.createElement('script');
script.src = src;
script.onload = resolve;
script.onerror = () => reject(new Error(`Unable to load ${src}`));
document.head.appendChild(script);
});
}
async function firebaseServices() {
if (local) return null;
if (!firebasePromise) {
firebasePromise = (async () => {
if (!window.firebase) await loadScript('https://www.gstatic.com/firebasejs/9.22.0/firebase-app-compat.js');
if (!firebase.auth) await loadScript('https://www.gstatic.com/firebasejs/9.22.0/firebase-auth-compat.js');
if (!firebase.firestore) await loadScript('https://www.gstatic.com/firebasejs/9.22.0/firebase-firestore-compat.js');
const app = firebase.apps.length ? firebase.app() : firebase.initializeApp(config);
return { auth: app.auth(), db: app.firestore() };
})().catch(error => {
firebasePromise = null;
throw error;
});
}
return firebasePromise;
}
function currentUser(auth) {
if (auth.currentUser) return Promise.resolve(auth.currentUser);
if (!initialAuthStatePromise) {
initialAuthStatePromise = new Promise(resolve => {
auth.onAuthStateChanged(resolve, () => resolve(null));
});
}
return initialAuthStatePromise.then(() => auth.currentUser);
}
function localKey(lessonId) {
return `houselearning:lesson-progress:${lessonId}`;
}
function readLocal(lessonId) {
try {
return JSON.parse(localStorage.getItem(localKey(lessonId)) || '{}');
} catch (error) {
return {};
}
}
function writeLocal(lessonId, values) {
const existing = readLocal(lessonId);
const updated = { ...existing, ...values };
localStorage.setItem(localKey(lessonId), JSON.stringify(updated));
return { saved: true, local: true };
}
async function getLessonProgress(lessonId) {
if (local) return readLocal(lessonId);
const { auth, db } = await firebaseServices();
const user = await currentUser(auth);
if (!user) return null;
const snapshot = await db.collection('UserLessonProgress')
.doc(user.uid)
.collection('lessons')
.doc(lessonId)
.get();
return snapshot.exists ? snapshot.data().lessonTools || {} : {};
}
async function saveLessonProgress(lessonId, values) {
if (local) return writeLocal(lessonId, values);
const { auth, db } = await firebaseServices();
const user = await currentUser(auth);
if (!user) return { saved: false, reason: 'signed-out' };
const ref = db.collection('UserLessonProgress')
.doc(user.uid)
.collection('lessons')
.doc(lessonId);
await ref.set({
lessonTools: values,
lessonToolsUpdatedAt: firebase.firestore.FieldValue.serverTimestamp()
}, { merge: true });
return { saved: true, local: false };
}
async function saveAttempt(lessonId, attempt) {
if (local) {
const state = readLocal(lessonId);
const attempts = Array.isArray(state.attempts) ? state.attempts : [];
attempts.push({ ...attempt, completedAt: new Date().toISOString() });
return writeLocal(lessonId, { attempts: attempts.slice(-20) });
}
const { auth, db } = await firebaseServices();
const user = await currentUser(auth);
if (!user) return { saved: false, reason: 'signed-out' };
const lessonRef = db.collection('UserLessonProgress')
.doc(user.uid)
.collection('lessons')
.doc(lessonId);
const attemptRef = lessonRef.collection('attempts').doc();
const summaryField = attempt.type === 'unit-test' ? 'unitTest' : 'quiz';
const batch = db.batch();
batch.set(attemptRef, {
type: attempt.type,
subject: attempt.subject,
grade: attempt.grade,
lessonId,
lessonTitle: attempt.lessonTitle,
score: attempt.score,
total: attempt.total,
passed: attempt.passed,
completedAt: firebase.firestore.FieldValue.serverTimestamp()
});
batch.set(lessonRef, {
lessonTools: {
[summaryField]: {
lastScore: attempt.score,
lastTotal: attempt.total,
lastPassed: attempt.passed,
lastCompletedAt: firebase.firestore.FieldValue.serverTimestamp(),
attemptCount: firebase.firestore.FieldValue.increment(1)
}
},
lessonToolsUpdatedAt: firebase.firestore.FieldValue.serverTimestamp()
}, { merge: true });
await batch.commit();
return { saved: true, local: false };
}
window.HouseLearningLessonProgress = {
getLessonProgress,
saveLessonProgress,
saveAttempt
};
})();