Skip to content

Commit

Permalink
header: logout integration
Browse files Browse the repository at this point in the history
  • Loading branch information
ashwanth1109 committed Jan 9, 2021
1 parent 24e1612 commit bc92bab
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 14 deletions.
4 changes: 2 additions & 2 deletions container/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ const HeaderContainer = styled.div`
`;

const App = () => {
const { login, history, isSignedIn$ } = useAuth();
const { login, history, isSignedIn$, logout } = useAuth();

return (
<div>
<HeaderContainer>
<Header isSignedIn$={isSignedIn$}/>
<Header isSignedIn$={isSignedIn$} logout={logout} />
</HeaderContainer>

<div>
Expand Down
6 changes: 3 additions & 3 deletions container/src/hooks/useAuth.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { useCallback, useEffect } from "react";
import { useHistory } from "react-router-dom";
import { History } from "history";
import { BehaviorSubject } from "rxjs";
import { BehaviorSubject, Observable } from "rxjs";

interface UseAuthFunctions {
login: VoidFunction;
logout: VoidFunction;
history: History<unknown>;
isSignedIn$: BehaviorSubject<boolean>;
isSignedIn$: Observable<boolean>;
}

const isSignedIn$ = new BehaviorSubject<boolean>(false);
Expand All @@ -32,7 +32,7 @@ const useAuth = (): UseAuthFunctions => {
const login = useCallback(() => isSignedIn$.next(true), []);
const logout = useCallback(() => isSignedIn$.next(false), []);

return { login, logout, history, isSignedIn$ };
return { login, logout, history, isSignedIn$: isSignedIn$.asObservable() };
};

export default useAuth;
12 changes: 9 additions & 3 deletions container/src/modules/Header.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
import React, { useEffect, useRef } from "react";
import { mount } from "header/HeaderComponent";
import useRouter from "../hooks/useRouter";
import { BehaviorSubject } from "rxjs";
import { Observable } from "rxjs";

export default ({ isSignedIn$ }: { isSignedIn$: BehaviorSubject<boolean> }) => {
export default ({
isSignedIn$,
logout,
}: {
isSignedIn$: Observable<boolean>;
logout: VoidFunction;
}) => {
const ref = useRef<HTMLDivElement>(null);
const { navigate } = useRouter();

useEffect(() => {
mount(ref.current, { navigate, isSignedIn$ });
mount(ref.current, { navigate, isSignedIn$, logout });
}, []);

return <div ref={ref} />;
Expand Down
3 changes: 2 additions & 1 deletion container/src/typings/header.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
interface HeaderMountOptions {
navigate: NavigateFunction;
isSignedIn$: import("rxjs").BehaviorSubject<boolean>;
isSignedIn$: import("rxjs").Observable<boolean>;
logout: VoidFunction;
}

declare module "header/HeaderComponent" {
Expand Down
4 changes: 3 additions & 1 deletion header/src/Header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const Container = styled.div`
justify-content: space-between;
`;

const Header = ({ navigate, isSignedIn$ }: HeaderMountOptions) => {
const Header = ({ navigate, isSignedIn$, logout }: HeaderMountOptions) => {
const [isSignedIn, setIsSignedIn] = useState(false);

const navigateToLanding = useCallback(() => {
Expand All @@ -38,9 +38,11 @@ const Header = ({ navigate, isSignedIn$ }: HeaderMountOptions) => {
<h3 onClick={navigateToLanding} style={{ cursor: "pointer" }}>
Header Microfrontend
</h3>

<Button
variant="outlined"
style={{ color: "white", borderColor: "white" }}
onClick={() => (isSignedIn ? logout() : navigate("/auth/login"))}
>
{isSignedIn ? "Logout" : "Login"}
</Button>
Expand Down
9 changes: 5 additions & 4 deletions header/src/bootstrap.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import React from "react";
import ReactDOM from "react-dom";
import Header from "./Header";
import { BehaviorSubject } from "rxjs";
import { Observable } from "rxjs";

const mount = (el: Element, { navigate, isSignedIn$ }: HeaderMountOptions) => {
ReactDOM.render(<Header navigate={navigate} isSignedIn$={isSignedIn$} />, el);
const mount = (el: Element, options: HeaderMountOptions) => {
ReactDOM.render(<Header {...options} />, el);
};

if (process.env.NODE_ENV === "development") {
Expand All @@ -15,7 +15,8 @@ if (process.env.NODE_ENV === "development") {
navigate: (route: string) => {
console.log(`Navigate to route: ${route}`);
},
isSignedIn$: new BehaviorSubject<boolean>(false),
isSignedIn$: new Observable<boolean>(),
logout: () => {},
};

mount(rootNode, mockOptions);
Expand Down

0 comments on commit bc92bab

Please sign in to comment.