diff --git a/src/App.tsx b/src/App.tsx index e5ab92c..5897bee 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -6,23 +6,34 @@ import Schedule from "./components/Calendar/"; 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"; + function App() { const [isOpenTransfer, setOpenTransfer] = useState(false); const [text, setText] = useState(""); return (
- { - setText(e.target.value); - }} - handleTransfer={(e) => { - setOpenTransfer(!isOpenTransfer); - }} - onLangChange={(e) => { - console.log(e); - }} - /> + + {(context) => ( + { + setText(e.target.value); + }} + handleTransfer={(e) => { + setOpenTransfer(!isOpenTransfer); + }} + onLangChange={(e) => { + console.log(e); + }} + handleLogout={() => { + (context as BuisnessProvided).reducers.userlogout(); + }} + /> + )} + { diff --git a/src/businesslogic/BusinessLogicContext.ts b/src/businesslogic/BusinessLogicContext.ts new file mode 100644 index 0000000..40ffea1 --- /dev/null +++ b/src/businesslogic/BusinessLogicContext.ts @@ -0,0 +1,5 @@ +import React from "react"; + +const BusinessLogicContext = React.createContext({}); + +export default BusinessLogicContext; diff --git a/src/businesslogic/BusinessLogicProvider.tsx b/src/businesslogic/BusinessLogicProvider.tsx new file mode 100644 index 0000000..4cdb84a --- /dev/null +++ b/src/businesslogic/BusinessLogicProvider.tsx @@ -0,0 +1,76 @@ +import BusinessLogicContext from "./BusinessLogicContext"; +import React, { Component } from "react"; +import { User } from "./models/user"; + +export interface BuisnessProvided { + states: BusinessState; + reducers: { + userlogout: () => void; + }; +} + +interface BusinessState { + user: User | null; +} + +interface Props {} + +class BusinessLogicProvider extends Component { + constructor(props: Props) { + super(props); + this.state = { + user: null, + }; + } + + componentDidMount() { + this.login(); + } + + login() { + const urlParams = new URLSearchParams(window.location.search); + const ticket = urlParams.get("ticket"); + + if (!ticket) { + this.redirectToCASLoginService(); + } + if (ticket) { + this.setState({ user: { ticket } }); + } + } + + logout() { + this.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; diff --git a/src/businesslogic/models/user.ts b/src/businesslogic/models/user.ts new file mode 100644 index 0000000..dee0426 --- /dev/null +++ b/src/businesslogic/models/user.ts @@ -0,0 +1,5 @@ +export type User = { + name?: string; + surname?: string; + ticket: string | null; +}; diff --git a/src/components/RightBar/index.tsx b/src/components/RightBar/index.tsx index 4007716..4cec1df 100644 --- a/src/components/RightBar/index.tsx +++ b/src/components/RightBar/index.tsx @@ -2,6 +2,8 @@ import React, { useState } from "react"; import "./index.scss"; import { Lecture } from "../../lectures"; import LectureCard from "./LectureCard"; +import BusinessLogicContext from "../../businesslogic/BusinessLogicContext"; +import { BuisnessProvided } from "../../businesslogic/BusinessLogicProvider"; interface RightBarProps { onGroupMouseOver: (id: string, name: string) => void; @@ -19,6 +21,9 @@ export default function RightBar({ lectures, onGroupMouseOver, onGroupClick }: R return (
+ + {(context) =>

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

} +
Hubert WrzesiƄski

Semestr zimowy 2020/2021 diff --git a/src/components/TopBar/Profile.tsx b/src/components/TopBar/Profile.tsx index 8b0a920..215598b 100644 --- a/src/components/TopBar/Profile.tsx +++ b/src/components/TopBar/Profile.tsx @@ -1,15 +1,18 @@ import { Menu, MenuItem } from "@material-ui/core"; import React, { FC } from "react"; - interface ProfileProps { - anchorEl: HTMLElement | null; - handleClose: () => void - + anchorEl: HTMLElement | null; + handleClose: () => void; + handleLogout: () => void; } -export const Profile : FC = ({anchorEl, handleClose, ...restProps}) => { - +export const Profile: FC = ({ + anchorEl, + handleClose, + handleLogout, + ...restProps +}) => { return ( = ({anchorEl, handleClose, ...restProps} > Profile My account - Logout + { + handleLogout(); + }} + > + Logout + ); }; diff --git a/src/components/TopBar/index.tsx b/src/components/TopBar/index.tsx index 4d5c22d..c0455ce 100644 --- a/src/components/TopBar/index.tsx +++ b/src/components/TopBar/index.tsx @@ -12,7 +12,10 @@ import { Profile } from "./Profile"; interface TopBarProps { handleTransfer: (e: React.MouseEvent) => void; onLangChange: (lang: boolean) => void; - textChangeHandler: (e: React.ChangeEvent) => void; + handleLogout: () => void; + textChangeHandler: ( + e: React.ChangeEvent + ) => void; } interface TopBarState { @@ -64,29 +67,55 @@ export default class TopBar extends React.Component { return (
- logo + logo
plan na plan
- search + search this.handleChange(e)} /> - close + close
- transfer + transfer change_language - profile - + profile +
); diff --git a/src/index.tsx b/src/index.tsx index 652b446..628c5bc 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -1,10 +1,13 @@ import React from "react"; import ReactDOM from "react-dom"; import App from "./App"; +import BuisnessLogicProvider from "./businesslogic/BusinessLogicProvider"; ReactDOM.render( <> - + + + , document.getElementById("root") );