Skip to content

Commit

Permalink
Merge pull request #78 from bushblade/socialurls
Browse files Browse the repository at this point in the history
Fix issues with social urls and skills array/string
  • Loading branch information
bradtraversy committed Feb 15, 2020
2 parents ac4e339 + 4d38b62 commit c56f8e9
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 30 deletions.
3 changes: 3 additions & 0 deletions client/src/components/profile-forms/EditProfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ const EditProfile = ({
for (const key in profile) {
if (key in profileData) profileData[key] = profile[key];
}
for (const key in profile.social) {
if (key in profileData) profileData[key] = profile.social[key];
}
setFormData(profileData);
}
}, [loading, getCurrentProfile, profile]);
Expand Down
19 changes: 13 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"gravatar": "^1.8.0",
"jsonwebtoken": "^8.5.1",
"mongoose": "^5.8.10",
"normalize-url": "^5.0.0",
"request": "^2.88.0"
},
"devDependencies": {
Expand Down
50 changes: 26 additions & 24 deletions routes/api/profile.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ const config = require('config');
const router = express.Router();
const auth = require('../../middleware/auth');
const { check, validationResult } = require('express-validator');
// bring in normalize to give us a proper url, regardless of what user entered
const normalize = require('normalize-url');

const Profile = require('../../models/Profile');
const User = require('../../models/User');
Expand Down Expand Up @@ -50,42 +52,42 @@ router.post(
if (!errors.isEmpty()) {
return res.status(400).json({ errors: errors.array() });
}

const {
company,
website,
location,
website,
bio,
skills,
status,
githubusername,
skills,
youtube,
facebook,
twitter,
instagram,
linkedin
linkedin,
facebook
} = req.body;

// Build profile object
const profileFields = {};
profileFields.user = req.user.id;
if (company) profileFields.company = company;
if (website) profileFields.website = website;
if (location) profileFields.location = location;
if (bio) profileFields.bio = bio;
if (status) profileFields.status = status;
if (githubusername) profileFields.githubusername = githubusername;
if (skills) {
profileFields.skills = skills.split(',').map(skill => skill.trim());
}
const profileFields = {
user: req.user.id,
company,
location,
website: website === '' ? '' : normalize(website, { forceHttps: true }),
bio,
skills: Array.isArray(skills)
? skills
: skills.split(',').map(skill => ' ' + skill.trim()),
status,
githubusername
};

// Build social object and add to profileFields
const socialfields = { youtube, twitter, instagram, linkedin, facebook };

// Build social object
profileFields.social = {};
if (youtube) profileFields.social.youtube = youtube;
if (twitter) profileFields.social.twitter = twitter;
if (facebook) profileFields.social.facebook = facebook;
if (linkedin) profileFields.social.linkedin = linkedin;
if (instagram) profileFields.social.instagram = instagram;
for (const [key, value] of Object.entries(socialfields)) {
if (value.length > 0)
socialfields[key] = normalize(value, { forceHttps: true });
}
profileFields.social = socialfields;

try {
// Using upsert option (creates new doc if no match is found):
Expand Down

0 comments on commit c56f8e9

Please sign in to comment.