Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bugfix: clear dialogs data upon reopening #339

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,17 @@ const SiteModal = ({ open, handleClose, siteId }: Props) => {
const [site, setSite] = useState(defaultSiteDto)
const [availableSiteTypes, setAvailableSiteTypes] = useState<SiteType[]>([])
const [siteImageURL, setSiteImageURL] = useState<string>()
const [loading, setLoading] = useState(true)

const fetchSite = useCallback(async () => {
if (!siteId) {
// Clear the image that could come from having opened the modal with a previous site
setSiteImageURL(undefined)

return
}

setLoading(true)

const siteDetail = await getApiClient().siteClient.detail(siteId)
const { dmsLatitude, dmsLongitude } = siteDetail.coordinate

Expand All @@ -82,10 +84,15 @@ const SiteModal = ({ open, handleClose, siteId }: Props) => {
visibleOnMap: siteDetail.visibleMap,
})
setSiteImageURL(URL.createObjectURL(blob))
setLoading(false)
}, [siteId, getApiClient])

const fetchSiteTypes = useCallback(
async () => setAvailableSiteTypes(await getApiClient().siteClient.types()),
async () => {
setLoading(true)
setAvailableSiteTypes(await getApiClient().siteClient.types())
setLoading(false)
},
[getApiClient],
)

Expand All @@ -97,13 +104,19 @@ const SiteModal = ({ open, handleClose, siteId }: Props) => {
useEffect(() => void fetchSiteTypes(), [fetchSiteTypes])

useEffect(() => {
if (!open) return
if (!open) {
if (!siteId) setSite(defaultSiteDto)

return
}

void fetchSite()
}, [open, fetchSite])
}, [open, fetchSite, siteId])

useEffect(() => setSite(defaultSiteDto), [siteId])

if (loading) return null
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

More of a "hide on opening" than a "clear on close". But whatever, as long as it works, this isn't the most robust part of the site.
As long as we keep improving the form validations, in #239, #240 and #242, that'll be the most important part.


return (
<Dialog fullWidth maxWidth='sm' onClose={(_, reason) => handleClose(reason)} open={open}>
<DialogTitle>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,12 @@ const PostCommentsDialog = ({ open, postId, siteId, handleClose }: Props) => {

useEffect(() => {
// Since this is a dialog, we only want to fetch the comments once it is opened
if (!open || commentsLoaded) return
if (!open || commentsLoaded) {
setCommentBody('')
setCommentBodyNumberOfWords(0)
setCommentBodyError(undefined)
return
}

const fetchComments = async () => setComments(await getApiClient().commentClient.all(postId))

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Dialog, DialogActions, DialogContent, DialogTitle } from '@mui/material'
import { useContext, useState } from 'react'
import { useContext, useEffect, useState } from 'react'
import { useTranslation } from 'react-i18next'

import { SnackbarContext } from '@components/context/SnackbarContext'
Expand Down Expand Up @@ -27,6 +27,10 @@ const SiteAnnouncementModal = ({ announcement, isOpen, handleClose }: Props) =>
const { getApiClient } = useApiClient()
const { openAlertSnackbar } = useContext(SnackbarContext)

useEffect(() => {
if (!isOpen) setEditedAnnouncement(announcement)
}, [isOpen, announcement])

const handleSubmitSiteAnnouncement = async () => {
try {
await getApiClient().announcementClient.update(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Dialog, DialogActions, DialogContent, DialogTitle } from '@mui/material'
import { useContext, useState } from 'react'
import { useContext, useEffect, useState } from 'react'
import { useTranslation } from 'react-i18next'

import facebookLogo from '@assets/icons/facebook-contact-logo.svg'
Expand Down Expand Up @@ -36,6 +36,10 @@ const SiteContactModal = ({ contact, isOpen, handleClose }: Props) => {
const { getApiClient } = useApiClient()
const { openAlertSnackbar } = useContext(SnackbarContext)

useEffect(() => {
if (!isOpen) setEditedContact(contact)
}, [isOpen, contact])

const handleSubmitSiteContact = (): void => {
getApiClient().contactClient.update(contact.id, editedContact as PatchedContact).then(
() => {
Expand Down
Loading