diff --git a/src/components/Dropdown/Dropdown.jsx b/src/components/Dropdown/Dropdown.jsx index d0a020b..5412451 100644 --- a/src/components/Dropdown/Dropdown.jsx +++ b/src/components/Dropdown/Dropdown.jsx @@ -3,18 +3,17 @@ import CSSModules from 'react-css-modules'; import ReactDropdown, {DropdownTrigger, DropdownContent} from 'react-simple-dropdown'; import styles from './Dropdown.scss'; -export const Dropdown = ({title, children}) => ( -
- - {title} - - {children} - - -
+export const Dropdown = ({onRef, title, children}) => ( + + {title} + + {children} + + ); Dropdown.propTypes = { + onRef: PropTypes.func, title: PropTypes.any.isRequired, children: PropTypes.any.isRequired, }; diff --git a/src/components/Dropdown/Dropdown.scss b/src/components/Dropdown/Dropdown.scss index 9acd9d2..61de447 100644 --- a/src/components/Dropdown/Dropdown.scss +++ b/src/components/Dropdown/Dropdown.scss @@ -1,18 +1,12 @@ -.dropdown { - :global { - .dropdown { - display: inline-block; - } - - - .dropdown--active .dropdown__content { - display: block; - } +:global { + .dropdown { + display: inline-block; + } + .dropdown--active .dropdown__content { + display: block; } } - - .content { display: none; position: absolute; diff --git a/src/components/Header/Header.jsx b/src/components/Header/Header.jsx index 77383d8..55e9742 100644 --- a/src/components/Header/Header.jsx +++ b/src/components/Header/Header.jsx @@ -8,129 +8,158 @@ import Dropdown from '../Dropdown'; import Notification from '../Notification'; import styles from './Header.scss'; -export const Header = ({ +/** + * TODO: This component cries: 'REFACTOR ME!' + * Seriously, it is such a mess now, should be split into separate sub-components! + */ + +export function Header({ location, selectedCategory, categories, user, notifications, - routes, handleNotification, doLogout, toggleNotif, loggedUser, -}) => ( + handleNotification, logoutAction, toggleNotif, loggedUser, +}) { + // Holds a reference to the function which hides the user dropdown (Profile, + // Logout, etc.). + let hideUserDropdown; - + ); +} Header.propTypes = { - routes: PropTypes.any.isRequired, + // routes: PropTypes.any.isRequired, location: PropTypes.string.isRequired, selectedCategory: PropTypes.string.isRequired, categories: PropTypes.array.isRequired, notifications: PropTypes.array.isRequired, user: PropTypes.object.isRequired, handleNotification: PropTypes.func, - doLogout: PropTypes.func.isRequired, + logoutAction: PropTypes.func.isRequired, toggleNotif: PropTypes.bool, loggedUser: PropTypes.bool, }; diff --git a/src/containers/HeaderContainer.js b/src/containers/HeaderContainer.js index 99ad526..4269472 100644 --- a/src/containers/HeaderContainer.js +++ b/src/containers/HeaderContainer.js @@ -1,12 +1,17 @@ import Header from 'components/Header'; import {asyncConnect} from 'redux-connect'; -import {actions, toggleNotification, loginAction, logoutAction, logout} from '../store/modules/global'; +import {actions, toggleNotification, loginAction, logoutAction} from '../store/modules/global'; const resolve = [{ promise: () => Promise.resolve(), }]; -const mapState = (state) => ({...state.global, doLogout: logout}); +const mapState = (state) => ({...state.global}); + +/* + TODO: This is not used anymore, should be checked if this is safe to remove + (i.e. if the toggleNotification and loginAction actions are part of + the acetions object, injected into the asyncConnect call below). const mapDispatchToProps = (dispatch) => ({ handleNotification: (value) => { @@ -14,6 +19,6 @@ const mapDispatchToProps = (dispatch) => ({ }, handleLogin: (userObj) => dispatch(loginAction(userObj)), }); +*/ export default asyncConnect(resolve, mapState, {...actions, logoutAction})(Header); - diff --git a/src/store/modules/global.js b/src/store/modules/global.js index 14a4798..89a6728 100644 --- a/src/store/modules/global.js +++ b/src/store/modules/global.js @@ -4,18 +4,14 @@ import UserApi from 'api/User.js'; import config from '../../config'; import APIService from 'services/APIService'; + const userApi = new UserApi(config.api.basePath); -const userInfoKey = 'userInfo'; +//------------------------------------------------------------------------------ +// Constants -function loadUserInfo() { - userInfo = localStorage.getItem(userInfoKey); - if (userInfo) { - userInfo = JSON.parse(userInfo); - APIService.accessToken = userInfo.accessToken; - } - return userInfo; -} +const LOGOUT_ACTION = 'LOGOUT_ACTION'; +const USER_INFO_KEY = 'userInfo'; // ------------------------------------ // Actions @@ -25,12 +21,21 @@ let hasError = false; let errorText = ''; let userInfo = {}; +function loadUserInfo() { + userInfo = localStorage.getItem(USER_INFO_KEY); + if (userInfo) { + userInfo = JSON.parse(userInfo); + APIService.accessToken = userInfo.accessToken; + } + return userInfo; +} + export const sendLoginRequest = (values) => new Promise((resolve) => { userApi.login(values.email, values.password).then((authResult) => { isLogged = true; hasError = false; userInfo = authResult; - localStorage.setItem(userInfoKey, JSON.stringify(authResult)); + localStorage.setItem(USER_INFO_KEY, JSON.stringify(authResult)); if (authResult.user.role === 'consumer') { browserHistory.push('/browse-provider'); } else if (authResult.user.role === 'provider') { @@ -61,25 +66,23 @@ export const sendSignupRequest = (values) => new Promise((resolve) => { resolve(); }); -export const logout = () => async(dispatch) => { - localStorage.removeItem(userInfoKey); - console.log('user info removed'); - userInfo = undefined; - isLogged = false; -}; - export const toggleNotification = createAction('TOGGLE_NOTIFICATION'); export const loginAction = createAction('LOGIN_ACTION'); -export const logoutAction = createAction('LOGOUT_ACTION'); +export const logoutAction = () => dispatch => { + browserHistory.push('/home'); + dispatch({ + type: LOGOUT_ACTION + }); +} export const signupAction = createAction('SIGNUP_ACTION'); export const actions = { toggleNotification, loginAction, logoutAction, }; -// console.log(loginAction(true)) + // ------------------------------------ // Reducer // ------------------------------------ @@ -90,15 +93,24 @@ export default handleActions({ [loginAction]: (state) => ({ ...state, loggedUser: isLogged, hasError, errorText, user: (loadUserInfo() ? loadUserInfo().user : {}), }), - [logoutAction]: (state) => ({ - ...state, loggedUser: isLogged, hasError, errorText, user: (loadUserInfo() ? loadUserInfo().user : {}), - }), + [LOGOUT_ACTION]: (state) => { + localStorage.removeItem(USER_INFO_KEY); + APIService.accessToken = ''; + isLogged = false; + return ({ + ...state, + loggedUser: false, + hasError, + errorText, + user: {}, + }); + }, [signupAction]: (state) => ({ ...state, loggedUser: isLogged, hasError, errorText, user: (loadUserInfo() ? loadUserInfo().user : {}), }), }, { toggleNotif: false, - loggedUser: loadUserInfo() != undefined, + loggedUser: Boolean(loadUserInfo()), location: 'Jakarta, Indonesia', selectedCategory: 'Category', categories: [ pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy