Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
import { createModel, RematchDispatch } from '@rematch/core'
import User from '_types/User'
import usersService from '_api/rest/usersService'
export interface UserState extends User {}
const model = createModel({
state: null,
reducers: {
set: (state: UserState, payload: User): UserState => payload,
},
effects: (dispatch: RematchDispatch) => ({
async request() {
const currentUser = await usersService.getProfile()
// dispatch.user.set(currentUser)
// this.set(currentUser)
dispatch.user.set(currentUser)
},
// TODO: Optional args breaks TypeScript autocomplete (e.g. payload: number = 1)
// async incrementAsync(payload: number) {
// await delay(500);
// this.increment(payload || 1);
static addModel(stateModel: any, modelConfig: Rematch.Model): any {
let store = StateManager.getStore();
// automatically create singleton store
if (!store) {
store = StateManager.createStore();
}
store.model(modelConfig);
Reflect.set(stateModel, MODEL, Rematch.createModel(modelConfig));
}
import { createModel } from '@rematch/core';
export const count = createModel({
state: 0,
reducers: {
increment: s => s + 1
},
effects: {
async asyncIncrement() {
await new Promise((resolve) => {
setTimeout(resolve, 1000)
})
this.increment()
}
},
})
static createModel(stateModel: any, modelConfig: Rematch.Model): any {
Reflect.set(stateModel, MODEL, Rematch.createModel(modelConfig));
}
const model = {
state: 0,
reducers: {
increment: (state: SharksState, payload: number): SharksState =>
state + payload,
},
effects: (dispatch: Dispatch) => ({
async incrementAsync(payload: number = 1) {
await delay(500)
dispatch.sharks.increment(payload)
},
}),
}
export const sharks: typeof model = createModel(model)
}
export interface AppStateData {
user?: User
}
export interface AppState extends AppStateStatus {
data: AppStateData
}
const initialState: AppState = {
loading: true,
error: undefined,
data: {},
}
const model = createModel({
state: initialState,
reducers: {
setStatus: (
state: AppState,
payload: { loading?: boolean; error?: any },
): AppState => ({
...state,
...payload,
}),
setData: (state: AppState, payload: AppStateData): AppState => ({
...state,
data: {
...state.data,
...payload,
},
}),
export type DolphinsState = number
const model = {
state: 0,
reducers: {
increment: (state: DolphinsState) => state + 1,
},
effects: (dispatch: Dispatch) => ({
async incrementAsync() {
await delay(500)
dispatch.dolphins.increment()
},
}),
}
export const dolphins: typeof model = createModel(model)