| 1 |
/** |
| 2 |
* @link https://codex.wordpress.org/Javascript_Reference/wp.media |
| 3 |
* @link https://wordpress.stackexchange.com/a/382291 |
| 4 |
*/ |
| 5 |
|
| 6 |
/** |
| 7 |
* External Dependencies |
| 8 |
*/ |
| 9 |
import _ from 'lodash'; |
| 10 |
|
| 11 |
/** |
| 12 |
* WordPress Dependencies |
| 13 |
*/ |
| 14 |
import {__, sprintf} from '@wordpress/i18n'; |
| 15 |
|
| 16 |
/** |
| 17 |
* Internal Dependencies |
| 18 |
*/ |
| 19 |
import { ProfileIcon } from '@givewp/components/AdminDetailsPage/Icons'; |
| 20 |
import styles from './styles.module.scss'; |
| 21 |
|
| 22 |
/** |
| 23 |
* @since 4.4.0 |
| 24 |
*/ |
| 25 |
type MediaLibrary = { |
| 26 |
id: string; |
| 27 |
value: string; |
| 28 |
onChange: (id: string, url: string, alt: string) => void; |
| 29 |
reset: () => void; |
| 30 |
label: string; |
| 31 |
}; |
| 32 |
|
| 33 |
/** |
| 34 |
* @since 4.4.0 |
| 35 |
*/ |
| 36 |
export default function UploadMedia({id, value, onChange, label, reset}: MediaLibrary) { |
| 37 |
// The media library uses Backbone.js, which can conflict with lodash. |
| 38 |
_.noConflict(); |
| 39 |
let frame; |
| 40 |
|
| 41 |
const openMediaLibrary = (event) => { |
| 42 |
event.preventDefault(); |
| 43 |
|
| 44 |
if (frame) { |
| 45 |
frame.open(); |
| 46 |
return; |
| 47 |
} |
| 48 |
|
| 49 |
frame = window.wp.media({ |
| 50 |
title: __('Upload Media', 'give'), |
| 51 |
button: { |
| 52 |
text: __('Use this media', 'gie'), |
| 53 |
}, |
| 54 |
library: { |
| 55 |
type: 'image', // Restricts media library to image files only |
| 56 |
}, |
| 57 |
multiple: false, // Set to true to allow multiple files to be selected |
| 58 |
}); |
| 59 |
|
| 60 |
frame.on('select', function () { |
| 61 |
// Get media attachment details from the frame state |
| 62 |
var attachment = frame.state().get('selection').first().toJSON(); |
| 63 |
|
| 64 |
if (!attachment.type || attachment.type !== 'image') { |
| 65 |
alert(__('Please select an image file only.', 'give')); |
| 66 |
frame.open(); |
| 67 |
return; |
| 68 |
} |
| 69 |
|
| 70 |
onChange(attachment.id, attachment.url, attachment.alt); |
| 71 |
}); |
| 72 |
|
| 73 |
// Finally, open the modal on click |
| 74 |
frame.open(); |
| 75 |
}; |
| 76 |
|
| 77 |
const resetImage = () => { |
| 78 |
reset(); |
| 79 |
}; |
| 80 |
|
| 81 |
return ( |
| 82 |
<div id={id} className={styles.controlWrapper}> |
| 83 |
<div className={styles.control}> |
| 84 |
{value ? ( |
| 85 |
<img |
| 86 |
className={styles.image} |
| 87 |
src={value} |
| 88 |
alt={__('uploaded image', 'give')} |
| 89 |
/> |
| 90 |
) : ( |
| 91 |
<ProfileIcon /> |
| 92 |
)} |
| 93 |
</div> |
| 94 |
|
| 95 |
<div className={styles.options}> |
| 96 |
<button |
| 97 |
className={styles.update} |
| 98 |
onClick={openMediaLibrary} |
| 99 |
> |
| 100 |
{sprintf(value ? __('Change %s', 'give') : __('Upload %s', 'give'), label.toLowerCase())} |
| 101 |
</button> |
| 102 |
{value && ( |
| 103 |
<button |
| 104 |
className={styles.remove} |
| 105 |
onClick={resetImage} |
| 106 |
> |
| 107 |
{sprintf(__('Remove %s', 'give'), label.toLowerCase())} |
| 108 |
</button> |
| 109 |
)} |
| 110 |
</div> |
| 111 |
</div> |
| 112 |
); |
| 113 |
} |
| 114 |
|