This repository has been archived by the owner on Aug 5, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add create-customer API call * Implement sign up form with styles and libraries * General layout improvement * ESLint and stylelint configuration fixes
- Loading branch information
1 parent
231d811
commit 1411893
Showing
13 changed files
with
337 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
{ | ||
"extends": [ | ||
"@strv/stylelint-config-styled-components" | ||
"@strv/stylelint-config-styled-components", | ||
"stylelint-config-prettier" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { getToken } from './get-token' | ||
import config from '../config' | ||
|
||
export const createCustomer = async ({ email, password, firstName }) => { | ||
const token = await getToken() | ||
|
||
const response = await fetch(`${config.apiUrl}/api/customers`, { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/vnd.api+json', | ||
Authorization: `Bearer ${token}`, | ||
}, | ||
body: JSON.stringify({ | ||
data: { | ||
type: 'customers', | ||
attributes: { | ||
email, | ||
password, | ||
metadata: { | ||
firstName, | ||
}, | ||
}, | ||
}, | ||
}), | ||
}) | ||
|
||
switch (response.status) { | ||
case 201: { | ||
const { | ||
data: { attributes }, | ||
} = await response.json() | ||
return { | ||
email: attributes.email, | ||
firstName: attributes.metadata.firstName, | ||
} | ||
} | ||
case 422: | ||
throw new Error('Email is already registered') | ||
default: | ||
throw new Error('Unexpected error') | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import styled from 'styled-components' | ||
import theme from '../../common/theme' | ||
|
||
const Form = styled.form` | ||
display: flex; | ||
flex-direction: column; | ||
margin: 14px auto; | ||
max-width: 100%; | ||
padding: 0 12px; | ||
width: 420px; | ||
` | ||
|
||
export const GlobalFormError = styled.div` | ||
color: ${theme.color.red}; | ||
margin-bottom: 12px; | ||
padding: 10px; | ||
text-align: center; | ||
` | ||
|
||
export default Form |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import React from 'react' | ||
|
||
import { Wrapper, Label, StyledInput, StyledError } from './styled' | ||
|
||
const Input = ({ error, label, name, onChange, type = 'text', value }) => ( | ||
<Wrapper> | ||
<Label htmlFor={name}>{label}</Label> | ||
<StyledInput | ||
hasError={!!error} | ||
id={name} | ||
name={name} | ||
onChange={onChange} | ||
type={type} | ||
value={value} | ||
/> | ||
{!!error && <StyledError>{error}</StyledError>} | ||
</Wrapper> | ||
) | ||
|
||
export default Input |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import styled from 'styled-components' | ||
import theme from '../../common/theme' | ||
|
||
export const Wrapper = styled.div` | ||
display: flex; | ||
flex-direction: column; | ||
margin-bottom: 16px; | ||
` | ||
|
||
export const StyledInput = styled.input` | ||
border: 2px solid | ||
${({ hasError }) => (hasError ? theme.color.red : theme.color.gray)}; | ||
border-radius: 4px; | ||
font-size: 16px; | ||
font-weight: 100; | ||
padding: 10px 12px; | ||
` | ||
|
||
export const Label = styled.label` | ||
font-size: 16px; | ||
margin-bottom: 6px; | ||
` | ||
|
||
export const StyledError = styled.div` | ||
color: ${theme.color.red}; | ||
margin-top: 4px; | ||
` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
import React, { Component } from 'react' | ||
import { Formik } from 'formik' | ||
|
||
import { createCustomer } from '../../api/create-customer' | ||
import Layout from '../../components/Layout' | ||
import { H1 } from '../../components/Typography' | ||
import Form, { GlobalFormError } from '../../components/Form' | ||
import Input from '../../components/Input' | ||
import Button from '../../components/Button' | ||
import schema from './schema' | ||
|
||
class SignUp extends Component { | ||
state = { | ||
globalError: '', | ||
hasSignedUp: false, | ||
} | ||
|
||
initialValues = { | ||
firstName: '', | ||
email: '', | ||
password: '', | ||
passwordConfirm: '', | ||
} | ||
|
||
handleSubmit = async (values, { setSubmitting }) => { | ||
try { | ||
setSubmitting(true) | ||
await createCustomer(values) | ||
this.setState({ | ||
hasSignedUp: true, | ||
}) | ||
} catch (error) { | ||
this.setState({ | ||
globalError: error.message, | ||
}) | ||
} | ||
setSubmitting(false) | ||
} | ||
|
||
renderSuccess() { | ||
return ( | ||
<Layout> | ||
<H1 textAlign="center">{`You've signed up!`}</H1> | ||
</Layout> | ||
) | ||
} | ||
|
||
render() { | ||
const { hasSignedUp, globalError } = this.state | ||
|
||
if (hasSignedUp) return this.renderSuccess() | ||
|
||
return ( | ||
<Layout> | ||
<H1 textAlign="center">Sign Up</H1> | ||
<Formik | ||
initialValues={this.initialValues} | ||
validationSchema={schema} | ||
onSubmit={this.handleSubmit} | ||
> | ||
{({ | ||
errors, | ||
handleChange, | ||
handleSubmit, | ||
isSubmitting, | ||
touched, | ||
values, | ||
}) => ( | ||
<Form onSubmit={handleSubmit}> | ||
{!!globalError && ( | ||
<GlobalFormError>{globalError}</GlobalFormError> | ||
)} | ||
<Input | ||
name="firstName" | ||
type="text" | ||
label="First name" | ||
value={values.firstName} | ||
onChange={handleChange} | ||
error={touched.firstName && errors.firstName} | ||
/> | ||
<Input | ||
name="email" | ||
type="email" | ||
label="Email address" | ||
value={values.email} | ||
onChange={handleChange} | ||
error={touched.email && errors.email} | ||
/> | ||
<Input | ||
name="password" | ||
type="password" | ||
label="Password" | ||
value={values.password} | ||
onChange={handleChange} | ||
error={touched.password && errors.password} | ||
/> | ||
<Input | ||
name="passwordConfirm" | ||
type="password" | ||
label="Confirm password" | ||
value={values.passwordConfirm} | ||
onChange={handleChange} | ||
error={touched.passwordConfirm && errors.passwordConfirm} | ||
/> | ||
<Button type="submit" disabled={isSubmitting}> | ||
{isSubmitting ? 'Signing Up...' : 'Sign Up'} | ||
</Button> | ||
</Form> | ||
)} | ||
</Formik> | ||
</Layout> | ||
) | ||
} | ||
} | ||
|
||
export { SignUp } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { object, string, ref } from 'yup' | ||
|
||
const schema = object().shape({ | ||
email: string() | ||
.email('Email is not valid') | ||
.required('Email is required'), | ||
firstName: string(), | ||
password: string() | ||
.min(6, 'Password is too short') | ||
.max(30, 'Password is too long') | ||
.matches(/[0-9]/u, 'Password should contain one number') | ||
.matches(/[a-z]/u, 'Password should contain at least one lowercase letter') | ||
.required('Password is required'), | ||
passwordConfirm: string() | ||
.oneOf([ref('password')], 'Passwords must match') | ||
.required('Must confirm password'), | ||
}) | ||
|
||
export default schema |
Oops, something went wrong.