Skip to content

Commit b6124c0

Browse files
committed
feat: implement profile and address update
1 parent a328305 commit b6124c0

File tree

16 files changed

+668
-15
lines changed

16 files changed

+668
-15
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
export const SET_ACCESS_TOKEN = 'SET_ACCESS_TOKEN';
22
export const SET_AUTH_MODAL = 'SET_AUTH_MODAL';
33
export const SET_IS_AUTH = 'SET_IS_AUTH';
4+
export const UPDATE_CURRENT_USER = 'UPDATE_CURRENT_USER';
45
export const SET_CURRENT_USER = 'SET_CURRENT_USER';
6+
export const SET_CURRENT_USER_FIELD = 'SET_CURRENT_USER_FIELD';
57
export const SET_LOGGING_IN = 'SET_LOGGING_IN';
68
export const SET_SIGNING_UP = 'SET_SIGNING_UP';
79
export const SET_USER_ERROR = 'SET_USER_ERROR';
810
export const SET_AUTH_FORM_FIELD = 'SET_AUTH_FORM_FIELD';
911
export const CLEAR_AUTH_FORM = 'CLEAR_AUTH_FORM';
1012
export const CLEAR_CURRENT_USER = 'CLEAR_CURRENT_USER';
13+
export const SET_UPDATING_CURRENT_USER = 'SET_UPDATING_CURRENT_USER';
14+
export const SET_UPDATING_CURRENT_USER_ADDRESS =
15+
'SET_UPDATING_CURRENT_USER_ADDRESS';
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export const SET_SHIPPING_REGION = 'SET_SHIPPING_REGION';
2+
export const SET_SHIPPING_REGIONS = 'SET_SHIPPING_REGIONS';
3+
export const SET_LOADING_SHIPPING_REGION = 'SET_LOADING_SHIPPING_REGION';
4+
export const SET_LOADING_SHIPPING_REGIONS = 'SET_LOADING_SHIPPING_REGIONS';

src/actions/currentUserActions.js

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,25 @@ export const setCurrentUser = payload => ({
4646
payload,
4747
});
4848

