Skip to content

Commit

Permalink
Review when to get getLoginInfo and getInitialState
Browse files Browse the repository at this point in the history
  • Loading branch information
axelboc committed Dec 11, 2023
1 parent 17fa748 commit f320c70
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 46 deletions.
36 changes: 15 additions & 21 deletions ui/src/actions/login.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,25 +86,23 @@ export function sendSelectProposal(number, navigate) {
export function getLoginInfo() {
return async (dispatch) => {
const loginInfo = await fetchLoginInfo();
if (!loginInfo.loggedIn) {
dispatch(resetLoginInfo());
throw new Error('Not authenticated');
}
dispatch(setLoginInfo(loginInfo));
};
}

export function logIn(proposal, password) {
return (dispatch) => {
return sendLogIn(proposal, password).then((res) => {
if (res.msg === '') {
dispatch(showErrorPanel(false));
dispatch(getInitialState());
} else {
dispatch(showErrorPanel(true, res.msg));
dispatch(setLoading(false));
}
});
return async (dispatch) => {
const res = await sendLogIn(proposal, password);

if (res.msg !== '') {
dispatch(showErrorPanel(true, res.msg));
dispatch(setLoading(false));
return;
}

dispatch(showErrorPanel(false));
await dispatch(getLoginInfo());
await dispatch(getInitialState());
};
}

Expand All @@ -119,16 +117,15 @@ export function signOut() {
return sendSignOut().then(() => {
dispatch({ type: 'SIGNOUT' });
dispatch(resetLoginInfo());
dispatch(applicationFetched(false));
});
};
}

export function getInitialState(navigate) {
return async (dispatch) => {
return (dispatch) => {
const state = {};

await dispatch(getLoginInfo());

const sampleVideoInfo = fetch('mxcube/api/v0.1/sampleview/camera', {
method: 'GET',
credentials: 'include',
Expand Down Expand Up @@ -270,11 +267,8 @@ export function getInitialState(navigate) {
.catch(notify),
];

const prom = Promise.all(pchains).then(() => {
Promise.all(pchains).then(() => {
dispatch(setInitialState(state));
});

prom.then(() => {
dispatch(applicationFetched(true));
});
};
Expand Down
37 changes: 12 additions & 25 deletions ui/src/components/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,9 @@ import HelpContainer from '../containers/HelpContainer';
import Main from './Main';
import LoadingScreen from '../components/LoadingScreen/LoadingScreen';

import { store } from '../store';
import { serverIO } from '../serverIO';
import { applicationFetched } from '../actions/general';
import { getInitialState } from '../actions/login';

export async function requireAuth() {
try {
store.dispatch(applicationFetched(false));
await store.dispatch(getInitialState());
serverIO.listen();
} catch {
store.dispatch(applicationFetched(true));
}
}
import { getLoginInfo } from '../actions/login';
import { bindActionCreators } from 'redux';

function PrivateOutlet() {
const loggedIn = useSelector((state) => state.login.loggedIn);
Expand Down Expand Up @@ -92,28 +81,26 @@ const router = createBrowserRouter([
]);

function App(props) {
const { applicationFetched } = props;
const { loggedIn, getLoginInfo } = props;

useEffect(() => {
requireAuth();
getLoginInfo();
serverIO.listen();

return () => {
serverIO.disconnect();
};
}, []);
}, [getLoginInfo]);

if (!applicationFetched) {
if (loggedIn === null) {
// Fetching login info
return <LoadingScreen />;
}

return <RouterProvider router={router} />;
}

function mapStateToProps(state) {
return {
loggedIn: state.login.loggedIn,
applicationFetched: state.general.applicationFetched,
};
}

export default connect(mapStateToProps)(App);
export default connect(
(state) => ({ loggedIn: state.login.loggedIn }),
(dispatch) => ({ getLoginInfo: bindActionCreators(getLoginInfo, dispatch) }),
)(App);
6 changes: 6 additions & 0 deletions ui/src/components/Main.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ import styles from './Main.module.css';

import 'react-chat-widget/lib/styles.css';
import './rachat.css';
import { store } from '../store';
import { getInitialState } from '../actions/login';

class Main extends React.Component {
constructor(props) {
Expand All @@ -43,6 +45,10 @@ class Main extends React.Component {
}

componentDidMount() {
if (!this.props.applicationFetched) {
store.dispatch(getInitialState());
}

// eslint-disable-next-line promise/prefer-await-to-then, promise/catch-or-return
getAllChatMessages().then((json) => {
json.messages.forEach((entry) => {
Expand Down

0 comments on commit f320c70

Please sign in to comment.