-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* refactor: add geolocation modal * refactor: add translations * refactor: update fr trans * refactor: upgrade query client
- Loading branch information
Showing
7 changed files
with
347 additions
and
114 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
156 changes: 156 additions & 0 deletions
156
src/components/item/settings/GeolocationModalButton.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,156 @@ | ||
import { useRef, useState } from 'react'; | ||
|
||
import { | ||
DialogActions, | ||
DialogContent, | ||
DialogContentText, | ||
Stack, | ||
TextField, | ||
} from '@mui/material'; | ||
import Dialog from '@mui/material/Dialog'; | ||
import DialogTitle from '@mui/material/DialogTitle'; | ||
|
||
import { CountryForm } from '@graasp/map'; | ||
import { DiscriminatedItem } from '@graasp/sdk'; | ||
import { COMMON } from '@graasp/translations'; | ||
import { Button } from '@graasp/ui'; | ||
|
||
import { useBuilderTranslation, useCommonTranslation } from '@/config/i18n'; | ||
import notifier from '@/config/notifier'; | ||
import { hooks, mutations } from '@/config/queryClient'; | ||
import { BUILDER } from '@/langs/constants'; | ||
|
||
type Props = { | ||
item: DiscriminatedItem; | ||
}; | ||
|
||
export const GeolocationModalButton = ({ item }: Props): JSX.Element => { | ||
const { t } = useBuilderTranslation(); | ||
const { t: commonT } = useCommonTranslation(); | ||
const [open, setOpen] = useState(false); | ||
const { data: geoloc } = hooks.useItemGeolocation(item.id); | ||
const { mutate: saveGeoloc } = mutations.usePutItemGeolocation(); | ||
|
||
const helperLabelRef = useRef<HTMLInputElement>(null); | ||
const addressLabelRef = useRef<HTMLInputElement>(null); | ||
const latRef = useRef<HTMLInputElement>(null); | ||
const lngRef = useRef<HTMLInputElement>(null); | ||
const [country, setCountry] = useState<string>(); | ||
|
||
const handleClickOpen = () => { | ||
setOpen(true); | ||
}; | ||
|
||
const handleClose = () => { | ||
setOpen(false); | ||
}; | ||
|
||
const onSave = () => { | ||
const lat = latRef.current?.value ?? geoloc?.lat; | ||
const lng = lngRef.current?.value ?? geoloc?.lng; | ||
|
||
if (!lat || !lng) { | ||
notifier({ | ||
type: 'PUT_GEOLOCATION_ERROR', | ||
payload: { | ||
error: new Error(t(BUILDER.ITEM_GEOLOCATION_ADVANCED_MODAL_ERROR)), | ||
}, | ||
}); | ||
return; | ||
} | ||
|
||
saveGeoloc({ | ||
itemId: item.id, | ||
geolocation: { | ||
helperLabel: helperLabelRef.current?.value ?? geoloc?.helperLabel, | ||
addressLabel: addressLabelRef.current?.value ?? geoloc?.addressLabel, | ||
country: country ?? geoloc?.country, | ||
lat: +lat, | ||
lng: +lng, | ||
}, | ||
}); | ||
setOpen(false); | ||
}; | ||
|
||
return ( | ||
<> | ||
<Button variant="text" onClick={handleClickOpen}> | ||
{t(BUILDER.ITEM_GEOLOCATION_ADVANCED_BUTTON)} | ||
</Button> | ||
<Dialog onClose={handleClose} open={open} scroll="body"> | ||
<DialogTitle> | ||
{t(BUILDER.ITEM_GEOLOCATION_ADVANCED_MODAL_TITLE)} | ||
</DialogTitle> | ||
|
||
<DialogContent> | ||
<Stack gap={2}> | ||
<Stack> | ||
<DialogContentText> | ||
{t(BUILDER.ITEM_GEOLOCATION_ADVANCED_MODAL_DESCRIPTION)} | ||
</DialogContentText> | ||
</Stack> | ||
<Stack direction="row" gap={2}> | ||
<Stack flexGrow={1}> | ||
<TextField | ||
inputRef={latRef} | ||
defaultValue={geoloc?.lat} | ||
label={t( | ||
BUILDER.ITEM_GEOLOCATION_ADVANCED_MODAL_LATITUDE_LABEL, | ||
)} | ||
type="number" | ||
/> | ||
</Stack> | ||
<Stack flexGrow={1}> | ||
<TextField | ||
inputRef={lngRef} | ||
defaultValue={geoloc?.lng} | ||
label={t( | ||
BUILDER.ITEM_GEOLOCATION_ADVANCED_MODAL_LONGITUDE_LABEL, | ||
)} | ||
type="number" | ||
/> | ||
</Stack> | ||
</Stack> | ||
<Stack> | ||
<TextField | ||
inputRef={addressLabelRef} | ||
label={t(BUILDER.ITEM_GEOLOCATION_ADVANCED_MODAL_ADDRESS_LABEL)} | ||
multiline | ||
defaultValue={geoloc?.addressLabel ?? undefined} | ||
/> | ||
</Stack> | ||
<Stack> | ||
<TextField | ||
inputRef={helperLabelRef} | ||
defaultValue={geoloc?.helperLabel ?? undefined} | ||
multiline | ||
label={t( | ||
BUILDER.ITEM_GEOLOCATION_ADVANCED_MODAL_SECONDARY_ADDRESS_LABEL, | ||
)} | ||
placeholder={t( | ||
BUILDER.ITEM_GEOLOCATION_ADVANCED_MODAL_SECONDARY_ADDRESS_PLACEHOLDER, | ||
)} | ||
/> | ||
</Stack> | ||
<Stack> | ||
<CountryForm | ||
label={t(BUILDER.ITEM_GEOLOCATION_ADVANCED_MODAL_COUNTRY_LABEL)} | ||
initialValue={geoloc?.country ?? undefined} | ||
onChange={(e) => { | ||
// eslint-disable-next-line no-console | ||
setCountry(e.alpha2); | ||
}} | ||
/> | ||
</Stack> | ||
</Stack> | ||
</DialogContent> | ||
<DialogActions> | ||
<Button variant="text">{commonT(COMMON.CANCEL_BUTTON)}</Button> | ||
<Button onClick={onSave}>{commonT(COMMON.SAVE_BUTTON)}</Button> | ||
</DialogActions> | ||
</Dialog> | ||
</> | ||
); | ||
}; | ||
|
||
export default GeolocationModalButton; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.