106 lines
2.8 KiB
JavaScript
106 lines
2.8 KiB
JavaScript
import { __ } from "@wordpress/i18n";
|
|
import { InspectorControls } from "@wordpress/block-editor";
|
|
import {
|
|
PanelBody,
|
|
SelectControl,
|
|
ComboboxControl,
|
|
} from "@wordpress/components";
|
|
import "./editor.scss";
|
|
import { Tip } from "@wordpress/components";
|
|
|
|
import { useSelect } from "@wordpress/data";
|
|
import { useEffect, useState } from "@wordpress/element";
|
|
import { decodeEntities } from "@wordpress/html-entities";
|
|
|
|
export default function OptionsSelectControl({ setAttributes, relatedPostId }) {
|
|
let [relatedQuestionPages, setRelatedQuestionPages] = useState(null);
|
|
|
|
const lang = getAdminLanguageFromCookie("wp-wpml_current_language");
|
|
|
|
function getAdminLanguageFromCookie(c_name) {
|
|
var c_value = document.cookie,
|
|
c_start = c_value.indexOf(" " + c_name + "=");
|
|
if (c_start == -1) c_start = c_value.indexOf(c_name + "=");
|
|
if (c_start == -1) {
|
|
c_value = null;
|
|
} else {
|
|
c_start = c_value.indexOf("=", c_start) + 1;
|
|
var c_end = c_value.indexOf(";", c_start);
|
|
if (c_end == -1) {
|
|
c_end = c_value.length;
|
|
}
|
|
c_value = unescape(c_value.substring(c_start, c_end));
|
|
}
|
|
return c_value;
|
|
}
|
|
|
|
function handleRelatedPostChange(postId) {
|
|
setAttributes({ relatedPostId: Number(postId) });
|
|
}
|
|
function buildSelectOptions(parcoursPages) {
|
|
let options = [];
|
|
if (parcoursPages) {
|
|
options.push({ value: 0, label: "Selectionnez une page" });
|
|
parcoursPages.forEach((page) => {
|
|
options.push({
|
|
value: page.id,
|
|
label: decodeEntities(page.title.rendered),
|
|
});
|
|
});
|
|
} else {
|
|
options.push({ value: 0, label: "Pas encore de questions..." });
|
|
}
|
|
return options;
|
|
}
|
|
|
|
const parcoursPages = useSelect((select) => {
|
|
let query = {
|
|
status: "publish",
|
|
per_page: -1,
|
|
lang: lang,
|
|
};
|
|
return select("core").getEntityRecords("postType", "parcours", query);
|
|
});
|
|
|
|
console.log("yalma");
|
|
console.log(parcoursPages);
|
|
|
|
useEffect(() => {
|
|
if (parcoursPages) {
|
|
setRelatedQuestionPages(buildSelectOptions(parcoursPages));
|
|
}
|
|
}, [parcoursPages]);
|
|
|
|
let panelTitle = "salut";
|
|
|
|
return (
|
|
<InspectorControls>
|
|
<PanelBody title={__("Question Relié", "homegrade-blocks")}>
|
|
{/* <SelectControl
|
|
label={panelTitle}
|
|
value={relatedPostId}
|
|
options={relatedQuestionPages}relatedPossiblePages
|
|
onChange={(e) => handleRelatedPostChange(e)}
|
|
/> */}
|
|
|
|
{parcoursPages && (
|
|
<ComboboxControl
|
|
label={panelTitle}
|
|
value={relatedPostId}
|
|
options={relatedQuestionPages}
|
|
onChange={(e) => handleRelatedPostChange(e)}
|
|
/>
|
|
)}
|
|
{relatedPostId && (
|
|
<Tip>
|
|
{__(
|
|
"Pour modifier le contenu de la question affichée ici, rendez-vous dans la fiche question correspondante.",
|
|
"homegrade-blocks",
|
|
)}
|
|
</Tip>
|
|
)}
|
|
</PanelBody>
|
|
</InspectorControls>
|
|
);
|
|
}
|