-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Ig 130 #136
base: master
Are you sure you want to change the base?
Ig 130 #136
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
import { ChangeEvent, useState } from 'react'; | ||
import { ChangeEvent, useState, useContext } from 'react'; | ||
import { Link, useNavigate } from 'react-router-dom'; | ||
|
||
import { RoutePaths } from '../../../models'; | ||
import { NotificationMode, RoutePaths } from '../../../models'; | ||
import { User } from '../../../models/user'; | ||
import { Api } from '../../../Api'; | ||
import { USER_TOKEN } from '../../../constants'; | ||
|
@@ -10,11 +10,15 @@ import { setLocalStorage } from '../../../utils/localStorage'; | |
import { Form, FormInput } from '../../shared'; | ||
|
||
import './style.css'; | ||
import { saveUserDataAction } from '../../../services/actions/userActions'; | ||
import { useDispatch } from 'react-redux'; | ||
import { NotificationContext } from '../../../contexts'; | ||
|
||
export const LoginPage = (): JSX.Element => { | ||
export const LoginPage = (): any => { | ||
const [form, setForm] = useState(new User()); | ||
const [errors, setErrors] = useState(new User()); | ||
const [isLoading, setIsLoading] = useState(false); | ||
const { addNotification } = useContext(NotificationContext); | ||
|
||
const handleChange = (e: ChangeEvent<{ value: string; name: string }>) => { | ||
const { name, value } = e.target; | ||
|
@@ -42,17 +46,20 @@ export const LoginPage = (): JSX.Element => { | |
let navigate = useNavigate(); | ||
|
||
const api = new Api(); | ||
const dispatch = useDispatch(); | ||
|
||
const login = async () => { | ||
setIsLoading(true); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why remove? |
||
try { | ||
// TODO save user data to reducer | ||
const userData = (await api.post('url..', form)) as User; | ||
const userData = (await api.post('/login', form)) as User; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. wrong url, its /auth/login There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. use generic types for POST // |
||
setLocalStorage(USER_TOKEN, userData.token); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. token doesnt exist in userData. its different (check response) |
||
dispatch(saveUserDataAction(form)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we want to store userData that comes from api (check api response). not form. PASSWORD and token cannot be stored here :p |
||
navigate(RoutePaths.MainPage); | ||
} catch (error: any) { | ||
// TODO display notification from context | ||
console.log(error?.response.data || error?.response.status); | ||
return addNotification({ | ||
mode: NotificationMode.INFO, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why INFO when its an error? |
||
title: 'Login', | ||
message: error, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think its error.message |
||
}); | ||
} finally { | ||
setIsLoading(false); | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,43 @@ | ||
import React, { ChangeEvent, useState } from 'react'; | ||
import React, { ChangeEvent, useContext, useState } from 'react'; | ||
|
||
import { Form, FormInput } from '../../shared'; | ||
|
||
import { Api } from '../../../Api'; | ||
import { useNavigate } from 'react-router-dom'; | ||
import { RoutePaths, User } from '../../../models'; | ||
import { NotificationMode, RoutePaths, User } from '../../../models'; | ||
|
||
import { USER_TOKEN } from '../../../constants'; | ||
import { setLocalStorage } from '../../../utils/localStorage'; | ||
|
||
import './style.css'; | ||
import { saveUserDataAction } from '../../../services/actions/userActions'; | ||
import { useDispatch } from 'react-redux'; | ||
import { NotificationContext } from '../../../contexts'; | ||
|
||
export const RegisterPage = (): JSX.Element => { | ||
const [errors, setErrors] = useState(new User()); | ||
const [form, setForm] = useState(new User()); | ||
const [isLoading, setLoading] = useState(false); | ||
const { addNotification } = useContext(NotificationContext); | ||
|
||
const navigate = useNavigate(); | ||
|
||
const api = new Api(); | ||
const dispatch = useDispatch(); | ||
|
||
async function register() { | ||
setLoading(true); | ||
try { | ||
// TODO save user data to reducer | ||
const userData = (await api.post('url...', form)) as User; | ||
const userData = (await api.post('/register', form)) as User; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. check login component comments and fix ALL register |
||
setLocalStorage(USER_TOKEN, userData.token); | ||
dispatch(saveUserDataAction(form)); | ||
navigate(RoutePaths.MainPage); | ||
} catch (error: any) { | ||
// TODO display notification from context | ||
console.log(error?.response.data || error?.response.status); | ||
return addNotification({ | ||
mode: NotificationMode.INFO, | ||
title: 'Register', | ||
message: error, | ||
}); | ||
} finally { | ||
setLoading(false); | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,12 @@ | ||
import { User } from '../../models/user'; | ||
|
||
export const ADD_USER = 'ADD_USER'; | ||
export const REMOVE_USER = 'REMOVE_USER'; | ||
export const SAVE_USER_DATA = 'SAVE_USER_DATA'; | ||
|
||
export const addUser = (value: User) => { | ||
export const saveUserDataAction = (user: User) => { | ||
return { | ||
type: ADD_USER, | ||
payload: value, | ||
type: SAVE_USER_DATA, | ||
payload: user, | ||
}; | ||
}; | ||
|
||
export const removeUser = (value: User) => { | ||
return { | ||
type: REMOVE_USER, | ||
payload: value, | ||
}; | ||
}; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,10 @@ | ||
import { combineReducers } from 'redux'; | ||
import { cartReducer as cart } from './cart.reducer'; | ||
import { pageResourceReducer as pageResource } from './page-resource.reducer'; | ||
import { userReducer as user } from './user.reducer'; | ||
|
||
export default combineReducers({ | ||
cart, | ||
pageResource | ||
pageResource, | ||
user, | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { User } from '../../models/user'; | ||
import { SAVE_USER_DATA } from '../actions/userActions'; | ||
|
||
const initUserState: User = new User(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we dont want initUserState. or if you want to just empty object |
||
|
||
export function userReducer(state = initUserState, action: any): User { | ||
switch (action.type) { | ||
case SAVE_USER_DATA: | ||
return { ...state, ...action.payload }; | ||
default: | ||
return state; | ||
} | ||
} |
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
dont have to set return type here