mirror of
https://github.com/rjNemo/devbook_ts
synced 2026-06-12 13:36:43 +00:00
update useForm hook to handle checkboxes
This commit is contained in:
parent
1a4930e220
commit
d8c0ca5737
2 changed files with 125 additions and 56 deletions
|
|
@ -24,7 +24,11 @@ const useForm = <T>(initFormData: T) => {
|
||||||
/** clean form after successful submition */
|
/** clean form after successful submition */
|
||||||
const resetForm = () => setFormData(initFormData);
|
const resetForm = () => setFormData(initFormData);
|
||||||
|
|
||||||
return {formData, handleChange, resetForm};
|
// /** update checkboxes */
|
||||||
|
const handleCheckboxesChange = (e: ChangeEvent<HTMLInputElement>): void =>
|
||||||
|
setFormData({...formData, [e.target.name]: e.target.checked});
|
||||||
|
|
||||||
|
return {formData, handleChange, handleCheckboxesChange, resetForm};
|
||||||
};
|
};
|
||||||
|
|
||||||
export default useForm;
|
export default useForm;
|
||||||
|
|
|
||||||
|
|
@ -1,10 +1,45 @@
|
||||||
import React, {FC} from 'react';
|
import React, {FC, useState} from 'react';
|
||||||
import FormHeader from '../components/FormHeader';
|
import FormHeader from '../components/FormHeader';
|
||||||
|
import {enhance} from '../store/firebase';
|
||||||
|
import Routes from '../constants/routes';
|
||||||
|
import {Link} from 'react-router-dom';
|
||||||
|
import Dev from '../models/Dev';
|
||||||
|
import {WithFirebaseProps, withFirebase} from 'react-redux-firebase';
|
||||||
|
import User from '../models/User';
|
||||||
|
import IAlert, {formAlert} from '../types/Alert';
|
||||||
|
import Alert from '../components/Alert';
|
||||||
|
import useForm from '../hooks';
|
||||||
|
|
||||||
|
interface FormData {
|
||||||
|
school: string;
|
||||||
|
degree: string;
|
||||||
|
from?: string;
|
||||||
|
to?: string;
|
||||||
|
current?: boolean;
|
||||||
|
description?: string;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Form to add an Education step to Profile
|
* Form to add an Education step to Profile
|
||||||
*/
|
*/
|
||||||
const AddEducation: FC = () => (
|
const AddEducation: FC<WithFirebaseProps<User>> = ({firebase}) => {
|
||||||
|
const [alert, setAlert] = useState<IAlert>(formAlert);
|
||||||
|
|
||||||
|
const initFormData: FormData = {
|
||||||
|
school: '',
|
||||||
|
degree: '',
|
||||||
|
from: '',
|
||||||
|
to: '',
|
||||||
|
current: false,
|
||||||
|
description: '',
|
||||||
|
};
|
||||||
|
const {formData, handleChange, handleCheckboxesChange, resetForm} = useForm<
|
||||||
|
FormData
|
||||||
|
>(initFormData);
|
||||||
|
|
||||||
|
const isDisabled: boolean = formData.school === '' || formData.degree === '';
|
||||||
|
|
||||||
|
return (
|
||||||
<section className="container">
|
<section className="container">
|
||||||
<FormHeader
|
<FormHeader
|
||||||
title="Add Your Education"
|
title="Add Your Education"
|
||||||
|
|
@ -18,8 +53,11 @@ const AddEducation: FC = () => (
|
||||||
<input
|
<input
|
||||||
type="text"
|
type="text"
|
||||||
placeholder="* School or Bootcamp"
|
placeholder="* School or Bootcamp"
|
||||||
name="school"
|
name="schools"
|
||||||
|
value={formData.school}
|
||||||
|
onChange={handleChange}
|
||||||
required
|
required
|
||||||
|
autoFocus
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
<div className="form-group">
|
<div className="form-group">
|
||||||
|
|
@ -27,6 +65,8 @@ const AddEducation: FC = () => (
|
||||||
type="text"
|
type="text"
|
||||||
placeholder="* Degree or Certificate"
|
placeholder="* Degree or Certificate"
|
||||||
name="degree"
|
name="degree"
|
||||||
|
value={formData.degree}
|
||||||
|
onChange={handleChange}
|
||||||
required
|
required
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
|
|
@ -35,15 +75,31 @@ const AddEducation: FC = () => (
|
||||||
</div>
|
</div>
|
||||||
<div className="form-group">
|
<div className="form-group">
|
||||||
<h4>From Date</h4>
|
<h4>From Date</h4>
|
||||||
<input type="date" name="from" />
|
<input
|
||||||
|
type="date"
|
||||||
|
name="from"
|
||||||
|
value={formData.from}
|
||||||
|
onChange={handleChange}
|
||||||
|
/>
|
||||||
</div>
|
</div>
|
||||||
<div className="form-group">
|
<div className="form-group">
|
||||||
<h4>To Date</h4>
|
<h4>To Date</h4>
|
||||||
<input type="date" name="to" />
|
<input
|
||||||
|
type="date"
|
||||||
|
name="to"
|
||||||
|
value={formData.to}
|
||||||
|
onChange={handleChange}
|
||||||
|
/>
|
||||||
</div>
|
</div>
|
||||||
<div className="form-group">
|
<div className="form-group">
|
||||||
<p>
|
<p>
|
||||||
<input type="checkbox" name="current" value="" /> Current School
|
<input
|
||||||
|
type="checkbox"
|
||||||
|
name="current"
|
||||||
|
checked={formData.current}
|
||||||
|
onChange={handleCheckboxesChange}
|
||||||
|
/>
|
||||||
|
Current School
|
||||||
</p>
|
</p>
|
||||||
</div>
|
</div>
|
||||||
<div className="form-group">
|
<div className="form-group">
|
||||||
|
|
@ -52,14 +108,23 @@ const AddEducation: FC = () => (
|
||||||
cols={30}
|
cols={30}
|
||||||
rows={5}
|
rows={5}
|
||||||
placeholder="Program Description"
|
placeholder="Program Description"
|
||||||
|
value={formData.description}
|
||||||
|
onChange={handleChange}
|
||||||
></textarea>
|
></textarea>
|
||||||
</div>
|
</div>
|
||||||
<input type="submit" className="btn btn-primary my-1" value="Submit" />
|
{alert.show && <Alert text={alert.text} color={alert.color} />}
|
||||||
<a className="btn btn-light my-1" href="dashboard.html">
|
<input
|
||||||
|
type="submit"
|
||||||
|
className="btn btn-primary my-1"
|
||||||
|
value="Submit"
|
||||||
|
disabled={isDisabled}
|
||||||
|
/>
|
||||||
|
<Link className="btn btn-light my-1" to={Routes.DASHBOARD}>
|
||||||
Go Back
|
Go Back
|
||||||
</a>
|
</Link>
|
||||||
</form>
|
</form>
|
||||||
</section>
|
</section>
|
||||||
);
|
);
|
||||||
|
};
|
||||||
|
|
||||||
export default AddEducation;
|
export default withFirebase(AddEducation);
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue