Bổ sung tính năng sửa chữa, cập nhật địa điểm trong chặng
This commit is contained in:
Vendored
+43
-14
@@ -33,7 +33,7 @@ const MapPicker = ({ onPick, center }) => {
|
||||
}, [center]);
|
||||
return (_jsx(_Fragment, { children: menuPos && (_jsx("div", { ref: menuRef, className: "absolute z-[3000] bg-white rounded-xl shadow-xl border border-gray-100 py-1 w-44 animate-in zoom-in-95 duration-200", style: { top: menuPos.y, left: menuPos.x }, children: _jsxs("button", { type: "button", onClick: () => { onPick(menuPos.latlng); setMenuPos(null); }, className: "w-full text-left px-4 py-2 hover:bg-blue-50 text-xs font-bold text-blue-600 flex items-center gap-2", children: [_jsx(MapPin, { className: "w-3 h-3" }), " Th\u00EAm v\u00E0o ch\u1EB7ng hi\u1EC7n t\u1EA1i"] }) })) }));
|
||||
};
|
||||
export const AddLocationModal = ({ isOpen, onClose, tourId, initialLegId }) => {
|
||||
export const AddLocationModal = ({ isOpen, onClose, tourId, initialLegId, editingLocation }) => {
|
||||
const [formData, setFormData] = useState({
|
||||
name: '',
|
||||
address: '',
|
||||
@@ -45,12 +45,31 @@ export const AddLocationModal = ({ isOpen, onClose, tourId, initialLegId }) => {
|
||||
plannedEnd: ''
|
||||
});
|
||||
const [isLoading, setIsLoading] = useState(false);
|
||||
const { legs, addLocation, mapCenter } = useTourStore();
|
||||
const { legs, addLocation, updateLocation, mapCenter } = useTourStore();
|
||||
useEffect(() => {
|
||||
if (initialLegId && formData.legId !== initialLegId) {
|
||||
setFormData(prev => ({ ...prev, legId: initialLegId }));
|
||||
if (isOpen) {
|
||||
if (editingLocation) {
|
||||
setFormData({
|
||||
name: editingLocation.name || '',
|
||||
address: editingLocation.address || '',
|
||||
latitude: editingLocation.latitude,
|
||||
longitude: editingLocation.longitude,
|
||||
type: editingLocation.type || 'VISIT',
|
||||
legId: editingLocation.legId || '',
|
||||
plannedStart: editingLocation.plannedStart ? editingLocation.plannedStart.slice(0, 16) : '',
|
||||
plannedEnd: editingLocation.plannedEnd ? editingLocation.plannedEnd.slice(0, 16) : ''
|
||||
});
|
||||
}
|
||||
else {
|
||||
setFormData(prev => ({
|
||||
...prev,
|
||||
name: '', address: '',
|
||||
legId: initialLegId || (legs.length > 0 ? legs[0].id : ''),
|
||||
plannedStart: '', plannedEnd: ''
|
||||
}));
|
||||
}
|
||||
}
|
||||
}, [initialLegId, isOpen]);
|
||||
}, [initialLegId, editingLocation, isOpen]);
|
||||
useEffect(() => {
|
||||
if (isOpen && !formData.name) {
|
||||
setFormData(prev => ({ ...prev, latitude: mapCenter[0], longitude: mapCenter[1] }));
|
||||
@@ -58,8 +77,8 @@ export const AddLocationModal = ({ isOpen, onClose, tourId, initialLegId }) => {
|
||||
}, [isOpen, mapCenter]);
|
||||
const currentLegId = formData.legId || (legs.length > 0 ? legs[0].id : '');
|
||||
const targetLeg = legs.find(l => l.id === currentLegId);
|
||||
const titleText = initialLegId ? `Thêm địa điểm cho ${targetLeg?.note || `Chặng ${targetLeg?.sequence}`}` : 'Thêm địa điểm mới';
|
||||
const buttonText = initialLegId ? 'Xác nhận thêm vào chặng' : 'Thêm địa điểm';
|
||||
const titleText = editingLocation ? `Sửa địa điểm: ${editingLocation.name}` : (initialLegId ? `Thêm địa điểm cho ${targetLeg?.note || `Chặng ${targetLeg?.sequence}`}` : 'Thêm địa điểm mới');
|
||||
const buttonText = editingLocation ? 'Cập nhật thay đổi' : (initialLegId ? 'Xác nhận thêm vào chặng' : 'Thêm địa điểm');
|
||||
const handlePickLocation = async (latlng) => {
|
||||
setFormData(prev => ({ ...prev, latitude: latlng.lat, longitude: latlng.lng }));
|
||||
try {
|
||||
@@ -80,16 +99,26 @@ export const AddLocationModal = ({ isOpen, onClose, tourId, initialLegId }) => {
|
||||
e.preventDefault();
|
||||
setIsLoading(true);
|
||||
try {
|
||||
await addLocation(tourId, {
|
||||
...formData,
|
||||
legId: currentLegId,
|
||||
latitude: parseFloat(formData.latitude),
|
||||
longitude: parseFloat(formData.longitude),
|
||||
});
|
||||
if (editingLocation) {
|
||||
await updateLocation(editingLocation.id, {
|
||||
...formData,
|
||||
legId: currentLegId,
|
||||
latitude: parseFloat(formData.latitude),
|
||||
longitude: parseFloat(formData.longitude),
|
||||
});
|
||||
}
|
||||
else {
|
||||
await addLocation(tourId, {
|
||||
...formData,
|
||||
legId: currentLegId,
|
||||
latitude: parseFloat(formData.latitude),
|
||||
longitude: parseFloat(formData.longitude),
|
||||
});
|
||||
}
|
||||
onClose();
|
||||
}
|
||||
catch (error) {
|
||||
alert('Lỗi khi thêm địa điểm');
|
||||
alert('Lỗi khi lưu địa điểm');
|
||||
}
|
||||
finally {
|
||||
setIsLoading(false);
|
||||
|
||||
Reference in New Issue
Block a user