From 0b8aaae9a4296d4e8df513104ca67a78d912918c Mon Sep 17 00:00:00 2001 From: Adam Butterworth Date: Fri, 22 Feb 2019 17:14:36 -0500 Subject: [PATCH] further --- src/components/UserProfile/SocialLinks.jsx | 247 ++++++++++++--------- 1 file changed, 139 insertions(+), 108 deletions(-) diff --git a/src/components/UserProfile/SocialLinks.jsx b/src/components/UserProfile/SocialLinks.jsx index 9ec3e49..646a6fb 100644 --- a/src/components/UserProfile/SocialLinks.jsx +++ b/src/components/UserProfile/SocialLinks.jsx @@ -39,108 +39,23 @@ class SocialLinks extends React.Component { } onInputChange(platform, value) { - console.log(platform, value) - this.setState({ [platform]: value}); - } - - renderEmpty() { - const { platforms } = this.props; - const onEdit = this.onEdit; - - return ( - - ); - } - - renderStatic(linkValues) { - const { platforms } = this.props; - const links = platforms.filter(({ key }) => Boolean(linkValues[key])); - - return ( - - -
    - {links.map(({ key, name}) => ( - - ))} -
-
- ); - } - - renderEditable(linkValues, isEmpty) { - const { visibility, platforms, showVisibility } = this.props; - const onEdit = this.onEdit; - - return ( - - -
    - {platforms.map(({ key, name }) => ( - - ))} -
-
- ); - } - - renderEditing(linkValues) { - const { platforms, saveState, visibility, onVisibilityChange, onCancel } = this.props; - const onInputChange = this.onInputChange; - - return ( - - -
    - {platforms.map(({ key, name }) => ( - - ))} -
- onCancel('socialLinks')} - onSave={this.onSave} - saveState={saveState} - visibility={visibility} - onVisibilityChange={e => onVisibilityChange('socialLinks', e.target.value)} - /> -
- ); + this.setState({ [platform]: value }); + this.props.onChange('socialLinks', this.state); } render() { - const { socialLinks, editMode } = this.props; - const socialLinksObj = socialLinks.reduce((acc, { platform, socialLink }) => { + const { + platforms, + socialLinks, + editMode, + visibility, + saveState, + onVisibilityChange, + onCancel, + + } = this.props; + const { onInputChange, onEdit } = this; + const socialLinksMap = socialLinks.reduce((acc, { platform, socialLink }) => { acc[platform] = socialLink; return acc; }, {}); @@ -151,10 +66,77 @@ class SocialLinks extends React.Component { className="mb-4" expression={editMode} cases={{ - empty: this.renderEmpty(), - static: this.renderStatic(socialLinksObj), - editing: this.renderEditing(socialLinksObj), - editable: this.renderEditable(socialLinksObj, isEmpty), + empty: ( + + ), + static: ( + + +
    + {socialLinks.map(({ platform, socialLink }) => ( + + ))} +
+
+ ), + editable: ( + + +
    + {platforms.map(({ key, name }) => ( + + ))} +
+
+ ), + editing: ( + + +
    + {platforms.map(({ key, name }) => ( + + ))} +
+ onCancel('socialLinks')} + onSave={this.onSave} + saveState={saveState} + visibility={visibility} + onVisibilityChange={e => onVisibilityChange('socialLinks', e.target.value)} + /> +
+ ), }} /> ); @@ -179,7 +161,6 @@ SocialLinks.propTypes = { onVisibilityChange: PropTypes.func.isRequired, saveState: PropTypes.string, }; - SocialLinks.defaultProps = { socialLinks: [], platforms: [ @@ -195,8 +176,6 @@ SocialLinks.defaultProps = { export default SocialLinks; - - function SocialLink({ url, name, platform }) { return ( @@ -206,7 +185,19 @@ function SocialLink({ url, name, platform }) { ); } -function EditableListItem({ url, platform, onEdit, name }) { +SocialLink.propTypes = { + url: PropTypes.string.isRequired, + platform: PropTypes.string.isRequired, + name: PropTypes.string.isRequired, +}; + + +function EditableListItem({ + url, + platform, + onEdit, + name, +}) { const linkDisplay = url != null ? : Add {name}; @@ -214,7 +205,24 @@ function EditableListItem({ url, platform, onEdit, name }) { return
  • {linkDisplay}
  • ; } -function EditingListItem({ platform, name, defaultValue, onChange }) { +EditableListItem.propTypes = { + url: PropTypes.string, + platform: PropTypes.string.isRequired, + name: PropTypes.string.isRequired, + onEdit: PropTypes.func, +}; +EditableListItem.defaultProps = { + url: null, + onEdit: null, +}; + + +function EditingListItem({ + platform, + name, + defaultValue, + onChange, +}) { return (
  • {name}
    @@ -227,6 +235,17 @@ function EditingListItem({ platform, name, defaultValue, onChange }) { ); } +EditingListItem.propTypes = { + platform: PropTypes.string.isRequired, + defaultValue: PropTypes.string, + name: PropTypes.string.isRequired, + onChange: PropTypes.func.isRequired, +}; +EditingListItem.defaultProps = { + defaultValue: null, +}; + + function EmptyListItem({ onClick, name }) { return (
  • @@ -235,6 +254,12 @@ function EmptyListItem({ onClick, name }) { ); } +EmptyListItem.propTypes = { + name: PropTypes.string.isRequired, + onClick: PropTypes.func.isRequired, +}; + + function StaticListItem({ name, url, platform }) { return (
  • @@ -242,3 +267,9 @@ function StaticListItem({ name, url, platform }) {
  • ); } + +StaticListItem.propTypes = { + name: PropTypes.string.isRequired, + url: PropTypes.string.isRequired, + platform: PropTypes.string.isRequired, +};