Links type,

This commit is contained in:
Ruidy Nemausat 2020-05-14 20:07:31 +02:00
parent e0f20d253d
commit 920ee01d65
3 changed files with 120 additions and 47 deletions

View file

@ -1,5 +1,6 @@
import Experience from '../types/Experience';
import Education from '../types/Education'; import Education from '../types/Education';
import Experience from '../types/Experience';
import Links from '../types/Links';
import Repo from '../types/Repo'; import Repo from '../types/Repo';
/** Shorter dev interface */ /** Shorter dev interface */
@ -17,30 +18,39 @@ export interface DevSummary {
*/ */
interface Dev extends DevSummary { interface Dev extends DevSummary {
bio: string; bio: string;
links: Object; status: string;
company: string;
links: Links;
experiences: Experience[]; experiences: Experience[];
educations: Education[]; educations: Education[];
repos: Repo[]; repos: Repo[];
} }
/** create profile tagline */
export const getDescription = (status: string, company: string) =>
`${status} at ${company}`;
/** /**
* sample Dev for development and tests * sample Dev for development and tests
*/ */
export const dummyDev: Dev = { export const dummyDev: Dev = {
id: '0', id: '0',
displayName: 'John Doe', displayName: 'John Doe',
status: 'Developer',
company: 'Microsoft',
picture: picture:
'https://www.gravatar.com/avatar/205e460b479e2e5b48aec07710c08d50?s=200', 'https://www.gravatar.com/avatar/205e460b479e2e5b48aec07710c08d50?s=200',
description: 'Developer at Microsoft', description: 'Developer at Microsoft',
location: 'Seattle, WA', location: 'Seattle, WA',
skills: ['HTML', 'CSS', 'JavaScript', 'Python'], skills: ['HTML', 'CSS', 'JavaScript', 'Python'],
links: { links: {
web: '#', website: '#',
instagram: 'http://insta.com', instagram: 'http://insta.com',
facebook: '#', facebook: '#',
linkedin: '#', linkedin: '#',
twitter: '#', twitter: '#',
github: '#', github: '#',
youtube: '#',
}, },
bio: bio:
'Lorem ipsum, dolor sit amet consectetur adipisicing elit. Blanditiis unde quae vero enim adipisci voluptas magni sapiente reprehenderit error minima.', 'Lorem ipsum, dolor sit amet consectetur adipisicing elit. Blanditiis unde quae vero enim adipisci voluptas magni sapiente reprehenderit error minima.',

View file

@ -1,6 +1,9 @@
import React, {FC, useState} from 'react'; import React, {FC, useState, useEffect} from 'react';
import {Link} from 'react-router-dom'; import {Link} from 'react-router-dom';
import Routes from '../constants/routes'; import Routes from '../constants/routes';
// Redux
import {enhance, selectProfile} from '../store/firebase';
import {WithFirebaseProps, isLoaded, isEmpty} from 'react-redux-firebase';
// Style // Style
import {FontAwesomeIcon} from '@fortawesome/react-fontawesome'; import {FontAwesomeIcon} from '@fortawesome/react-fontawesome';
import { import {
@ -15,7 +18,12 @@ import Statuses from '../constants/statuses';
// Form // Form
import useForm from '../hooks'; import useForm from '../hooks';
interface InitFormData { import User from '../models/User';
import Dev from '../models/Dev';
import Links from '../types/Links';
import {useSelector} from 'react-redux';
interface FormData {
status: string; status: string;
company: string; company: string;
website: string; website: string;
@ -29,61 +37,99 @@ interface InitFormData {
twitter: string; twitter: string;
youtube: string; youtube: string;
} }
interface IProps extends Dev, WithFirebaseProps<User> {}
/** /**
* Form to update dev's personal information. * Form to update dev's personal information.
*/ */
const EditProfile: FC = () => { const EditProfile: FC<IProps> = ({
firebase,
status,
skills,
company,
links,
location,
bio,
}) => {
const [showLinks, setShowLinks] = useState(false); const [showLinks, setShowLinks] = useState(false);
const [variable, setVariable] = useState(useSelector(selectProfile));
const initFormData = { const initFormData = {
status: '', status: status ?? 'Developer',
company: '', company: company,
website: '', location: location ?? '',
location: '', bio: bio ?? '',
skills: '', skills: skills?.toString() ?? '',
github: '', website: links?.website ?? '',
bio: '', github: links?.github ?? '',
facebook: '', facebook: links?.facebook ?? '',
linkedin: '', linkedin: links?.linkedin ?? '',
instagram: '', instagram: links?.instagram ?? '',
twitter: '', twitter: links?.twitter ?? '',
youtube: '', youtube: links?.youtube ?? '',
}; };
const {formData, handleChange, resetForm} = useForm<InitFormData>(
initFormData,
);
const { const {formData, handleChange, resetForm} = useForm<FormData>(initFormData);
console.log(company, formData.company, variable, isEmpty(firebase.auth));
/** construct profile object from formData */
const makeProfile = ({
status, status,
company, company,
website,
location, location,
skills,
github,
bio, bio,
website,
instagram,
facebook, facebook,
linkedin, linkedin,
instagram,
twitter, twitter,
github,
youtube, youtube,
} = formData; skills,
}: FormData) => {
const newLinks: Links = {
website,
instagram,
facebook,
linkedin,
twitter,
github,
youtube,
};
const newSkills: string[] = skills?.split(',');
return {
status,
company,
location,
bio,
links: newLinks,
skills: newSkills,
};
};
const handleSubmit = (e: React.FormEvent<HTMLFormElement>): void => { const handleSubmit = (e: React.FormEvent<HTMLFormElement>): void => {
e.preventDefault(); e.preventDefault();
console.log('Submitted'); const updatedDev = makeProfile(formData);
firebase.updateProfile(updatedDev, {useSet: true, merge: true});
console.log(`Submitted ${JSON.stringify(formData, null, 2)}`);
resetForm();
}; };
const revealSocialLinks = () => setShowLinks(true); const isDisabled: boolean = formData.status === '' || formData.skills === '';
const toggleSocialLinks = () => setShowLinks(!showLinks);
return ( return (
<section className="container"> <section className="container">
<FormHeader <FormHeader
title="Create your profile" title="Edit your profile"
lead="Let's get some information to make your profile stand out" lead="Let's get some information to make your profile stand out"
/> />
<form className="form" onSubmit={handleSubmit}> <form className="form" onSubmit={handleSubmit}>
<div className="form-group"> <div className="form-group">
<select name="status" required autoFocus onChange={handleChange}> <select name="status" required onChange={handleChange}>
<option value="0">* Select Professional Status</option> <option disabled>* Select Professional Status</option>
{Statuses.map((s: string, i: number) => ( {Statuses.map((s: string, i: number) => (
<option value={s} key={i}> <option value={s} key={i}>
{s} {s}
@ -99,7 +145,8 @@ const EditProfile: FC = () => {
type="text" type="text"
placeholder="Company" placeholder="Company"
name="company" name="company"
value={company} value={formData.company}
// value={variable}
onChange={handleChange} onChange={handleChange}
/> />
<small className="form-text"> <small className="form-text">
@ -111,7 +158,7 @@ const EditProfile: FC = () => {
type="text" type="text"
placeholder="Website" placeholder="Website"
name="website" name="website"
value={website} value={formData.website}
onChange={handleChange} onChange={handleChange}
/> />
<small className="form-text"> <small className="form-text">
@ -123,7 +170,7 @@ const EditProfile: FC = () => {
type="text" type="text"
placeholder="Location" placeholder="Location"
name="location" name="location"
value={location} value={formData.location}
onChange={handleChange} onChange={handleChange}
/> />
<small className="form-text"> <small className="form-text">
@ -136,7 +183,7 @@ const EditProfile: FC = () => {
placeholder="* Skills" placeholder="* Skills"
name="skills" name="skills"
required required
value={skills} value={formData.skills}
onChange={handleChange} onChange={handleChange}
/> />
<small className="form-text"> <small className="form-text">
@ -148,7 +195,7 @@ const EditProfile: FC = () => {
type="text" type="text"
placeholder="Github Username" placeholder="Github Username"
name="github" name="github"
value={github} value={formData.github}
onChange={handleChange} onChange={handleChange}
/> />
<small className="form-text"> <small className="form-text">
@ -160,7 +207,7 @@ const EditProfile: FC = () => {
<textarea <textarea
placeholder="A short bio of yourself" placeholder="A short bio of yourself"
name="bio" name="bio"
value={bio} value={formData.bio}
onChange={handleChange} onChange={handleChange}
></textarea> ></textarea>
<small className="form-text">Tell us a little about yourself</small> <small className="form-text">Tell us a little about yourself</small>
@ -170,9 +217,9 @@ const EditProfile: FC = () => {
<button <button
type="button" type="button"
className="btn btn-light" className="btn btn-light"
onClick={revealSocialLinks} onClick={toggleSocialLinks}
> >
Add Social Network Links {showLinks ? 'Hide' : 'Add'} Social Network Links
</button> </button>
<span>Optional</span> <span>Optional</span>
</div> </div>
@ -185,7 +232,7 @@ const EditProfile: FC = () => {
type="text" type="text"
placeholder="Facebook URL" placeholder="Facebook URL"
name="facebook" name="facebook"
value={facebook} value={formData.facebook}
onChange={handleChange} onChange={handleChange}
/> />
</div> </div>
@ -196,7 +243,7 @@ const EditProfile: FC = () => {
type="text" type="text"
placeholder="Instagram URL" placeholder="Instagram URL"
name="instagram" name="instagram"
value={instagram} value={formData.instagram}
onChange={handleChange} onChange={handleChange}
/> />
</div> </div>
@ -207,7 +254,7 @@ const EditProfile: FC = () => {
type="text" type="text"
placeholder="Linkedin URL" placeholder="Linkedin URL"
name="linkedin" name="linkedin"
value={linkedin} value={formData.linkedin}
onChange={handleChange} onChange={handleChange}
/> />
</div> </div>
@ -218,7 +265,7 @@ const EditProfile: FC = () => {
type="text" type="text"
placeholder="Twitter URL" placeholder="Twitter URL"
name="twitter" name="twitter"
value={twitter} value={formData.twitter}
onChange={handleChange} onChange={handleChange}
/> />
</div> </div>
@ -229,14 +276,19 @@ const EditProfile: FC = () => {
type="text" type="text"
placeholder="YouTube URL" placeholder="YouTube URL"
name="youtube" name="youtube"
value={youtube} value={formData.youtube}
onChange={handleChange} onChange={handleChange}
/> />
</div> </div>
</> </>
)} )}
<input type="submit" className="btn btn-primary my-1" value="Submit" /> <input
type="submit"
className="btn btn-primary my-1"
value="Submit"
disabled={isDisabled}
/>
<Link to={Routes.DASHBOARD} className="btn btn-light my-1"> <Link to={Routes.DASHBOARD} className="btn btn-light my-1">
Go Back Go Back
</Link> </Link>
@ -245,4 +297,4 @@ const EditProfile: FC = () => {
); );
}; };
export default EditProfile; export default enhance(EditProfile);

11
src/types/Links.ts Normal file
View file

@ -0,0 +1,11 @@
interface Links {
website: string;
instagram: string;
facebook: string;
linkedin: string;
twitter: string;
github: string;
youtube: string;
}
export default Links;