Skip to content

Instantly share code, notes, and snippets.

@goltsevnet
Created November 1, 2020 11:09
Show Gist options
  • Select an option

  • Save goltsevnet/4e479ea68152f6c136b29c4b23b44b5b to your computer and use it in GitHub Desktop.

Select an option

Save goltsevnet/4e479ea68152f6c136b29c4b23b44b5b to your computer and use it in GitHub Desktop.
import {CaseReducer, createSlice, PayloadAction} from "@reduxjs/toolkit";
const ProfileState = {
email: '' as string,
rating: 0 as number,
username: '' as string,
first_name: '' as string,
last_name: null as string | null,
slug: '' as string,
avatar: '/static/user.svg' as string,
privacy_policy: false as boolean,
is_active: false as boolean,
is_vendor: false as boolean,
is_staff: false as boolean,
date_joined: null as number | null,
last_login: null as number | null,
orders_complete: 0 as number,
orders_pending: 0 as number,
orders_canceled: 0 as number,
}
type ProfileStateType = typeof ProfileState | null
export interface State {
logged_in: boolean
whoami?: 0 | 1
errorMessage: string | null
profile: ProfileStateType
}
const State: State = {
logged_in: false,
whoami: 0,
errorMessage: null,
profile: null,
}
const setLoggedInFunc: CaseReducer<State, PayloadAction<boolean>> =
(state, action) => {
state.logged_in = action.payload
state.whoami = 1
}
const setLoginFunc: CaseReducer<State> =
(state, action) => {
state.logged_in = true
state.whoami = 1
}
const setLogoutFunc: CaseReducer<State> =
(state, action) => {
state.profile = null
state.logged_in = false
state.whoami = 1
}
const setWhoamiFunc: CaseReducer<State> =
(state, action) => {
state.profile = null
state.logged_in = false
state.whoami = 1
}
const setErrorMessageFunc: CaseReducer<State, PayloadAction<string>> =
(state, action) => {
state.errorMessage = action.payload
}
const setUserInfoFunc: CaseReducer<State, PayloadAction<ProfileStateType>> =
(state, action) => {
state.profile = action.payload
state.logged_in = true
state.whoami = 1
state.errorMessage = null
}
type reducers = keyof typeof reducers
const reducers = {
setLoggedIn: setLoggedInFunc,
setErrorMessage: setErrorMessageFunc,
setLogin: setLoginFunc,
setLogout: setLogoutFunc,
setWhoami: setWhoamiFunc,
setUserInfo: setUserInfoFunc,
}
interface Action<T> {
type: string;
payload: T;
error?: boolean;
meta?: any;
}
type CasesType =
ReturnType<typeof setLoggedInFunc> |
ReturnType<typeof setLoginFunc> |
ReturnType<typeof setLogoutFunc> |
ReturnType<typeof setWhoamiFunc> |
ReturnType<typeof setErrorMessageFunc> |
ReturnType<typeof setUserInfoFunc>
const slice = createSlice({
name: 'login',
initialState: State,
reducers,
extraReducers: {
'REDUX_WEBSOCKET::MESSAGE': (state, action) => {
const message = JSON.parse(action.payload.message)
if (Array.isArray(message)) {
message.forEach((message) => (
MessageSwitch(state, message)
))
} else {
MessageSwitch(state, message)
}
},
}
})
function MessageSwitch(state: any, action: any) {
const splitArg = action.type.split('/')
if (splitArg.length == 2 && splitArg[0] === 'login') {
reducers[splitArg[1] as reducers](state, action)
}
}
// export interface LoginActions {
// setLoggedIn: typeof setLoggedIn
// }
export default slice.reducer
export const {
setLoggedIn,
setErrorMessage,
setLogin,
setLogout,
setWhoami,
setUserInfo
} = slice.actions
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment