Commit 3642d07c authored by Bruno Antunes Magrini's avatar Bruno Antunes Magrini
Browse files

adiciona verificação de chave de api no bloco hacklab-video-playlist

parent e41d9f41
......@@ -7,6 +7,7 @@
import { TextControl, SelectControl, __experimentalNumberControl as NumberControl } from '@wordpress/components';
import { __ } from "@wordpress/i18n";
import React, {useCallback , useSelect, useState, useEffect } from 'react';
/**
* React hook that is used to mark the block wrapper element.
* It provides all the necessary props like the class name.
......@@ -27,17 +28,32 @@ import { useBlockProps } from '@wordpress/block-editor';
*
* @return {WPElement} Element to render.
*/
export default function Edit( {
attributes: {
title,
youtubeFormat,
youtubeId,
numItems,
style
import apiFetch from '@wordpress/api-fetch';
// GET
export default function Edit( {
attributes: {
title,
youtubeFormat,
youtubeId,
numItems,
style
},
setAttributes } ) {
const [youtubeKey, setyoutubeKey] = useState('');
useEffect(() => {
apiFetch( { path: 'hacklab-blocks/v1/options/' } ).then( ( option ) => {
setyoutubeKey(option);
} );
},
setAttributes } ) {
[]);
return (
<>
{ ! youtubeKey ?
<div>Preencha a chave do youtube nas configurações para utilizar o bloco video-playlist</div>
:
<div className="video-gallery-wrapper" key="container">
<div>
<TextControl
......@@ -93,6 +109,8 @@ export default function Edit( {
</div>
</div>
}
</>
);
}
......@@ -194,3 +194,20 @@ function hacklab_video_playlist_callback( $attributes = [] ) {
return $embeds;
}
// Cria endpoint para o pegar youtub id das options
function api_get_option( $data ) {
$option = get_option( 'youtube_key');
if ( empty( $option ) ) {
return null;
}
return $option;
}
add_action( 'rest_api_init', function () {
register_rest_route( 'hacklab-blocks/v1', '/options/', array(
'methods' => 'GET',
'callback' => 'hacklabBlocks\api_get_option',
'permission_callback' => function () {
return current_user_can( 'edit_others_posts' );
}
));
});
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment