diff --git a/src/components/app.js b/src/components/app.js index 96b60c2..11e895d 100644 --- a/src/components/app.js +++ b/src/components/app.js @@ -1,4 +1,4 @@ -import { Component, createRef, h } from "preact"; +import { Component, h } from "preact"; import { Router } from "preact-router"; import Header from "./header"; import Login from "../routes/login"; @@ -6,9 +6,13 @@ import Profile from "../routes/profile"; import Menu from "./menu"; import Users from "../routes/users"; import { useCallback, useState } from "preact/hooks"; -const menu = createRef(); -class App extends Component { - toggleMenu() { +import { AppData, Provider, Consumer } from '../utils' +const App = () => { + const appdata = AppData(); + let session = appdata.get("Session"); + //session.sub(forceUpdate); + const toggleMenu = () => { + const [visible, setValue] = useState(false); const toggle = useCallback(() => { setValue(!visible); @@ -16,29 +20,24 @@ class App extends Component { }, [visible]); return { visible, toggle }; } - authenticateUser() { - const [session, setValue] = useState({ token: null }); - const login = (username, password) => { - if (username == "admin" && password == "admin") { - setValue({ ...session, token: "ABCDEFG" }); - return true; - } - }; - const logout = () => setValue({ ...session, token: null }); - const isAuthenticated = () => session.token != null; - return { login, logout, isAuthenticated }; - } - render() { - const menu = this.toggleMenu(); - const auth = this.authenticateUser(); - return ( + + + const menu = toggleMenu(); + return ( + +
-
+ + {(appdata) => { + console.log(appdata); + }} + +
{!menu.visible && - (!auth.isAuthenticated() ? ( - + (!session.data || session.data.username === '' ? ( + ) : (
@@ -50,8 +49,9 @@ class App extends Component {
))}
- ); - } +
+ ); + } export default App; diff --git a/src/components/header/index.jsx b/src/components/header/index.jsx index 5188ee1..c5777af 100644 --- a/src/components/header/index.jsx +++ b/src/components/header/index.jsx @@ -1,20 +1,23 @@ -import { Component, createRef, h } from 'preact'; +import { h, forceUpdate } from 'preact'; +import { Consumer } from '../../utils' //import { Link } from 'preact-router/match'; -const Header = (props) => ( -
-
-

Login

- {props.auth.isAuthenticated() && (
props.menu.toggle()}> -
-
-
-
)} -
-
-); - +const Header = (props, ctx) => { + console.log(ctx.data) + return ( +
+
+

Login

+ {ctx.data && (
props.menu.toggle()}> +
+
+
+
)} +
+
+ ); +} diff --git a/src/index.js b/src/index.js index 1d691c9..3ff9b5d 100644 --- a/src/index.js +++ b/src/index.js @@ -1,6 +1,6 @@ import './style/style.sass'; import App from './components/app'; -import { h, render, Component } from "preact" +import { h, render } from "preact" render(, document.body) -//export default App; + diff --git a/src/routes/login/index.jsx b/src/routes/login/index.jsx index b41f4d8..51f2a3e 100644 --- a/src/routes/login/index.jsx +++ b/src/routes/login/index.jsx @@ -1,41 +1,41 @@ -import { Component, h } from 'preact'; +import { h } from 'preact'; +import { useState } from 'preact/hooks'; + import Breadcrumbs from '../../components/breadcrumbs'; -class Login extends Component { - state = { username: '', password: '' }; - navigation = ["Login"]; - onSubmit = (e) => { +const Login = (props,ctx) => { + const [val,set] = useState({ username: '', password: '' }); + const navigation = ["Login"]; + const session = ctx.get('Session'); + function onSubmit (e) { e.preventDefault(); - console.log(this.state); - this.setState({ username: '', password: '' }); - if(!this.props.auth.login(this.state.username,this.state.password)) - alert("Wrong login") + session.data = val; + console.log(session.data); + set({username: '', password: '' }); } - render() { + return (
+
+ - return (
-
- +
+

+ Bitte melden Sie sich mit ihren Nutzerdaten an. +

+
+ set( {...val, username: e.target.value} )} value={val.username} /> + +
+
+ set( {...val, password: e.target.value} )} value={val.password} /> + +
+ - -

- Bitte melden Sie sich mit ihren Nutzerdaten an. -

-
- this.setState(prev => ({ ...prev, username: e.target.value }))} value={this.state.username} /> - -
-
- this.setState(prev => ({ ...prev, password: e.target.value }))} value={this.state.password} /> - -
- +
+
+
); - -
-
); - } } export default Login; \ No newline at end of file diff --git a/src/utils/appdata/Consumer.js b/src/utils/appdata/Consumer.js new file mode 100644 index 0000000..297be4d --- /dev/null +++ b/src/utils/appdata/Consumer.js @@ -0,0 +1,10 @@ +function Consumer(props, ctx){ + this.componentWillMount=()=>{ + console.log("Component mounted"); + } + + if(props.datapath!==undefined) + return props.children(ctx.get(props.datapath)); + return props.children(ctx); +} +export default Consumer; \ No newline at end of file diff --git a/src/utils/appdata/ObservableData.js b/src/utils/appdata/ObservableData.js new file mode 100644 index 0000000..c09f9fb --- /dev/null +++ b/src/utils/appdata/ObservableData.js @@ -0,0 +1,40 @@ + + +function subscribe (f) { + if ('function' !== typeof (f)) + throw new Error("Subscriber needs to be a function."); + this.observers.push(f); + return () => { this.observers.splice(this.observers.indexOf(f), 1) } +} +function getStore(path){ + let path_slices = path.split("/"); + let current=this; + for(let s of path_slices){ + if(!(s in current.tree)) + current.tree[s] = new ObservableData(); + current = current.tree[s]; + } + return current; +} +function ObservableData() { + this.observers = []; + this.tree = {}; + this._data = {}; + const dispatchChange = () => { + this.observers.map(o => o()); + } + return { + tree:this.tree, + sub: subscribe.bind(this), + get: getStore.bind(this), + get data() { + return this._data; + }, + set data(val) { + this._data = val; + dispatchChange(); + }, + + } +} +export default ObservableData; \ No newline at end of file diff --git a/src/utils/appdata/Provider.js b/src/utils/appdata/Provider.js new file mode 100644 index 0000000..f17f551 --- /dev/null +++ b/src/utils/appdata/Provider.js @@ -0,0 +1,14 @@ +function Provider(props) { + if (this.root !== undefined) props.appdata = this.root; + if (!this.getChildContext && props.appdata !== undefined) { + this.getChildContext = () => props.appdata; + this.shouldComponentUpdate = function (_props) { + if (this.props.appdata !== _props.appdata) { + return true; + } + } + } + + return props.children; +} +export default Provider; \ No newline at end of file diff --git a/src/utils/appdata/index.js b/src/utils/appdata/index.js new file mode 100644 index 0000000..8fbf7eb --- /dev/null +++ b/src/utils/appdata/index.js @@ -0,0 +1,22 @@ +import ObservableData from './ObservableData' +import Provider from './Provider' +import Consumer from './Consumer' + +let instance = null; +function AppData() { + if(instance===null){ + console.log("New AppData created") + const root = new ObservableData(); + root.get("affe1234/123"); + instance = { + data: root.data, + get: root.get, + Provider: Provider.bind(this), + Consumer: Consumer.bind(this), + } + } + + return instance; +} + +export default AppData; \ No newline at end of file diff --git a/src/utils/appdata/oldSession.js b/src/utils/appdata/oldSession.js new file mode 100644 index 0000000..3d60419 --- /dev/null +++ b/src/utils/appdata/oldSession.js @@ -0,0 +1,40 @@ +import {createContext} from 'preact'; + + +class Session { + static instance=null; + observers = []; + static getInstance(){ + if(this.instance==null) + this.instance = new Session(); + return this.instance; + } + constructor(){ + console.log("new Instance of Session manager created"); + this.state={token:null,expiry:null,name:null} + } + observe(o){ + this.observers.push(o); + } + get Context(){ + return createContext(this); + } + setState(updated){ + this.state = {...this.state,...updated} + this.observers.map(o=>o.forceUpdate()); + console.log(this.state); + } + Login(username, password) { + if (username == "admin" && password == "admin") + { this.setState({token:"TEST",expiry: new Date(Date.now() + 60000*60),name:username}); + return true; + } + return false; + } + Logout = () => this.setState({token:null,expiry:null,name:null}); + get isAuthenticated(){ + return this.state.token!=null + } +} + +export default ()=> Session.getInstance(); \ No newline at end of file diff --git a/src/utils/index.js b/src/utils/index.js new file mode 100644 index 0000000..67c4d37 --- /dev/null +++ b/src/utils/index.js @@ -0,0 +1,5 @@ +import AppData from './appdata' +import oldSession from './appdata/oldSession'; +import Provider from './appdata/Provider'; +import Consumer from './appdata/Consumer'; +export {AppData, Provider, Consumer, oldSession};