From 5f87ea8afd937ffbdb4818def9356e347fd72779 Mon Sep 17 00:00:00 2001 From: Tanner Collin Date: Sun, 27 Jan 2019 00:35:28 -0700 Subject: [PATCH] Ask user which courses they have taken --- webclient/src/App.js | 94 +++++++++++++++++++++++++++++-------- webclient/src/Categories.js | 34 +++++++------- webclient/src/Tool.js | 2 +- 3 files changed, 92 insertions(+), 38 deletions(-) diff --git a/webclient/src/App.js b/webclient/src/App.js index 26431d3..bc59b64 100644 --- a/webclient/src/App.js +++ b/webclient/src/App.js @@ -32,6 +32,7 @@ class App extends Component { toolStatus: null, connected: false, network: true, + selectedCourses: null, }; } @@ -60,7 +61,10 @@ class App extends Component { fetch(AUTH_SERVER_URL + '/tooldata/') .then(response => { if (response.ok) { - response.json().then(data => this.setState({ toolData: data })); + response.json().then(data => this.setState({ + toolData: data, + selectedCourses: data.courses.map(() => false), + })); } else { this.noNetwork(); } @@ -154,6 +158,34 @@ class App extends Component { window.location.reload(); } + toggleCourse = (i, data) => { + let selectedCourses = this.state.selectedCourses; + selectedCourses[i] = data.checked; + this.setState({ selectedCourses: selectedCourses }); + } + + submitCourses = () => { + const toolData = this.state.toolData; + const selectedCourses = this.state.selectedCourses; + + fetch(AUTH_SERVER_URL + '/select-courses/', { + method: 'PUT', + headers: {'Authorization': 'Token ' + this.state.login.token, 'Content-Type': 'application/json; charset=utf-8'}, + body: JSON.stringify({'courses': toolData.courses.map(x => x.slug).filter((x, i) => selectedCourses[i]) }) + }) + .then(response => { + if (response.ok) { + this.getUser(); + } else { + this.noNetwork(); + } + }) + .catch(error => { + console.log(error) + this.handleLogout(); + }); + } + render() { const login = this.state.login; const user = this.state.user; @@ -161,6 +193,9 @@ class App extends Component { const toolStatus = this.state.toolStatus; const connected = this.state.connected; const network = this.state.network; + const selectedCourses = this.state.selectedCourses; + + console.log(this.state); return ( network ? @@ -189,24 +224,45 @@ class App extends Component { {login.token ?
{toolData && user ? -
- - - } /> - - - - } /> - - - - } /> -
+ + {user.profile.selected_courses ? +
+ + + } /> + + + + } /> + + + + } /> +
+ : +
+ +
+ {toolData.courses.map((x, i) => + this.toggleCourse(i, data)} + label={x.name} + key={i} + /> + )} +
+ + +
Note: your selection will be reviewed by a lockout admin +
+ } +
: Loading diff --git a/webclient/src/Categories.js b/webclient/src/Categories.js index 1af1d88..473e7f8 100644 --- a/webclient/src/Categories.js +++ b/webclient/src/Categories.js @@ -1,5 +1,5 @@ import React, { Component } from 'react'; -import { Breadcrumb, Container, Dropdown, Header, Icon, Item, Menu, Segment, Input } from 'semantic-ui-react' +import { Breadcrumb, Dropdown, Header, Icon, Item, Menu, Segment, Input } from 'semantic-ui-react' import { Link } from 'react-router-dom'; class Categories extends Component { @@ -9,24 +9,22 @@ class Categories extends Component { return (
- - - Categories - + + Categories + - - {data.categories.map((x, n) => - - - - {x.name} - {x.info} - {x.tools.length} tools - - - )} - - + + {data.categories.map((x, n) => + + + + {x.name} + {x.info} + {x.tools.length} tools + + + )} +
); } diff --git a/webclient/src/Tool.js b/webclient/src/Tool.js index 75b58c5..aaca7fe 100644 --- a/webclient/src/Tool.js +++ b/webclient/src/Tool.js @@ -15,7 +15,7 @@ class Tool extends Component { } else if (status.state == 'armed') { return { msg: 'Armed.', canArm: false, canDisarm: true, }; } else if (status.state == 'on') { - return { msg: 'On.', canArm: false, canDisarm: true, }; + return { msg: 'On.', canArm: false, canDisarm: false, }; } else { return { msg: 'Error: Impossible state!', canArm: false, canDisarm: false, }; }