|
@@ -4,20 +4,19 @@ import Layout from "../components/Layout";
|
|
|
import { useAuth } from "../context/UserContext";
|
|
import { useAuth } from "../context/UserContext";
|
|
|
import useForm from "../hooks/useForm";
|
|
import useForm from "../hooks/useForm";
|
|
|
import { useRouter } from "next/router";
|
|
import { useRouter } from "next/router";
|
|
|
|
|
+import Spinner from "../components/Spinner";
|
|
|
import api from "../utils/api";
|
|
import api from "../utils/api";
|
|
|
|
|
|
|
|
const Login: NextPage = () => {
|
|
const Login: NextPage = () => {
|
|
|
const { login } = useAuth();
|
|
const { login } = useAuth();
|
|
|
const router = useRouter();
|
|
const router = useRouter();
|
|
|
|
|
|
|
|
- const { error, submitHandler, success } = useForm(api.login);
|
|
|
|
|
|
|
+ const { error, submitHandler, success, loading } = useForm(api.login);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
useEffect(() => {
|
|
|
if (success) {
|
|
if (success) {
|
|
|
login(success.result);
|
|
login(success.result);
|
|
|
- setTimeout(() => {
|
|
|
|
|
- router.push("/");
|
|
|
|
|
- }, 1000);
|
|
|
|
|
|
|
+ router.push("/");
|
|
|
}
|
|
}
|
|
|
}, [success, login, router]);
|
|
}, [success, login, router]);
|
|
|
|
|
|
|
@@ -51,19 +50,14 @@ const Login: NextPage = () => {
|
|
|
required
|
|
required
|
|
|
/>
|
|
/>
|
|
|
</div>
|
|
</div>
|
|
|
- <button type="submit" className="form-button">
|
|
|
|
|
- Log in
|
|
|
|
|
|
|
+ <button type="submit" className="form-button" disabled={loading}>
|
|
|
|
|
+ {loading ? <Spinner /> : "Log in"}
|
|
|
</button>
|
|
</button>
|
|
|
{error && !success && (
|
|
{error && !success && (
|
|
|
<div role="alert" className="form-alert-msg">
|
|
<div role="alert" className="form-alert-msg">
|
|
|
{error.message}
|
|
{error.message}
|
|
|
</div>
|
|
</div>
|
|
|
)}
|
|
)}
|
|
|
- {success && (
|
|
|
|
|
- <div role="alert" className="form-success-msg">{`${
|
|
|
|
|
- success.message || "Logged in successfully!"
|
|
|
|
|
- } Redirecting to home`}</div>
|
|
|
|
|
- )}
|
|
|
|
|
</form>
|
|
</form>
|
|
|
</Layout>
|
|
</Layout>
|
|
|
);
|
|
);
|