115 lines
3.2 KiB
TypeScript
Raw Normal View History

2024-11-14 18:50:58 +07:00
import { Text, ScrollView, Card, Separator, XStack } from "tamagui";
import React from "react";
import FormField from "@/components/ui/form";
import { InputField } from "@/components/ui/input";
import { useZForm } from "@/hooks/useZForm";
2024-11-14 18:50:58 +07:00
import { Link, router, Stack } from "expo-router";
import Button from "@/components/ui/button";
import ThemeSwitcher from "@/components/containers/theme-switcher";
import { useMutation } from "@tanstack/react-query";
import { z } from "zod";
import { ErrorAlert } from "@/components/ui/alert";
2024-11-14 18:50:58 +07:00
import { loginSchema } from "./schema";
import Icons from "@/components/ui/icons";
2024-11-12 19:15:13 +07:00
import tamaguiConfig from "@/tamagui.config";
2024-11-14 18:50:58 +07:00
import { useLoginMutation } from "./hooks";
export default function LoginPage() {
const form = useZForm(loginSchema);
2024-11-14 18:50:58 +07:00
const login = useLoginMutation();
const onSubmit = form.handleSubmit((values) => {
login.mutate(values);
});
return (
<>
<Stack.Screen
options={{
contentStyle: {
width: "100%",
2024-11-12 19:15:13 +07:00
maxWidth: tamaguiConfig.media.xs.maxWidth,
marginHorizontal: "auto",
},
title: "Login",
2024-11-12 17:17:10 +00:00
headerTitle: "",
2024-11-15 09:39:35 +00:00
headerRight: () => <ThemeSwitcher $gtSm={{ mr: "$3" }} />,
}}
/>
<ScrollView
contentContainerStyle={{
padding: "$4",
pb: "$12",
justifyContent: "center",
flexGrow: 1,
}}
>
<Card bordered p="$4" gap="$4">
2024-11-14 18:50:58 +07:00
<Text fontSize="$9" mt="$4">
Login
</Text>
<ErrorAlert error={login.error} />
<FormField vertical label="Username/Email">
2024-11-12 04:43:59 +07:00
<InputField
form={form}
name="username"
2024-11-15 04:08:37 +07:00
autoCapitalize="none"
2024-11-12 04:43:59 +07:00
onSubmitEditing={onSubmit}
2024-11-16 23:50:02 +07:00
autoFocus
2024-11-12 04:43:59 +07:00
/>
</FormField>
<FormField vertical label="Password">
2024-11-12 04:43:59 +07:00
<InputField
form={form}
name="password"
2024-11-15 04:08:37 +07:00
autoCapitalize="none"
2024-11-12 04:43:59 +07:00
secureTextEntry
onSubmitEditing={onSubmit}
/>
</FormField>
<Separator />
<Button
2024-11-14 18:50:58 +07:00
icon={<Icons name="login" size={16} />}
onPress={onSubmit}
isLoading={login.isPending}
>
2024-11-14 18:50:58 +07:00
Login
</Button>
2024-11-14 18:50:58 +07:00
<XStack justifyContent="space-between">
<Text textAlign="center" fontSize="$4">
Not registered yet?{" "}
<Link href="/auth/register" asChild>
<Text cursor="pointer" fontWeight="600">
Register Now.
</Text>
</Link>
</Text>
<Link href="/auth/reset-password" asChild>
<Text cursor="pointer" fontWeight="600" fontSize="$4">
Reset Password
</Text>
</Link>
</XStack>
<Separator w="100%" />
<Button
onPress={() => router.push("/server")}
bg="$colorTransparent"
icon={<Icons name="desktop-classic" size={16} />}
>
Change Server
</Button>
</Card>
</ScrollView>
</>
);
}