'use client'; import React, { FC, useEffect, useState } from 'react'; import { Button, Form, message, Upload } from 'antd'; import type { GetProp, UploadFile, UploadProps } from 'antd'; import ImgCrop from 'antd-img-crop'; import { CameraOutlined, DeleteOutlined } from '@ant-design/icons'; import { useRouter } from '../../navigation'; import { i18nText } from '../../i18nKeys'; import { ProfileRequest } from '../../types/profile'; import { validateImage } from '../../utils/account'; import { useProfileSettings } from '../../actions/hooks/useProfileSettings'; import { CustomInput } from '../view/CustomInput'; import { OutlinedButton } from '../view/OutlinedButton'; import {FilledButton, FilledSquareButton, FilledYellowButton} from '../view/FilledButton'; import { DeleteAccountModal } from '../Modals/DeleteAccountModal'; import { Loader } from '../view/Loader'; type ProfileSettingsProps = { locale: string; }; type FileType = Parameters>[0]; export const ProfileSettings: FC = ({ locale }) => { const [form] = Form.useForm(); const { profileSettings, fetchProfileSettings, save, fetchLoading } = useProfileSettings(locale); const [showDeleteModal, setShowDeleteModal] = useState(false); const [saveLoading, setSaveLoading] = useState(false); const [photo, setPhoto] = useState(); const router = useRouter(); useEffect(() => { fetchProfileSettings() }, []); useEffect(() => { if (profileSettings) { form.setFieldsValue(profileSettings); } }, [profileSettings]); const onSaveProfile = () => { form.validateFields() .then(({ login, surname, username }) => { const { phone, role, languagesLinks } = profileSettings; const newProfile: ProfileRequest = { phone, role, login, surname, username, isPasswordKeepExisting: true, isFaceImageKeepExisting: true, languagesLinks: languagesLinks?.map(({ languageId }) => ({ languageId })) || [] }; if (photo) { console.log(photo); const formData = new FormData(); formData.append('file', photo as FileType); newProfile.faceImage = `[${(photo as File).arrayBuffer()}]`; newProfile.isFaceImageKeepExisting = false; } console.log(newProfile); setSaveLoading(true); save(newProfile) .then(() => { fetchProfileSettings(); }) .catch(() => { message.error('Не удалось сохранить изменения'); }) .finally(() => { setSaveLoading(false); }) }) } const beforeCrop = (file: UploadFile) => { return validateImage(file, true); } const beforeUpload = (file: UploadFile) => { const isValid = validateImage(file); if (isValid) { setPhoto(file); } return false; } const onDeleteAccount = () => setShowDeleteModal(true); return (
} />
{i18nText('photoDesc', locale)}
{/*
*/}
{i18nText('save', locale)} router.push('change-password')}> {i18nText('changePass', locale)} } danger > {i18nText('deleteAcc', locale)}
setShowDeleteModal(false)} />
); };