Skip to content

Commit

Permalink
Fix Analytics Tracking Sidebar component
Browse files Browse the repository at this point in the history
- It should be merged before we move to WP6.5
  • Loading branch information
dantovbein authored and comzeradd committed May 20, 2024
1 parent 7476fe5 commit f1791c5
Showing 1 changed file with 32 additions and 39 deletions.
71 changes: 32 additions & 39 deletions assets/src/block-editor/Sidebar/AnalyticsTrackingSidebar.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import {useEffect} from '@wordpress/element';
import {PluginDocumentSettingPanel} from '@wordpress/edit-post';
import {dispatch, useSelect} from '@wordpress/data';
import {SelectSidebarField} from '../SidebarFields/SelectSidebarField';
Expand All @@ -6,54 +7,46 @@ import {getSidebarFunctions} from './getSidebarFunctions';

const {__} = wp.i18n;

dispatch('core').addEntities([{
baseURL: '/planet4/v1/analytics-values',
kind: 'planet4/v1',
name: 'analytics-values',
label: 'Analytics and tracking values',
}]);

const GLOBAL_PROJECT = 'p4_campaign_name';
const LOCAL_PROJECT = 'p4_local_project';
const BASKET = 'p4_basket_name';
const DEPARTMENT = 'p4_department';

export const AnalyticsTrackingSidebar = {
getId: () => 'planet4-analytics-sidebar',
render: () => {
const {getParams} = getSidebarFunctions();
const postId = wp.data.select('core/editor').getCurrentPostId();
const options = useSelect(select => {
return select('core').getEntityRecords('planet4/v1', 'analytics-values', {id: postId});
return select('core').getEntityRecord('planet4/v1', 'analytics-values');
});

const globalOptions = options ? options[0].global_projects : [];
const localOptions = options ? options[0].local_projects : [];
const basketOptions = options ? options[0].baskets : [];
useEffect(() => {
dispatch('core').addEntities([{
baseURL: '/planet4/v1/analytics-values',
baseURLParams: {post_id: postId},
kind: 'planet4/v1',
name: 'analytics-values',
label: 'Analytics and tracking values',
}]);
}, [postId]);

return (
<>
<PluginDocumentSettingPanel
name="analytics-panel"
title={__('Analytics & Tracking', 'planet4-blocks-backend')}
>
<SelectSidebarField
label={__('Global Project', 'planet4-master-theme-backend')}
options={globalOptions}
{...getParams(GLOBAL_PROJECT)} />
<SelectSidebarField
label={__('Local Projects', 'planet4-master-theme-backend')}
options={localOptions}
{...getParams(LOCAL_PROJECT)} />
<SelectSidebarField
label={__('Basket Name', 'planet4-master-theme-backend')}
options={basketOptions}
{...getParams(BASKET)} />
<TextSidebarField
label={__('Department', 'planet4-master-theme-backend')}
{...getParams(DEPARTMENT)} />
</PluginDocumentSettingPanel>
</>
return options && (
<PluginDocumentSettingPanel
name="analytics-panel"
title={__('Analytics & Tracking', 'planet4-blocks-backend')}
>
<SelectSidebarField
label={__('Global Project', 'planet4-master-theme-backend')}
options={options.global_projects || []}
{...getParams('p4_campaign_name')} />
<SelectSidebarField
label={__('Local Projects', 'planet4-master-theme-backend')}
options={options.local_projects || []}
{...getParams('p4_local_project')} />
<SelectSidebarField
label={__('Basket Name', 'planet4-master-theme-backend')}
options={options.baskets || []}
{...getParams('p4_basket_name')} />
<TextSidebarField
label={__('Department', 'planet4-master-theme-backend')}
{...getParams('p4_department')} />
</PluginDocumentSettingPanel>
);
},
};

0 comments on commit f1791c5

Please sign in to comment.