Skip to content

Commit

Permalink
(Task #1): Finish first time teacher setup flow
Browse files Browse the repository at this point in the history
  • Loading branch information
MirTalpur committed Apr 8, 2020
1 parent fb0c58e commit 7706d75
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 60 deletions.
4 changes: 2 additions & 2 deletions pages/dashboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
9 changes: 6 additions & 3 deletions pages/sign_up.js
Original file line number Diff line number Diff line change
@@ -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';


Expand Down Expand Up @@ -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({
Expand Down
178 changes: 124 additions & 54 deletions pages/teacher_setup.js
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -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,
Expand Down Expand Up @@ -163,12 +173,12 @@ class TeacherInterview extends React.Component {
</form>
<button
content='Back'
onClick={() => this.handleBackClick}>
onClick={() => this.handleBackClick(event)}>
Back
</button>
<button
content='Next'
onClick={() => this.handleYesClick}>
onClick={() => this.handleYesClick(event)}>
Next
</button>
</div>
Expand Down Expand Up @@ -209,6 +219,7 @@ class TeacherInterview extends React.Component {
data-id={idx}
value={this.state.teacherStudent[idx].email}
className="email"
onChange={this.state.handleStudentChange}
/>
<label htmlFor={passwordId}>Password</label>
<input
Expand All @@ -233,12 +244,12 @@ class TeacherInterview extends React.Component {
</button>
<button
content='Back'
onClick={() => this.handleBackClick}>
onClick={() => this.handleBackClick(event)}>
Back
</button>
<button
content='Next'
onClick={() => this.handleYesClick}>
onClick={() => this.handleYesClick(event)}>
Next
</button>
</form>
Expand Down Expand Up @@ -278,12 +289,12 @@ class TeacherInterview extends React.Component {
</form>
<button
content='Back'
onClick={() => this.handleBackClick}>
onClick={() => this.handleBackClick(event)}>
Back
</button>
<button
content='Next'
onClick={() => this.handleYesClick}>
onClick={() => this.handleYesClick(event)}>
Next
</button>
</div>
Expand Down Expand Up @@ -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);
});
}

Expand Down Expand Up @@ -358,7 +412,7 @@ class TeacherInterview extends React.Component {
e.preventDefault();
this.setState((prevState) => ({
teacherStudent: [...prevState.teacherStudent, {email: "", password: ""}]
}))
}));
}

handleTeacherName = (e) => {
Expand Down Expand Up @@ -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) => {
Expand All @@ -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 {
Expand Down
5 changes: 4 additions & 1 deletion src/firebase/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
};

0 comments on commit 7706d75

Please sign in to comment.