Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

File Field: Cache data when picking #1086

Open
wants to merge 3 commits into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 4 additions & 6 deletions core/REST_API/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class Router {
'path' => '/attachment',
'callback' => 'get_attachment_data',
'permission_callback' => 'allow_access',
'methods' => 'GET',
'methods' => array( 'GET', 'POST' ),
'args' => 'attachment_data_args_schema',
),
'block_renderer' => array(
Expand Down Expand Up @@ -368,11 +368,9 @@ public function get_association_options() {
*
* @return array
*/
public function get_attachment_data() {
$type = sanitize_text_field( $_GET['type'] );
$value = sanitize_text_field( $_GET['value'] );

return Helper::get_attachment_metadata( $value, $type );
public function get_attachment_data( $request ) {
$options = $request->get_params();
return Helper::get_attachment_metadata( $options['value'], $options['type'] );
}

/**
Expand Down
87 changes: 67 additions & 20 deletions packages/core/fields/file/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { get } from 'lodash';
*/
import './style.scss';
import MediaLibrary from '../../components/media-library';
import apiFetch from '../../utils/api-fetch';
import apiFetch from '@wordpress/api-fetch';

class FileField extends Component {
/**
Expand All @@ -27,25 +27,55 @@ class FileField extends Component {
* @return {void}
*/
componentDidMount() {
const { value, field } = this.props;

if ( value ) {
let endpoint = '';

if ( window.wpApiSettings.root.indexOf( '?rest_route' ) !== -1 ) {
endpoint = `${ window.wpApiSettings.root }carbon-fields/v1/attachment&type=${ field.value_type }&value=${ value }`;
} else {
endpoint = `${ window.wpApiSettings.root }carbon-fields/v1/attachment?type=${ field.value_type }&value=${ value }`;
}

// TODO: Refactor this to use `@wordpress/api-fetch` package.
apiFetch(
endpoint,
'get'
).then( this.handleFileDataChange );
}
this.fetchMetadata();
}

getCacheKey(field, value) {
let cacheKey = 'cf_filefield_cache_';
if (field.value_type === 'id') {
cacheKey += value;
} else {
cacheKey += btoa(value);
}

return cacheKey;
}

getCachedMetadata(cacheKey) {
return JSON.parse(localStorage.getItem(cacheKey));
}

setCachedMetadata(cacheKey, value) {
localStorage.setItem(cacheKey, JSON.stringify(value));
}

fetchMetadata() {
const { value, field } = this.props;

if (value) {
let cacheKey = this.getCacheKey(field, value);
let data = this.getCachedMetadata(cacheKey);

if (data !== null) {
this.handleFileDataChange(data);
return;
}

apiFetch({
method: 'post',
path: '/wp-json/carbon-fields/v1/attachment',
data: {
type: field.value_type,
value: value
}
}).then( (data) => {
this.setCachedMetadata(cacheKey, data);
this.handleFileDataChange(data);
});

}
}

/**
* Returns an URL to the attachment's thumbnail.
*
Expand Down Expand Up @@ -117,10 +147,27 @@ class FileField extends Component {
} = this.props;

const [ file ] = files;

onChange( id, get( file, field.value_type, file.id ) );

this.handleFileDataChange( file );
let value;
switch (field.value_type) {
case 'id':
value = file.id;
break;
case 'url':
value = file.url;
break;
default:
break;
}

this.setCachedMetadata(this.getCacheKey(field, value), {
id: file.id,
filename: file.filename,
sizes: file.sizes
});

this.handleFileDataChange( file );
}

/**
Expand Down
2 changes: 0 additions & 2 deletions packages/metaboxes/components/container/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,6 @@ class Container extends Component {
[ `cf-container--tabbed cf-container--${ container.layout }` ]: hasTabs
}
] );

return (
<div className={ classes }>
<input
Expand Down Expand Up @@ -156,7 +155,6 @@ class Container extends Component {
<div className="cf-container__fields" key={ tabName } hidden={ tabName !== currentTab }>
{ map( fieldNames, ( fieldName ) => {
const field = find( container.fields, [ 'name', fieldName ] );

return this.renderField( field );
} ) }
</div>
Expand Down