import React, { useState } from 'react'; import Button from '@/Components/Button'; import Checkbox from '@/Components/Checkbox'; import Guest from '@/Layouts/Guest'; import Input from '@/Components/Input'; import Label from '@/Components/Label'; import ValidationErrors from '@/Components/ValidationErrors'; import { Head, Link, useForm } from '@inertiajs/inertia-react'; import { getAllUsers } from '@/Services/User'; import { forEach } from 'lodash'; import { Inertia } from '@inertiajs/inertia'; export default function Login({ status, canResetPassword }) { const [isLoading, setLoading] = useState(false) const [error, setError] = useState('') const { data, setData, post } = useForm({ name: '', username: '', password: '', is_admin: '' }); const onHandleChange = (event) => { setData(event.target.name, event.target.type === 'checkbox' ? event.target.checked : event.target.value); }; const checkLogin = (users) => { const user = users.find(item => item.username == data.username && item.password == data.password) if (user) { Inertia.post(route('login'), { name: user.name, username: user.username, password: user.password, is_admin: user.is_admin }) } else { setError('Username atau password tidak cocok') } } const submit = (e) => { e.preventDefault(); setLoading(true) getAllUsers() .then(items => checkLogin(items)) .finally(() => setLoading(false)) }; return ( {status &&
{status}
}
{error}
{canResetPassword && ( Forgot your password? )}
); }