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);