-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
remove eslint error & initials unit tests
- Loading branch information
1 parent
7986e89
commit 3060c54
Showing
8 changed files
with
313 additions
and
29 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import HSButton from '@/components/form/HSButton'; | ||
import { render, screen } from '@testing-library/react'; | ||
import { describe, it } from 'vitest'; | ||
|
||
describe('HSButton component Tests', () => { | ||
it('it should render the HSButton', () => { | ||
render(<HSButton title="tittle" />); | ||
screen.debug(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { describe, it } from 'vitest'; | ||
import SignIn from '@/pages/SignIn'; | ||
import { render, screen } from '@testing-library/react'; | ||
describe('Sign Component Tests', () => { | ||
it('it should render Sign In title on Sign Pagde', () => { | ||
render(<SignIn />); | ||
screen.debug(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
import { createSlice, createAsyncThunk, PayloadAction } from '@reduxjs/toolkit'; | ||
import axios from 'axios'; | ||
import { jwtDecode } from 'jwt-decode'; | ||
|
||
interface SignInState { | ||
token: string | null; | ||
loading: boolean; | ||
error: string | null; | ||
message: string | null; | ||
role: string | null; | ||
needsVerification: boolean; | ||
needs2FA: boolean; | ||
} | ||
|
||
interface DecodedToken { | ||
user: { | ||
userType: { | ||
name: string; | ||
}; | ||
}; | ||
} | ||
|
||
interface LoginResponse { | ||
token: string; | ||
message: string; | ||
} | ||
|
||
interface LoginError { | ||
message: string; | ||
} | ||
|
||
const initialState: SignInState = { | ||
token: null, | ||
loading: false, | ||
error: null, | ||
message: null, | ||
role: null, | ||
needsVerification: false, | ||
needs2FA: false, | ||
}; | ||
|
||
export const loginUser = createAsyncThunk( | ||
'signIn/loginUser', | ||
async (credentials: { email: string; password: string }, thunkAPI) => { | ||
try { | ||
const response = await axios.post( | ||
'https://dynamites-ecomm-be.onrender.com/api/v1/user/login', | ||
credentials | ||
); | ||
return response.data; | ||
} catch (error: any) { | ||
return thunkAPI.rejectWithValue(error.response.data); | ||
} | ||
} | ||
); | ||
|
||
const signInSlice = createSlice({ | ||
name: 'signIn', | ||
initialState, | ||
reducers: { | ||
logout(state) { | ||
state.token = null; | ||
state.role = null; | ||
state.needsVerification = false; | ||
state.needs2FA = false; | ||
localStorage.removeItem('token'); | ||
}, | ||
}, | ||
extraReducers: (builder) => { | ||
builder.addCase(loginUser.pending, (state) => { | ||
state.loading = true; | ||
state.error = null; | ||
state.message = null; | ||
state.needsVerification = false; | ||
state.needs2FA = false; | ||
}); | ||
builder.addCase( | ||
loginUser.fulfilled, | ||
(state, action: PayloadAction<LoginResponse>) => { | ||
state.loading = false; | ||
state.message = action.payload.message; | ||
|
||
if (action.payload.message.includes('2FA')) { | ||
state.needs2FA = true; | ||
state.token = null; | ||
} else { | ||
state.token = action.payload.token; | ||
localStorage.setItem('token', action.payload.token); | ||
const decodedToken = jwtDecode<DecodedToken>(action.payload.token); | ||
state.role = decodedToken.user.userType.name; | ||
} | ||
} | ||
); | ||
builder.addCase(loginUser.rejected, (state, action: PayloadAction<any>) => { | ||
state.loading = false; | ||
state.error = action.payload.message; | ||
state.message = null; | ||
if (action.payload.message.includes('verify your email')) { | ||
state.needsVerification = true; | ||
} | ||
}); | ||
}, | ||
}); | ||
|
||
export const { logout } = signInSlice.actions; | ||
|
||
export default signInSlice.reducer; |
Oops, something went wrong.