Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

123 link auth to form #167

Open
wants to merge 7 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions client/actions/auth.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {setToken} from '../utils/tokens'
import {register as registerApi, signin as signinApi} from '../api/auth'
import {registerWithApi, signinWithApi} from '../api/auth'

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need 2 consecutive blank lines? Your linter should complain about this.


export const signinPending = _ => {
return {
Expand Down Expand Up @@ -41,7 +42,7 @@ export const registerError = error => {

export const signin = (user) => dispatch => {
dispatch(signinPending())
return signinApi(user)
return signinWithApi(user)
.then(res => {
setToken(res.body.token)
dispatch(signinSuccess())
Expand All @@ -51,7 +52,7 @@ export const signin = (user) => dispatch => {

export const register = (user) => dispatch => {
dispatch(registerPending())
return registerApi(user)
return registerWithApi(user)
.then(res => {
setToken(res.body.token)
dispatch(registerSuccess())
Expand Down
6 changes: 3 additions & 3 deletions client/api/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@ import request from 'superagent'

const url = 'http://localhost:3000/api/v1/auth'

export function register (user) {
export function registerWithApi (user) {
return request
.post(`${url}/register`)
.send(user)
.then(res => res.body)
}

export const signin = (user) => {
export const signinWithApi = (user) => {
return request
.post(`${url}/signin`)
.send({user})
.send(user)
.then(res => res.body)
}
8 changes: 4 additions & 4 deletions client/components/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ const App = () => {
return (
<Switch>
<Route exact path="/" component={Home} />
<Route path="/student/:id" component={Dashboard} />
<Route path="/login" component={Login} />
<Route path="/register" component={Register} />
<Route path="/evidence" component={Evidence} />
<Route exact path="/student/:id" component={Dashboard} />
<Route exact path="/login" component={Login} />
<Route exact path="/register" component={Register} />
<Route exact path="/evidence" component={Evidence} />
</Switch>
)
}
Expand Down
3 changes: 1 addition & 2 deletions client/components/Dashboard.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import React, { Component } from 'react'
import ProgressModule from './ProgressModule'
import DropdownModule from './DropdownModule'
Expand Down Expand Up @@ -51,4 +50,4 @@ export default class Dashboard extends Component {
</React.Fragment>
)
}
}
}
64 changes: 31 additions & 33 deletions client/components/Register.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import { Paper,
MenuItem,
InputLabel } from '@material-ui/core'

import {connect} from 'react-redux'
import {register, registerError} from '../actions/auth'

class Register extends React.Component {
state = {
name: '',
Expand All @@ -25,22 +28,30 @@ class Register extends React.Component {
})
}

handleSubmit (evt) {
this.props.saveItem(this.state)
this.setState(this.state)
evt.preventDefault()
}
registering = () => {
const { name, surname, password } = this.state

handleChange = event => {
this.setState({ [event.target.name]: event.target.value });
}
if(!name) {
return this.props.dispatch(registerFailure('Must provide name.'))
}
if(!surname) {
return this.props.dispatch(registerFailure('Must provide surname.'))
}
if(!password) {
return this.props.dispatch(registerFailure('Must provide email.'))
}

this.props.dispatch(register({name, surname, password}))

resetForm (evt) {
this.setState({
item: this.state,
invalid: {}
name: '',
surname: '',
password: ''
})
evt && evt.preventDefault()
}

handleChange = event => {
this.setState({ [event.target.name]: event.target.value });
}

render () {
Expand All @@ -57,26 +68,20 @@ class Register extends React.Component {
<Paper style={styles.paper} >
<h1>Register</h1>
<form >
<br/>
<br/>
<TextField
variant="outlined"
label="First name"
name='name'
value={this.state.name}
onChange={this.handleChange}
/>
<br/>
<br/>
<TextField
variant="outlined"
label="Last name"
name='surname'
value={this.state.surname}
onChange={this.handleChange}
/>
<br/>
<br/>
<TextField
variant="outlined"
type='password'
Expand All @@ -86,9 +91,6 @@ class Register extends React.Component {
value={this.state.password}
onChange={this.handleChange}
/>
<br/>
<br/>

<FormControl variant="outlined" style={{minWidth: '200px'}} >
<InputLabel
ref={ref => {
Expand Down Expand Up @@ -117,9 +119,6 @@ class Register extends React.Component {
</Select>
</FormControl>

<br/>
<br/>

<FormControl variant="outlined" style={{minWidth: '200px'}} >
<InputLabel
ref={ref => {
Expand Down Expand Up @@ -147,14 +146,12 @@ class Register extends React.Component {
<MenuItem value={'Kowhai2018'}>Kowhai2018</MenuItem>
</Select>
</FormControl>
<br/>
<br/>
<br/>
<button type='submit' className='button-primary' value='Add' onSubmit={this.handleSubmit}
onClick={this.resetForm}>Register</button>
<br/>
<br/>
<br/>
<button
type='submit'
className='button-primary'
value='Add'
onClick={this.registering}
>Register</button>
</form>
</Paper>
</Grid>
Expand All @@ -164,4 +161,5 @@ class Register extends React.Component {
}
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you want to refactor lines 96-100, you can do it with one line: const mapStateToProps = ({auth}) => ({auth}) 😸


export default Register
const mapStateToProps = ({auth}) => ({auth})
export default connect(mapStateToProps)(Register)
4 changes: 0 additions & 4 deletions client/components/SignIn.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@ import TextField from '@material-ui/core/TextField';
import Button from '@material-ui/core/Button'

class SignIn extends React.Component() {
constructor(props) {
super(props)
}

render() {
return (
<div>
Expand Down