UNPKG

@selfcommunity/react-core

Version:

React Core Components useful for integrating UI Community components (react-ui).

96 lines (93 loc) 3.82 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.useSCVote = exports.SCVoteContext = void 0; const jsx_runtime_1 = require("react/jsx-runtime"); const react_1 = require("react"); const api_services_1 = require("@selfcommunity/api-services"); const utils_1 = require("@selfcommunity/utils"); const Errors_1 = require("../../../constants/Errors"); const SCContextProvider_1 = require("../SCContextProvider"); const types_1 = require("@selfcommunity/types"); const SCPreferencesProvider_1 = require("../SCPreferencesProvider"); /** * Creates Vote Context * :::tip Context can be consumed in one of the following ways: ```jsx 1. <SCVoteContext.Consumer>{(reactions) => (...)}</SCVoteContext.Consumer> ``` ```jsx 2. const scVoteContext: SCVoteContextType = useContext(SCVoteContext); ``` ```jsx 3. const scVoteContext: SCVoteContextType = useSCVote(); ```` ::: */ exports.SCVoteContext = (0, react_1.createContext)({}); /** * #### Description: * This component imports all reactions if the feature 'reaction' is enabled. * @param children * @return * ```jsx * <SCVoteContext.Provider value={{reactions}}>{!isLoading && children}</SCVoteContext.Provider> * ``` */ function SCVoteProvider({ children = null }) { const scContext = (0, SCContextProvider_1.useSCContext)(); const scPreferencesContext = (0, react_1.useContext)(SCPreferencesProvider_1.SCPreferencesContext); const [reactions, setReactions] = (0, react_1.useState)(scContext.settings.vote.reactions); const [, setError] = (0, react_1.useState)(); const [initialized, setInitialized] = (0, react_1.useState)((scPreferencesContext.features && !scPreferencesContext.features.includes(types_1.SCFeatureName.REACTION)) || scContext.settings.vote.reactions !== null); const [isLoading, setIsLoading] = (0, react_1.useState)(scPreferencesContext.features && scPreferencesContext.features.includes(types_1.SCFeatureName.REACTION) && scContext.settings.vote.reactions === null); /** * Helper to refresh reactions list */ const refreshReactions = (0, react_1.useCallback)(() => api_services_1.ReactionService.getAllReactionsList().then((data) => { setReactions(data); return data; }), []); /** * Initialize component * Load all reactions if the feature 'reaction' is enabled */ const _initComponent = (0, react_1.useCallback)(() => { if (!initialized) { setInitialized(true); setIsLoading(true); refreshReactions() .then(() => { setIsLoading(false); }) .catch((_error) => { utils_1.Logger.error(Errors_1.SCOPE_SC_CORE, _error); setError(_error); }); } }, [isLoading, initialized]); // EFFECTS (0, react_1.useEffect)(() => { let _t; if (scPreferencesContext.features && scPreferencesContext.features.includes(types_1.SCFeatureName.REACTION) && !reactions) { _t = setTimeout(_initComponent); return () => { _t && clearTimeout(_t); }; } }, [scPreferencesContext.features, reactions]); /** * Nesting all necessary providers * All child components will use help contexts to works */ return (0, jsx_runtime_1.jsx)(exports.SCVoteContext.Provider, Object.assign({ value: { reactions, isLoading, refreshReactions } }, { children: initialized && children })); } exports.default = SCVoteProvider; /** * Let's only export the `useSCPreferences` hook instead of the context. * We only want to use the hook directly and never the context component. */ function useSCVote() { return (0, react_1.useContext)(exports.SCVoteContext); } exports.useSCVote = useSCVote;