web: add redux store
parent
51ffb6fe71
commit
d26ef10958
@ -0,0 +1,24 @@
|
||||
import {Action} from "redux";
|
||||
|
||||
import {ActionCreator} from "../types";
|
||||
|
||||
export type ModalName = "compose" | "notifications";
|
||||
|
||||
export interface OpenModalAppAction extends Action<"app/openModal"> {
|
||||
id: ModalName,
|
||||
}
|
||||
|
||||
export type CloseModalAppAction = Action<"app/closeModal">;
|
||||
|
||||
export type AppAction
|
||||
= OpenModalAppAction
|
||||
| CloseModalAppAction;
|
||||
|
||||
export const openModal: ActionCreator<OpenModalAppAction, [ModalName]> = (id) => ({
|
||||
type: "app/openModal",
|
||||
id,
|
||||
});
|
||||
|
||||
export const closeModal: ActionCreator<CloseModalAppAction> = () => ({
|
||||
type: "app/closeModal",
|
||||
});
|
@ -0,0 +1,29 @@
|
||||
import {Reducer} from "redux";
|
||||
|
||||
import {ModalName} from "../actions/app";
|
||||
import {RootAction} from "../store";
|
||||
|
||||
export interface AppState {
|
||||
modal: ModalName | null;
|
||||
}
|
||||
|
||||
const INITIAL_STATE: AppState = {
|
||||
modal: null,
|
||||
};
|
||||
|
||||
export const appReducer: Reducer<AppState, RootAction> = (state = INITIAL_STATE, action) => {
|
||||
switch (action.type) {
|
||||
case "app/openModal":
|
||||
return {
|
||||
modal: action.id,
|
||||
...state
|
||||
};
|
||||
case "app/closeModal":
|
||||
return {
|
||||
modal: null,
|
||||
...state
|
||||
};
|
||||
default:
|
||||
return state;
|
||||
}
|
||||
};
|
@ -0,0 +1,17 @@
|
||||
import {configureStore, Store} from "@reduxjs/toolkit";
|
||||
|
||||
import {AppAction} from "./actions/app";
|
||||
import {appReducer, AppState} from "./reducers/app";
|
||||
|
||||
export interface RootState {
|
||||
app?: AppState;
|
||||
}
|
||||
|
||||
export type RootAction = AppAction;
|
||||
|
||||
const store: Store<RootState, RootAction> = configureStore({
|
||||
reducer: {
|
||||
app: appReducer,
|
||||
},
|
||||
});
|
||||
export default store;
|
@ -0,0 +1,7 @@
|
||||
import {Action} from "redux";
|
||||
|
||||
/**
|
||||
* The `ActionCreator` type provided by redux sucks balls
|
||||
* because its params are `any[]`, so here is our own one
|
||||
*/
|
||||
export type ActionCreator<A extends Action, P extends any[] = []> = (...params: P) => A;
|
Loading…
Reference in New Issue