profile page connected to backend and PATCH added, plagiatCheck updated and sertificate generate updated base backend types

This commit is contained in:
nabijonovdavronbek619@gmail.com
2026-04-07 21:30:27 +05:00
parent 50a8d6dbd7
commit c61182adcf
15 changed files with 208 additions and 198 deletions

View File

@@ -1,35 +1,76 @@
'use client';
import { useState } from 'react';
import { useState, useEffect } from 'react';
import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
import { apiRequest } from '@/shared/request/apiRequest';
import { links } from '@/shared/request/links';
import type { UserProfile } from '../types';
interface ProfileForm extends UserProfile {
currentPassword: string;
newPassword: string;
confirmPassword: string;
interface ProfileFormState {
first_name: string;
last_name: string;
phone: string;
password: string;
}
export const useProfile = (initial: UserProfile) => {
const [form, setForm] = useState<ProfileForm>({
...initial,
currentPassword: '',
newPassword: '',
confirmPassword: '',
});
const [isSaving, setIsSaving] = useState(false);
export const useProfile = () => {
const queryClient = useQueryClient();
const [saved, setSaved] = useState(false);
const [form, setForm] = useState<ProfileFormState>({
first_name: '',
last_name: '',
phone: '',
password: '',
});
const handleChange = (field: keyof ProfileForm, value: string) => {
const { data: profile, isLoading } = useQuery({
queryKey: ['profile'],
queryFn: () => apiRequest<UserProfile>('GET', links.users),
select: (res) => res.data,
});
useEffect(() => {
if (profile) {
setForm({
first_name: profile.first_name,
last_name: profile.last_name,
phone: profile.phone,
password: '',
});
}
}, [profile]);
const { mutate, isPending: isSaving } = useMutation({
mutationFn: (payload: Record<string, string>) =>
apiRequest<UserProfile>('PATCH', links.users, payload),
onSuccess: () => {
setSaved(true);
queryClient.invalidateQueries({ queryKey: ['profile'] });
setTimeout(() => setSaved(false), 3000);
},
});
const handleChange = (field: keyof ProfileFormState, value: string) => {
setForm((prev) => ({ ...prev, [field]: value }));
setSaved(false);
};
const handleSave = async () => {
setIsSaving(true);
// TODO: replace with real API call
await new Promise((res) => setTimeout(res, 800));
setIsSaving(false);
setSaved(true);
const handleSave = () => {
const payload: Record<string, string> = {
first_name: form.first_name,
last_name: form.last_name,
};
if (form.phone) payload.phone = form.phone;
if (form.password) payload.password = form.password;
mutate(payload);
};
return { form, isSaving, saved, handleChange, handleSave };
return {
form,
profile,
isLoading,
isSaving,
saved,
handleChange,
handleSave,
};
};