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 MailboxList from './Mailbox';
|
||||
import React, { useEffect, useState } from 'react'
|
||||
import "./App.css";
|
||||
import 'bootstrap/dist/css/bootstrap.min.css';
|
||||
|
||||
//import MailboxList from "./Mailbox";
|
||||
import AuthModal from "./AuthModal";
|
||||
import React, { useEffect, useState } from "react"
|
||||
|
||||
|
||||
interface IUser {
|
||||
name: string;
|
||||
id: string;
|
||||
interface IAuth {
|
||||
password: string;
|
||||
username: string;
|
||||
}
|
||||
|
||||
const App = () => {
|
||||
const [users, setUsers] = useState<Array<IUser>>([])
|
||||
const [state, setInternalState] = useState<IAuth|null>(null)
|
||||
|
||||
const fetchUserData = () => {
|
||||
fetch("https://jsonplaceholder.typicode.com/users")
|
||||
|
@ -17,23 +20,30 @@ const App = () => {
|
|||
return response.json()
|
||||
})
|
||||
.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(() => {
|
||||
fetchUserData()
|
||||
loadAuth()
|
||||
//fetchUserData()
|
||||
}, [])
|
||||
|
||||
return (
|
||||
<div className="App">
|
||||
{users.length > 0 && (
|
||||
<ul>
|
||||
{users.map(user => (
|
||||
<li key={user.id}>{user.name}</li>
|
||||
))}
|
||||
</ul>
|
||||
)}
|
||||
{state ?
|
||||
<p>{state.username}</p> :
|
||||
<AuthModal></AuthModal>}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue