Skip to content

Install and configure Redux Toolkit #2187

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Jan 2, 2024
5 changes: 5 additions & 0 deletions client/middleware.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { createListenerMiddleware } from '@reduxjs/toolkit';

const listenerMiddleware = createListenerMiddleware();

export default listenerMiddleware;
6 changes: 2 additions & 4 deletions client/modules/App/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import PropTypes from 'prop-types';
import React, { useEffect, useRef, useState } from 'react';
import { useDispatch, useSelector } from 'react-redux';
import { useLocation } from 'react-router-dom';
import getConfig from '../../utils/getConfig';
import { showReduxDevTools } from '../../store';
import DevTools from './components/DevTools';
import { setPreviousPath } from '../IDE/actions/ide';
import { setLanguage } from '../IDE/actions/preferences';
Expand Down Expand Up @@ -52,9 +52,7 @@ const App = ({ children }) => {
return (
<div className="app">
<CookieConsent hide={hide} />
{isMounted &&
!window.devToolsExtension &&
getConfig('NODE_ENV') === 'development' && <DevTools />}
{isMounted && showReduxDevTools() && <DevTools />}
{children}
</div>
);
Expand Down
2 changes: 1 addition & 1 deletion client/modules/IDE/selectors/collections.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createSelector } from 'reselect';
import { createSelector } from '@reduxjs/toolkit';
import differenceInMilliseconds from 'date-fns/differenceInMilliseconds';
import { find, orderBy } from 'lodash';
import { DIRECTION } from '../actions/sorting';
Expand Down
2 changes: 1 addition & 1 deletion client/modules/IDE/selectors/files.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createSelector } from 'reselect';
import { createSelector } from '@reduxjs/toolkit';

const selectFiles = (state) => state.files;

Expand Down
2 changes: 1 addition & 1 deletion client/modules/IDE/selectors/project.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createSelector } from 'reselect';
import { createSelector } from '@reduxjs/toolkit';

export const selectProjectOwner = (state) => state.project.owner;
export const selectProjectId = (state) => state.project.id;
Expand Down
2 changes: 1 addition & 1 deletion client/modules/IDE/selectors/projects.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createSelector } from 'reselect';
import { createSelector } from '@reduxjs/toolkit';
import differenceInMilliseconds from 'date-fns/differenceInMilliseconds';
import { orderBy } from 'lodash';
import { DIRECTION } from '../actions/sorting';
Expand Down
2 changes: 1 addition & 1 deletion client/modules/IDE/selectors/users.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createSelector } from 'reselect';
import { createSelector } from '@reduxjs/toolkit';
import getConfig from '../../../utils/getConfig';

export const getAuthenticated = (state) => state.user.authenticated;
Expand Down
42 changes: 24 additions & 18 deletions client/store.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,36 @@
import { createStore, applyMiddleware, compose } from 'redux';
import thunk from 'redux-thunk';
import { configureStore } from '@reduxjs/toolkit';
import listenerMiddleware from './middleware';
import DevTools from './modules/App/components/DevTools';
import rootReducer from './reducers';
import { clearState, loadState } from './persistState';
import getConfig from './utils/getConfig';

export default function configureStore(initialState) {
const enhancers = [applyMiddleware(thunk)];

if (getConfig('CLIENT') && getConfig('NODE_ENV') === 'development') {
// Enable DevTools only when rendering on client and during development.
enhancers.push(
window.devToolsExtension
? window.devToolsExtension()
: DevTools.instrument()
);
}
// Enable DevTools only when rendering on client and during development.
// Display the dock monitor only if no browser extension is found.
export function showReduxDevTools() {
return (
getConfig('CLIENT') &&
getConfig('NODE_ENV') === 'development' &&
!window.__REDUX_DEVTOOLS_EXTENSION__
);
}

export default function setupStore(initialState) {
const savedState = loadState();
clearState();

const store = createStore(
rootReducer,
savedState != null ? savedState : initialState,
compose(...enhancers)
);
const store = configureStore({
reducer: rootReducer,
middleware: (getDefaultMiddleware) =>
getDefaultMiddleware({
thunk: true,
serializableCheck: true,
// TODO: enable immutableCheck once the mutations are fixed.
immutableCheck: false
}).concat(listenerMiddleware.middleware),
preloadedState: savedState || initialState,
enhancers: showReduxDevTools() ? [DevTools.instrument()] : []
});

if (module.hot) {
// Enable Webpack hot module replacement for reducers
Expand Down
Loading