Skip to content

Commit

Permalink
feat: allow null position as negation, dont display that as venue
Browse files Browse the repository at this point in the history
  • Loading branch information
nzambello committed Apr 17, 2024
1 parent f08c2a7 commit 17cd359
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 21 deletions.
10 changes: 7 additions & 3 deletions src/components/Header/Header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,13 @@ const Header: React.FC<Props> = ({
)}
{memori.needsPosition && position && (
<div className="memori-header--position">
<span className="memori-header--position-placeName">
{position.placeName}
</span>
{position.latitude !== 0 &&
position.longitude !== 0 &&
position.placeName !== 'Position' && (
<span className="memori-header--position-placeName">
{position.placeName}
</span>
)}
<Button
primary
shape="circle"
Expand Down
12 changes: 10 additions & 2 deletions src/components/PositionDrawer/PositionDrawer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export interface Props {
open: boolean;
onClose: (venue?: Venue) => void;
venue?: Venue;
setVenue: (venue?: Venue) => void;
setVenue: (venue: Venue) => void;
}

const PositionDrawer = ({ memori, open, onClose, venue, setVenue }: Props) => {
Expand All @@ -24,7 +24,15 @@ const PositionDrawer = ({ memori, open, onClose, venue, setVenue }: Props) => {
animated={false}
>
<p>{t('write_and_speak.requirePositionHelp', { name: memori.name })} </p>
<VenueWidget venue={venue} setVenue={setVenue} showUncertainty={false} />
<VenueWidget
venue={venue}
setVenue={setVenue}
showUncertainty={false}
saveAndClose={venue => {
setVenue(venue);
onClose(venue);
}}
/>
</Drawer>
);
};
Expand Down
49 changes: 37 additions & 12 deletions src/components/VenueWidget/VenueWidget.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,10 @@ export type NominatimItem = {

export interface Props {
venue?: Venue;
setVenue: (venue?: Venue) => void;
setVenue: (venue: Venue) => void;
showUncertainty?: boolean;
showGpsButton?: boolean;
saveAndClose?: (venue: Venue) => void;
}

const Circle = ({
Expand Down Expand Up @@ -138,6 +139,7 @@ const VenueWidget = ({
setVenue,
showUncertainty = false,
showGpsButton = true,
saveAndClose,
}: Props) => {
const { t } = useTranslation();
const [isClient, setIsClient] = useState(false);
Expand All @@ -152,31 +154,35 @@ const VenueWidget = ({

navigator.geolocation.getCurrentPosition(
async coords => {
let venue: Venue = {
latitude: coords.coords.latitude,
longitude: coords.coords.longitude,
placeName: 'Position',
uncertainty: coords.coords.accuracy / 1000,
};

try {
const result = await fetch(
`https://nominatim.openstreetmap.org/reverse?lat=${coords.coords.latitude}&lon=${coords.coords.longitude}&format=jsonv2&addressdetails=1`
);
const response = (await result.json()) as NominatimItem;

const placeName = getPlaceName(response);

setVenue({
venue = {
latitude: coords.coords.latitude,
longitude: coords.coords.longitude,
placeName: placeName,
uncertainty: coords.coords.accuracy / 1000,
});
};
setVenue(venue);
} catch (e) {
let err = e as Error;
console.error('[POSITION ERROR]', err);
if (err?.message) toast.error(err.message);

setVenue({
latitude: coords.coords.latitude,
longitude: coords.coords.longitude,
placeName: 'Position',
uncertainty: coords.coords.accuracy / 1000,
});
setVenue(venue);
} finally {
if (saveAndClose) saveAndClose(venue);
}

setUpdatingPosition(false);
Expand Down Expand Up @@ -324,10 +330,11 @@ const VenueWidget = ({
)}
</Combobox>
</div>

{showGpsButton && (
<Button
className="memori--venue-widget__gps-button"
outlined
primary
loading={updatingPosition}
onClick={() => {
setUpdatingPosition(true);
Expand All @@ -340,6 +347,24 @@ const VenueWidget = ({
</>
)}
</div>
<div>
<Button
outlined
className="memori--venue-widget__no-location-button"
onClick={() => {
let venue: Venue = {
latitude: 0,
longitude: 0,
placeName: 'Position',
uncertainty: 0,
};
setVenue(venue);
if (saveAndClose) saveAndClose(venue);
}}
>
{t('write_and_speak.dontWantToProvidePosition')}
</Button>
</div>
{showUncertainty && (
<label className="memori--venue-widget__select-label">
<span>{t('uncertain')}: </span>
Expand Down Expand Up @@ -378,7 +403,7 @@ const VenueWidget = ({
)}
</div>
<div className="memori--venue-widget__form-item">
{venue?.placeName && (
{venue?.placeName && venue.placeName !== 'Position' && (
<p className="memori--venue--widget__place-name">
<strong>{t('venue')}</strong>: {venue.placeName}
</p>
Expand Down
43 changes: 39 additions & 4 deletions src/components/VenueWidget/__snapshots__/VenueWidget.test.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,18 @@ exports[`renders VenueWidget with venue set, exact location 1`] = `
/>
</div>
<button
class="memori-button memori-button--outlined memori-button--rounded memori-button--padded memori--venue-widget__gps-button"
class="memori-button memori-button--primary memori-button--rounded memori-button--padded memori--venue-widget__gps-button"
>
write_and_speak.useMyPosition
</button>
</div>
<div>
<button
class="memori-button memori-button--outlined memori-button--rounded memori-button--padded memori--venue-widget__no-location-button"
>
write_and_speak.dontWantToProvidePosition
</button>
</div>
</div>
<div
class="memori--venue-widget__form-item"
Expand Down Expand Up @@ -101,11 +108,18 @@ exports[`renders VenueWidget with venue set, with uncertainty radius 1`] = `
/>
</div>
<button
class="memori-button memori-button--outlined memori-button--rounded memori-button--padded memori--venue-widget__gps-button"
class="memori-button memori-button--primary memori-button--rounded memori-button--padded memori--venue-widget__gps-button"
>
write_and_speak.useMyPosition
</button>
</div>
<div>
<button
class="memori-button memori-button--outlined memori-button--rounded memori-button--padded memori--venue-widget__no-location-button"
>
write_and_speak.dontWantToProvidePosition
</button>
</div>
</div>
<div
class="memori--venue-widget__form-item"
Expand Down Expand Up @@ -172,6 +186,13 @@ exports[`renders VenueWidget without gps button unchanged 1`] = `
/>
</div>
</div>
<div>
<button
class="memori-button memori-button--outlined memori-button--rounded memori-button--padded memori--venue-widget__no-location-button"
>
write_and_speak.dontWantToProvidePosition
</button>
</div>
</div>
<div
class="memori--venue-widget__form-item"
Expand Down Expand Up @@ -238,11 +259,18 @@ exports[`renders VenueWidget without uncertainty unchanged 1`] = `
/>
</div>
<button
class="memori-button memori-button--outlined memori-button--rounded memori-button--padded memori--venue-widget__gps-button"
class="memori-button memori-button--primary memori-button--rounded memori-button--padded memori--venue-widget__gps-button"
>
write_and_speak.useMyPosition
</button>
</div>
<div>
<button
class="memori-button memori-button--outlined memori-button--rounded memori-button--padded memori--venue-widget__no-location-button"
>
write_and_speak.dontWantToProvidePosition
</button>
</div>
</div>
<div
class="memori--venue-widget__form-item"
Expand Down Expand Up @@ -309,11 +337,18 @@ exports[`renders empty VenueWidget unchanged 1`] = `
/>
</div>
<button
class="memori-button memori-button--outlined memori-button--rounded memori-button--padded memori--venue-widget__gps-button"
class="memori-button memori-button--primary memori-button--rounded memori-button--padded memori--venue-widget__gps-button"
>
write_and_speak.useMyPosition
</button>
</div>
<div>
<button
class="memori-button memori-button--outlined memori-button--rounded memori-button--padded memori--venue-widget__no-location-button"
>
write_and_speak.dontWantToProvidePosition
</button>
</div>
</div>
<div
class="memori--venue-widget__form-item"
Expand Down
1 change: 1 addition & 0 deletions src/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@
"placeNotFound": "Place not found!",
"requirePosition": "Insert your position before start",
"requirePositionHelp": "To talk to {{name}} you have to decide whether to provide your position or not. This is because different answers that it can provide you may depend on where you are.",
"dontWantToProvidePosition": "I don't want to provide my position",
"attachmentsLabel": "Enrich your message",
"iWantToTalkToIn": "I want to talk to {{name}} in"
},
Expand Down
1 change: 1 addition & 0 deletions src/locales/it.json
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@
"updatingPosition": "Aggiorno la posizione...",
"placeNotFound": "Luogo sconosciuto!",
"requirePositionHelp": "Per parlare con {{name}} devi decidere se fornire o meno la tua posizione. Questo perchè diverse risposte che potrà fornirti potrebbero dipendere da dove ti trovi.",
"dontWantToProvidePosition": "Non voglio fornire la mia posizione",
"attachmentsLabel": "Arricchisci il tuo messaggio",
"iWantToTalkToIn": "Voglio parlare con {{name}} in"
},
Expand Down

0 comments on commit 17cd359

Please sign in to comment.