How to use the piral-core.useGlobalState function in piral-core

To help you get started, we’ve selected a few piral-core examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github smapiot / piral / packages / piral-sample / src / index.tsx View on Github external
const Sitemap: React.SFC = () => {
  const pages = useGlobalState(s => s.components.pages);

  return (
    <ul>
      <li>
        Go to /
      </li>
      {Object.keys(pages)
        .map(url =&gt; url.replace(':id', `${~~(Math.random() * 1000)}`))
        .map(url =&gt; (
          <li>
            Go to {url}
          </li>
        ))}
      <li>
        Go to /sitemap
      </li></ul>
github smapiot / piral / src / packages / piral-forms / src / useForm.ts View on Github external
export function useForm(
  initialData: TFormData,
  history: History,
  options: InputFormOptions,
  existingId?: string,
) {
  const { silent, message } = options;
  const [id] = useState(existingId || generateId);
  const state = useGlobalState(m =&gt; m.forms[id] || createDefaultState(initialData));
  const updateState = useAction('updateFormState');
  usePrompt(!silent &amp;&amp; state.changed, history, message);
  useEffect(() =&gt; {
    updateState(id, state, {
      active: true,
    });

    return () =&gt;
      updateState(id, state, {
        active: false,
      });
  }, [state.submitting]);
  return createProps(id, state, updateState, options);
}
github smapiot / piral / src / samples / sample-piral-core / src / index.tsx View on Github external
const Sitemap: React.FC = () =&gt; {
  const pages = useGlobalState(s =&gt; s.registry.pages);

  return (
    <ul>
      <li>
        Go to /
      </li>
      {Object.keys(pages)
        .map(url =&gt; url.replace(':id', `${~~(Math.random() * 1000)}`))
        .map(url =&gt; (
          <li>
            Go to {url}
          </li>
        ))}
      <li>
        Go to /sitemap
      </li></ul>
github smapiot / piral / src / packages / piral / src / components / SwitchErrorInfo.tsx View on Github external
export const SwitchErrorInfo: React.FC = props =&gt; {
  const components = useGlobalState(m =&gt; m.components);

  switch (props.type) {
    case 'not_found':
      return renderComponent(components, 'NotFoundErrorInfo', props);
    case 'page':
      return renderComponent(components, 'PageErrorInfo', props);
    case 'tile':
      return renderComponent(components, 'TileErrorInfo', props);
    case 'menu':
      return renderComponent(components, 'MenuErrorInfo', props);
    case 'extension':
      return renderComponent(components, 'ExtensionErrorInfo', props);
    case 'modal':
      return renderComponent(components, 'ModalErrorInfo', props);
    case 'loading':
      return renderComponent(components, 'LoadingErrorInfo', props);
github smapiot / piral / src / samples / sample-cross-fx / src / index.tsx View on Github external
const Sitemap: React.FC = () =&gt; {
  const pages = useGlobalState(s =&gt; s.registry.pages);

  return (
    <ul>
      <li>
        Go to /
      </li>
      {Object.keys(pages)
        .map(url =&gt; url.replace(':id', `${~~(Math.random() * 1000)}`))
        .map(url =&gt; (
          <li>
            Go to {url}
          </li>
        ))}
      <li>
        Go to /sitemap
      </li></ul>
github smapiot / piral / packages / piral / src / hooks / index.ts View on Github external
export function useTranslation() {
  const { selected, translations } = useGlobalState(m => m.app.language);
  return translations[selected];
}
github smapiot / piral / src / packages / piral-notifications / src / Notifications.tsx View on Github external
export const Notifications: React.FC = () =&gt; {
  const notifications = useGlobalState(s =&gt; s.notifications);

  return (
    
      {notifications.map(({ component: Component, close, options, id }) =&gt; (
        
          
        
      ))}
    
  );
};
Notifications.displayName = 'Notifications';
github smapiot / piral / src / packages / piral-feeds / src / useFeed.ts View on Github external
export function useFeed(options: ConnectorDetails): [boolean, TData, any] {
  const { loaded, loading, error, data } = useGlobalState(s =&gt; s.feeds[options.id]);
  const load = useAction('loadFeed');

  useEffect(() =&gt; {
    if (!loaded &amp;&amp; !loading) {
      load(options);
    }
  }, [loaded]);

  return [loaded, data, error];
}
github smapiot / piral / src / packages / piral-containers / src / withPiletState.tsx View on Github external
const StateView: React.FC = props =&gt; {
    const state = useGlobalState(s =&gt; s.containers[id]);
    return ;
  };
  StateView.displayName = `StateView_${id}`;