Merge pull request #35 from apuc/authentication

redirect to auth
This commit is contained in:
kavalar 2021-08-24 15:41:01 +03:00 committed by GitHub
commit 6f9a3fb792
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 39 additions and 25 deletions

View File

@ -1,7 +1,7 @@
import React, { useEffect } from 'react'; import React, { useEffect } from 'react';
import { useHistory, useParams, Link } from 'react-router-dom'; import { useHistory, useParams, Link } from 'react-router-dom';
import { useSelector, useDispatch } from 'react-redux'; import { useSelector, useDispatch } from 'react-redux';
import { currentCandidate, selectCurrentCandidate } from '../../redux/outstaffingSlice'; import { currentCandidate, selectCurrentCandidate, auth } from '../../redux/outstaffingSlice';
import arrow from '../../images/right-arrow.png'; import arrow from '../../images/right-arrow.png';
import rectangle from '../../images/rectangle_secondPage.png'; import rectangle from '../../images/rectangle_secondPage.png';
import Sidebar from '../Sidebar/Sidebar'; import Sidebar from '../Sidebar/Sidebar';
@ -26,7 +26,7 @@ const Candidate = () => {
}, []) }, [])
useEffect(() => { useEffect(() => {
fetchItemsForId({ link: `${process.env.REACT_APP_API_URL}/api/profile/`, index:Number(candidateId), history, role }).then((el) => fetchItemsForId({ link: `${process.env.REACT_APP_API_URL}/api/profile/`, index:Number(candidateId), history, role, logout: dispatch(auth(false)) }).then((el) =>
dispatch(currentCandidate(el)) dispatch(currentCandidate(el))
); );
}, [dispatch, candidateId]); }, [dispatch, candidateId]);

View File

