| 1 |
import classnames from 'classnames/dedupe'; |
| 2 |
|
| 3 |
import { InspectorControls, useBlockProps } from '@wordpress/block-editor'; |
| 4 |
import { |
| 5 |
ExternalLink, |
| 6 |
PanelBody, |
| 7 |
Placeholder, |
| 8 |
Spinner, |
| 9 |
TextControl, |
| 10 |
ToggleControl, |
| 11 |
} from '@wordpress/components'; |
| 12 |
import { useSelect } from '@wordpress/data'; |
| 13 |
import { applyFilters } from '@wordpress/hooks'; |
| 14 |
import { __ } from '@wordpress/i18n'; |
| 15 |
|
| 16 |
import GapSettings from '../../components/gap-settings'; |
| 17 |
import RangeControl from '../../components/range-control'; |
| 18 |
import getIcon from '../../utils/get-icon'; |
| 19 |
|
| 20 |
/** |
| 21 |
* Block Edit Class. |
| 22 |
* |
| 23 |
* @param props |
| 24 |
*/ |
| 25 |
export default function BlockEdit(props) { |
| 26 |
const { setAttributes, attributes } = props; |
| 27 |
let { className } = props; |
| 28 |
|
| 29 |
const { |
| 30 |
accessToken, |
| 31 |
count, |
| 32 |
columns, |
| 33 |
gap, |
| 34 |
gapCustom, |
| 35 |
showProfileAvatar, |
| 36 |
profileAvatarSize, |
| 37 |
showProfileName, |
| 38 |
showProfileStats, |
| 39 |
showProfileBio, |
| 40 |
showProfileWebsite, |
| 41 |
} = attributes; |
| 42 |
|
| 43 |
const { instagramFeed, instagramProfile } = useSelect((select) => { |
| 44 |
if (!accessToken) { |
| 45 |
return false; |
| 46 |
} |
| 47 |
|
| 48 |
return { |
| 49 |
instagramFeed: select('ghostkit/blocks/instagram').getInstagramFeed( |
| 50 |
{ |
| 51 |
access_token: accessToken, |
| 52 |
count, |
| 53 |
} |
| 54 |
), |
| 55 |
instagramProfile: select( |
| 56 |
'ghostkit/blocks/instagram' |
| 57 |
).getInstagramProfile({ |
| 58 |
access_token: accessToken, |
| 59 |
}), |
| 60 |
}; |
| 61 |
}); |
| 62 |
|
| 63 |
const showProfile = |
| 64 |
attributes.showProfile && |
| 65 |
(showProfileName || |
| 66 |
showProfileAvatar || |
| 67 |
showProfileBio || |
| 68 |
showProfileWebsite || |
| 69 |
showProfileStats); |
| 70 |
|
| 71 |
className = classnames( |
| 72 |
'ghostkit-instagram', |
| 73 |
gap && `ghostkit-instagram-gap-${gap}`, |
| 74 |
columns && `ghostkit-instagram-columns-${columns}`, |
| 75 |
className |
| 76 |
); |
| 77 |
|
| 78 |
className = applyFilters('ghostkit.editor.className', className, props); |
| 79 |
|
| 80 |
const blockProps = useBlockProps({ className }); |
| 81 |
|
| 82 |
return ( |
| 83 |
<> |
| 84 |
<InspectorControls> |
| 85 |
{accessToken ? ( |
| 86 |
<> |
| 87 |
<PanelBody> |
| 88 |
<RangeControl |
| 89 |
label={__('Photos Number', 'ghostkit')} |
| 90 |
value={count} |
| 91 |
onChange={(value) => |
| 92 |
setAttributes({ count: value }) |
| 93 |
} |
| 94 |
min={1} |
| 95 |
max={20} |
| 96 |
allowCustomMax |
| 97 |
/> |
| 98 |
<RangeControl |
| 99 |
label={__('Columns', 'ghostkit')} |
| 100 |
value={columns} |
| 101 |
onChange={(value) => |
| 102 |
setAttributes({ columns: value }) |
| 103 |
} |
| 104 |
min={1} |
| 105 |
max={8} |
| 106 |
allowCustomMax |
| 107 |
/> |
| 108 |
</PanelBody> |
| 109 |
<PanelBody> |
| 110 |
<GapSettings |
| 111 |
gap={gap} |
| 112 |
gapCustom={gapCustom} |
| 113 |
onChange={(data) => { |
| 114 |
setAttributes(data); |
| 115 |
}} |
| 116 |
/> |
| 117 |
</PanelBody> |
| 118 |
<PanelBody title={__('Profile Info', 'ghostkit')}> |
| 119 |
<ToggleControl |
| 120 |
label={__('Show Profile Info', 'ghostkit')} |
| 121 |
checked={!!attributes.showProfile} |
| 122 |
onChange={(val) => |
| 123 |
setAttributes({ showProfile: val }) |
| 124 |
} |
| 125 |
/> |
| 126 |
{attributes.showProfile ? ( |
| 127 |
<> |
| 128 |
<ToggleControl |
| 129 |
label={__('Show Avatar', 'ghostkit')} |
| 130 |
checked={!!showProfileAvatar} |
| 131 |
onChange={(val) => |
| 132 |
setAttributes({ |
| 133 |
showProfileAvatar: val, |
| 134 |
}) |
| 135 |
} |
| 136 |
/> |
| 137 |
{showProfileAvatar ? ( |
| 138 |
<RangeControl |
| 139 |
label={__( |
| 140 |
'Avatar Size', |
| 141 |
'ghostkit' |
| 142 |
)} |
| 143 |
value={profileAvatarSize} |
| 144 |
onChange={(value) => |
| 145 |
setAttributes({ |
| 146 |
profileAvatarSize: value, |
| 147 |
}) |
| 148 |
} |
| 149 |
min={30} |
| 150 |
max={150} |
| 151 |
allowCustomMin |
| 152 |
allowCustomMax |
| 153 |
/> |
| 154 |
) : null} |
| 155 |
<ToggleControl |
| 156 |
label={__('Show Name', 'ghostkit')} |
| 157 |
checked={!!showProfileName} |
| 158 |
onChange={(val) => |
| 159 |
setAttributes({ |
| 160 |
showProfileName: val, |
| 161 |
}) |
| 162 |
} |
| 163 |
/> |
| 164 |
<ToggleControl |
| 165 |
label={__('Show Stats', 'ghostkit')} |
| 166 |
checked={!!showProfileStats} |
| 167 |
onChange={(val) => |
| 168 |
setAttributes({ |
| 169 |
showProfileStats: val, |
| 170 |
}) |
| 171 |
} |
| 172 |
/> |
| 173 |
<ToggleControl |
| 174 |
label={__('Show BIO', 'ghostkit')} |
| 175 |
checked={!!showProfileBio} |
| 176 |
onChange={(val) => |
| 177 |
setAttributes({ |
| 178 |
showProfileBio: val, |
| 179 |
}) |
| 180 |
} |
| 181 |
/> |
| 182 |
<ToggleControl |
| 183 |
label={__('Show Website', 'ghostkit')} |
| 184 |
checked={!!showProfileWebsite} |
| 185 |
onChange={(val) => |
| 186 |
setAttributes({ |
| 187 |
showProfileWebsite: val, |
| 188 |
}) |
| 189 |
} |
| 190 |
/> |
| 191 |
</> |
| 192 |
) : null} |
| 193 |
</PanelBody> |
| 194 |
</> |
| 195 |
) : null} |
| 196 |
|
| 197 |
<PanelBody |
| 198 |
title={__('API Data', 'ghostkit')} |
| 199 |
initialOpen={!accessToken} |
| 200 |
> |
| 201 |
<TextControl |
| 202 |
placeholder={__('Access Token', 'ghostkit')} |
| 203 |
value={accessToken} |
| 204 |
onChange={(value) => |
| 205 |
setAttributes({ accessToken: value }) |
| 206 |
} |
| 207 |
/> |
| 208 |
<p> |
| 209 |
<em> |
| 210 |
{__( |
| 211 |
'A valid Access Token is required to use Instagram feed. How to get token', |
| 212 |
'ghostkit' |
| 213 |
)}{' '} |
| 214 |
<ExternalLink href="http://instagram.pixelunion.net/"> |
| 215 |
http://instagram.pixelunion.net/ |
| 216 |
</ExternalLink> |
| 217 |
</em> |
| 218 |
</p> |
| 219 |
</PanelBody> |
| 220 |
</InspectorControls> |
| 221 |
<div {...blockProps}> |
| 222 |
{accessToken && |
| 223 |
showProfile && |
| 224 |
instagramProfile && |
| 225 |
instagramProfile.data ? ( |
| 226 |
<div className="ghostkit-instagram-profile"> |
| 227 |
{showProfileAvatar && |
| 228 |
instagramProfile.data.profile_picture ? ( |
| 229 |
<div className="ghostkit-instagram-profile-avatar"> |
| 230 |
<img |
| 231 |
src={instagramProfile.data.profile_picture} |
| 232 |
alt={instagramProfile.data.full_name} |
| 233 |
width={profileAvatarSize} |
| 234 |
height={profileAvatarSize} |
| 235 |
/> |
| 236 |
</div> |
| 237 |
) : null} |
| 238 |
<div className="ghostkit-instagram-profile-side"> |
| 239 |
{showProfileName && |
| 240 |
instagramProfile.data.username ? ( |
| 241 |
<div className="ghostkit-instagram-profile-name"> |
| 242 |
{instagramProfile.data.username} |
| 243 |
</div> |
| 244 |
) : null} |
| 245 |
{showProfileStats && |
| 246 |
instagramProfile.data.counts ? ( |
| 247 |
<div className="ghostkit-instagram-profile-stats"> |
| 248 |
<div> |
| 249 |
<strong> |
| 250 |
{instagramProfile.data.counts.media} |
| 251 |
</strong>{' '} |
| 252 |
<span>{__('Posts', 'ghostkit')}</span> |
| 253 |
</div> |
| 254 |
<div> |
| 255 |
<strong> |
| 256 |
{ |
| 257 |
instagramProfile.data.counts |
| 258 |
.followed_by |
| 259 |
} |
| 260 |
</strong>{' '} |
| 261 |
<span> |
| 262 |
{__('Followers', 'ghostkit')} |
| 263 |
</span> |
| 264 |
</div> |
| 265 |
<div> |
| 266 |
<strong> |
| 267 |
{ |
| 268 |
instagramProfile.data.counts |
| 269 |
.follows |
| 270 |
} |
| 271 |
</strong>{' '} |
| 272 |
<span> |
| 273 |
{__('Following', 'ghostkit')} |
| 274 |
</span> |
| 275 |
</div> |
| 276 |
</div> |
| 277 |
) : null} |
| 278 |
{showProfileBio && instagramProfile.data.bio ? ( |
| 279 |
<div className="ghostkit-instagram-profile-bio"> |
| 280 |
<h2>{instagramProfile.data.full_name}</h2> |
| 281 |
<div>{instagramProfile.data.bio}</div> |
| 282 |
</div> |
| 283 |
) : null} |
| 284 |
{showProfileWebsite && |
| 285 |
instagramProfile.data.website ? ( |
| 286 |
<div className="ghostkit-instagram-profile-website"> |
| 287 |
<a href={instagramProfile.data.website}> |
| 288 |
{instagramProfile.data.website} |
| 289 |
</a> |
| 290 |
</div> |
| 291 |
) : null} |
| 292 |
</div> |
| 293 |
</div> |
| 294 |
) : null} |
| 295 |
{accessToken && instagramFeed && instagramFeed.data ? ( |
| 296 |
<div className="ghostkit-instagram-items"> |
| 297 |
{instagramFeed.data.map((item, i) => { |
| 298 |
const itemName = `instagram-item-${i}`; |
| 299 |
|
| 300 |
return ( |
| 301 |
<div |
| 302 |
className="ghostkit-instagram-item" |
| 303 |
key={itemName} |
| 304 |
> |
| 305 |
<span> |
| 306 |
<img |
| 307 |
src={ |
| 308 |
item.images.standard_resolution |
| 309 |
.url |
| 310 |
} |
| 311 |
width={ |
| 312 |
item.images.standard_resolution |
| 313 |
.width |
| 314 |
} |
| 315 |
height={ |
| 316 |
item.images.standard_resolution |
| 317 |
.height |
| 318 |
} |
| 319 |
alt={item.caption || ''} |
| 320 |
/> |
| 321 |
</span> |
| 322 |
</div> |
| 323 |
); |
| 324 |
})} |
| 325 |
</div> |
| 326 |
) : null} |
| 327 |
{accessToken && (!instagramFeed || !instagramFeed.data) ? ( |
| 328 |
<div className="ghostkit-instagram-spinner"> |
| 329 |
<Spinner /> |
| 330 |
</div> |
| 331 |
) : null} |
| 332 |
{!accessToken ? ( |
| 333 |
<Placeholder |
| 334 |
icon={getIcon('block-instagram')} |
| 335 |
label={__('Instagram', 'ghostkit')} |
| 336 |
instructions={__( |
| 337 |
'A valid Access Token is required to use Instagram feed. You can fill it in the block settings in Inspector.', |
| 338 |
'ghostkit' |
| 339 |
)} |
| 340 |
className={className} |
| 341 |
/> |
| 342 |
) : null} |
| 343 |
</div> |
| 344 |
</> |
| 345 |
); |
| 346 |
} |
| 347 |
|