From 80895656b6d47be94d16926b6078915ba1710aab Mon Sep 17 00:00:00 2001 From: Ruidy Nemausat Date: Tue, 2 Jun 2020 19:22:15 +0200 Subject: [PATCH] refactor: EditProfile folder --- src/pages/EditProfile.tsx | 319 -------------------------------- src/pages/EditProfile/Form.tsx | 211 +++++++++++++++++++++ src/pages/EditProfile/index.tsx | 147 +++++++++++++++ 3 files changed, 358 insertions(+), 319 deletions(-) delete mode 100644 src/pages/EditProfile.tsx create mode 100644 src/pages/EditProfile/Form.tsx create mode 100644 src/pages/EditProfile/index.tsx diff --git a/src/pages/EditProfile.tsx b/src/pages/EditProfile.tsx deleted file mode 100644 index 3fe5b5e..0000000 --- a/src/pages/EditProfile.tsx +++ /dev/null @@ -1,319 +0,0 @@ -import React, {FC, useState} from 'react'; -import {Link} from 'react-router-dom'; -import Routes from '../constants/routes'; -// Redux -import {enhance} from '../store/firebase'; -import {WithFirebaseProps} from 'react-redux-firebase'; -// Style -import {FontAwesomeIcon} from '@fortawesome/react-fontawesome'; -import { - faTwitter, - faFacebook, - faYoutube, - faLinkedin, - faInstagram, -} from '@fortawesome/free-brands-svg-icons'; -import FormHeader from '../components/FormHeader'; -import Alert from '../components/Alert'; -import Statuses from '../constants/statuses'; -// Form -import useForm from '../hooks'; -import getGithubRepos from '../services/github'; -// Typing -import Dev from '../models/Dev'; -import User from '../models/User'; -import Links, {parseLink, getGithubLink} from '../types/Links'; -import IAlert, {formAlert} from '../types/Alert'; - -interface FormData { - status: string; - company: string; - website: string; - location: string; - skills: string; - github: string; - bio: string; - facebook: string; - linkedin: string; - instagram: string; - twitter: string; - youtube: string; -} - -interface IProps extends Dev, WithFirebaseProps {} - -/** - * Form to update dev's personal information. - */ -const EditProfile: FC = ({ - firebase, - status, - skills, - company, - links, - location, - bio, - github, -}) => { - const [showLinks, setShowLinks] = useState(false); - const [alert, setAlert] = useState(formAlert); - - const initFormData = { - status: status ?? 'Developer', - company: company, - location: location ?? '', - bio: bio ?? '', - skills: skills?.toString() ?? '', - website: links?.website ?? '', - github: github ?? '', - facebook: links?.facebook ?? '', - linkedin: links?.linkedin ?? '', - instagram: links?.instagram ?? '', - twitter: links?.twitter ?? '', - youtube: links?.youtube ?? '', - }; - - const {formData, handleChange} = useForm(initFormData); - - /** construct profile object from formData */ - const makeProfile = async ({ - status, - company, - location, - bio, - website, - instagram, - facebook, - linkedin, - twitter, - github, - youtube, - skills, - }: FormData) => { - const newLinks: Links = { - website: parseLink(website), - instagram: parseLink(instagram), - facebook: parseLink(facebook), - linkedin: parseLink(linkedin), - twitter: parseLink(twitter), - github: getGithubLink(github), - youtube: parseLink(youtube), - }; - const newSkills: string[] = skills?.split(','); - const newRepos = await getGithubRepos(github); - return { - status, - company, - location, - bio, - github, - links: newLinks, - skills: newSkills, - repos: newRepos, - }; - }; - - const handleSubmit = async (e: React.FormEvent) => { - e.preventDefault(); - const updatedDev = await makeProfile(formData); - try { - firebase.updateProfile(updatedDev, {useSet: true, merge: true}); - setAlert({ - show: true, - color: 'success', - text: - 'Profile successfully updated. You may go back to your dashboard.', - }); - } catch (err) { - console.error(err); - setAlert({...alert, show: true}); - } - }; - - const isDisabled: boolean = formData.status === '' || formData.skills === ''; - - const toggleSocialLinks = () => setShowLinks(!showLinks); - return ( -
- - -
-
- - - Give us an idea of where you are at in your career - -
-
- - - Could be your own company or one you work for - -
-
- - - Could be your own or a company website - -
-
- - - City & state suggested (eg. Boston, MA) - -
-
- - - Please use comma separated values (eg. HTML,CSS,JavaScript,PHP) - -
-
- - - If you want your latest repos and a Github link, include your - username - -
-
- - Tell us a little about yourself -
- -
- - Optional -
- - {showLinks && ( - <> -
- - -
- -
- - -
- -
- - -
- -
- - -
- -
- - -
- - )} - {alert.show && } - - - Go Back - - -
- ); -}; - -export default enhance(EditProfile); diff --git a/src/pages/EditProfile/Form.tsx b/src/pages/EditProfile/Form.tsx new file mode 100644 index 0000000..9df3481 --- /dev/null +++ b/src/pages/EditProfile/Form.tsx @@ -0,0 +1,211 @@ +import React, {FC} from 'react'; +// Routing +import {Link} from 'react-router-dom'; +import {FontAwesomeIcon} from '@fortawesome/react-fontawesome'; +import { + faFacebook, + faInstagram, + faLinkedin, + faTwitter, + faYoutube, +} from '@fortawesome/free-brands-svg-icons'; +import {IProfileForm} from '.'; + +import Routes from '../../constants/routes'; +import Statuses from '../../constants/statuses'; + +interface IProps { + showLinks: boolean; + isDisabled: boolean; + formData: IProfileForm; + handleSubmit: (e: React.FormEvent) => void; + handleChange: ( + e: React.ChangeEvent< + HTMLInputElement | HTMLTextAreaElement | HTMLSelectElement + >, + ) => void; + toggleSocialLinks: () => void; +} + +const EditProfileForm: FC = ({ + showLinks, + handleSubmit, + formData, + handleChange, + isDisabled, + toggleSocialLinks, +}) => ( +
+
+ + + Give us an idea of where you are at in your career + +
+
+ + + Could be your own company or one you work for + +
+
+ + + Could be your own or a company website + +
+
+ + + City & state suggested (eg. Boston, MA) + +
+
+ + + Please use comma separated values (eg. HTML,CSS,JavaScript,PHP) + +
+
+ + + If you want your latest repos and a Github link, include your username + +
+
+ + Tell us a little about yourself +
+ +
+ + Optional +
+ + {showLinks && ( + <> +
+ + +
+ +
+ + +
+ +
+ + +
+ +
+ + +
+ +
+ + +
+ + )} + + + + Go Back + +
+); + +export default EditProfileForm; diff --git a/src/pages/EditProfile/index.tsx b/src/pages/EditProfile/index.tsx new file mode 100644 index 0000000..9c10d3d --- /dev/null +++ b/src/pages/EditProfile/index.tsx @@ -0,0 +1,147 @@ +import React, {FC, useState} from 'react'; +// Redux +import {enhance} from '../../store/firebase'; +import {WithFirebaseProps} from 'react-redux-firebase'; +// Style +import FormHeader from '../../components/FormHeader'; +import Alert from '../../components/Alert'; +// Form +import useForm from '../../hooks'; +import getGithubRepos from '../../services/github'; +// Typing +import Dev from '../../models/Dev'; +import User from '../../models/User'; +import Links, {parseLink, getGithubLink} from '../../types/Links'; +import IAlert, {formAlert} from '../../types/Alert'; +import EditProfileForm from './Form'; + +export interface IProfileForm { + status: string; + company: string; + website: string; + location: string; + skills: string; + github: string; + bio: string; + facebook: string; + linkedin: string; + instagram: string; + twitter: string; + youtube: string; +} + +interface IProps extends Dev, WithFirebaseProps {} + +/** + * Form to update dev's personal information. + */ +const EditProfile: FC = ({ + firebase, + status, + skills, + company, + links, + location, + bio, + github, +}) => { + const [showLinks, setShowLinks] = useState(false); + const [alert, setAlert] = useState(formAlert); + + const initFormData = { + status: status ?? 'Developer', + company: company, + location: location ?? '', + bio: bio ?? '', + skills: skills?.toString() ?? '', + website: links?.website ?? '', + github: github ?? '', + facebook: links?.facebook ?? '', + linkedin: links?.linkedin ?? '', + instagram: links?.instagram ?? '', + twitter: links?.twitter ?? '', + youtube: links?.youtube ?? '', + }; + + const {formData, handleChange} = useForm(initFormData); + + /** construct profile object from formData */ + const makeProfile = async ({ + status, + company, + location, + bio, + website, + instagram, + facebook, + linkedin, + twitter, + github, + youtube, + skills, + }: IProfileForm) => { + const newLinks: Links = { + website: parseLink(website), + instagram: parseLink(instagram), + facebook: parseLink(facebook), + linkedin: parseLink(linkedin), + twitter: parseLink(twitter), + github: getGithubLink(github), + youtube: parseLink(youtube), + }; + const newSkills: string[] = skills?.split(','); + const newRepos = await getGithubRepos(github); + return { + status, + company, + location, + bio, + github, + links: newLinks, + skills: newSkills, + repos: newRepos, + }; + }; + + const handleSubmit = async (e: React.FormEvent) => { + e.preventDefault(); + const updatedDev = await makeProfile(formData); + try { + firebase.updateProfile(updatedDev, {useSet: true, merge: true}); + setAlert({ + show: true, + color: 'success', + text: + 'Profile successfully updated. You may go back to your dashboard.', + }); + } catch (err) { + console.error(err); + setAlert({...alert, show: true}); + } + }; + + const isDisabled: boolean = formData.status === '' || formData.skills === ''; + + const toggleSocialLinks = () => setShowLinks(!showLinks); + + return ( +
+ + + {alert.show && } + +
+ ); +}; + +export default enhance(EditProfile);