@ -4,13 +4,14 @@ import male from '../../images/medium_male.png';
import rectangle from '../../images/rectangle_secondPage.png'; import rectangle from '../../images/rectangle_secondPage.png';
import { Link, useHistory } from 'react-router-dom'; import { Link, useHistory } from 'react-router-dom';
import { LEVELS, SKILLS } from '../constants/constants'; import { LEVELS, SKILLS } from '../constants/constants';
import { selectProfiles, selectFilteredCandidates, selectItems } from '../../redux/outstaffingSlice'; import { selectProfiles, selectFilteredCandidates, selectItems, auth } from '../../redux/outstaffingSlice';
import { useSelector } from 'react-redux'; import { useSelector, useDispatch } from 'react-redux';
import { fetchProfile } from '../../server/server'; import { fetchProfile } from '../../server/server';
import { Loader } from '../Loader/Loader'; import { Loader } from '../Loader/Loader';
import { getRole } from '../../redux/roleSlice'; import { getRole } from '../../redux/roleSlice';
const Description = ({ onLoadMore, isLoadingMore }) => { const Description = ({ onLoadMore, isLoadingMore }) => {
const dispatch = useDispatch();
const [isLoaded, setIsLoaded] = useState(false); const [isLoaded, setIsLoaded] = useState(false);
const history = useHistory(); const history = useHistory();
const role = useSelector(getRole) const role = useSelector(getRole)
@ -20,7 +21,7 @@ const Description = ({ onLoadMore, isLoadingMore }) => {
const [allCandidates, getAllCandidates] = useState([]); const [allCandidates, getAllCandidates] = useState([]);
useEffect(() => { useEffect(() => {
fetchProfile({ link: `${process.env.REACT_APP_API_URL}/api/profile?limit=`, index: 1000, history, role }).then((p) => { fetchProfile({ link: `${process.env.REACT_APP_API_URL}/api/profile?limit=`, index: 1000, history, role, logout: dispatch(auth(false)) }).then((p) => {
getAllCandidates(p); getAllCandidates(p);
setIsLoaded(true); setIsLoaded(true);
}); });
@ -32,7 +33,7 @@ const Description = ({ onLoadMore, isLoadingMore }) => {
<div className="container"> <div className="container">
<div className={style.description__wrapper}> <div className={style.description__wrapper}>
{ {
candidatesListArr.length > 0 ? candidatesListArr.map((el) => ( candidatesListArr && candidatesListArr.length > 0 ? candidatesListArr.map((el) => (
<div className="row" key={el.id}> <div className="row" key={el.id}>
<div className="col-2"> <div className="col-2">
<img className={style.description__img} src={el.photo} alt="" /> <img className={style.description__img} src={el.photo} alt="" />

View File

@ -1,6 +1,7 @@
import React, { useState } from 'react'; import React, { useState } from 'react';
import style from './Form.module.css'; import style from './Form.module.css';
import { fetchForm } from '../../server/server'; import { fetchForm } from '../../server/server';
import { auth } from '../../redux/outstaffingSlice';
import { useHistory, useParams, Redirect } from 'react-router-dom'; import { useHistory, useParams, Redirect } from 'react-router-dom';
import PhoneInput from 'react-phone-input-2' import PhoneInput from 'react-phone-input-2'
import 'react-phone-input-2/lib/style.css' import 'react-phone-input-2/lib/style.css'
@ -8,12 +9,13 @@ import './form.css';
import { withSwalInstance } from 'sweetalert2-react'; import { withSwalInstance } from 'sweetalert2-react';
import swal from 'sweetalert2'; import swal from 'sweetalert2';
import { useSelector } from 'react-redux'; import { useSelector, useDispatch } from 'react-redux';
import { getRole } from '../../redux/roleSlice'; import { getRole } from '../../redux/roleSlice';
const SweetAlert = withSwalInstance(swal); const SweetAlert = withSwalInstance(swal);
const Form = () => { const Form = () => {
const dispatch = useDispatch();
const history = useHistory(); const history = useHistory();
const role = useSelector(getRole); const role = useSelector(getRole);
const urlParams = useParams(); const urlParams = useParams();
@ -45,7 +47,7 @@ const Form = () => {
fetchForm({ link: `${process.env.REACT_APP_API_URL}/api/profile/add-to-interview`, index: { fetchForm({ link: `${process.env.REACT_APP_API_URL}/api/profile/add-to-interview`, index: {
profile_id: urlParams.id, profile_id: urlParams.id,
...data, ...data,
}, history, role }).then( (res)=> res.json() }, history, role, logout: dispatch(auth(false)) }).then( (res)=> res.json()
.then( resJSON => setStatus(resJSON)) .then( resJSON => setStatus(resJSON))
) )
}; };

View File

@ -3,7 +3,7 @@ import { useDispatch, useSelector } from 'react-redux';
import Outstaffing from '../Outstaffing/Outstaffing'; import Outstaffing from '../Outstaffing/Outstaffing';
import Description from '../Description/Description'; import Description from '../Description/Description';
import { fetchProfile, fetchSkills } from '../../server/server'; import { fetchProfile, fetchSkills } from '../../server/server';
import { profiles, tags } from '../../redux/outstaffingSlice'; import { profiles, tags, auth } from '../../redux/outstaffingSlice';
import { getRole } from '../../redux/roleSlice'; import { getRole } from '../../redux/roleSlice';
import { Footer } from '../Footer/Footer'; import { Footer } from '../Footer/Footer';
import { useHistory } from 'react-router-dom'; import { useHistory } from 'react-router-dom';
@ -18,12 +18,13 @@ const Home = () => {
useEffect(() => { useEffect(() => {
setIsLoadingMore(true); setIsLoadingMore(true);
fetchProfile({ link:`${process.env.REACT_APP_API_URL}/api/profile?limit=`, index, history, role}).then((profileArr) => { fetchProfile({ link:`${process.env.REACT_APP_API_URL}/api/profile?limit=`, index, history, role, logout: dispatch(auth(false)) }).then((profileArr) => {
dispatch(profiles(profileArr)); dispatch(profiles(profileArr));
setIsLoadingMore(false); setIsLoadingMore(false);
}); });
fetchSkills({ link: `${process.env.REACT_APP_API_URL}/api/skills/skills-on-main-page`, history, role}).then((skills) => { fetchSkills({ link: `${process.env.REACT_APP_API_URL}/api/skills/skills-on-main-page`, history, role, logout: dispatch(auth(false)) }).then((skills) => {
if(!skills) { return [] }
const keys = Object.keys(skills); const keys = Object.keys(skills);
const values = Object.values(skills); const values = Object.values(skills);

View File

@ -1,7 +1,7 @@
import React from 'react'; import React from 'react';
import OutsideClickHandler from 'react-outside-click-handler'; import OutsideClickHandler from 'react-outside-click-handler';
import { useDispatch, useSelector } from 'react-redux'; import { useDispatch, useSelector } from 'react-redux';
import { selectItems, selectedItems, filteredCandidates } from '../../redux/outstaffingSlice'; import { selectItems, selectedItems, filteredCandidates, auth } from '../../redux/outstaffingSlice';
import { fetchItemsForId } from '../../server/server'; import { fetchItemsForId } from '../../server/server';
import style from './Outstaffing.module.css'; import style from './Outstaffing.module.css';
@ -11,14 +11,14 @@ import { getRole } from '../../redux/roleSlice';
const handlePositionClick = ({dispatch, positionId, isSelected, onSelect, history, role}) => { const handlePositionClick = ({dispatch, positionId, isSelected, onSelect, history, role}) => {
if(isSelected) { if(isSelected) {
fetchProfile({ link: `${process.env.REACT_APP_API_URL}/api/profile?limit=`, index: 4, history, role }).then((profileArr) => { fetchProfile({ link: `${process.env.REACT_APP_API_URL}/api/profile?limit=`, index: 4, history, role, logout: dispatch(auth(false)) }).then((profileArr) => {
dispatch(filteredCandidates(profileArr)); dispatch(filteredCandidates(profileArr));
dispatch(selectedItems([])); dispatch(selectedItems([]));
onSelect(positionId); onSelect(positionId);
} }
); );
} else { } else {
fetchItemsForId({ link: `${process.env.REACT_APP_API_URL}/api/profile?position_id=`, index: positionId, history, role, }).then((el) => { fetchItemsForId({ link: `${process.env.REACT_APP_API_URL}/api/profile?position_id=`, index: positionId, history, role, logout: dispatch(auth(false)) }).then((el) => {
dispatch(filteredCandidates(el)); dispatch(filteredCandidates(el));
dispatch(selectedItems([])); dispatch(selectedItems([]));
onSelect(positionId); onSelect(positionId);

View File

@ -3,7 +3,7 @@ import { useSelector, useDispatch } from 'react-redux';
import Select from 'react-select'; import Select from 'react-select';
import { Loader } from '../Loader/Loader'; import { Loader } from '../Loader/Loader';
import style from './TagSelect.module.css'; import style from './TagSelect.module.css';
import { selectedItems, selectItems, selectTags, filteredCandidates, setPositionId } from '../../redux/outstaffingSlice'; import { selectedItems, selectItems, selectTags, filteredCandidates, setPositionId, auth } from '../../redux/outstaffingSlice';
import { fetchItemsForId } from '../../server/server'; import { fetchItemsForId } from '../../server/server';
import { useHistory } from 'react-router-dom'; import { useHistory } from 'react-router-dom';
import { getRole } from '../../redux/roleSlice'; import { getRole } from '../../redux/roleSlice';
@ -24,7 +24,7 @@ const TagSelect = () => {
dispatch(setPositionId(null)); dispatch(setPositionId(null));
const filterItemsId = itemsArr.map((item) => item.id).join(); const filterItemsId = itemsArr.map((item) => item.id).join();
fetchItemsForId({ link: `${process.env.REACT_APP_API_URL}/api/profile?skills=`, index: filterItemsId, history, role, }).then((el) => { fetchItemsForId({ link: `${process.env.REACT_APP_API_URL}/api/profile?skills=`, index: filterItemsId, history, role, logout: dispatch(auth(false)) }).then((el) => {
dispatch(filteredCandidates(el)) dispatch(filteredCandidates(el))
setSearchLoading(false) setSearchLoading(false)
}); });

View File

@ -1,7 +1,7 @@
import React from 'react'; import React from 'react';
import { useDispatch, useSelector } from 'react-redux'; import { useDispatch, useSelector } from 'react-redux';
import { useHistory, useParams, Link } from 'react-router-dom'; import { useHistory, useParams, Link } from 'react-router-dom';
import { currentCandidate, selectCurrentCandidate } from '../redux/outstaffingSlice'; import { currentCandidate, selectCurrentCandidate, auth } from '../redux/outstaffingSlice';
import SVG from 'react-inlinesvg'; import SVG from 'react-inlinesvg';
import { WithLogout } from '../hoc/withLogout'; import { WithLogout } from '../hoc/withLogout';
import Form from '../components/Form/Form'; import Form from '../components/Form/Form';
@ -28,7 +28,7 @@ const FormPage = () => {
const role = useSelector(getRole); const role = useSelector(getRole);
if(!candidate.id) { if(!candidate.id) {
fetchItemsForId({ link: `${process.env.REACT_APP_API_URL}/api/profile/`, index: Number(params.id), history, role, }).then((el) => fetchItemsForId({ link: `${process.env.REACT_APP_API_URL}/api/profile/`, index: Number(params.id), history, role, logout: dispatch(auth(false)) }).then((el) =>
dispatch(currentCandidate(el)) dispatch(currentCandidate(el))
); );
} }

View File

@ -1,15 +1,17 @@
export const withAuthRedirect = actionCall => ({link, index, history, role}) => { export const withAuthRedirect = actionCall => ({link, index, history, role, logout}) => {
return actionCall(link, index) return actionCall(link, index)
.then(res => { .then(res => {
if(res.status && res.status == 401) { if(res.status && res.status == 401) {
localStorage.clear(); localStorage.clear();
history.push(role === 'ROLE_DEV' ? '/authdev' : '/auth') logout();
history.push(role === 'ROLE_DEV' ? '/authdev' : '/auth') ;
} }
return res; return res;
}) })
.catch(err => { .catch(err => {
localStorage.clear(); localStorage.clear();
logout();
history.push(role === 'ROLE_DEV' ? '/authdev' : '/auth'); history.push(role === 'ROLE_DEV' ? '/authdev' : '/auth');
}) })
} }

View File

@ -13,7 +13,9 @@ export const fetchProfile = withAuthRedirect(async (link, index) => {
let data = await response.json() let data = await response.json()
return data return data
} catch (error) {} } catch (error) {
console.log('Query error', error)
}
}) })
export const fetchSkills = withAuthRedirect(async (link) => { export const fetchSkills = withAuthRedirect(async (link) => {
@ -29,7 +31,9 @@ export const fetchSkills = withAuthRedirect(async (link) => {
let data = await response.json() let data = await response.json()
return data return data
} catch (error) {} } catch (error) {
console.log('Query error', error)
}
}) })
export const fetchItemsForId = withAuthRedirect(async (link, id) => { export const fetchItemsForId = withAuthRedirect(async (link, id) => {
@ -46,7 +50,9 @@ export const fetchItemsForId = withAuthRedirect(async (link, id) => {
let data = await response.json() let data = await response.json()
return data return data
} catch (error) {} } catch (error) {
console.log('Query error', error)
}
}) })
export const fetchForm = withAuthRedirect(async (link, info) => { export const fetchForm = withAuthRedirect(async (link, info) => {
@ -63,7 +69,9 @@ export const fetchForm = withAuthRedirect(async (link, info) => {
}) })
return response return response
} catch (error) {} } catch (error) {
console.log('Query error', error)
}
}) })
export const fetchAuth = async ({ username, password, dispatch, catchError }) => { export const fetchAuth = async ({ username, password, dispatch, catchError }) => {