Skip to content
This repository has been archived by the owner on Jun 21, 2022. It is now read-only.

MF-56: Add logout to primary nav bar #5

Merged
merged 3 commits into from Sep 18, 2019
Merged

MF-56: Add logout to primary nav bar #5

merged 3 commits into from Sep 18, 2019

Conversation

joeldenning
Copy link
Collaborator

No description provided.

Copy link
Collaborator Author

@joeldenning joeldenning left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I think this is a good approach for redirecting to login.

const [isLoggedIn, setIsLoggedIn] = React.useState(true);

React.useEffect(() => {
authResource.getLoggedInUser().subscribe(({ person }) => {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should store the subscription and unsubscribe during the effect's cleanup function. See https://reactjs.org/docs/hooks-effect.html#effects-with-cleanup

const subscription = authResource.getLoggedInUser().subscribe(...)
return () => subscription.unsubscribe()

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops I keep forgetting to do that

.logoutUser()
.then(() => setIsLoggedIn(false))
.catch(err => {
throw err;
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.catch(err => {throw err}) is a no-op.

Calling .catch changes the promise from error to resolved status. But rethrowing in the callback sets it back into error status. This will cause an "unhandled promise rejection" error, which is not a good thing because not all browsers have a way of capturing and recording those.

It's good to be thinking about error handling, but we haven't established the patterns yet and this is not the right pattern. For now, you could delete this .catch() or you could do the following, which is closer to what the error handling will be:

.catch(err => {
  // setTimeout makes this error thrown to window instead of just changing
  // the promise's status.
  setTimeout(() => {
    throw err
  })
})

<div className={styles.avatar}>AB</div>
</nav>
<BrowserRouter>
<React.Fragment>
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: you can do <> instead of <React.Fragment> for all use cases except when you're adding a key prop to the fragment.

logoutUser
};

function getLoggedInUser() {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this function worth it? Maybe we just use getCurrentUser directly inside of the component?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed

src/root.component.tsx Outdated Show resolved Hide resolved
@fatmali
Copy link
Contributor

fatmali commented Sep 12, 2019

Hi @joeldenning, I updated this PR, however I noticed something strange. Sending a DELETE request doesn't log out the user so the user ends up being redirected to the home page yet again

Copy link
Collaborator Author

@joeldenning joeldenning left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Top priority is figuring out why the DELETE isn't working.

@@ -14,29 +32,65 @@ function Root() {
};
}, [sidenavOpen]);

function logout() {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: performing API calls inside of a click handler isn't usually best practice -- better to have an Effect handle the api request, and call setIsLoggingOut(true) inside of the click handler.

Reason is that effects get cleaned up when the component unmounts, but click handlers don't. Also that having an isLoggingOut boolean state property can be helpful in disabling other functionality while we're waiting for the logout to happen.

}).then(() => setIsLoggedIn(false));
}

function getOpenmrsSpaBase() {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function seems rather useless. Did you create it so you could @ts-ignore outside of the JSX?

See this issue for how you can use ts-ignore in JSX. The syntax is quite odd, but works.

{/* 
// @ts-ignore */}
{!isLoggedIn && <Redirect to={`${getOpenmrsSpaBase()}login`} />} 

Or:

{!isLoggedIn && <
  // @ts-ignore
  Redirect to={`${getOpenmrsSpaBase()}login`} />
} 

function logout() {
return openmrsFetch("/ws/rest/v1/session", {
method: "DELETE"
}).then(() => setIsLoggedIn(false));
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we're not handling errors on this again. Andrei and I will be working on the error handling stuff this week so we'll have better answers for how to handle the errors then. So your code here is fine for now. But just wanted to point out that it's always best to handle promise rejections.

@jonathandick
Copy link
Member

@fatmali , did you follow up on the delete request failing to log out the user? I had noticed something similar when trying to do so myself manually (from console).

@fatmali
Copy link
Contributor

fatmali commented Sep 17, 2019

@jonathandick @joeldenning Hitting DELETE now works as expected, also updated this PR with the changes requested.

Copy link
Collaborator Author

@joeldenning joeldenning left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Build failed. Looks like you committed some files without letting the prettier pre-commit hook run. Specifically the root.component.tsx file

Copy link
Collaborator Author

@joeldenning joeldenning left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@joeldenning joeldenning merged commit 7cd583b into master Sep 18, 2019
@joeldenning joeldenning deleted the MF-56 branch September 18, 2019 15:45
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants