forked from abner/for-legacy-web
Add Redux and reducers.
Load i18n files and add dayjs.
This commit is contained in:
62
src/redux/index.ts
Normal file
62
src/redux/index.ts
Normal file
@@ -0,0 +1,62 @@
|
||||
import { createStore } from "redux";
|
||||
import rootReducer from "./reducers";
|
||||
import localForage from "localforage";
|
||||
|
||||
import { Typing } from "./reducers/typing";
|
||||
import { Drafts } from "./reducers/drafts";
|
||||
import { AuthState } from "./reducers/auth";
|
||||
import { Language } from "../context/Locale";
|
||||
import { Unreads } from "./reducers/unreads";
|
||||
import { SyncOptions } from "./reducers/sync";
|
||||
import { Settings } from "./reducers/settings";
|
||||
import { QueuedMessage } from "./reducers/queue";
|
||||
import { ExperimentOptions } from "./reducers/experiments";
|
||||
|
||||
export type State = {
|
||||
locale: Language;
|
||||
auth: AuthState;
|
||||
settings: Settings;
|
||||
unreads: Unreads;
|
||||
queue: QueuedMessage[];
|
||||
typing: Typing;
|
||||
drafts: Drafts;
|
||||
sync: SyncOptions;
|
||||
experiments: ExperimentOptions;
|
||||
};
|
||||
|
||||
export const store = createStore((state: any, action: any) => {
|
||||
if (process.env.NODE_ENV === "development") {
|
||||
console.debug("State Update:", action);
|
||||
}
|
||||
|
||||
if (action.type === "__INIT") {
|
||||
return action.state;
|
||||
}
|
||||
|
||||
return rootReducer(state, action);
|
||||
});
|
||||
|
||||
// Save state using localForage.
|
||||
store.subscribe(() => {
|
||||
const {
|
||||
locale,
|
||||
auth,
|
||||
settings,
|
||||
unreads,
|
||||
queue,
|
||||
drafts,
|
||||
sync,
|
||||
experiments
|
||||
} = store.getState() as State;
|
||||
|
||||
localForage.setItem("state", {
|
||||
locale,
|
||||
auth,
|
||||
settings,
|
||||
unreads,
|
||||
queue,
|
||||
drafts,
|
||||
sync,
|
||||
experiments
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user