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

Updated post group selection UI #177

Merged
merged 1 commit into from
Dec 10, 2024
Merged
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
16 changes: 16 additions & 0 deletions plugins/bcc-login/includes/class-bcc-login-visibility.php
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,7 @@ function on_block_editor_assets() {
);

if (!empty($this->_settings->site_groups) ) {

wp_add_inline_script(
$script_handle,
'var siteGroups = ' . json_encode($this->_coreapi->get_translated_site_groups()),
Expand All @@ -295,6 +296,21 @@ function on_block_editor_assets() {
);
}

if (!empty($this->_settings->site_group_tags) ) {

wp_add_inline_script(
$script_handle,
'var siteGroupTags = ' . json_encode($this->_settings->site_group_tags),
'before'
);
} else {
wp_add_inline_script(
$script_handle,
'var siteGroupTags = []',
'before'
);
}

}

/**
Expand Down
29 changes: 21 additions & 8 deletions plugins/bcc-login/src/components/group-selector.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import React, { useState, useEffect } from 'react';
import { MultiSelect } from 'primereact/multiselect';


const GroupSelector = ({ tags, options, name, label, value, readonly }) => {
const GroupSelector = ({ tags, options, name, label, value, readonly, onChange }) => {


const selectedGroupUids = value ? value.split(',') : [];
const optionGroups = [];
Expand All @@ -20,7 +21,7 @@ const GroupSelector = ({ tags, options, name, label, value, readonly }) => {

// Add other items to groups based on tags (in order of tags)
tags.forEach(tag => {
const items = options.filter(option => option.tags && option.tags.indexOf(tag) != -1).sort((a, b) => a.name > b.name ? 1 : -1)
const items = options.filter(option => selectedGroupUids.indexOf(option.uid) == -1 && option.tags && option.tags.indexOf(tag) != -1).sort((a, b) => a.name > b.name ? 1 : -1)
if (items.length > 0) {
optionGroups.push({
tag: tag,
Expand All @@ -35,7 +36,7 @@ const GroupSelector = ({ tags, options, name, label, value, readonly }) => {

return (
<div>
{label && <label htmlFor={name}>{label}</label>}
{label && <h2 htmlFor={name}>{label}</h2>}
<MultiSelect
id={name}
name={name+"_selector"}
Expand All @@ -46,17 +47,29 @@ const GroupSelector = ({ tags, options, name, label, value, readonly }) => {
optionValue="uid"
optionGroupLabel="tag"
optionGroupChildren="items"
onChange={(e) => setSelectedGroups(e.value)}
onChange={(e) => { setSelectedGroups(e.value); if (onChange) { onChange(e.value); } }}
filter
size="small"
maxSelectedLabels={5}
maxSelectedLabels={3}
placeholder="Select Groups"
disabled={readonly}
className="w-full md:w-20rem"
/>
{/* Hidden input for form submission */}
<input type="hidden" name={name} value={(selectedGroups || []).join(',')} />

<style>
{`
.p-multiselect-panel {
max-width: 300px;
}
.p-multiselect-label, .p-multiselect-item {
font-size: 13px;
text-wrap-mode: wrap;
}
.p-multiselect {
font-size: 13px;
border: 1px solid #ced4da;
}
`}
</style>
</div>
);
};
Expand Down
61 changes: 54 additions & 7 deletions plugins/bcc-login/src/visibility.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
CheckboxControl,
SearchControl,
} from "@wordpress/components";
import GroupSelector from './components/group-selector';
import { registerPlugin } from "@wordpress/plugins";
import { InspectorControls } from "@wordpress/block-editor";
import { PluginPostStatusInfo } from "@wordpress/edit-post";
Expand Down Expand Up @@ -159,20 +160,52 @@ registerPlugin("bcc-login-visibility", {
)),
});

registerPlugin("bcc-groups", {
// registerPlugin("bcc-groups", {
// render: compose([
// withSelect((select) => {
// const { getEditedPostAttribute } = select("core/editor");
// const meta = getEditedPostAttribute("meta");
// return {
// selectedGroups: meta.bcc_groups ?? [],
// siteGroups: window.siteGroups,
// };
// }),
// withDispatch((dispatch) => {
// const { editPost } = dispatch("core/editor");
// return {
// onUpdateGroup(value) {
// editPost({
// meta: {
// bcc_groups: value,
// },
// });
// },
// };
// }),
// withInstanceId,
// ])((props) => (
// <PluginPostStatusInfo>
// <GroupsOptions heading={__("Post Groups")} {...props} />
// </PluginPostStatusInfo>
// )),
// });


registerPlugin("bcc-groups-2", {
render: compose([
withSelect((select) => {
const { getEditedPostAttribute } = select("core/editor");
const meta = getEditedPostAttribute("meta");
return {
selectedGroups: meta.bcc_groups ?? [],
siteGroups: window.siteGroups,
value: (meta.bcc_groups ?? []).join(","),
options: window.siteGroups,
tags: window.siteGroupTags,
};
}),
withDispatch((dispatch) => {
const { editPost } = dispatch("core/editor");
return {
onUpdateGroup(value) {
onChange(value) {
editPost({
meta: {
bcc_groups: value,
Expand All @@ -184,7 +217,7 @@ registerPlugin("bcc-groups", {
withInstanceId,
])((props) => (
<PluginPostStatusInfo>
<GroupsOptions heading={__("Post Groups")} {...props} />
<GroupSelector label={__("Post Groups")} {...props} />
</PluginPostStatusInfo>
)),
});
Expand All @@ -210,7 +243,21 @@ addFilter(
}}
{...props}
/>
<PanelRow>
<PanelRow>
<GroupSelector
label={__("Block Groups")}
tags={window.siteGroupTags}
options={window.siteGroups}
value={(attributes.bccGroups ?? []).join(",")}
onChange={(value) => {
setAttributes({
bccGroups: value,
});
}}
/>
</PanelRow>

{/* <PanelRow>
<GroupsOptions
heading={__("Block Groups")}
siteGroups={window.siteGroups}
Expand All @@ -222,7 +269,7 @@ addFilter(
}}
{...props}
/>
</PanelRow>
</PanelRow> */}
</PanelBody>
</InspectorControls>
<BlockEdit {...props} />
Expand Down
Loading