| 1 |
import React, { Component } from "react"; |
| 2 |
import PropTypes from "prop-types"; |
| 3 |
import FontIconPicker from "@fonticonpicker/react-fonticonpicker"; |
| 4 |
import arrayMove from "array-move"; |
| 5 |
import SortableComponent from "./SortableComponent"; |
| 6 |
// import "./style.scss"; |
| 7 |
|
| 8 |
class SocialProfiles extends Component { |
| 9 |
state = { |
| 10 |
iconList: this.props.iconList, |
| 11 |
profiles: this.props.profiles || [], //example profiles: [{ icon: 'facebook', link: 'www.fb.com/john', isExpanded: false}] |
| 12 |
selectedIcon: null, |
| 13 |
url: "", |
| 14 |
color: "", |
| 15 |
}; |
| 16 |
|
| 17 |
onSelectIcon = (selectedIcon) => { |
| 18 |
// When a social profile icon is selected, store it in state and pass it |
| 19 |
// to the callback function |
| 20 |
|
| 21 |
if (selectedIcon) { |
| 22 |
let icon = selectedIcon.replace(/^fab fa-/i, ""); |
| 23 |
let profiles = [ |
| 24 |
...this.state.profiles, |
| 25 |
{ icon: icon, color: "", link: "", isExpanded: false }, |
| 26 |
]; |
| 27 |
|
| 28 |
this.setState({ profiles, selectedIcon }); |
| 29 |
this.props.onProfileAdd(profiles); |
| 30 |
} |
| 31 |
}; |
| 32 |
|
| 33 |
onDeleteProfile = (position) => { |
| 34 |
// Remove clicked social profile, store rest of the |
| 35 |
// profiles in state, and pass deleted profile name to the callback function |
| 36 |
let profiles = [...this.state.profiles]; |
| 37 |
profiles.splice(position, 1); |
| 38 |
|
| 39 |
this.setState({ profiles }); |
| 40 |
this.props.onProfileAdd(profiles); |
| 41 |
}; |
| 42 |
|
| 43 |
onProfileClick = (icon) => { |
| 44 |
// When a profile is clicked, expand/collapse link input form and |
| 45 |
// store profile icon name, url in state |
| 46 |
let profiles = [...this.state.profiles]; |
| 47 |
let url = this.state.url; |
| 48 |
let selectedIcon = icon; |
| 49 |
let color = this.state.color; |
| 50 |
|
| 51 |
profiles = profiles.map((profile) => { |
| 52 |
if (profile.icon === icon) { |
| 53 |
url = profile.link; |
| 54 |
color = profile.color; |
| 55 |
return { ...profile, isExpanded: !profile.isExpanded }; |
| 56 |
} |
| 57 |
|
| 58 |
return { ...profile, isExpanded: false }; |
| 59 |
}); |
| 60 |
|
| 61 |
this.setState({ profiles, selectedIcon, url, color }); |
| 62 |
}; |
| 63 |
|
| 64 |
onUrlChange = (event) => this.setState({ url: event.target.value }); |
| 65 |
|
| 66 |
onSubmit = (icon) => { |
| 67 |
// When new link is submitted, store it in profile object, collapse input form and |
| 68 |
// pass updated profiles to callback function |
| 69 |
let profiles = [...this.state.profiles]; |
| 70 |
let url = this.state.url; |
| 71 |
|
| 72 |
profiles = profiles.map((profile) => |
| 73 |
profile.icon === icon |
| 74 |
? { ...profile, link: url, isExpanded: false } |
| 75 |
: profile |
| 76 |
); |
| 77 |
|
| 78 |
this.setState({ profiles, url: "" }); |
| 79 |
this.props.onProfileAdd(profiles); |
| 80 |
}; |
| 81 |
|
| 82 |
onSortEnd = ({ oldIndex, newIndex }) => { |
| 83 |
// Rearrange profiles array after drag and drop, pass |
| 84 |
// updated array to edit view |
| 85 |
let profiles = arrayMove(this.state.profiles, oldIndex, newIndex); |
| 86 |
|
| 87 |
this.setState({ profiles }); |
| 88 |
this.props.onProfileAdd(profiles); |
| 89 |
}; |
| 90 |
|
| 91 |
onColorChange = (color, index) => { |
| 92 |
let profiles = [...this.state.profiles]; |
| 93 |
profiles[index].color = color; |
| 94 |
|
| 95 |
this.setState({ profiles }); |
| 96 |
this.props.onProfileAdd(profiles); |
| 97 |
}; |
| 98 |
|
| 99 |
render() { |
| 100 |
const { iconList, selectedIcon, url, profiles } = this.state; |
| 101 |
|
| 102 |
return ( |
| 103 |
<div> |
| 104 |
<label>Social Media</label> |
| 105 |
<FontIconPicker |
| 106 |
icons={iconList} |
| 107 |
value={selectedIcon ? `fab fa-${selectedIcon}` : null} |
| 108 |
onChange={this.onSelectIcon} |
| 109 |
appendTo="body" |
| 110 |
iconsPerPage={25} |
| 111 |
closeOnSelect |
| 112 |
/> |
| 113 |
|
| 114 |
<SortableComponent |
| 115 |
profiles={profiles} |
| 116 |
onProfileClick={this.onProfileClick} |
| 117 |
onDeleteProfile={this.onDeleteProfile} |
| 118 |
selectedIcon={selectedIcon} |
| 119 |
url={url} |
| 120 |
onUrlChange={this.onUrlChange} |
| 121 |
onSubmit={this.onSubmit} |
| 122 |
onProfileAdd={this.onProfileAdd} |
| 123 |
onSortEnd={this.onSortEnd} |
| 124 |
onColorChange={this.onColorChange} |
| 125 |
/> |
| 126 |
</div> |
| 127 |
); |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
SocialProfiles.propTypes = { |
| 132 |
iconList: PropTypes.array.isRequired, |
| 133 |
profiles: PropTypes.shape({ |
| 134 |
icon: PropTypes.string.isRequired, |
| 135 |
color: PropTypes.string, |
| 136 |
link: PropTypes.string, |
| 137 |
isExpanded: PropTypes.bool.isRequired, |
| 138 |
}), |
| 139 |
onProfileAdd: PropTypes.func.isRequired, |
| 140 |
}; |
| 141 |
|
| 142 |
export default SocialProfiles; |
| 143 |
|