Break into files

This commit is contained in:
Ceda EI 2019-03-29 11:01:28 +05:30
parent 762d95c815
commit 6c5dc60dac
5 changed files with 45 additions and 36 deletions

20
src/components/login.js Normal file
View File

@ -0,0 +1,20 @@
import React, { useState } from "react";
import PropTypes from "prop-types";
export default (player) => {
function Login(props) {
const [input, setInput] = useState("");
return (
<div className="login">
<input type="text" placeholder="Token ID" value={input}
onChange={evt => setInput(evt.target.value)} />
<button onClick={() => player.auth(input).then(x => props.setLoggedIn(x))}>Submit</button>
</div>
);
}
Login.propTypes = {
setLoggedIn: PropTypes.func,
};
return Login;
};

View File

@ -0,0 +1,7 @@
import React from "react";
export default () => function MainApp() {
return (
<div className="main">
</div>
);
};

View File

@ -1,49 +1,20 @@
import React, { useState } from "react"; import React, { useState } from "react";
import ReactDOM from "react-dom"; import ReactDOM from "react-dom";
import PropTypes from "prop-types";
import "./index.css"; import "./index.css";
import { config } from "./config"; import { config } from "./config";
import axios from "axios"; import axios from "axios";
import login from "./components/login";
import mainapp from "./components/mainapp";
import models from "./models";
function auth(token) { const { player } = models(config, axios);
return axios.get(`${config.apiUrl}/auth?token=${encodeURIComponent(token)}`)
.then((res) => res.data.success);
}
function Login(props) { const Login = login(player);
const [input, setInput] = useState(""); const MainApp = mainapp();
return (
<div className="login">
<input type="text" placeholder="Token ID" value={input}
onChange={evt => setInput(evt.target.value)} />
<button onClick={() => auth(input).then(x => props.setLoggedIn(x))}>Submit</button>
</div>
);
}
Login.propTypes = {
setLoggedIn: PropTypes.func,
};
function MainApp() {
return (
<div className="main">
</div>
);
}
function App(){ function App(){
const [loggedIn, setLoggedIn] = useState(false); const [loggedIn, setLoggedIn] = useState(false);
if (loggedIn){ return loggedIn ? <MainApp /> : <Login setLoggedIn={setLoggedIn} />;
return (
<MainApp />
);
}
else {
return (
<Login setLoggedIn={setLoggedIn} />
);
}
} }
ReactDOM.render(<App />, document.getElementById("root")); ReactDOM.render(<App />, document.getElementById("root"));

5
src/models/index.js Normal file
View File

@ -0,0 +1,5 @@
import player from "./player";
export default (config, axios) => { return {
player: player(config, axios),
};};

6
src/models/player.js Normal file
View File

@ -0,0 +1,6 @@
export default (config, axios) => {return {
auth: (token) => {
return axios.get(`${config.apiUrl}/auth?token=${encodeURIComponent(token)}`)
.then((res) => res.data.success);
},
};};