I'm using redux-toolkit and have 2 slices: "auth" and "profile"
auth => handle information about token
profile => handle information about user account
When user tries to login, I send request to api, that returns me the user token and account information. Then I need to save this information. I put token to corresponding field (in same slice). And I need to put my account information to "profile" slice (login processing takes place in the "auth" slice). Now i'm just dispatch setProfile action from 'auth' slice.
Is it anti-pattern to dispatch "profile" action from "auth" slice? Or I have to move this logic from redux to component? Or make "login" action outside of slice? Or do I need to keep it all in one slice?
// PROFILE SLICE | profile.js
const initialState = {
data: {},
status: 'idle'
}
export const profileSlice = createSlice({
name: 'profile',
initialState,
reducers: {
setProfile(s, {payload: profile}) {
s.profile = profile
}
}
})
export const {setProfile} = userSlice.actions;
export default profileSlice.reducer
// AUTH SLICE | auth.js
import {setProfile} from './profile' // import action creator from profile slice
const initialState = {
token: null,
status: 'idle'
}
export const authSlice = createSlice({
name: 'auth',
initialState,
reducers: {
setToken(s, {payload: token}) {
s.token = token
}
}
})
export const login = ({email, password}) => dispatch => {
return api.auth.login({
email,
password
})
.then(res => {
const {token, ...profile} = res.data
dispatch(setToken(token))
dispatch(setProfile(profile)
})
}
export const {setToken} = authSlice.actions;
export default authSlice.reducer