Efforts for making datasource parent and template work alongside in metadata blocks. #566.

This commit is contained in:
mateuswetah 2022-09-23 09:20:38 -03:00
parent f6e6faac9b
commit 0eda0da01d
5 changed files with 163 additions and 113 deletions

View File

@ -8,7 +8,7 @@ import SingleItemMetadataSectionModal from '../../js/selection/single-item-metad
import tainacan from '../../js/axios.js';
import axios from 'axios';
export default function ({ attributes, setAttributes, className, isSelected, context }) {
export default function ({ attributes, setAttributes, className, isSelected }) {
let {
content,
@ -120,7 +120,7 @@ export default function ({ attributes, setAttributes, className, isSelected, con
itemId: Number(itemId),
collectionId: Number(collectionId),
metadata: sectionMetadata,
dataSource: 'parent'
dataSource: dataSource
}
]);
}
@ -137,39 +137,43 @@ export default function ({ attributes, setAttributes, className, isSelected, con
}
function checkIfTemplateEdition() {
// Check custom template edition state
const queryParams = new URLSearchParams(window.location.search);
if (queryParams.get('postType') == 'wp_template') {
if (dataSource !== 'parent' && !collectionId) {
// Extracts collectionId from a string like theme-slug//single-tnc_col_123_item
let postId = queryParams.get('postId');
if (typeof postId == 'string') {
postId = postId.split('single-tnc_col_');
const queryParams = new URLSearchParams(window.location.search);
if (queryParams.get('postType') == 'wp_template') {
// Extracts collectionId from a string like theme-slug//single-tnc_col_123_item
let postId = queryParams.get('postId');
if (postId.length == 2) {
postId = postId[1];
if (typeof postId == 'string') {
postId = postId.split('single-tnc_col_');
if (postId.length == 2) {
postId = postId[1];
if (typeof postId == 'string') {
postId = postId.split('_item');
if (typeof postId == 'string') {
postId = postId.split('_item');
if (postId.length == 2) {
postId = postId[0];
if (postId.length == 2) {
postId = postId[0];
collectionId = Number(postId);
itemId = 0;
collectionId = Number(postId);
itemId = 0;
const shouldSetContent = dataSource !== 'template';
dataSource = 'template';
const shouldSetContent = dataSource !== 'template';
dataSource = 'template';
setAttributes({
collectionId: itemId,
itemId: itemId,
dataSource: dataSource
});
setAttributes({
collectionId: collectionId,
itemId: itemId,
dataSource: dataSource
});
if (shouldSetContent)
setContent();
if (shouldSetContent)
setContent();
}
}
}
}
@ -228,7 +232,7 @@ export default function ({ attributes, setAttributes, className, isSelected, con
) : null
}
{ !itemId && dataSource !== 'template' ? (
{ !sectionId && (dataSource !== 'parent' || dataSource !== 'template') ? (
<Placeholder
className="tainacan-block-placeholder"
icon={(

View File

@ -92,39 +92,43 @@ export default function ({ attributes, setAttributes, className, isSelected }) {
}
function checkIfTemplateEdition() {
// Check custom template edition state
const queryParams = new URLSearchParams(window.location.search);
if (queryParams.get('postType') == 'wp_template') {
if (dataSource !== 'parent' && !collectionId) {
// Extracts collectionId from a string like theme-slug//single-tnc_col_123_item
let postId = queryParams.get('postId');
if (typeof postId == 'string') {
postId = postId.split('single-tnc_col_');
const queryParams = new URLSearchParams(window.location.search);
if (queryParams.get('postType') == 'wp_template') {
// Extracts collectionId from a string like theme-slug//single-tnc_col_123_item
let postId = queryParams.get('postId');
if (postId.length == 2) {
postId = postId[1];
if (typeof postId == 'string') {
postId = postId.split('single-tnc_col_');
if (postId.length == 2) {
postId = postId[1];
if (typeof postId == 'string') {
postId = postId.split('_item');
if (typeof postId == 'string') {
postId = postId.split('_item');
if (postId.length == 2) {
postId = postId[0];
if (postId.length == 2) {
postId = postId[0];
collectionId = Number(postId);
itemId = 0;
collectionId = Number(postId);
itemId = 0;
const shouldSetContent = dataSource !== 'template';
dataSource = 'template';
const shouldSetContent = dataSource !== 'template';
dataSource = 'template';
setAttributes({
collectionId: itemId,
itemId: itemId,
dataSource: dataSource
});
setAttributes({
collectionId: collectionId,
itemId: itemId,
dataSource: dataSource
});
if (shouldSetContent)
setContent();
if (shouldSetContent)
setContent();
}
}
}
}

View File

@ -18,49 +18,89 @@ export default function ({ attributes, setAttributes, className, isSelected, con
itemMetadataRequestSource,
isModalOpen,
itemMetadata,
metadata,
itemMetadataTemplate,
dataSource,
sectionId
dataSource
} = attributes;
// Gets blocks props from hook
const blockProps = tainacan_blocks.wp_version < '5.6' ? { className: className } : useBlockProps();
checkIfTemplateEdition();
function setContent() {
isLoading = true;
setAttributes({
isLoading: isLoading
});
if (itemMetadataRequestSource != undefined && typeof itemMetadataRequestSource == 'function')
itemMetadataRequestSource.cancel('Previous metadata sections search canceled.');
itemMetadataRequestSource = axios.CancelToken.source();
let endpoint = '/item/' + itemId + '/metadata';
tainacan.get(endpoint, { cancelToken: itemMetadataRequestSource.token })
.then(response => {
itemMetadata = response.data ? response.data : [];
getItemMetadataTemplates({
metadata: itemMetadata.map(anItemMetadata => anItemMetadata.metadatum),
itemMetadata: itemMetadata,
itemMetadataRequestSource: itemMetadataRequestSource
});
})
.catch((error) => {
console.error(error);
setAttributes({
itemMetadata: [],
isLoading: false
});
if ( dataSource === 'parent' && metadata.length && itemMetadata.length) {
getItemMetadataTemplates({
metadata: metadata,
itemMetadata: itemMetadata,
itemMetadataRequestSource: itemMetadataRequestSource
});
} else if ( dataSource === 'template' ) {
if (itemMetadataRequestSource != undefined && typeof itemMetadataRequestSource == 'function')
itemMetadataRequestSource.cancel('Previous metadata sections search canceled.');
itemMetadataRequestSource = axios.CancelToken.source();
let endpoint = '/collection/' + collectionId + '/metadata';
tainacan.get(endpoint, { cancelToken: itemMetadataRequestSource.token })
.then(response => {
metadata = response.data ? response.data : [];
getItemMetadataTemplates({
metadata: metadata,
itemMetadata: [],
itemMetadataRequestSource: itemMetadataRequestSource
});
})
.catch((error) => {
console.error(error);
setAttributes({
metadata: [],
itemMetadata: [],
isLoading: false
});
});
} else {
if (itemMetadataRequestSource != undefined && typeof itemMetadataRequestSource == 'function')
itemMetadataRequestSource.cancel('Previous metadata sections search canceled.');
itemMetadataRequestSource = axios.CancelToken.source();
let endpoint = '/item/' + itemId + '/metadata';
tainacan.get(endpoint, { cancelToken: itemMetadataRequestSource.token })
.then(response => {
itemMetadata = response.data ? response.data : [];
getItemMetadataTemplates({
metadata: itemMetadata.map(anItemMetadata => anItemMetadata.metadatum),
itemMetadata: itemMetadata,
itemMetadataRequestSource: itemMetadataRequestSource
});
})
.catch((error) => {
console.error(error);
setAttributes({
metadata: [],
itemMetadata: [],
isLoading: false
});
});
}
}
function getItemMetadataTemplates({
@ -69,8 +109,7 @@ export default function ({ attributes, setAttributes, className, isSelected, con
itemMetadataRequestSource
}) {
let itemMetadataTemplate = [];
if (dataSource === 'template') {
if (dataSource === 'template' || (dataSource === 'parent' && !itemId)) {
metadata.forEach((aMetadatum) => {
itemMetadataTemplate.push([
'tainacan/item-metadatum',
@ -78,7 +117,7 @@ export default function ({ attributes, setAttributes, className, isSelected, con
placeholder: __( 'Item Metadatum', 'tainacan' ),
metadatumId: aMetadatum.id,
collectionId: Number(collectionId),
dataSource: 'template'
dataSource: dataSource
}
]);
});
@ -113,39 +152,42 @@ export default function ({ attributes, setAttributes, className, isSelected, con
}
function checkIfTemplateEdition() {
// Check custom template edition state
const queryParams = new URLSearchParams(window.location.search);
if (queryParams.get('postType') == 'wp_template') {
if (dataSource !== 'parent' && !collectionId) {
// Extracts collectionId from a string like theme-slug//single-tnc_col_123_item
let postId = queryParams.get('postId');
if (typeof postId == 'string') {
postId = postId.split('single-tnc_col_');
const queryParams = new URLSearchParams(window.location.search);
if (queryParams.get('postType') == 'wp_template') {
// Extracts collectionId from a string like theme-slug//single-tnc_col_123_item
let postId = queryParams.get('postId');
if (postId.length == 2) {
postId = postId[1];
if (typeof postId == 'string') {
postId = postId.split('single-tnc_col_');
if (postId.length == 2) {
postId = postId[1];
if (typeof postId == 'string') {
postId = postId.split('_item');
if (typeof postId == 'string') {
postId = postId.split('_item');
if (postId.length == 2) {
postId = postId[0];
if (postId.length == 2) {
postId = postId[0];
collectionId = Number(postId);
itemId = 0;
collectionId = Number(postId);
itemId = 0;
const shouldSetContent = dataSource !== 'template';
dataSource = 'template';
const shouldSetContent = dataSource !== 'template';
dataSource = 'template';
setAttributes({
collectionId: collectionId,
itemId: itemId,
dataSource: dataSource
});
setAttributes({
collectionId: itemId,
itemId: itemId,
dataSource: dataSource
});
if (shouldSetContent)
setContent();
if (shouldSetContent)
setContent();
}
}
}
}
@ -199,7 +241,7 @@ export default function ({ attributes, setAttributes, className, isSelected, con
) : null
}
{ !itemId && dataSource !== 'template' ? (
{ !itemId && dataSource !== 'template' && dataSource !== 'parent' ? (
<Placeholder
className="tainacan-block-placeholder"
icon={(

View File

@ -16,7 +16,7 @@ const levelToPath = {
6: 'M9 15H7v-4H3v4H1V5h2v4h4V5h2v10zm8.6-7.5c-.2-.2-.5-.4-.8-.5-.6-.2-1.3-.2-1.9 0-.3.1-.6.3-.8.5l-.6.9c-.2.5-.2.9-.2 1.4.4-.3.8-.6 1.2-.8.4-.2.8-.3 1.3-.3.4 0 .8 0 1.2.2.4.1.7.3 1 .6.3.3.5.6.7.9.2.4.3.8.3 1.3s-.1.9-.3 1.4c-.2.4-.5.7-.8 1-.4.3-.8.5-1.2.6-1 .3-2 .3-3 0-.5-.2-1-.5-1.4-.9-.4-.4-.8-.9-1-1.5-.2-.6-.3-1.3-.3-2.1s.1-1.6.4-2.3c.2-.6.6-1.2 1-1.6.4-.4.9-.7 1.4-.9.6-.3 1.1-.4 1.7-.4.7 0 1.4.1 2 .3.5.2 1 .5 1.4.8 0 .1-1.3 1.4-1.3 1.4zm-2.4 5.8c.2 0 .4 0 .6-.1.2 0 .4-.1.5-.2.1-.1.3-.3.4-.5.1-.2.1-.5.1-.7 0-.4-.1-.8-.4-1.1-.3-.2-.7-.3-1.1-.3-.3 0-.7.1-1 .2-.4.2-.7.4-1 .7 0 .3.1.7.3 1 .1.2.3.4.4.6.2.1.3.3.5.3.2.1.5.2.7.1z',
};
export default function ({ attributes, setAttributes, className, isSelected, context }) {
export default function ({ attributes, setAttributes, className, isSelected }) {
let {
content,
@ -30,7 +30,7 @@ export default function ({ attributes, setAttributes, className, isSelected, con
textAlign,
style
} = attributes;
// Gets blocks props from hook
const blockProps = tainacan_blocks.wp_version < '5.6' ? { className: className } : useBlockProps( {
className: {
@ -193,7 +193,7 @@ export default function ({ attributes, setAttributes, className, isSelected, con
) : null
}
{ (collectionId && (itemId || dataSource == 'template') && metadatumId) ? (
{ (collectionId && (itemId || dataSource === 'template' || dataSource === 'parent') && metadatumId) ? (
<div className={ 'item-metadatum-edit-container' }>
<ServerSideRender
block="tainacan/item-metadatum"

View File

@ -43,7 +43,7 @@ function tainacan_blocks_render_item_metadatum( $block_attributes, $content, $bl
// Checks if we are in the edit page or in the published
$current_post = get_post();
if ( $data_source === 'template' && $collection_id ) {
if ( ($data_source === 'template' || ($data_source === 'parent' && !$item_id) ) && $collection_id ) {
$collection_pt_pattern = '/' . \Tainacan\Entities\Collection::$db_identifier_prefix . '\d+' . \Tainacan\Entities\Collection::$db_identifier_sufix . '/';
if ( $current_post === NULL )