Merge pull request 'ContextHooks' (#11) from ContextHooks into master

Reviewed-by: wrzesinski-hubert <wrzesinski.hubert@gmail.com>
This commit is contained in:
wrzesinski-hubert 2020-07-10 21:39:26 +02:00
commit bce990e1f0
6 changed files with 68 additions and 92 deletions

View File

@ -1,4 +1,4 @@
import React, { useState } from "react"; import React, { useState, useContext } from "react";
import TopBar from "./components/TopBar/"; import TopBar from "./components/TopBar/";
import Transfer from "./components/Transfer/"; import Transfer from "./components/Transfer/";
import "./App.scss"; import "./App.scss";
@ -7,17 +7,16 @@ import { appointments } from "./components/Calendar/appointments";
import RightBar from "./components/RightBar"; import RightBar from "./components/RightBar";
import { lectures } from "./lectures"; import { lectures } from "./lectures";
import BusinessLogicContext from "./businesslogic/BusinessLogicContext"; import { BusinessLogicContext } from "./businesslogic/BusinessLogicProvider";
import { BuisnessProvided } from "./businesslogic/BusinessLogicProvider";
function App() { function App() {
const [isOpenTransfer, setOpenTransfer] = useState(false); const [isOpenTransfer, setOpenTransfer] = useState(false);
const [text, setText] = useState(""); const [text, setText] = useState("");
const { logout } = useContext(BusinessLogicContext).actions;
return ( return (
<div className="App"> <div className="App">
<BusinessLogicContext.Consumer>
{(context) => (
<TopBar <TopBar
textChangeHandler={(e) => { textChangeHandler={(e) => {
setText(e.target.value); setText(e.target.value);
@ -29,11 +28,9 @@ function App() {
console.log(e); console.log(e);
}} }}
handleLogout={() => { handleLogout={() => {
(context as BuisnessProvided).reducers.userlogout(); logout();
}} }}
/> />
)}
</BusinessLogicContext.Consumer>
<Transfer <Transfer
isOpen={isOpenTransfer} isOpen={isOpenTransfer}
handleClose={(e) => { handleClose={(e) => {

View File

@ -1,5 +0,0 @@
import React from "react";
const BusinessLogicContext = React.createContext({});
export default BusinessLogicContext;

View File

@ -1,76 +1,54 @@
import BusinessLogicContext from "./BusinessLogicContext"; import React, { useState, useEffect } from "react";
import React, { Component } from "react";
import { User } from "./models/user"; import { User } from "./models/user";
import { redirectToCASLoginService, redirectToCASLogoutService } from "./utilites";
export interface BuisnessProvided { export interface IBusinessLogicContext {
states: BusinessState; user: User | null;
reducers: { actions: {
userlogout: () => void; logout: () => void;
}; };
} }
interface BusinessState { export const BusinessLogicContext = React.createContext<IBusinessLogicContext>({
user: User | null; user: null,
} actions: { logout: () => {} },
});
interface Props {} interface Props {}
class BusinessLogicProvider extends Component<Props, BusinessState> { export const BusinessLogicProvider: React.FC<Props> = (props) => {
constructor(props: Props) { const [user, setUser] = useState<User | null>(null);
super(props);
this.state = {
user: null,
};
}
componentDidMount() { useEffect(() => {
this.login(); login();
} }, []);
login() { const login = () => {
const urlParams = new URLSearchParams(window.location.search); const urlParams = new URLSearchParams(window.location.search);
const ticket = urlParams.get("ticket"); const ticket = urlParams.get("ticket");
if (!ticket) { if (!ticket) {
this.redirectToCASLoginService(); redirectToCASLoginService();
} }
if (ticket) { if (ticket) {
this.setState({ user: { ticket } }); setUser({ ticket });
}
} }
};
logout() { const logout = () => {
this.redirectToCASLogoutService(); 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 ( return (
<BusinessLogicContext.Provider <BusinessLogicContext.Provider
value={{ value={{
states: this.state, user: user,
reducers: { actions: {
userlogout: () => { logout: logout,
this.logout();
},
}, },
}} }}
> >
{this.props.children} {props.children}
</BusinessLogicContext.Provider> </BusinessLogicContext.Provider>
); );
} };
}
export default BusinessLogicProvider;

View File

@ -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`);
};

View File

@ -1,9 +1,8 @@
import React, { useState } from "react"; import React, { useState, useContext } from "react";
import "./index.scss"; import "./index.scss";
import { Lecture } from "../../lectures"; import { Lecture } from "../../lectures";
import LectureCard from "./LectureCard"; import LectureCard from "./LectureCard";
import BusinessLogicContext from "../../businesslogic/BusinessLogicContext"; import { BusinessLogicContext } from "../../businesslogic/BusinessLogicProvider";
import { BuisnessProvided } from "../../businesslogic/BusinessLogicProvider";
interface RightBarProps { interface RightBarProps {
onGroupMouseOver: (id: string, name: string) => void; onGroupMouseOver: (id: string, name: string) => void;
@ -14,6 +13,8 @@ interface RightBarProps {
export default function RightBar({ lectures, onGroupMouseOver, onGroupClick }: RightBarProps) { export default function RightBar({ lectures, onGroupMouseOver, onGroupClick }: RightBarProps) {
const [selectedCardId, setSelectedCardId] = useState<string | null>(null); const [selectedCardId, setSelectedCardId] = useState<string | null>(null);
const ticket = useContext(BusinessLogicContext).user?.ticket;
const onCardClick = (e: React.MouseEvent) => { const onCardClick = (e: React.MouseEvent) => {
const target = e.currentTarget as HTMLElement; const target = e.currentTarget as HTMLElement;
selectedCardId === target.id ? setSelectedCardId(null) : setSelectedCardId(target.id); selectedCardId === target.id ? setSelectedCardId(null) : setSelectedCardId(target.id);
@ -21,9 +22,7 @@ export default function RightBar({ lectures, onGroupMouseOver, onGroupClick }: R
return ( return (
<div className="right-bar"> <div className="right-bar">
<BusinessLogicContext.Consumer> <p>{ticket}</p>
{(context) => <p>{JSON.stringify((context as BuisnessProvided).states.user?.ticket)}</p>}
</BusinessLogicContext.Consumer>
<div className="right-bar__text"> <div className="right-bar__text">
Hubert Wrzesiński<br></br> Hubert Wrzesiński<br></br>
Semestr zimowy 2020/2021 Semestr zimowy 2020/2021

View File

@ -1,13 +1,13 @@
import React from "react"; import React from "react";
import ReactDOM from "react-dom"; import ReactDOM from "react-dom";
import App from "./App"; import App from "./App";
import BuisnessLogicProvider from "./businesslogic/BusinessLogicProvider"; import { BusinessLogicProvider } from "./businesslogic/BusinessLogicProvider";
ReactDOM.render( ReactDOM.render(
<> <>
<BuisnessLogicProvider> <BusinessLogicProvider>
<App /> <App />
</BuisnessLogicProvider> </BusinessLogicProvider>
</>, </>,
document.getElementById("root") document.getElementById("root")
); );