Skip to content

Commit

Permalink
change redux legacy updates method
Browse files Browse the repository at this point in the history
  • Loading branch information
Zasa-san committed Apr 13, 2024
1 parent 0665955 commit bce457e
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 44 deletions.
28 changes: 20 additions & 8 deletions app/react/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { Provider as ReduxProvider } from 'react-redux';
import { getRoutes } from './Routes';
import CustomProvider from './App/Provider';
import { settingsAtom, templatesAtom, translationsAtom } from './V2/atoms';
import { relationshipTypesAtom } from './V2/atoms/relationshipTypes';
import { store } from './store';

const reduxState = store?.getState();
Expand All @@ -14,18 +15,29 @@ const templates = reduxState?.templates.toJS() || [];

const router = createBrowserRouter(getRoutes(settings, reduxState?.user.get('_id')));

const atomsGlobalState = () => {
const myStore = createStore();
myStore.set(settingsAtom, settings);
myStore.set(templatesAtom, templates);
myStore.set(translationsAtom, { locale: reduxState?.locale || 'en' });
return myStore;
};
const atomStore = createStore();
atomStore.set(settingsAtom, settings);
atomStore.set(templatesAtom, templates);
atomStore.set(translationsAtom, { locale: reduxState?.locale || 'en' });

//sync deprecated redux store
atomStore.sub(settingsAtom, () => {
const value = atomStore.get(settingsAtom);
store?.dispatch({ type: 'settings/collection/SET', value });
});
atomStore.sub(templatesAtom, () => {
const value = atomStore.get(templatesAtom);
store?.dispatch({ type: 'templates/SET', value });
});
atomStore.sub(relationshipTypesAtom, () => {
const value = atomStore.get(relationshipTypesAtom);
store?.dispatch({ type: 'relationTypes/SET', value });
});

const App = () => (
<ReduxProvider store={store as any}>
<CustomProvider>
<Provider store={atomsGlobalState()}>
<Provider store={atomStore}>
<RouterProvider router={router} fallbackElement={null} />
</Provider>
</CustomProvider>
Expand Down
8 changes: 0 additions & 8 deletions app/react/V2/atoms/relationshipTypes.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
import { atom } from 'jotai';
import { atomEffect } from 'jotai-effect';
import { ClientRelationshipType } from 'app/apiResponseTypes';
import { store } from 'app/store';

const relationshipTypesAtom = atom([] as ClientRelationshipType[]);

//sync deprecated redux store
atomEffect(get => {
const value = get(relationshipTypesAtom);
store?.dispatch({ type: 'relationTypes/SET', value });
});

export { relationshipTypesAtom };
8 changes: 0 additions & 8 deletions app/react/V2/atoms/settingsAtom.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
import { atom } from 'jotai';
import { atomEffect } from 'jotai-effect';
import { store } from 'app/store';
import { ClientSettings } from 'app/apiResponseTypes';

const settingsAtom = atom({} as ClientSettings);

//sync deprecated redux store
atomEffect(get => {
const value = get(settingsAtom);
store?.dispatch({ type: 'settings/collection/SET', value });
});

export { settingsAtom };
8 changes: 0 additions & 8 deletions app/react/V2/atoms/templatesAtom.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
import { atom } from 'jotai';
import { atomEffect } from 'jotai-effect';
import { store } from 'app/store';
import { Template } from 'app/apiResponseTypes';

const templatesAtom = atom([] as Template[]);

//sync deprecated redux store
atomEffect(get => {
const value = get(templatesAtom);
store?.dispatch({ type: 'templates/SET', value });
});

export { templatesAtom };
9 changes: 3 additions & 6 deletions app/react/entry-server.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -272,16 +272,13 @@ const EntryServer = async (req: ExpressRequest, res: Response) => {

resetTranslations();

const atomsGlobalState = () => {
const myStore = createStore();
myStore.set(settingsAtom, settings);
return myStore;
};
const atomStore = createStore();
atomStore.set(settingsAtom, settings);

const componentHtml = ReactDOMServer.renderToString(
<ReduxProvider store={initialStore as any}>
<CustomProvider initialData={initialState} user={req.user} language={initialState.locale}>
<Provider store={atomsGlobalState()}>
<Provider store={atomStore}>
<React.StrictMode>
<StaticRouterProvider
router={router}
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,6 @@
"is-reachable": "^5.2.1",
"isomorphic-fetch": "3.0.0",
"jotai": "^2.8.0",
"jotai-effect": "^1.0.0",
"json-schema": "^0.4.0",
"json-schema-to-typescript": "^13.1.2",
"jvent": "1.0.2",
Expand Down
5 changes: 0 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -13227,11 +13227,6 @@ joi@^17.6.0:
"@sideway/formula" "^3.0.0"
"@sideway/pinpoint" "^2.0.0"

jotai-effect@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/jotai-effect/-/jotai-effect-1.0.0.tgz#caf5342fa7ddef1db84c99be4b04a631980f4120"
integrity sha512-eCgKKG4BACDzuJGYTu0xZRk1C1MEOvbAhC3L8w7YufQ2lSLORwNX/WFnCuZxLFX0sDLkTUeoUzOYaw8wnXY+UQ==

jotai@^2.8.0:
version "2.8.0"
resolved "https://registry.yarnpkg.com/jotai/-/jotai-2.8.0.tgz#5a6585cd5576c400c2c5f8e157b83ad2ba70b2ab"
Expand Down

0 comments on commit bce457e

Please sign in to comment.