Skip to content

Commit

Permalink
refactor react toastr
Browse files Browse the repository at this point in the history
  • Loading branch information
tsubik committed Nov 14, 2024
1 parent 1f97234 commit 912f158
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 19 deletions.
19 changes: 5 additions & 14 deletions components/layout/layout.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
import React from 'react';
import PropTypes from 'prop-types';
import classnames from 'classnames';
import { connect } from 'react-redux';
import dynamic from 'next/dynamic';

// Components
import Header from 'components/layout/header';
import Footer from 'components/layout/footer';
import Head from 'components/layout/head';
import Icons from 'components/layout/icons';
import Modal from 'components/ui/modal';
import Toastr from 'components/ui/toastr';
import Notifications from 'components/ui/notifications';
import RouterSpinner from 'components/layout/router-spinner';

const Toastr = dynamic(() => import('react-redux-toastr'), { ssr: false });

const Layout = ({ title, description, url, children, className, footer, toastr }) => {
const Layout = ({ title, description, url, children, className, footer }) => {
const classNames = classnames({
[className]: !!className
});
Expand All @@ -40,12 +37,7 @@ const Layout = ({ title, description, url, children, className, footer, toastr }
{footer !== false && <Footer />}

<Modal />

{toastr && <Toastr
preventDuplicates
transitionIn="fadeIn"
transitionOut="fadeOut"
/>}
<Toastr />

<Notifications />
<RouterSpinner />
Expand All @@ -59,8 +51,7 @@ Layout.propTypes = {
children: PropTypes.any.isRequired,
url: PropTypes.object.isRequired,
className: PropTypes.string,
footer: PropTypes.bool,
toastr: PropTypes.object
footer: PropTypes.bool
};

export default connect((state) => ({ toastr: state.toastr }))(Layout);
export default Layout;
29 changes: 29 additions & 0 deletions components/ui/toastr.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { useEffect } from 'react';
import { connect, useStore } from 'react-redux';
import dynamic from 'next/dynamic';

const ReduxToastr = dynamic(() => import('react-redux-toastr'), { ssr: false });

const Toastr = ({ toastr }) => {
const store = useStore();

useEffect(() => {
async function injectReducer() {
if (!store.asyncReducers.toastr) {
const { reducer: toastrReducer } = await import('react-redux-toastr');
store.injectReducer('toastr', toastrReducer);
}
}
injectReducer();
}, []);

if (!toastr) return null;

return <ReduxToastr
preventDuplicates
transitionIn="fadeIn"
transitionOut="fadeOut"
/>;
}

export default connect((state) => ({ toastr: state.toastr }))(Toastr);
6 changes: 1 addition & 5 deletions pages/_app.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ const MyApp = ({ Component, ...rest }) => {
const { store, props } = wrapper.useWrappedStore(rest);
const { pageProps, defaultLocale, language, messages, url } = props;

useEffect(async () => {
useEffect(() => {
const state = store.getState();

if (!state.operators.data.length) {
Expand All @@ -61,10 +61,6 @@ const MyApp = ({ Component, ...rest }) => {
if (!state.countries.data.length) {
store.dispatch(getCountries());
}

// TODO: maybe move this to dedicated component that would load toastr with its reducer
const { reducer: toastrReducer } = await import('react-redux-toastr');
store.injectReducer('toastr', toastrReducer);
}, []);

if (pageProps.errorCode) {
Expand Down

0 comments on commit 912f158

Please sign in to comment.