diff --git a/pages/index.tsx b/pages/index.tsx index b42e69d..60d729e 100644 --- a/pages/index.tsx +++ b/pages/index.tsx @@ -63,7 +63,7 @@ export const Home = () => { places={lastData && lastData.places ? lastData.places : []} focusedPlaceId={focusedPlaceId} setFocusedPlaceId={setFocusedPlaceId} - searchedCoordiantes={({ latitude, longitude }) => + searchedCoordinates={({ latitude, longitude }) => setSearchedViewport((viewport) => { return { ...viewport, latitude, longitude, zoom: 11 }; }) diff --git a/pages/places/[id]/index.tsx b/pages/places/[id]/index.tsx index b9ff5cf..238aea9 100644 --- a/pages/places/[id]/index.tsx +++ b/pages/places/[id]/index.tsx @@ -169,7 +169,7 @@ const PlaceId: PlaceIdFC = () => { refetch(); setEditMode(false); }} - searchedCoordiantes={({ latitude, longitude }) => + searchedCoordinates={({ latitude, longitude }) => setSearchedViewport((viewport) => { return { ...viewport, latitude, longitude }; }) diff --git a/pages/places/add.tsx b/pages/places/add.tsx index 64f2843..f69e9ea 100644 --- a/pages/places/add.tsx +++ b/pages/places/add.tsx @@ -43,7 +43,7 @@ const Add = () => { onSubmitted={(id) => { router.push(`${urls.places}/${id}`); }} - searchedCoordiantes={({ latitude, longitude }) => + searchedCoordinates={({ latitude, longitude }) => setSearchedViewport((viewport) => { return { ...viewport, latitude, longitude }; }) diff --git a/src/components/PlaceForm.tsx b/src/components/PlaceForm.tsx index 8dac301..e1b3141 100644 --- a/src/components/PlaceForm.tsx +++ b/src/components/PlaceForm.tsx @@ -45,7 +45,7 @@ interface IPlace { interface IPlaceFormProps { place?: IPlace; onSubmitted: (id: string) => void; - searchedCoordiantes: ({ + searchedCoordinates: ({ latitude, longitude, }: { @@ -147,7 +147,7 @@ const uploadImage = async ( const PlaceForm: FC = ({ place, onSubmitted, - searchedCoordiantes, + searchedCoordinates, }) => { const { addToast } = useToasts(); const [previewImage, setPreviewImage] = useState(""); @@ -314,78 +314,78 @@ const PlaceForm: FC = ({ const handleUpdatePlace = async (data: IFormData) => { setSubmitting(true); try { - if (place) { - let image = place.image; + if (!place) { + throw new Error("Something went wrong! Please try again later"); + } - if (!watch("publicId")) { - const { - data: signatureData, - errors: signatureErrors, - } = await createSignature(); + let image = place.image; - if (signatureErrors && signatureErrors.length) { - signatureErrors.map((error) => { - throw new Error( - error.message ?? "Something went wrong! Please try again later" - ); - }); - } + if (!watch("publicId")) { + const { + data: signatureData, + errors: signatureErrors, + } = await createSignature(); - if (signatureData) { - const { signature, timestamp } = signatureData.createImageSignature; - const imageData = await uploadImage( - data.image[0] as File, - signature, - timestamp - ); + if (signatureErrors && signatureErrors.length) { + const errors = signatureErrors.map( + (error) => + error.message ?? "Something went wrong! Please try again later" + ); + throw new Error(errors.join(", ")); + } - if (imageData.error) { - throw new Error( - imageData.error.message ?? - "Something went wrong! Please try again later" - ); - } + if (signatureData) { + const { signature, timestamp } = signatureData.createImageSignature; + const imageData = await uploadImage( + data.image[0] as File, + signature, + timestamp + ); - image = imageData.secure_url; + if (imageData.error) { + throw new Error( + imageData.error.message ?? + "Something went wrong! Please try again later" + ); } + + image = imageData.secure_url; } + } - const { data: placeData, errors: placeErrors } = await updatePlace({ - variables: { - id: place.id, - input: { - placeName: data.placeName, - placeType: data.placeType, - description: data.description, - address: data.address, - image: image, - coordinates: { - latitude: data.latitude, - longitude: data.longitude, - }, + const { data: placeData, errors: placeErrors } = await updatePlace({ + variables: { + id: place.id, + input: { + placeName: data.placeName, + placeType: data.placeType, + description: data.description, + address: data.address, + image: image, + coordinates: { + latitude: data.latitude, + longitude: data.longitude, }, }, - }); + }, + }); - if (placeErrors && placeErrors.length) { - placeErrors.map((error) => { - throw new Error( - error.message ?? "Something went wrong! Please try again later" - ); - }); - } + if (placeErrors && placeErrors.length) { + const errors = placeErrors.map( + (error) => + error.message ?? "Something went wrong! Please try again later" + ); + throw new Error(errors.join(", ")); + } - if (placeData?.updatePlace?.id) { - addToast("Place updated successfully", { - appearance: "success", - }); - onSubmitted(placeData.updatePlace.id); - } else { - throw new Error("Something went wrong! Please try again later"); - } - } else { + if (!placeData?.updatePlace?.id) { throw new Error("Something went wrong! Please try again later"); } + + addToast("Place updated successfully", { + appearance: "success", + }); + onSubmitted(placeData.updatePlace.id); } catch (err) { console.error(`😱: ${err}`); addToast(err.message ?? "Something went wrong! Please try again later.", { @@ -425,7 +425,7 @@ const PlaceForm: FC = ({ shouldValidate: true, shouldDirty: true, }); - searchedCoordiantes({ latitude: lat, longitude: lng }); + searchedCoordinates({ latitude: lat, longitude: lng }); }} error={errors?.address || errors?.latitude || errors?.longitude} /> diff --git a/src/components/PlaceList.tsx b/src/components/PlaceList.tsx index b8769c0..a233825 100644 --- a/src/components/PlaceList.tsx +++ b/src/components/PlaceList.tsx @@ -6,12 +6,13 @@ import Link from "next/link"; import LocationSearch from "./LocationSearch"; import { IoLocationSharp } from "react-icons/io5"; import { minWidthXl } from "src/utils"; +import { useAuth } from "src/contexts"; interface IPlaceList { places: GetPlacesQuery_places[]; focusedPlaceId: string; setFocusedPlaceId: Dispatch>; - searchedCoordiantes: ({ + searchedCoordinates: ({ latitude, longitude, }: { @@ -24,8 +25,9 @@ const PlaceList: FC = ({ places, focusedPlaceId, setFocusedPlaceId, - searchedCoordiantes, + searchedCoordinates, }) => { + const { authenticated } = useAuth(); return (
= ({ - searchedCoordiantes({ latitude: lat, longitude: lng }) + searchedCoordinates({ latitude: lat, longitude: lng }) } />
+ {places.length === 0 && ( +
+
Nothing found here!
+ {authenticated ? ( +
+ Want to share something interesting about this place, click on + "Add a new Place" +
+ ) : ( +
+ Want to add something to this place? Please SignIn / SignUp +
+ )} +
+ )} {places.map((place) => (