'use client'; import { Map, Placemark, YMaps } from '@pbe/react-yandex-maps'; import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query'; import { useEffect, useState } from 'react'; import axiosInstance from '../../api/axiosInstance'; // Reverse geocoding (OSM orqali) async function getAddress(lat, lon) { const response = await fetch(`https://nominatim.openstreetmap.org/reverse?lat=${lat}&lon=${lon}&format=json`); const data = await response.json(); const address = data.address || {}; return { fullAddress: data.display_name || '', country: address.country || '', region: address.state || '', city: address.city || address.town || address.village || '', street: address.road || '', house: address.house_number || '', postalCode: address.postcode || '', }; } const CreateBranches = ({ branches, setAlert, setBranches }) => { const queryClient = useQueryClient(); const [formData, setFormData] = useState({ id: '', title: '', titleRu: '', address: '', addressRu: '', workingHours: '', workingHoursRu: '', phone: '', telegramAdmin: '', telegramChannel: '', code: '', active: true, }); const [coords, setCoords] = useState({ latitude: 41.311081, // Toshkent longitude: 69.240562, }); const [locationDetails, setLocationDetails] = useState({ fullAddress: '', country: '', region: '', city: '', street: '', house: '', postalCode: '', }); const resetForm = () => { setFormData({ id: '', title: '', titleRu: '', address: '', addressRu: '', workingHours: '', workingHoursRu: '', phone: '', telegramAdmin: '', telegramChannel: '', code: '', active: true, }); setCoords({ latitude: 41.311081, longitude: 69.240562, }); setLocationDetails({ fullAddress: '', country: '', region: '', city: '', street: '', house: '', postalCode: '', }); }; useEffect(() => { if (locationDetails.fullAddress) { setFormData((prev) => ({ ...prev, address: locationDetails.fullAddress, addressRu: locationDetails.fullAddress, })); } }, [locationDetails]); const { data: branchData, refetch } = useQuery({ queryKey: ['one_branch', branches], queryFn: () => axiosInstance.get(`/branches/${branches}`).then((res) => res.data), enabled: !!branches, }); useEffect(() => { if (branches) { refetch(); } }, [branches, refetch]); useEffect(() => { if (branchData) { setFormData({ id: branchData.id || '', title: branchData.title || '', titleRu: branchData.titleRu || '', address: branchData.address || '', addressRu: branchData.addressRu || '', workingHours: branchData.workingHours || '', workingHoursRu: branchData.workingHoursRu || '', phone: branchData.phone || '', telegramAdmin: branchData.telegramAdmin || '', telegramChannel: branchData.telegramChannel || '', code: branchData.code || '', active: branchData.active ?? true, }); setCoords({ latitude: branchData.latitude || 41.311081, longitude: branchData.longitude || 69.240562, }); } }, [branchData]); const { mutate: createBranch } = useMutation({ mutationFn: (body) => axiosInstance.post('/branches', body), onSuccess: () => { setAlert({ type: 'success', message: "Filial muvaffaqiyatli qo'shildi." }); queryClient.invalidateQueries({ queryKey: ['branch_list'] }); resetForm(); }, onError: () => { setAlert({ type: 'warning', message: "Filial qo'shilmadi, xatolik yuz berdi.", }); }, }); const { mutate: editBranch } = useMutation({ mutationFn: (body) => axiosInstance.put(`/branches/${body.id}`, body), onSuccess: () => { setBranches(null); setAlert({ type: 'success', message: 'Filial muvaffaqiyatli yangilandi.', }); queryClient.invalidateQueries({ queryKey: ['branch_list'] }); resetForm(); }, onError: () => { setAlert({ type: 'warning', message: 'Filial yangilanmadi, xatolik yuz berdi.', }); }, }); const addBranch = () => { const { id, ...dataWithoutId } = formData; createBranch({ ...dataWithoutId, latitude: Number(coords.latitude) || 0, longitude: Number(coords.longitude) || 0, }); }; const updateBranch = (id) => { editBranch({ ...formData, id, latitude: Number(coords.latitude) || 0, longitude: Number(coords.longitude) || 0, }); }; const handleMapClick = async (e) => { const lat = e.get('coords')[0]; const lon = e.get('coords')[1]; setCoords({ latitude: lat, longitude: lon }); const details = await getAddress(lat, lon); setLocationDetails(details); }; return (