49+
export const updateCurrentUser = payload => ({
50+
type: types.UPDATE_CURRENT_USER,
51+
payload,
52+
});
53+
54+
export const setCurrentUserField = payload => ({
55+
type: types.SET_CURRENT_USER_FIELD,
56+
payload,
57+
});
58+
4959
export const submitLogin = credential => dispatch => {
5060
dispatch(setLoggingIn(true));
5161
return axios
5262
.post('/customers/login', credential)
5363
.then(({ data }) => {
5464
dispatch(setCurrentUser(data.customer));
5565
dispatch(setAccessToken(data.accessToken));
66+
localStorage.setItem('accessToken', data.accessToken);
67+
axios.defaults.headers.common['USER-KEY'] = data.accessToken;
5668
dispatch(setIsAuth(true));
5769
dispatch(setLoggingIn(false));
5870
})
@@ -70,6 +82,7 @@ export const submitRegister = credential => dispatch => {
7082
.then(({ data }) => {
7183
dispatch(setCurrentUser(data.customer));
7284
dispatch(setAccessToken(data.accessToken));
85+
axios.defaults.headers.common['USER-KEY'] = data.accessToken;
7386
dispatch(setIsAuth(true));
7487
dispatch(setSigningUp(false));
7588
})
@@ -88,3 +101,55 @@ export const signout = () => dispatch => {
88101
localStorage.removeItem('accessToken');
89102
dispatch(clearCurrentUser());
90103
};
104+
105+
export const setUpdatingCurrentUser = payload => ({
106+
type: types.SET_UPDATING_CURRENT_USER,
107+
payload,
108+
});
109+
110+
export const setUpdatingCurrentUserAddress = payload => ({
111+
type: types.SET_UPDATING_CURRENT_USER_ADDRESS,
112+
payload,
113+
});
114+
115+
export const fetchCurrentUser = token => dispatch => {
116+
return axios
117+
.get('/customer')
118+
.then(({ data }) => {
119+
dispatch(setCurrentUser(data));
120+
})
121+
.catch(err => {
122+
const error =
123+
err.response && err.response.data ? err.response.data.error : err;
124+
dispatch(setUserError(error));
125+
});
126+
};
127+
128+
export const submitUpdateUser = user => dispatch => {
129+
dispatch(setUpdatingCurrentUser(true));
130+
return axios
131+
.put('/customer', user)
132+
.then(({ data }) => {
133+
dispatch(updateCurrentUser(data));
134+
dispatch(setUpdatingCurrentUser(false));
135+
})
136+
.catch(({ response }) => {
137+
const { error } = response.data;
138+
dispatch(setUserError(error));
139+
dispatch(setUpdatingCurrentUser(false));
140+
});
141+
};
142+
143+
export const submitUpdateUserAddress = address => dispatch => {
144+
dispatch(setUpdatingCurrentUserAddress(true));
145+
return axios
146+
.put('/customers/address', address)
147+
.then(({ data }) => {
148+
dispatch(setUpdatingCurrentUserAddress(false));
149+
})
150+
.catch(({ response }) => {
151+
const { error } = response.data;
152+
dispatch(setUserError(error));
153+
dispatch(setUpdatingCurrentUserAddress(false));
154+
});
155+
};

src/actions/shippingActions.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import * as types from '../actions-types/shippingActionsTypes';
2+
import axios from '../utils/axios';
3+
4+
export const setShippingRegion = payload => ({
5+
type: types.SET_SHIPPING_REGION,
6+
payload,
7+
});
8+
9+
export const setShippingRegions = payload => ({
10+
type: types.SET_SHIPPING_REGIONS,
11+
payload,
12+
});
13+
14+
export const setLoadingShippingRegion = payload => ({
15+
type: types.SET_LOADING_SHIPPING_REGION,
16+
payload,
17+
});
18+
19+
export const setLoadingShippingRegions = payload => ({
20+
type: types.SET_LOADING_SHIPPING_REGIONS,
21+
payload,
22+
});
23+
24+
export const fetchShippingRegions = () => dispatch => {
25+
return axios
26+
.get('/shipping/regions')
27+
.then(({ data }) => {
28+
dispatch(setShippingRegions(data));
29+
})
30+
.catch(err => {
31+
return err;
32+
});
33+
};

src/components/AuthModal/AuthModal.jsx

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ export class AuthModal extends Component {
120120
</div>
121121
<div className="level-right">
122122
<button
123-
onClick={() => _setAuthModal('Register')}
123+
onClick={() => _setAuthModal('Sign Up')}
124124
className="bottom-btn"
125125
>
126126
Crate an account
@@ -131,7 +131,7 @@ export class AuthModal extends Component {
131131
);
132132
};
133133

134-
_renderRegister = () => {
134+
_renderSignup = () => {
135135
const {
136136
userError,
137137
_handleInput,
@@ -204,11 +204,12 @@ export class AuthModal extends Component {
204204
</button>
205205

206206
<div className="has-text-centered mt-20 mb-20">
207+
Already a member?
207208
<button
208209
onClick={() => _setAuthModal('Sign In')}
209210
className="bottom-btn"
210211
>
211-
Have an account
212+
Sign In
212213
</button>
213214
</div>
214215
</form>
@@ -248,9 +249,7 @@ export class AuthModal extends Component {
248249
<h1 className="modal-card-title">{title}</h1>
249250
</header>
250251
<section className="modal-card-body">
251-
{title === 'Register'
252-
? this._renderRegister()
253-
: this._renderSignin()}
252+
{title === 'Sign Up' ? this._renderSignup() : this._renderSignin()}
254253
</section>
255254
</div>
256255
</div>

src/components/AuthModal/AuthModal.scss

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
@import '../../styles/variables';
33

44
.modal-view {
5-
.modal-card {
6-
&.auth {
5+
&.auth {
6+
.modal-card {
77
max-width: 400px;
88
.modal-card-head {
99
display: block;

src/components/Header/Header.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ export class Header extends Component {
9292
</button>{' '}
9393
or
9494
<button
95-
onClick={() => _setAuthModal('Register')}
95+
onClick={() => _setAuthModal('Sign Up')}
9696
className="auth-btn ml-10"
9797
>
9898
Register

src/components/Routes.jsx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ import { PropTypes } from 'prop-types';
1111

1212
import Home from '../pages/Home/Home';
1313
import SingleItem from '../pages/SingleItem/SingleItem';
14-
import Login from '../pages/Home/Home';
1514
import MyCartModal from './MyCartModal/MyCartModal';
1615
import AuthModal from './AuthModal/AuthModal';
1716
import NotFound from '../pages/NotFound/NotFound';
17+
import Settings from '../pages/Settings/Settings';
1818

1919
export const Routes = ({ isAuth, cartModal, authModal }) => (
2020
<Router>
@@ -26,8 +26,10 @@ export const Routes = ({ isAuth, cartModal, authModal }) => (
2626
<Route exact path="/products/:productId" component={SingleItem} />
2727
<Route
2828
exact
29-
path="/login"
30-
render={props => (!isAuth ? <Login {...props} /> : <Redirect to="/" />)}
29+
path="/settings"
30+
render={props =>
31+
isAuth ? <Settings {...props} /> : <Redirect to="/" />
32+
}
3133
/>
3234
<Route path="*" component={NotFound} />
3335
</Switch>

src/index.scss

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
@import './styles/colors';
22
@import './styles/forms';
3+
@import './styles/mixins';
34

45
@font-face {
56
font-family: Playfair-Bold;
@@ -254,7 +255,8 @@ button {
254255
cursor: pointer;
255256
}
256257

257-
.input {
258+
.input,
259+
.textarea {
258260
box-shadow: none;
259261
}
260262

0 commit comments

Comments
 (0)