landing page added
This commit is contained in:
@@ -2,10 +2,35 @@
|
||||
|
||||
import React, { useState } from 'react';
|
||||
import { useTranslations } from 'next-intl';
|
||||
import { useMutation } from '@tanstack/react-query';
|
||||
import { apiRequest } from '@/shared/request/apiRequest';
|
||||
import { links } from '@/shared/request/links';
|
||||
import { useLoginModal } from '@/shared/zustand/auth';
|
||||
import { toast } from 'react-toastify';
|
||||
|
||||
interface LoginData {
|
||||
phone: string;
|
||||
}
|
||||
|
||||
export function useLoginForm() {
|
||||
const [phone, setPhone] = useState('');
|
||||
const [error, setError] = useState('');
|
||||
const toggleLoginModal = useLoginModal((state) => state.toggleLoginModal);
|
||||
const loginReqest = useMutation({
|
||||
mutationKey: ['login'],
|
||||
mutationFn: (data: LoginData) => apiRequest('POST', links.login, data),
|
||||
onSuccess: (data) => {
|
||||
console.log('Login successful:', data);
|
||||
toggleLoginModal();
|
||||
toast.success('Kirish muvaffaqiyatli!');
|
||||
},
|
||||
onError: (err) => {
|
||||
setError(err instanceof Error ? err.message : 'Unknown error');
|
||||
// toggleLoginModal();
|
||||
console.error('Login failed:', err);
|
||||
toast.error(err instanceof Error ? err.message : 'Unknown error');
|
||||
},
|
||||
});
|
||||
|
||||
const t = useTranslations();
|
||||
|
||||
@@ -18,6 +43,7 @@ export function useLoginForm() {
|
||||
return;
|
||||
}
|
||||
|
||||
loginReqest.mutate({ phone: `+998${phone}` });
|
||||
sessionStorage.setItem('prev_page', 'login');
|
||||
};
|
||||
|
||||
|
||||
@@ -3,13 +3,43 @@
|
||||
import { useCallback, useState } from 'react';
|
||||
import { useRegisterZustand } from './registerZustand';
|
||||
import { validateRegister, RegisterErrors } from './validateRegister';
|
||||
import { useRegisterModal } from '@/shared/zustand/auth';
|
||||
import { useMutation } from '@tanstack/react-query';
|
||||
import { apiRequest } from '@/shared/request/apiRequest';
|
||||
import { links } from '@/shared/request/links';
|
||||
import { toast } from 'react-toastify';
|
||||
|
||||
interface RegisterData {
|
||||
name: string;
|
||||
surname: string;
|
||||
phone: string;
|
||||
}
|
||||
|
||||
export function useRegisterForm() {
|
||||
const { registerData, setItem, setOferta, clearRegisterData } =
|
||||
useRegisterZustand();
|
||||
const [errors, setErrors] = useState<RegisterErrors>({});
|
||||
const [loading, setLoading] = useState(false);
|
||||
const [success, setSuccess] = useState(false);
|
||||
const toggleRegisterModal = useRegisterModal(
|
||||
(state) => state.toggleRegisterModal,
|
||||
);
|
||||
|
||||
const registerRequest = useMutation({
|
||||
mutationKey: ['register'],
|
||||
mutationFn: (data: RegisterData) =>
|
||||
apiRequest('POST', links.register, data),
|
||||
onSuccess: (data) => {
|
||||
console.log('Register successful:', data);
|
||||
toggleRegisterModal();
|
||||
setSuccess(true);
|
||||
toast.success("Ro'yxatdan o'tish muvaffaqiyatli!");
|
||||
},
|
||||
onError: (err) => {
|
||||
// toggleLoginModal();
|
||||
console.error('Register failed:', err);
|
||||
toast.error(err instanceof Error ? err.message : 'Unknown error');
|
||||
},
|
||||
});
|
||||
|
||||
const handleChange = useCallback(
|
||||
(e: React.ChangeEvent<HTMLInputElement>) => {
|
||||
@@ -33,23 +63,12 @@ export function useRegisterForm() {
|
||||
async (e: React.FormEvent) => {
|
||||
e.preventDefault();
|
||||
const validationErrors = validateRegister(registerData);
|
||||
console.log('register data on submit:', registerData);
|
||||
if (Object.keys(validationErrors).length > 0) {
|
||||
setErrors(validationErrors);
|
||||
return;
|
||||
}
|
||||
|
||||
setLoading(true);
|
||||
try {
|
||||
// Replace with your real API call:
|
||||
// await api.post('/register', registerData);
|
||||
await new Promise((r) => setTimeout(r, 1200)); // simulated delay
|
||||
setSuccess(true);
|
||||
clearRegisterData();
|
||||
} catch {
|
||||
setErrors({ name: 'Something went wrong. Please try again.' });
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
registerRequest.mutate(registerData);
|
||||
},
|
||||
[registerData, clearRegisterData],
|
||||
);
|
||||
@@ -57,10 +76,11 @@ export function useRegisterForm() {
|
||||
return {
|
||||
registerData,
|
||||
errors,
|
||||
loading,
|
||||
loading: registerRequest.isPending,
|
||||
success,
|
||||
handleChange,
|
||||
handleOferta,
|
||||
handleSubmit,
|
||||
setItem,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -28,8 +28,8 @@ export function validateRegister(data: {
|
||||
const digits = data.phone.replace(/\D/g, '');
|
||||
if (!digits) {
|
||||
errors.phone = 'Phone is required';
|
||||
} else if (digits.length !== 9) {
|
||||
errors.phone = 'Enter a valid 9-digit phone number';
|
||||
} else if (digits.length !== 9 && digits.length !== 12) {
|
||||
errors.phone = 'Enter a valid 9-digit or 13-digit phone number';
|
||||
}
|
||||
|
||||
if (!data.oferta) {
|
||||
|
||||
@@ -63,6 +63,7 @@ function Field({
|
||||
}
|
||||
|
||||
export function RegisterFormUI() {
|
||||
const [phone, setPhone] = React.useState('');
|
||||
const {
|
||||
registerData,
|
||||
errors,
|
||||
@@ -71,16 +72,22 @@ export function RegisterFormUI() {
|
||||
handleChange,
|
||||
handleOferta,
|
||||
handleSubmit,
|
||||
setItem,
|
||||
} = useRegisterForm();
|
||||
const [phone, setPhone] = React.useState(registerData.phone || '');
|
||||
const [isFocused, setIsFocused] = React.useState(false);
|
||||
const handlePhoneChange = useCallback(
|
||||
(e: React.ChangeEvent<HTMLInputElement>) => {
|
||||
setPhone(normalizeDigits(e.target.value));
|
||||
if (normalizeDigits(e.target.value).length === 9)
|
||||
setItem('phone', `998${phone}`);
|
||||
},
|
||||
[setPhone],
|
||||
);
|
||||
|
||||
if (errors) {
|
||||
console.log('Validation errors:', errors);
|
||||
}
|
||||
|
||||
// ── Success state ──────────────────────────────────────
|
||||
if (success) {
|
||||
return (
|
||||
|
||||
Reference in New Issue
Block a user