Skip to content

Commit 0f0c970

Browse files
Benjamín Thomas Kramm Pinojfcampos1
authored andcommitted
fix: payload at sign in accept (#7)
* feat: add SignUpForm component * feat: add working sign up screen * fix: remove alias webpack * feat: add sign in screen * fix: fix payload at submit
1 parent 10de40d commit 0f0c970

6 files changed

Lines changed: 118 additions & 58 deletions

File tree

src/App.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { BrowserRouter, Route, Switch } from 'react-router-dom'
33

44
import './styles/App.css'
55

6-
import { Home, SignUp, NotFound } from './screens'
6+
import { Home, SignIn, SignUp, NotFound } from './screens'
77
import Navbar from './components/Navbar/Navbar'
88

99
const App = () => {
@@ -14,6 +14,7 @@ const App = () => {
1414
<BrowserRouter>
1515
<Switch>
1616
<Route exact path="/" component={Home} />
17+
<Route exact path="/sign-in" component={SignIn} />
1718
<Route exact path="/sign-up" component={SignUp} />
1819
<Route component={NotFound} />
1920
</Switch>
Lines changed: 42 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,94 +1,79 @@
1-
import React, { Component } from 'react'
2-
import { StyleSheet } from 'react-native'
1+
import React from 'react'
32
import PropTypes from 'prop-types'
43

5-
import Layout from '../../constants/Layout'
4+
// Components
5+
import { Button, FormControl, Input, InputLabel } from '@material-ui/core'
66

7-
class SignInForm extends Component {
7+
class SignInForm extends React.Component {
88
constructor(props) {
99
super(props)
10+
1011
this.state = {
1112
email: '',
1213
password: '',
13-
inputValidity: true,
14+
15+
validity: { email: false },
1416
}
1517

16-
this.onChangePassword = this.onChangePassword.bind(this)
1718
this.onChangeEmail = this.onChangeEmail.bind(this)
19+
this.onChangePassword = this.onChangePassword.bind(this)
20+
21+
this.getValidity = this.getValidity.bind(this)
1822
}
1923

20-
onChangeEmail(email) {
24+
onChangeEmail({ target: { value: email } }) {
2125
const validity = email.match(
2226
// eslint-disable-next-line no-useless-escape
2327
/^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
2428
)
25-
this.setState({ email, inputValidity: validity })
29+
30+
this.setState(oldState => ({
31+
email,
32+
validity: { ...oldState.validity, email: !!validity },
33+
}))
2634
}
2735

28-
onChangePassword(password) {
36+
onChangePassword({ target: { value: password } }) {
2937
this.setState({ password })
3038
}
3139

40+
getValidity() {
41+
return this.state.validity.email
42+
}
43+
3244
render() {
45+
const { onSubmit } = this.props
46+
const { email, password } = this.state
47+
3348
return (
34-
<Form style={styles.form}>
35-
<Item inlineLabel regular style={styles.item}>
36-
<Input
37-
placeholder="Email"
38-
style={styles.input}
39-
onChangeText={this.onChangeEmail}
40-
keyboardType="email-address"
41-
autoCapitalize="none"
42-
value={this.state.email}
43-
/>
44-
</Item>
45-
<Item inlineLabel last regular style={styles.item}>
49+
<FormControl>
50+
<FormControl>
51+
<InputLabel>E-mail</InputLabel>
52+
<Input type="email" value={email} onChange={this.onChangeEmail} />
53+
</FormControl>
54+
55+
<FormControl>
56+
<InputLabel>Contraseña</InputLabel>
4657
<Input
47-
placeholder="Contraseña"
48-
style={styles.input}
49-
onChangeText={this.onChangePassword}
50-
secureTextEntry
51-
value={this.state.password}
58+
type="password"
59+
value={password}
60+
onChange={this.onChangePassword}
5261
/>
53-
</Item>
62+
</FormControl>
63+
5464
<Button
55-
block
56-
borderRadius={10}
57-
style={styles.button}
58-
disabled={!this.state.inputValidity}
59-
onPress={() =>
60-
this.props.onSend(this.state.email, this.state.password)
61-
}
65+
disabled={!this.getValidity()}
66+
onClick={() => onSubmit({ email, password })}
6267
>
63-
<Text>Entrar</Text>
68+
Ingresar
6469
</Button>
65-
</Form>
70+
</FormControl>
6671
)
6772
}
6873
}
6974

7075
SignInForm.propTypes = {
71-
onSend: PropTypes.func.isRequired,
76+
onSubmit: PropTypes.func.isRequired,
7277
}
7378

74-
const styles = StyleSheet.create({
75-
button: {
76-
backgroundColor: '#0000FF',
77-
marginTop: 20,
78-
},
79-
form: {
80-
alignItems: 'center',
81-
height: 250,
82-
margin: 15,
83-
},
84-
input: {
85-
width: Layout.window.width * 0.85,
86-
},
87-
item: {
88-
borderRadius: 10,
89-
marginBottom: 20,
90-
paddingHorizontal: 10,
91-
},
92-
})
93-
9479
export default SignInForm

src/components/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1+
export { default as SignInForm } from './authentication/SignInForm/index'
12
export { default as SignUpForm } from './authentication/SignUpForm/index'

src/screens/SignIn/index.js

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import React from 'react'
2+
import PropTypes from 'prop-types'
3+
import './style.sass'
4+
5+
import cookies from 'js-cookie'
6+
7+
// Store
8+
import { connect } from 'react-redux'
9+
import { loginUser } from '../../redux/actions/user'
10+
11+
// Components
12+
import { SignInForm } from '../../components/index'
13+
import { CircularProgress } from '@material-ui/core'
14+
15+
const MESSAGE =
16+
'Hubo un problema iniciando sesión. Por favor intentalo de nuevo.'
17+
18+
class SignInScreen extends React.Component {
19+
constructor(props) {
20+
super(props)
21+
22+
this.state = {
23+
loading: false,
24+
}
25+
26+
this.onSubmit = this.onSubmit.bind(this)
27+
}
28+
29+
async onSubmit(payload) {
30+
const { signIn } = this.props
31+
32+
this.setState({ loading: true })
33+
34+
const user = await signIn(payload)
35+
36+
this.setState({ loading: false })
37+
38+
if (user.error || !user.payload.data.email) return alert(MESSAGE)
39+
40+
cookies.set('@userToken', user.token)
41+
cookies.set('@userId', user.user_id)
42+
}
43+
44+
render() {
45+
const { loading } = this.state
46+
47+
return (
48+
<div className="sign-up">
49+
<SignInForm onSubmit={this.onSubmit} />
50+
51+
{loading && <CircularProgress />}
52+
</div>
53+
)
54+
}
55+
}
56+
57+
SignInScreen.propTypes = {
58+
signIn: PropTypes.func.isRequired,
59+
}
60+
61+
const mapStateToProps = state => ({
62+
user: state.user,
63+
})
64+
65+
const mapDispatchToProps = dispatch => ({
66+
signIn: payload => dispatch(loginUser(payload.name, payload.password)),
67+
})
68+
69+
export default connect(
70+
mapStateToProps,
71+
mapDispatchToProps
72+
)(SignInScreen)

src/screens/SignIn/style.sass

Whitespace-only changes.

src/screens/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
export { default as Home } from './Home/index'
2+
export { default as SignIn } from './SignIn/index'
23
export { default as SignUp } from './SignUp/index'
34

45
export { default as NotFound } from './NotFound'

0 commit comments

Comments
 (0)