mirror of
https://github.com/rjNemo/devbook_ts
synced 2026-06-12 13:36:43 +00:00
connect navBar to the sotre
This commit is contained in:
parent
bb5bea1351
commit
9fdd759e5d
3 changed files with 38 additions and 10 deletions
|
|
@ -1,17 +1,23 @@
|
||||||
import React, {FC} from 'react';
|
import React, {FC} from 'react';
|
||||||
|
// Routing
|
||||||
import {Link} from 'react-router-dom';
|
import {Link} from 'react-router-dom';
|
||||||
|
import * as ROUTES from '../constants/routes';
|
||||||
|
//Redux
|
||||||
|
import {connect} from 'react-redux';
|
||||||
|
import {selectAuthState} from '../store/auth';
|
||||||
|
// Style
|
||||||
import {FontAwesomeIcon} from '@fortawesome/react-fontawesome';
|
import {FontAwesomeIcon} from '@fortawesome/react-fontawesome';
|
||||||
import {faCode, faSignOutAlt, faUser} from '@fortawesome/free-solid-svg-icons';
|
import {faCode, faSignOutAlt, faUser} from '@fortawesome/free-solid-svg-icons';
|
||||||
import * as ROUTES from '../constants/routes';
|
|
||||||
|
|
||||||
interface IProps {
|
interface IProps {
|
||||||
isAuthenticated?: boolean;
|
isAuthenticated: boolean;
|
||||||
loading?: boolean;
|
loading: boolean;
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* Main Navbar serves navigation routes.
|
* Main Navbar serves navigation routes.
|
||||||
*/
|
*/
|
||||||
const NavBar: FC<IProps> = ({isAuthenticated = false, loading = false}) => {
|
const NavBar: FC<IProps> = ({isAuthenticated, loading}) => {
|
||||||
|
console.log(isAuthenticated, loading);
|
||||||
const publicLinks = (
|
const publicLinks = (
|
||||||
<ul data-testid="publicLinks">
|
<ul data-testid="publicLinks">
|
||||||
<li>
|
<li>
|
||||||
|
|
@ -74,4 +80,5 @@ const NavBar: FC<IProps> = ({isAuthenticated = false, loading = false}) => {
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
export default NavBar;
|
/** connect HOC subscribes to the store */
|
||||||
|
export default connect(selectAuthState)(NavBar);
|
||||||
|
|
|
||||||
|
|
@ -1,15 +1,33 @@
|
||||||
import {createSlice} from '@reduxjs/toolkit';
|
import {createSlice} from '@reduxjs/toolkit';
|
||||||
|
import {RootState} from '..';
|
||||||
|
|
||||||
|
interface SliceState {
|
||||||
|
isAuthenticated: boolean;
|
||||||
|
loading: boolean;
|
||||||
|
// TODO: switch any by the actual user interface
|
||||||
|
user: any;
|
||||||
|
}
|
||||||
|
|
||||||
|
const initialState: SliceState = {
|
||||||
|
isAuthenticated: false,
|
||||||
|
loading: true,
|
||||||
|
user: null,
|
||||||
|
};
|
||||||
|
|
||||||
const authSlice = createSlice({
|
const authSlice = createSlice({
|
||||||
name: 'auth',
|
name: 'auth',
|
||||||
initialState: {
|
initialState,
|
||||||
isAuthenticated: false,
|
|
||||||
loading: true,
|
|
||||||
user: null,
|
|
||||||
},
|
|
||||||
reducers: {},
|
reducers: {},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// export actions
|
||||||
export const {} = authSlice.actions;
|
export const {} = authSlice.actions;
|
||||||
|
|
||||||
|
// export selectors
|
||||||
|
export const selectAuthState = (state: RootState) => {
|
||||||
|
const {isAuthenticated, loading} = state.auth;
|
||||||
|
return {isAuthenticated, loading};
|
||||||
|
};
|
||||||
|
|
||||||
|
// export reducer
|
||||||
export default authSlice.reducer;
|
export default authSlice.reducer;
|
||||||
|
|
|
||||||
|
|
@ -7,4 +7,7 @@ const store = configureStore({
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// State type
|
||||||
|
export type RootState = ReturnType<typeof store.getState>;
|
||||||
|
|
||||||
export default store;
|
export default store;
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue