From 7706d7543c0582243689ef2f8727a7320f27f116 Mon Sep 17 00:00:00 2001 From: Mir Ali Talpur Date: Wed, 8 Apr 2020 13:21:52 -0700 Subject: [PATCH] (Task #1): Finish first time teacher setup flow --- pages/dashboard.js | 4 +- pages/sign_up.js | 9 ++- pages/teacher_setup.js | 178 ++++++++++++++++++++++++++++------------- src/firebase/index.js | 5 +- 4 files changed, 136 insertions(+), 60 deletions(-) diff --git a/pages/dashboard.js b/pages/dashboard.js index fc223c5..11319dc 100644 --- a/pages/dashboard.js +++ b/pages/dashboard.js @@ -71,8 +71,8 @@ class Dashboard extends React.Component { const renderConditionalDashboard = () => { console.log("be here now") if(this.state.newTeacherUserFlow === true) { - console.log("i'm here man") - Router.push('/teacher_setup') + console.log("i'm here man"); + Router.push('/teacher_setup'); } else { return null } diff --git a/pages/sign_up.js b/pages/sign_up.js index e9bc301..e28d409 100644 --- a/pages/sign_up.js +++ b/pages/sign_up.js @@ -1,7 +1,7 @@ import React from 'react'; import Link from 'next/link'; import Nav from '../components/nav'; -import { auth, firebase, db } from '../src/firebase'; +import { auth, firebase, db, functions } from '../src/firebase'; import Router from 'next/router'; @@ -244,8 +244,11 @@ class SignUp extends React.Component { "isNewUser": firebaseUser.additionalUserInfo.isNewUser, "userType": "teacher" } - db.collection("users").doc(firebaseUser.user.uid).set(userDataCollection).then(() =>{ - Router.push('/dashboard'); + db.collection("users").doc(firebaseUser.user.uid).set(userDataCollection).then(() => { + const addAdminRole = functions.httpsCallable('addAdminRole'); + addAdminRole({ email: firebaseUser.user.email }).then(results => { + Router.push('/dashboard'); + }); }).catch((error) =>{ console.log(error); this.setState({ diff --git a/pages/teacher_setup.js b/pages/teacher_setup.js index 70d6487..f8c3d5f 100644 --- a/pages/teacher_setup.js +++ b/pages/teacher_setup.js @@ -1,8 +1,8 @@ import React from 'react'; import withAuth from '../src/helpers/withAuth'; import ErrorPage from 'next/error' -import { db, storage } from "../src/firebase"; - +import { db, storage, functions } from "../src/firebase"; +import Router from 'next/router'; class TeacherInterview extends React.Component { constructor(props) { @@ -27,22 +27,32 @@ class TeacherInterview extends React.Component { teacherStudent: [{ email: "", password: "", + validationPasswordError: { + message: "", + }, + validationEmailError: { + message: "", + }, + serverSideError: { + message: "", + } }], homeSchoolName: "", teacherProfilePicFile: '', teacherProfilePicBlob: '', - teacherName: '', + teacherName: null, + teacherProfileFile: null, teacherDataCollection: { - "uid": "", - "displayName": "", - "photoUrl": "", - "email": "", - "emailVerified": "", - "isNewUser": "", - "userType": "teacher", - "teacherStudents": [], - "teacherName": "", - "homeschoolName": "", + uid: "", + displayName: "", + photoUrl: "", + email: "", + emailVerified: "", + isNewUser: "", + userType: "teacher", + teacherStudents: [], + teacherName: "", + homeschoolName: "", }, validationTeacherName: { message: null, @@ -163,12 +173,12 @@ class TeacherInterview extends React.Component { @@ -209,6 +219,7 @@ class TeacherInterview extends React.Component { data-id={idx} value={this.state.teacherStudent[idx].email} className="email" + onChange={this.state.handleStudentChange} /> @@ -278,12 +289,12 @@ class TeacherInterview extends React.Component { @@ -311,25 +322,68 @@ class TeacherInterview extends React.Component { () => { // complete function ... storage - .ref("images") - .child(image.name) - .getDownloadURL() - .then(url => { - this.setState({ - teacherDataCollection: { - "photoUrl": url, - } - }); + .ref("images") + .child(image.name) + .getDownloadURL() + .then(url => { + this.setState({ + teacherProfileFile: url }); + // this.setState((prevState) => ({ + // teacherStudent: [...prevState.teacherDataCollection.photoUrl: url] + // })); + }); } ); } handleFirstTimeSetup(){ - this.setState({ - "uid": this.state.authUser.uid, - "email": this.state.authUser.email, - "isNewUser": false, + const addTeacherDocuments = functions.httpsCallable('addTeacherDocuments'); + // uid: data.uid, + // displayName: data.displayName, + // photoUrl: data.photoURL, + // email: data.email, + // emailVerified: data.emailVerified, + // isNewUser: data.isNewUser, + // teacherStudents: data.teacherStudents, + // teacherName: data.teacherName, + // homeschoolName: data.homeschoolName, + + // uid: data.uid, + // displayName: data.displayName, + // photoUrl: data.photoURL, + // email: data.email, + // emailVerified: data.emailVerified, + // isNewUser: data.isNewUser, + // userType: "teacher", + // teacherStudents: data.teacherStudents, + // teacherName: data.teacherName, + // homeschoolName: data.homeschoolName, + + console.log("uid" + this.state.authUser.uid) + console.log("displayName" + this.state.teacherName) + console.log("photoUrl" + this.state.teacherProfileFile) + console.log("email" + this.state.authUser.email) + console.log("emailVerified" + this.state.authUser.emailVerified) + console.log("teacherStudents" + this.state.teacherDataCollection.teacherStudents) + console.log("teacherName" + this.state.teacherName) + console.log("homeSchoolName" + this.state.homeSchoolName) + + addTeacherDocuments({ + uid: this.state.authUser.uid, + displayName: this.state.teacherName, + photoUrl: this.state.teacherProfileFile, + email: this.state.authUser.email, + emailVerified: this.state.authUser.emailVerified, + isNewUser: false, + teacherStudents: this.state.teacherDataCollection.teacherStudents, + teacherName: this.state.teacherName, + homeSchoolName: this.state.homeSchoolName + }).then(results => { + console.log(results); + Router.push('/dashboard'); + }).catch(err => { + console.log(err); }); } @@ -358,7 +412,7 @@ class TeacherInterview extends React.Component { e.preventDefault(); this.setState((prevState) => ({ teacherStudent: [...prevState.teacherStudent, {email: "", password: ""}] - })) + })); } handleTeacherName = (e) => { @@ -420,27 +474,6 @@ class TeacherInterview extends React.Component { teacherProfilePicFile: image, })); } - // let blob = new Blob([event.target.result], { type: "image/jpg" }); - // this.setState({ - // teacherProfilePicBlob: blob, - // }); - - // if (event.target.files && event.target.files[0]) { - // let reader = new FileReader(); - // reader.onload = (e) => { - // this.setState({ - // teacherProfilePicFile: e.target.result, - // }); - // } - // } - } - - setRef = (ref) => { - console.log("in setREf"); - this.setState({ - teacherProfilePicFile: ref, - }); - console.log(this.state.teacherProfilePicFile); } handleImageUpload = (e) => { @@ -451,11 +484,48 @@ class TeacherInterview extends React.Component { }); } + handleStudentSignUp = (email, password, uid) => { + const addUserAsAdmin = functions.httpsCallable('addUserAsAdmin'); + addUserAsAdmin({ email: email, password: password, uid: uid }).then(results => { + console.log("results from api:" + JSON.stringify(results.data.user.uid)); + const teacherDataCollection = this.state.teacherDataCollection; + teacherDataCollection.teacherStudents.push(results.data.user.uid); + this.setState({ + teacherDataCollection + }); + return true + }).catch((error)=> { + console.log(error); + return false; + }); + } + + handleUserStudentsSignUp = () => { + let context = this; + const uid = this.state.authUser.uid; + this.state.teacherStudent.forEach(function (student, index) { + console.log(student.email); + console.log(student.password); + if(context.handleStudentSignUp(student.email, student.password, uid) == false) { + // let students = [this.] + this.setState(prevState => ({ + ...prevState.teacherStudent, + [prevState.teacherStudent[index].serverSideError.message]: "Invalid Password or Email", + })); + } + console.log('teacherDataCollection:' + context.teacherDataCollection); + }); + } + handleYesClick(event) { event.preventDefault(); if(this.state.questionState == 2){ this.uploadProfileFile(); } + if(this.state.questionState == 6){ + console.log("Made it to the questionState number #6"); + this.handleUserStudentsSignUp(); + } if(this.state.questionState == 8){ this.handleSubmit(); } else { diff --git a/src/firebase/index.js b/src/firebase/index.js index 23e7c0e..f6522b0 100644 --- a/src/firebase/index.js +++ b/src/firebase/index.js @@ -3,6 +3,7 @@ import firebase from 'firebase/app'; import 'firebase/auth'; import 'firebase/firestore'; import 'firebase/storage'; +import 'firebase/functions'; const config = { apiKey: process.env.FIREBASE_API_KEY, @@ -20,10 +21,12 @@ if (!firebase.apps.length) { const auth = firebase.auth(); const db = firebase.firestore(); const storage = firebase.storage(); +const functions = firebase.functions(); export { auth, firebase, db, - storage + storage, + functions, };