Skip to content

Instantly share code, notes, and snippets.

@imkrish
Created March 9, 2020 03:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save imkrish/2675a983ef024ddb0ac141a239634708 to your computer and use it in GitHub Desktop.
Save imkrish/2675a983ef024ddb0ac141a239634708 to your computer and use it in GitHub Desktop.
import { createSlice, PayloadAction } from '@reduxjs/toolkit';
import { UserProfileApi, UserSettingsApi } from '@selfplat/api-model';
import { AppThunk } from '../root.store';
import { ProfileService } from './profile.service';
export interface UserState {
profile: UserProfileApi;
settings: UserSettingsApi;
}
const initialState: UserState = {
profile: null,
settings: {
themeColor: 'teal',
},
};
const auth0Slice = createSlice({
name: 'user',
initialState,
reducers: {
setProfile(state, action: PayloadAction<UserProfileApi>) {
const { payload: profile } = action;
state.profile = profile;
},
setSettings(state, action: PayloadAction<UserSettingsApi>) {
const { payload: settings } = action;
state.settings = settings;
},
},
});
export const { setProfile, setSettings } = auth0Slice.actions;
export const { reducer: userReducer, name: userName } = auth0Slice;
export const fetchProfile = (): AppThunk => async dispatch => {
try {
const profile = await ProfileService.getProfile();
dispatch(setProfile(profile));
} catch (err) {
console.log('failed to get user profile', err);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment