From 5b153d11cfd42e8c30e1d26949d06a5c113bed4e Mon Sep 17 00:00:00 2001 From: maciekglowacki Date: Mon, 29 Jun 2020 22:12:53 +0200 Subject: [PATCH 1/2] Simplified context with hooks --- src/App.tsx | 39 ++++----- src/businesslogic/BusinessLogicContext.ts | 5 -- src/businesslogic/BusinessLogicProvider.tsx | 92 ++++++++------------- src/businesslogic/utilites.ts | 7 ++ src/components/RightBar/index.tsx | 11 ++- src/index.tsx | 6 +- 6 files changed, 68 insertions(+), 92 deletions(-) delete mode 100644 src/businesslogic/BusinessLogicContext.ts create mode 100644 src/businesslogic/utilites.ts diff --git a/src/App.tsx b/src/App.tsx index 5897bee..327f1f6 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -1,4 +1,4 @@ -import React, { useState } from "react"; +import React, { useState, useContext } from "react"; import TopBar from "./components/TopBar/"; import Transfer from "./components/Transfer/"; import "./App.scss"; @@ -7,33 +7,30 @@ import { appointments } from "./components/Calendar/appointments"; import RightBar from "./components/RightBar"; import { lectures } from "./lectures"; -import BusinessLogicContext from "./businesslogic/BusinessLogicContext"; -import { BuisnessProvided } from "./businesslogic/BusinessLogicProvider"; +import { BusinessLogicContext } from "./businesslogic/BusinessLogicProvider"; function App() { const [isOpenTransfer, setOpenTransfer] = useState(false); const [text, setText] = useState(""); + const businessLogicContext = useContext(BusinessLogicContext); + return (
- - {(context) => ( - { - setText(e.target.value); - }} - handleTransfer={(e) => { - setOpenTransfer(!isOpenTransfer); - }} - onLangChange={(e) => { - console.log(e); - }} - handleLogout={() => { - (context as BuisnessProvided).reducers.userlogout(); - }} - /> - )} - + { + setText(e.target.value); + }} + handleTransfer={(e) => { + setOpenTransfer(!isOpenTransfer); + }} + onLangChange={(e) => { + console.log(e); + }} + handleLogout={() => { + businessLogicContext.actions.logout(); + }} + /> { diff --git a/src/businesslogic/BusinessLogicContext.ts b/src/businesslogic/BusinessLogicContext.ts deleted file mode 100644 index 40ffea1..0000000 --- a/src/businesslogic/BusinessLogicContext.ts +++ /dev/null @@ -1,5 +0,0 @@ -import React from "react"; - -const BusinessLogicContext = React.createContext({}); - -export default BusinessLogicContext; diff --git a/src/businesslogic/BusinessLogicProvider.tsx b/src/businesslogic/BusinessLogicProvider.tsx index 4cdb84a..44b51bd 100644 --- a/src/businesslogic/BusinessLogicProvider.tsx +++ b/src/businesslogic/BusinessLogicProvider.tsx @@ -1,76 +1,54 @@ -import BusinessLogicContext from "./BusinessLogicContext"; -import React, { Component } from "react"; +import React, { useState, useEffect } from "react"; import { User } from "./models/user"; +import { redirectToCASLoginService, redirectToCASLogoutService } from "./utilites"; -export interface BuisnessProvided { - states: BusinessState; - reducers: { - userlogout: () => void; +export interface IBusinessLogicContext { + user: User | null; + actions: { + logout: () => void; }; } -interface BusinessState { - user: User | null; -} +export const BusinessLogicContext = React.createContext({ + user: null, + actions: { logout: () => {} }, +}); interface Props {} -class BusinessLogicProvider extends Component { - constructor(props: Props) { - super(props); - this.state = { - user: null, - }; - } +export const BusinessLogicProvider: React.FC = (props) => { + const [user, setUser] = useState(null); - componentDidMount() { - this.login(); - } + useEffect(() => { + login(); + }, []); - login() { + const login = () => { const urlParams = new URLSearchParams(window.location.search); const ticket = urlParams.get("ticket"); if (!ticket) { - this.redirectToCASLoginService(); + redirectToCASLoginService(); } if (ticket) { - this.setState({ user: { ticket } }); + setUser({ ticket }); } - } + }; - logout() { - this.redirectToCASLogoutService(); - } + const logout = () => { + redirectToCASLogoutService(); + }; - redirectToCASLogoutService() { - window.location.replace( - `https://cas.amu.edu.pl/cas/logout?service=${window.origin}` - ); - } - - redirectToCASLoginService() { - window.location.replace( - `https://cas.amu.edu.pl/cas/login?service=${window.origin}&locale=pl` - ); - } - - render() { - return ( - { - this.logout(); - }, - }, - }} - > - {this.props.children} - - ); - } -} - -export default BusinessLogicProvider; + return ( + + {props.children} + + ); +}; diff --git a/src/businesslogic/utilites.ts b/src/businesslogic/utilites.ts new file mode 100644 index 0000000..1d3952b --- /dev/null +++ b/src/businesslogic/utilites.ts @@ -0,0 +1,7 @@ +export const redirectToCASLogoutService = () => { + window.location.replace(`https://cas.amu.edu.pl/cas/logout?service=${window.origin}`); +}; + +export const redirectToCASLoginService = () => { + window.location.replace(`https://cas.amu.edu.pl/cas/login?service=${window.origin}&locale=pl`); +}; diff --git a/src/components/RightBar/index.tsx b/src/components/RightBar/index.tsx index 4cec1df..a31fa5c 100644 --- a/src/components/RightBar/index.tsx +++ b/src/components/RightBar/index.tsx @@ -1,9 +1,8 @@ -import React, { useState } from "react"; +import React, { useState, useContext } from "react"; import "./index.scss"; import { Lecture } from "../../lectures"; import LectureCard from "./LectureCard"; -import BusinessLogicContext from "../../businesslogic/BusinessLogicContext"; -import { BuisnessProvided } from "../../businesslogic/BusinessLogicProvider"; +import { BusinessLogicContext } from "../../businesslogic/BusinessLogicProvider"; interface RightBarProps { onGroupMouseOver: (id: string, name: string) => void; @@ -14,6 +13,8 @@ interface RightBarProps { export default function RightBar({ lectures, onGroupMouseOver, onGroupClick }: RightBarProps) { const [selectedCardId, setSelectedCardId] = useState(null); + const businessLogicContext = useContext(BusinessLogicContext); + const onCardClick = (e: React.MouseEvent) => { const target = e.currentTarget as HTMLElement; selectedCardId === target.id ? setSelectedCardId(null) : setSelectedCardId(target.id); @@ -21,9 +22,7 @@ export default function RightBar({ lectures, onGroupMouseOver, onGroupClick }: R return (
- - {(context) =>

{JSON.stringify((context as BuisnessProvided).states.user?.ticket)}

} -
+

{businessLogicContext.user?.ticket}

Hubert Wrzesiński

Semestr zimowy 2020/2021 diff --git a/src/index.tsx b/src/index.tsx index 628c5bc..94b1939 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -1,13 +1,13 @@ import React from "react"; import ReactDOM from "react-dom"; import App from "./App"; -import BuisnessLogicProvider from "./businesslogic/BusinessLogicProvider"; +import { BusinessLogicProvider } from "./businesslogic/BusinessLogicProvider"; ReactDOM.render( <> - + - + , document.getElementById("root") ); From 51ee8c44fad376f1526ec5eecb98b02c93a86dac Mon Sep 17 00:00:00 2001 From: maciekglowacki Date: Mon, 29 Jun 2020 22:43:50 +0200 Subject: [PATCH 2/2] Simplified context with hooks --- src/App.tsx | 4 ++-- src/components/RightBar/index.tsx | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/App.tsx b/src/App.tsx index 327f1f6..70fa0c8 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -13,7 +13,7 @@ function App() { const [isOpenTransfer, setOpenTransfer] = useState(false); const [text, setText] = useState(""); - const businessLogicContext = useContext(BusinessLogicContext); + const { logout } = useContext(BusinessLogicContext).actions; return (
@@ -28,7 +28,7 @@ function App() { console.log(e); }} handleLogout={() => { - businessLogicContext.actions.logout(); + logout(); }} /> (null); - const businessLogicContext = useContext(BusinessLogicContext); + const ticket = useContext(BusinessLogicContext).user?.ticket; const onCardClick = (e: React.MouseEvent) => { const target = e.currentTarget as HTMLElement; @@ -22,7 +22,7 @@ export default function RightBar({ lectures, onGroupMouseOver, onGroupClick }: R return (
-

{businessLogicContext.user?.ticket}

+

{ticket}

Hubert Wrzesiński

Semestr zimowy 2020/2021