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

cleanup and validation #55

Merged
merged 1 commit into from
Mar 19, 2021
Merged
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
4 changes: 3 additions & 1 deletion components/Input/Input.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from 'react';

const Input = (
{ htmlFor, spanLabel, id, type, name, placeholder, onChange, value }
{ htmlFor, spanLabel, id, type, name, placeholder, onChange, value, onBlur }
) => (
<label className="subs-form__labels" htmlFor={htmlFor}>
<span>{spanLabel}</span>
Expand All @@ -12,6 +12,8 @@ const Input = (
placeholder={placeholder}
onChange={onChange}
value={value}
onBlur={onBlur}
required
/>
</label>
)
Expand Down
82 changes: 0 additions & 82 deletions components/forms/subscribe-form.js

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,24 @@ import { useFormik } from 'formik';
import { SUBSCRIBE_ACTION } from '../../constants/path';
import Input from '../Input';
import Checkbox from '../Checkbox';
import * as Yup from 'yup';

const SubscribeForm = () => {
const formik = useFormik({
initialValues: {
email: '',
name: ''
}
},
validationSchema: Yup.object({
email: Yup.string()
.min(5, 'Too Short!')
.email('Invalid email address!')
.required('Email is required!'),
name: Yup.string()
.min(2, 'Too Short!')
.max(50, 'Too Long!')
.required('Name is required!'),
}),
});
return (
<div className="subs-form">
Expand All @@ -28,14 +39,14 @@ const SubscribeForm = () => {
brief</a>:
an indiscriminate daily digest from our RSS feed for your reading pleasure and professional
development.
</p>
</p>

<p className="subs-form__text">
oh! and if you let us know which of our editorial streams most floats your boat,
we'll also send you the occasional curated letter from one of the humans on
our team of editors - all of whom are far too busy
publishing stories to spam you in any way, we promise.
</p>
</p>
</div>

<div className="subs-form__inputs">
Expand All @@ -47,8 +58,12 @@ const SubscribeForm = () => {
htmlFor="email"
spanLabel="Email Address"
value={formik.values.email}
onBlur={formik.handleBlur}
onChange={formik.handleChange}
/>
{formik.touched.email && formik.errors.email ? (
<div className="error-msg" style={{ marginBottom: '10px' }}>{formik.errors.email}</div>
) : null}

<Input
id="name"
Expand All @@ -58,8 +73,12 @@ const SubscribeForm = () => {
htmlFor="name"
spanLabel="First Name"
value={formik.values.name}
onBlur={formik.handleBlur}
onChange={formik.handleChange}
/>
{formik.touched.name && formik.errors.name ? (
<div className="error-msg">{formik.errors.name}</div>
) : null}
</div>

<div className="subs-form__checkboxes">
Expand Down Expand Up @@ -103,7 +122,13 @@ const SubscribeForm = () => {
<input type="hidden" name="list" value="OMTKMVEF3BfWHMve4EY8925g" />
<input type="hidden" name="subform" value="yes" />

<button className="subs-form__btn" type="submit" name="submit" id="submit">Get great stories by email</button>
<button
id="submit"
type="submit"
name="submit"
className="subs-form__btn"
disabled={formik.errors.name || formik.errors.email}
>Get great stories by email</button>
</form>
</div>
</div >
Expand Down
20 changes: 0 additions & 20 deletions components/forms/unsubscribe-form.js

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
import React from 'react';
import { useFormik } from 'formik';
import * as Yup from 'yup';
import Input from '../Input';

const UnsubscribeForm = () => {
const formik = useFormik({
initialValues: {
email: ''
},
validationSchema: Yup.object({
email: Yup.string()
.min(5, 'Too Short!')
.email('Invalid email address!')
.required('Email is required!')
}),
onSubmit: values => {
console.log(values);
alert(JSON.stringify(values, null, 2));
}
});
return (
Expand All @@ -35,8 +42,12 @@ const UnsubscribeForm = () => {
htmlFor="email"
spanLabel="Email Address"
value={formik.values.email}
onBlur={formik.handleBlur}
onChange={formik.handleChange}
/>
{formik.touched.email && formik.errors.email ? (
<div className="error-msg">{formik.errors.email}</div>
) : null}
</div>
<button className="subs-form__btn unsubscribe" type="submit">Unsubscribe</button>
</form>
Expand Down
3 changes: 0 additions & 3 deletions components/subscribe/index.js

This file was deleted.

16 changes: 0 additions & 16 deletions components/subscribe/subscribe.js

This file was deleted.

3 changes: 0 additions & 3 deletions components/unsubscribe/index.js

This file was deleted.

14 changes: 0 additions & 14 deletions components/unsubscribe/unsubscribe.js

This file was deleted.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"formik": "^2.2.6",
"next": "10.0.8",
"react": "17.0.1",
"react-dom": "17.0.1"
"react-dom": "17.0.1",
"yup": "^0.32.9"
}
}
4 changes: 0 additions & 4 deletions pages/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,6 @@ export default function Home() {
<Link href='/subscribe'>Subscribe page</Link>
<br />
<Link href='/unsubscribe'>Unsubscribe page</Link>
<br />
<Link href='/subscribe-formik'>Subscribe page(formik)</Link>
<br />
<Link href='/unsubscribe-formik'>Unsubscribe page(formik)</Link>
</div>
</>
)
Expand Down
5 changes: 0 additions & 5 deletions pages/subscribe-formik/index.js

This file was deleted.

6 changes: 3 additions & 3 deletions pages/subscribe/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Subscribe from '../../components/subscribe';
import SubscribeForm from '../../components/forms/subscribe';

const subscribe = () => <Subscribe />
const form = () => <SubscribeForm />

export default subscribe;
export default form;
5 changes: 0 additions & 5 deletions pages/unsubscribe-formik/index.js

This file was deleted.

4 changes: 2 additions & 2 deletions pages/unsubscribe/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Unubscribe from '../../components/unsubscribe';
import UnsubscribeForm from '../../components/forms/unsubscribe';

const unsubscribe = () => <Unubscribe />
const unsubscribe = () => <UnsubscribeForm />

export default unsubscribe;
5 changes: 5 additions & 0 deletions styles/globals.css
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,11 @@ a {
}
}

.error-msg {
margin-top: -15px;
color: red;
}

@media only screen and (max-width: 375px) {
.subs-form__inputs {
margin-top: 10px;
Expand Down