import { createAsyncThunk, createSlice } from '@reduxjs/toolkit'
import axios from './../../utils/axios'
export const registerUser = createAsyncThunk(
'auth/registerUser',
async ({email, fullName, password, tel, garden}) => {
try {
console.log('sdsssssssssssss')
const { data } = await axios.post('/auth/register', {
fullName,
password,
email,
tel,
garden
})
console.log(data)
if (data.token) {
localStorage.setItem('token', data.token)
}
return data
} catch (e) {
console.log(e)
}
}
)
const initialState = {
user: null,
token: null,
isLoading: null,
status: null
}
export const authSlice = createSlice({
name: 'auth',
initialState,
reducers: {},
extraReducers: builder => {
builder
.addCase(registerUser.pending, (state, action) => {
state.isLoading = true
state.status = null
})
.addCase(registerUser.fulfilled, (state, action) => {
console.log(action.payload)
state.isLoading = false
state.status = action.payload.message
state.user = action.payload.user
state.token = action.payload.token
})
.addCase(registerUser.rejected, (state, action) => {
state.status = action.payload.message
state.isLoading = false
})
}
})
export const {} = authSlice.actions
export default authSlice.reducer