From e0e0729ee57d2f9b1188a0604e3cc4a51317b0ed Mon Sep 17 00:00:00 2001 From: it-fixcomart Date: Thu, 12 Jun 2025 11:00:00 +0700 Subject: fix pinpoint --- src/lib/maps/components/PinPointMap.jsx | 24 +++++++++++++++++++----- src/lib/maps/stores/useMaps.js | 33 ++++++++++++++++++++++++--------- 2 files changed, 43 insertions(+), 14 deletions(-) (limited to 'src/lib/maps') diff --git a/src/lib/maps/components/PinPointMap.jsx b/src/lib/maps/components/PinPointMap.jsx index fde1f36c..c46d838a 100644 --- a/src/lib/maps/components/PinPointMap.jsx +++ b/src/lib/maps/components/PinPointMap.jsx @@ -19,7 +19,7 @@ const defaultCenter = { lng: 106.816666, }; -const PinpointLocation = () => { +const PinpointLocation = ({ initialLatitude, initialLongitude, initialAddress }) => { const { isLoaded } = useJsApiLoader({ googleMapsApiKey: process.env.NEXT_PUBLIC_GOOGLE_API_KEY, libraries: ['places'], @@ -34,8 +34,15 @@ const PinpointLocation = () => { setPinedMaps, } = useMaps(); - const [tempAddress, setTempAddress] = useState(''); - const [tempPosition, setTempPosition] = useState(defaultCenter); + const [tempAddress, setTempAddress] = useState(initialAddress || ''); + const [tempPosition, setTempPosition] = useState( + initialLatitude && initialLongitude + ? { lat: parseFloat(initialLatitude), lng: parseFloat(initialLongitude) } + : selectedPosition.lat && selectedPosition.lng + ? selectedPosition + : defaultCenter + ); + const [markerIcon, setMarkerIcon] = useState(null); const autocompleteRef = useRef(null); @@ -47,7 +54,12 @@ const PinpointLocation = () => { scaledSize: new window.google.maps.Size(25, 40), }); } - }, [isLoaded]); + + // If we have initial coordinates but no address, fetch the address + if (initialLatitude && initialLongitude && !initialAddress) { + getAddress(parseFloat(initialLatitude), parseFloat(initialLongitude)); + } + }, [isLoaded, initialLatitude, initialLongitude, initialAddress]); const getAddressComponent = (components, type) => { const component = components.find((comp) => comp.types.includes(type)); @@ -147,6 +159,8 @@ const PinpointLocation = () => { setTempAddress(e.target.value)} style={{ width: '100%', padding: '8px' }} /> @@ -209,4 +223,4 @@ const PinpointLocation = () => { ); }; -export default PinpointLocation; +export default PinpointLocation; \ No newline at end of file diff --git a/src/lib/maps/stores/useMaps.js b/src/lib/maps/stores/useMaps.js index 4daf7f62..c57a05ad 100644 --- a/src/lib/maps/stores/useMaps.js +++ b/src/lib/maps/stores/useMaps.js @@ -6,12 +6,27 @@ const center = { }; export const useMaps = create((set) => ({ - selectedPosition: center, - addressMaps: '', - detailAddress: {}, - pinedMaps : false, - setSelectedPosition: (position) => set({ selectedPosition: position }), - setAddressMaps: (addressMaps) => set({ addressMaps }), - setDetailAddress: (detailAddress) => set({ detailAddress }), - setPinedMaps: (pinedMaps) => set({pinedMaps}) - })); \ No newline at end of file + // State existing + selectedPosition: center, + addressMaps: '', + detailAddress: {}, + pinedMaps: false, + + // State tambahan untuk penyimpanan posisi sementara + tempPositionCreate: null, + tempPositionEdit: null, + + // Setter existing + setSelectedPosition: (position) => set({ selectedPosition: position }), + setAddressMaps: (addressMaps) => set({ addressMaps }), + setDetailAddress: (detailAddress) => set({ detailAddress }), + setPinedMaps: (pinedMaps) => set({ pinedMaps }), + + // Setter tambahan untuk posisi sementara + setTempPositionCreate: (position) => set({ tempPositionCreate: position }), + setTempPositionEdit: (position) => set({ tempPositionEdit: position }), + + // Opsional: Reset jika ingin clear saat keluar halaman + resetTempPositionCreate: () => set({ tempPositionCreate: null }), + resetTempPositionEdit: () => set({ tempPositionEdit: null }), +})); -- cgit v1.2.3