Add authentication modal dialog and a stab at localstorage
This commit is contained in:
parent
8ea7aac4ed
commit
16d9d4c4a4
42
src/App.tsx
42
src/App.tsx
|
@ -1,15 +1,18 @@
|
||||||
import './App.css';
|
import "./App.css";
|
||||||
//import MailboxList from './Mailbox';
|
import 'bootstrap/dist/css/bootstrap.min.css';
|
||||||
import React, { useEffect, useState } from 'react'
|
|
||||||
|
//import MailboxList from "./Mailbox";
|
||||||
|
import AuthModal from "./AuthModal";
|
||||||
|
import React, { useEffect, useState } from "react"
|
||||||
|
|
||||||
|
|
||||||
interface IUser {
|
interface IAuth {
|
||||||
name: string;
|
password: string;
|
||||||
id: string;
|
username: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
const App = () => {
|
const App = () => {
|
||||||
const [users, setUsers] = useState<Array<IUser>>([])
|
const [state, setInternalState] = useState<IAuth|null>(null)
|
||||||
|
|
||||||
const fetchUserData = () => {
|
const fetchUserData = () => {
|
||||||
fetch("https://jsonplaceholder.typicode.com/users")
|
fetch("https://jsonplaceholder.typicode.com/users")
|
||||||
|
@ -17,23 +20,30 @@ const App = () => {
|
||||||
return response.json()
|
return response.json()
|
||||||
})
|
})
|
||||||
.then(data => {
|
.then(data => {
|
||||||
setUsers(data)
|
//setUsers(data)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const loadAuth = () => {
|
||||||
|
const auth = localStorage.getItem("auth")
|
||||||
|
if (!auth) return;
|
||||||
|
setInternalState(JSON.parse(auth))
|
||||||
|
}
|
||||||
|
|
||||||
|
const setState = (auth: IAuth) => {
|
||||||
|
localStorage.setItem("auth", JSON.stringify(auth))
|
||||||
|
}
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
fetchUserData()
|
loadAuth()
|
||||||
|
//fetchUserData()
|
||||||
}, [])
|
}, [])
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="App">
|
<div className="App">
|
||||||
{users.length > 0 && (
|
{state ?
|
||||||
<ul>
|
<p>{state.username}</p> :
|
||||||
{users.map(user => (
|
<AuthModal></AuthModal>}
|
||||||
<li key={user.id}>{user.name}</li>
|
|
||||||
))}
|
|
||||||
</ul>
|
|
||||||
)}
|
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue