lifeto-shop/src/components/login.tsx

81 lines
2.3 KiB
TypeScript
Raw Normal View History

2025-05-13 21:02:59 +00:00
import { useState } from "react"
2024-08-12 01:13:42 +00:00
import useLocalStorage from "use-local-storage"
2025-05-13 21:02:59 +00:00
import { useAtom } from "jotai"
import { loginStatusAtom } from "../state/atoms"
import { LoginHelper } from "../lib/session"
2024-08-12 01:13:42 +00:00
export const LoginWidget = () => {
const [username, setUsername] = useLocalStorage("input_username","", {syncData: false})
const [password, setPassword] = useState("")
2025-05-13 21:02:59 +00:00
const [{data:loginState, refetch: refetchLoginState}] = useAtom(loginStatusAtom)
const [loginError, setLoginError] = useState("")
if(loginState?.logged_in){
return <>
<div className="flex flex-row justify-between px-2">
<div>
{loginState.community_name}
2024-08-12 01:13:42 +00:00
</div>
2025-05-13 21:02:59 +00:00
<div className="flex flex-row gap-2">
2024-08-12 01:13:42 +00:00
<button
onClick={()=>{
2025-05-13 21:02:59 +00:00
LoginHelper.logout().finally(()=>{
refetchLoginState()
})
2024-08-12 01:13:42 +00:00
return
}}
2025-05-13 21:02:59 +00:00
className="text-blue-400 text-xs hover:cursor-pointer hover:text-blue-600">
2024-08-12 01:13:42 +00:00
logout
</button>
</div>
</div>
2025-05-13 21:02:59 +00:00
</>
}
return <>
<div className="flex flex-col">
<form action={
()=>{
LoginHelper.login(username,password).catch((e)=>{
setLoginError(e.message)
}).finally(()=>{
refetchLoginState()
refetchLoginState()
})
}}
className="flex flex-col gap-1 p-2 justify-left">
{ loginError ? (<div className="text-red-500 text-xs">
{loginError}
</div>) : null}
<div>
<input
onChange={(e)=>{
setUsername(e.target.value)
}}
value={username}
id="username"
placeholder="username" className="w-32 pl-2 pb-1 border-b border-gray-600 placeholder-gray-500"/>
</div>
<div>
<input
onChange={(e)=>{
setPassword(e.target.value)
}}
value={password}
type="password" placeholder="password" className="w-32 pl-2 pb-1 border-b border-gray-600 placeholder-gray-500"/>
</div>
<button
type="submit"
className="border-b border-gray-600 px-2 py-1 hover:text-gray-600 hover:cursor-pointer">
login
</button>
</form>
2024-08-12 01:13:42 +00:00
</div>
</>
}