Ask user which courses they have taken

This commit is contained in:
Tanner Collin 2019-01-27 00:35:28 -07:00
parent 373d3efaff
commit 5f87ea8afd
3 changed files with 90 additions and 36 deletions

View File

@ -32,6 +32,7 @@ class App extends Component {
toolStatus: null, toolStatus: null,
connected: false, connected: false,
network: true, network: true,
selectedCourses: null,
}; };
} }
@ -60,7 +61,10 @@ class App extends Component {
fetch(AUTH_SERVER_URL + '/tooldata/') fetch(AUTH_SERVER_URL + '/tooldata/')
.then(response => { .then(response => {
if (response.ok) { 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 { } else {
this.noNetwork(); this.noNetwork();
} }
@ -154,6 +158,34 @@ class App extends Component {
window.location.reload(); 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() { render() {
const login = this.state.login; const login = this.state.login;
const user = this.state.user; const user = this.state.user;
@ -161,6 +193,9 @@ class App extends Component {
const toolStatus = this.state.toolStatus; const toolStatus = this.state.toolStatus;
const connected = this.state.connected; const connected = this.state.connected;
const network = this.state.network; const network = this.state.network;
const selectedCourses = this.state.selectedCourses;
console.log(this.state);
return ( return (
network ? network ?
@ -189,24 +224,45 @@ class App extends Component {
{login.token ? {login.token ?
<div> <div>
{toolData && user ? {toolData && user ?
<div> <Container>
<Route exact path='/' render={props => {user.profile.selected_courses ?
<Categories {...props} data={toolData} /> <div>
} /> <Route exact path='/' render={props =>
<Categories {...props} data={toolData} />
} />
<Route exact path='/:category' render={props => <Route exact path='/:category' render={props =>
<Category {...props} data={toolData} user={user} /> <Category {...props} data={toolData} user={user} />
} /> } />
<Route exact path='/:category/:slug' render={props => <Route exact path='/:category/:slug' render={props =>
<Tool {...props} <Tool {...props}
data={toolData} data={toolData}
user={user} user={user}
toolStatus={toolStatus} toolStatus={toolStatus}
requestInterlock={this.requestInterlock} requestInterlock={this.requestInterlock}
/> />
} /> } />
</div> </div>
:
<div>
<Message visible warning header='Please select which courses youve taken:' />
<Form size='massive' onSubmit={this.submitCourses}>
{toolData.courses.map((x, i) =>
<Form.Checkbox
checked={selectedCourses[i]}
onChange={(e, data) => this.toggleCourse(i, data)}
label={x.name}
key={i}
/>
)}
<br />
<Button type='submit'>Submit</Button>
</Form>
<br />Note: your selection will be reviewed by a lockout admin
</div>
}
</Container>
: :
<Dimmer active> <Dimmer active>
<Loader>Loading</Loader> <Loader>Loading</Loader>

View File

@ -1,5 +1,5 @@
import React, { Component } from 'react'; 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'; import { Link } from 'react-router-dom';
class Categories extends Component { class Categories extends Component {
@ -9,24 +9,22 @@ class Categories extends Component {
return ( return (
<div> <div>
<Container> <Breadcrumb size='large'>
<Breadcrumb size='large'> <Breadcrumb.Section active>Categories</Breadcrumb.Section>
<Breadcrumb.Section active>Categories</Breadcrumb.Section> </Breadcrumb>
</Breadcrumb>
<Item.Group link unstackable divided> <Item.Group link unstackable divided>
{data.categories.map((x, n) => {data.categories.map((x, n) =>
<Item as={Link} to={'/' + x.slug} key={n}> <Item as={Link} to={'/' + x.slug} key={n}>
<Item.Image size='tiny' src={x.photo} /> <Item.Image size='tiny' src={x.photo} />
<Item.Content> <Item.Content>
<Item.Header>{x.name}</Item.Header> <Item.Header>{x.name}</Item.Header>
<Item.Description>{x.info}</Item.Description> <Item.Description>{x.info}</Item.Description>
<Item.Extra>{x.tools.length} tools</Item.Extra> <Item.Extra>{x.tools.length} tools</Item.Extra>
</Item.Content> </Item.Content>
</Item> </Item>
)} )}
</Item.Group> </Item.Group>
</Container>
</div> </div>
); );
} }

View File

@ -15,7 +15,7 @@ class Tool extends Component {
} else if (status.state == 'armed') { } else if (status.state == 'armed') {
return { msg: 'Armed.', canArm: false, canDisarm: true, }; return { msg: 'Armed.', canArm: false, canDisarm: true, };
} else if (status.state == 'on') { } else if (status.state == 'on') {
return { msg: 'On.', canArm: false, canDisarm: true, }; return { msg: 'On.', canArm: false, canDisarm: false, };
} else { } else {
return { msg: 'Error: Impossible state!', canArm: false, canDisarm: false, }; return { msg: 'Error: Impossible state!', canArm: false, canDisarm: false, };
} }