Skip to content

Commit

Permalink
#104 added useStateAuthenticated`
Browse files Browse the repository at this point in the history
  • Loading branch information
artzub committed May 30, 2023
1 parent c104f28 commit c08991c
Show file tree
Hide file tree
Showing 10 changed files with 153 additions and 42 deletions.
6 changes: 3 additions & 3 deletions src/components/Header/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import branchesSlice from '@/redux/modules/branches';
import profilesSlice from '@/redux/modules/profiles';
import repositoriesSlice from '@/redux/modules/repositories';

import { useRouteMatches, useUIProperty } from '@/shared/hooks';
import { useUIProperty } from '@/shared/hooks';

import BranchStepBody from './components/BranchStep/Body';
import BranchStepHeader from './components/BranchStep/Header';
Expand Down Expand Up @@ -77,7 +77,7 @@ const StepBodies = {
};

const Header = () => {
const { service } = useRouteMatches();
const { authenticated } = useSelector(profilesSlice.selectors.getState);
const [step, setStep] = useUIProperty('step');
const [bodyOpen, setBodyOpen] = useUIProperty('bodyOpen');
const ref = useRef();
Expand Down Expand Up @@ -151,7 +151,7 @@ const Header = () => {
<Container>
<ProfileStepHeader
onClick={onClick(StageTypes.profile)}
disabled={!service}
disabled={!authenticated}
divider
ref={profileRef}
/>
Expand Down
7 changes: 2 additions & 5 deletions src/components/Settings/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,7 @@ const items = [
const Body = () => {
return (
<Fragment>
<Collection
items={items}
path="."
/>
<Collection items={items} />

<Outlet />
</Fragment>
Expand All @@ -77,7 +74,7 @@ const Settings = () => {
>
<Route
path="connection/github"
element={<GitHub path="." />}
element={<GitHub parent="../.." />}
/>
<Route path="*" />
</Route>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@ import { useStageBranches } from './useStageBranches';
import { useStageCommits } from './useStageCommits';
import { useStageProfiles } from './useStageProfiles';
import { useStageRepositories } from './useStageRepositories';
import { useStateAuthenticated } from './useStateAuthenticated';

const StageController = () => {
const Authenticated = () => {
const { service, profile, repository, branch, commits } = useRouteMatches();

console.log('Authenticated');

useGithubEmojis(service);
useStageProfiles(service, profile);
useStageRepositories(repository);
Expand All @@ -18,4 +21,18 @@ const StageController = () => {
return null;
};

const Authentication = () => {
const { service } = useRouteMatches();

const isAuthenticated = useStateAuthenticated(service);

return isAuthenticated ? <Authenticated /> : null;
};

const StageController = () => {
const { service } = useRouteMatches();

return service ? <Authentication /> : null;
};

export default StageController;
2 changes: 1 addition & 1 deletion src/components/StageController/useGithubEmojis.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ export const useGithubEmojis = (service) => {
dispatch(emojisSlice.actions.cancel());
};
},
[service, dispatch],
[service],
);
};
11 changes: 9 additions & 2 deletions src/components/StageController/useSetSelected.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,14 @@ const defaultCondition = (name, selected) => name === selected;
* @param {Function} [skipCondition]
* @param {Function} [preAction]
*/
export const useSetSelected = ({ name, selected, items, action, skipCondition, preAction }) => {
export const useSetSelected = ({
name,
selected,
items,
action,
skipCondition,
preAction,
}) => {
const dispatch = useDispatch();

const skip = skipCondition || defaultCondition;
Expand All @@ -34,6 +41,6 @@ export const useSetSelected = ({ name, selected, items, action, skipCondition, p

dispatch(action(found));
},
[name, selected, items, dispatch, action, preAction, skip],
[name, selected, items, action, preAction, skip],
);
};
16 changes: 10 additions & 6 deletions src/components/StageController/useStageBranches.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ export const useStageBranches = (name) => {
const dispatch = useDispatch();
const redirectTo = useRedirectTo(UrlPratTypes.branch);

const { selected: repository } = useSelector(repositoriesSlice.selectors.getState);
const { selected: repository } = useSelector(
repositoriesSlice.selectors.getState,
);
const { selected: profile } = useSelector(profilesSlice.selectors.getState);
const { selected, items } = useSelector(branchesSlice.selectors.getState);

Expand All @@ -33,16 +35,18 @@ export const useStageBranches = (name) => {
return undefined;
}

dispatch(branchesSlice.actions.fetch({
owner,
repo,
}));
dispatch(
branchesSlice.actions.fetch({
owner,
repo,
}),
);

return () => {
dispatch(branchesSlice.actions.cancel());
};
},
[dispatch, owner, repo],
[owner, repo],
);

useSetSelected({
Expand Down
29 changes: 19 additions & 10 deletions src/components/StageController/useStageCommits.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ export const useStageCommits = (amount) => {
const [, setPause] = useUIProperty('pause');
const [refreshKey] = useUIProperty('refreshKey');

const { selected: repository } = useSelector(repositoriesSlice.selectors.getState);
const { selected: repository } = useSelector(
repositoriesSlice.selectors.getState,
);
const { selected: profile } = useSelector(profilesSlice.selectors.getState);
const { selected } = useSelector(branchesSlice.selectors.getState);

Expand All @@ -38,21 +40,28 @@ export const useStageCommits = (amount) => {

setStoredValue(fixedAmount);

dispatch(commitsSlice.actions.fetch({
owner,
repo,
branch,
amount: fixedAmount,
}));
dispatch(
commitsSlice.actions.fetch({
owner,
repo,
branch,
amount: fixedAmount,
}),
);

return () => {
dispatch(commitsSlice.actions.cancel());
};
},
[
branch, owner, repo,
dispatch, amount, setStoredValue,
refreshKey, setStart, setPause,
branch,
owner,
repo,
amount,
setStoredValue,
refreshKey,
setStart,
setPause,
],
);
};
11 changes: 2 additions & 9 deletions src/components/StageController/useStageProfiles.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,7 @@ export const useStageProfiles = (service, profile) => {
dispatch(profilesSlice.actions.cancel('global'));
};
},
[dispatch, service],
);

useEffect(
() => {
dispatch(profilesSlice.actions.clear());
},
[dispatch, service],
[service],
);

useEffect(
Expand All @@ -39,6 +32,6 @@ export const useStageProfiles = (service, profile) => {
dispatch(profilesSlice.actions.cancel('profile'));
};
},
[profile, login, dispatch],
[profile, login],
);
};
12 changes: 7 additions & 5 deletions src/components/StageController/useStageRepositories.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,18 @@ export const useStageRepositories = (name) => {
useEffect(
() => {
dispatch(repositoriesSlice.actions.clear());
dispatch(repositoriesSlice.actions.fetch({
owner,
amount,
}));
dispatch(
repositoriesSlice.actions.fetch({
owner,
amount,
}),
);

return () => {
dispatch(repositoriesSlice.actions.cancel());
};
},
[owner, amount, dispatch],
[owner, amount],
);

useSetSelected({
Expand Down
82 changes: 82 additions & 0 deletions src/components/StageController/useStateAuthenticated.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import { useEffect, useRef } from 'react';
import { useDispatch, useSelector } from 'react-redux';
import { useLocation, useNavigate } from 'react-router-dom';

import useEventCallback from '@mui/material/utils/useEventCallback';

import errorsSlice from '@/redux/modules/errors';
import profilesSlice from '@/redux/modules/profiles';

const allowedErrors = [401, 403];

export const useStateAuthenticated = (service) => {
const dispatch = useDispatch();

const { pathname } = useLocation();
const navigate = useNavigate();

const { authenticated } = useSelector(profilesSlice.selectors.getState);
const { errorStatus } = useSelector(errorsSlice.selectors.getState);

const isAuthenticated = useRef(false);
isAuthenticated.current = !!authenticated;

const needRefresh = useRef(0);
const prevPathname = useRef(pathname);

if (
prevPathname.current !== pathname
&& pathname.startsWith(`/${service}`)
&& !isAuthenticated.current
) {
needRefresh.current += 1;
}

console.log(
'useStateAuthenticated',
prevPathname.current,
pathname,
needRefresh.current,
);

prevPathname.current = pathname;

const redirectToConnection = useEventCallback(() => {
const path = `/settings/connection/${service}`;
if (pathname !== path && service) {
navigate(path, { state: { from: pathname } });
}
});

useEffect(
() => {
console.log('request', service, needRefresh.current);
isAuthenticated.current = false;
dispatch(profilesSlice.actions.clear());
dispatch(profilesSlice.actions.fetchAuthenticated(null, 'authenticated'));

return () => {
dispatch(profilesSlice.actions.cancel('authenticated'));
};
},
[service, needRefresh.current],
);

useEffect(
() => {
if (!allowedErrors.includes(errorStatus)) {
return;
}

isAuthenticated.current = false;

dispatch(profilesSlice.actions.clear());
dispatch(errorsSlice.actions.clear());

redirectToConnection();
},
[errorStatus],
);

return isAuthenticated.current;
};

0 comments on commit c08991c

Please sign in to comment.