{"size": 1175, "ext": "js", "max_stars_repo_path": "src/components/misc/toaster/toaster-context.js", "max_stars_repo_name": "AnthonyDS-symphony/symphony-bdk-ui-toolkit", "max_stars_repo_stars_event_min_datetime": "2020-05-30T13:49:43.000Z", "max_stars_repo_stars_event_max_datetime": "2022-02-25T06:52:10.000Z", "max_issues_repo_path": "src/components/misc/toaster/toaster-context.js", "max_issues_repo_name": "AnthonyDS-symphony/symphony-bdk-ui-toolkit", "max_issues_repo_issues_event_min_datetime": "2020-05-11T08:31:37.000Z", "max_issues_repo_issues_event_max_datetime": "2022-02-10T22:30:05.000Z", "max_forks_repo_path": "src/components/misc/toaster/toaster-context.js", "max_forks_repo_name": "AnthonyDS-symphony/symphony-bdk-ui-toolkit", "max_forks_repo_forks_event_min_datetime": "2020-05-13T11:20:39.000Z", "max_forks_repo_forks_event_max_datetime": "2020-12-04T09:39:28.000Z"}
{"max_stars_count": 4, "max_issues_count": 22, "max_forks_count": 6, "avg_line_length": 21.3636363636, "max_line_length": 76, "alphanum_fraction": 0.5991489362}
import React, { createContext, useState } from 'react';
import PropTypes from 'prop-types';
import Toast from './index';

const ToasterContext = createContext({
  props: {},
  showToast: () => {},
});

export const ToasterProvider = ({ children }) => {
  const [toastState, changeToastState] = useState({
    show: false,
    message: '',
    type: '',
    showToast: ({ message, type }) => {
      changeToastState({
        ...toastState,
        show: true,
        message,
        type,
      });
    },
  });

  return (
    <ToasterContext.Provider value={toastState}>
      {toastState.show && (
        <Toast
          hideToast={() => changeToastState({ ...toastState, show: false })}
          type={toastState.type}
        >
          {toastState.message}
        </Toast>
      )}
      {children}
    </ToasterContext.Provider>
  );
};

ToasterProvider.propTypes = {
  children: PropTypes.any.isRequired,
};

export const ToasterConsumer = ({ children }) => (
  <ToasterContext.Consumer>
    {context => children(context)}
  </ToasterContext.Consumer>
);

ToasterConsumer.propTypes = {
  children: PropTypes.any.isRequired,
};

export default ToasterContext;
