Skip to content

Commit

Permalink
Merge pull request #13 from newfold-labs/fix_visiblity
Browse files Browse the repository at this point in the history
fire local storage events when changing visibility
  • Loading branch information
amartya-dev authored Jul 6, 2023
2 parents b3eb984 + db90a2d commit fb61730
Show file tree
Hide file tree
Showing 8 changed files with 204 additions and 21,783 deletions.
2 changes: 1 addition & 1 deletion build/index.asset.php
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<?php return array('dependencies' => array('lodash', 'react', 'wp-api-fetch', 'wp-data', 'wp-dom-ready', 'wp-edit-post', 'wp-element', 'wp-i18n', 'wp-plugins'), 'version' => 'f8ef71c8f4b8ded14664');
<?php return array('dependencies' => array('lodash', 'react', 'wp-api-fetch', 'wp-data', 'wp-dom-ready', 'wp-edit-post', 'wp-element', 'wp-i18n', 'wp-plugins'), 'version' => '8fb0e0925d501ddd708d');
2 changes: 1 addition & 1 deletion build/index.js

Large diffs are not rendered by default.

21,855 changes: 112 additions & 21,743 deletions package-lock.json

Large diffs are not rendered by default.

16 changes: 15 additions & 1 deletion src/components/HelpCenter.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const HelpCenter = (props) => {
"eef54890add97ea2583ff1e417ff86ea"
);

const visible = LocalStorageUtils.getHelpVisible();
const [visible, setVisible] = useState(false);
const [helpEnabled, setHelpEnabled] = useState(false);
const getHelpStatus = async () => {
try {
Expand All @@ -25,6 +25,20 @@ const HelpCenter = (props) => {
getHelpStatus();
}, []);

useEffect(() => {
const updateVisibility = () => {
setVisible(LocalStorageUtils.getHelpVisible());
};

// Add the event listener on component mount
window.addEventListener("storage", updateVisibility);

// Remove the event listener when the component unmounts
return () => {
window.removeEventListener("storage", updateVisibility);
};
}, []);

if (!helpEnabled || !visible) {
return <></>;
}
Expand Down
46 changes: 46 additions & 0 deletions src/components/HelpCenterSidebar.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { useEffect, useState } from "@wordpress/element";
import algoliasearch from "algoliasearch";
import { Configure, Index, InstantSearch } from "react-instantsearch-hooks-web";
import SearchResults from "./SearchResults";
import { CapabilityAPI, LocalStorageUtils } from "../utils";

const HelpCenterSidebar = (props) => {
// Set up the instant search results
const searchClient = algoliasearch(
"AVE0JWZU92",
"eef54890add97ea2583ff1e417ff86ea"
);

const [helpEnabled, setHelpEnabled] = useState(false);
const getHelpStatus = async () => {
try {
const response = await CapabilityAPI.getHelpCenterCapability();
setHelpEnabled(response);
} catch (exception) {
setHelpEnabled(false);
}
};
useEffect(() => {
getHelpStatus();
}, []);

if (!helpEnabled) {
return <></>;
}

return (
<div className="nfd-help-center">
<InstantSearch
searchClient={searchClient}
indexName="nfd_help_searchable_posts"
>
<Index indexName="nfd_help_searchable_posts">
<Configure hitsPerPage={3} getRankingInfo={true} />
<SearchResults refresh={props.refresh} />
</Index>
</InstantSearch>
</div>
);
};

export default HelpCenterSidebar;
1 change: 0 additions & 1 deletion src/components/Modal.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ const Modal = ({ onClose }) => {
<HelpCenter
closeHelp={() => {
onClose();
console.log("Setting to refresh");
setRefresh(!refresh);
}}
refresh={refresh}
Expand Down
3 changes: 2 additions & 1 deletion src/components/SearchResults.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,10 +156,11 @@ const SearchResults = (props) => {
</b>
</p>
)}
{results.hits.map((result) => {
{results.hits.map((result, index) => {
return (
<>
<AlgoliaResult
key={index}
searchTitle={result.post_title}
onGo={() => {
setSearchInput(result.post_title);
Expand Down
62 changes: 27 additions & 35 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import React, { render, useState } from "@wordpress/element";
import { __ } from "@wordpress/i18n";
//
import { PluginSidebar } from "@wordpress/edit-post";
import React, { render, useState, useEffect } from "@wordpress/element";
import { registerPlugin } from "@wordpress/plugins";
//
import domReady from "@wordpress/dom-ready";
import { HiiveAnalytics } from "@newfold-labs/js-utility-ui-analytics";
import { __ } from "@wordpress/i18n";
//
import "../styles.scss";
import HelpCenter from "./components/HelpCenter";
import Modal from "./components/Modal";
import { ReactComponent as Help } from "./icons/help-plugin-sidebar-icon.svg";
import { Analytics, LocalStorageUtils } from "./utils";
import { Analytics, LocalStorageUtils, CapabilityAPI } from "./utils";
import HelpCenterSidebar from "./components/HelpCenterSidebar";

domReady(() => {
// Run only once DOM is ready, else this won't work.
Expand All @@ -29,10 +32,10 @@ export const toggleHelp = (visible) => {
let nfdHelpContainer = document.getElementById("nfd-help-center");
nfdHelpContainer.classList.toggle("help-container", visible);
LocalStorageUtils.updateHelpVisible(visible);
window.dispatchEvent(new Event("storage"));
};

window.newfoldEmbeddedHelp = {};
window.newfoldEmbeddedHelp.toggleNFDLaunchedEmbeddedHelp = () => {
const toggleHelpViaLocalStorage = () => {
const helpVisible = LocalStorageUtils.getHelpVisible();
if (Object.is(helpVisible, undefined)) {
toggleHelp(true);
Expand All @@ -43,41 +46,30 @@ window.newfoldEmbeddedHelp.toggleNFDLaunchedEmbeddedHelp = () => {
Analytics.sendEvent("page", "closed");
};

window.newfoldEmbeddedHelp.toggleNFDUnlaunchedEmbeddedHelp =
function toggleNFDUnlaunchedEmbeddedHelp() {
let helpContainer = document.getElementById("nfd-help-center");
wpContentContainer.removeChild(helpContainer);
newfoldEmbeddedHelp.renderEmbeddedHelp();
};
window.newfoldEmbeddedHelp = {};
window.newfoldEmbeddedHelp.toggleNFDLaunchedEmbeddedHelp = () => {
toggleHelpViaLocalStorage();
};

//For rendering embedded help in Add, edit and View Pages
const HelpCenterPluginSidebar = () => {
const [helpEnabled, setHelpEnabled] = useState(false);
const getHelpStatus = async () => {
try {
const response = await CapabilityAPI.getHelpCenterCapability();
setHelpEnabled(response);
} catch (exception) {
setHelpEnabled(false);
}
};
useEffect(() => {
getHelpStatus();
}, []);

if (!helpEnabled) {
return <></>;
}
CapabilityAPI.getHelpCenterCapability().then((response) => {
setHelpEnabled(response);
});

return (
<PluginSidebar
name="nfd-help-sidebar"
className="nfd-plugin-sidebar"
title="Help Center"
icon={<Help />}
>
<HelpCenter />
</PluginSidebar>
<>
<PluginSidebar
name="nfd-help-sidebar"
className="nfd-plugin-sidebar"
title="Help Center"
icon={<Help />}
isPinnable={helpEnabled}
>
<HelpCenterSidebar />
</PluginSidebar>
</>
);
};

Expand Down

0 comments on commit fb61730

Please sign in to comment.