Skip to content

Commit 6fe37c8

Browse files
committed
bugfix: clear dialogs data upon reopening
1 parent 01f22a2 commit 6fe37c8

File tree

4 files changed

+33
-7
lines changed

4 files changed

+33
-7
lines changed

canopeum_frontend/src/components/analytics/site-modal/SiteModal.tsx

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,17 @@ const SiteModal = ({ open, handleClose, siteId }: Props) => {
5050
const [site, setSite] = useState(defaultSiteDto)
5151
const [availableSiteTypes, setAvailableSiteTypes] = useState<SiteType[]>([])
5252
const [siteImageURL, setSiteImageURL] = useState<string>()
53+
const [loading, setLoading] = useState(true)
5354

5455
const fetchSite = useCallback(async () => {
5556
if (!siteId) {
5657
// Clear the image that could come from having opened the modal with a previous site
5758
setSiteImageURL(undefined)
58-
5959
return
6060
}
6161

62+
setLoading(true)
63+
6264
const siteDetail = await getApiClient().siteClient.detail(siteId)
6365
const { dmsLatitude, dmsLongitude } = siteDetail.coordinate
6466

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

8790
const fetchSiteTypes = useCallback(
88-
async () => setAvailableSiteTypes(await getApiClient().siteClient.types()),
91+
async () => {
92+
setLoading(true)
93+
setAvailableSiteTypes(await getApiClient().siteClient.types())
94+
setLoading(false)
95+
},
8996
[getApiClient],
9097
)
9198

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

99106
useEffect(() => {
100-
if (!open) return
107+
if (!open) {
108+
if (!siteId) setSite(defaultSiteDto)
109+
110+
return
111+
}
101112

102113
void fetchSite()
103-
}, [open, fetchSite])
114+
}, [open, fetchSite, siteId])
104115

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

118+
if (loading) return null
119+
107120
return (
108121
<Dialog fullWidth maxWidth='sm' onClose={(_, reason) => handleClose(reason)} open={open}>
109122
<DialogTitle>

canopeum_frontend/src/components/social/PostCommentsDialog.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,12 @@ const PostCommentsDialog = ({ open, postId, siteId, handleClose }: Props) => {
4141

4242
useEffect(() => {
4343
// Since this is a dialog, we only want to fetch the comments once it is opened
44-
if (!open || commentsLoaded) return
44+
if (!open || commentsLoaded) {
45+
setCommentBody('')
46+
setCommentBodyNumberOfWords(0)
47+
setCommentBodyError(undefined)
48+
return
49+
}
4550

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

canopeum_frontend/src/components/social/site-modal/SiteAnnouncementModal.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Dialog, DialogActions, DialogContent, DialogTitle } from '@mui/material'
2-
import { useContext, useState } from 'react'
2+
import { useContext, useEffect, useState } from 'react'
33
import { useTranslation } from 'react-i18next'
44

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

30+
useEffect(() => {
31+
if (!isOpen) setEditedAnnouncement(announcement)
32+
}, [isOpen, announcement])
33+
3034
const handleSubmitSiteAnnouncement = async () => {
3135
try {
3236
await getApiClient().announcementClient.update(

canopeum_frontend/src/components/social/site-modal/SiteContactModal.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Dialog, DialogActions, DialogContent, DialogTitle } from '@mui/material'
2-
import { useContext, useState } from 'react'
2+
import { useContext, useEffect, useState } from 'react'
33
import { useTranslation } from 'react-i18next'
44

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

39+
useEffect(() => {
40+
if (!isOpen) setEditedContact(contact)
41+
}, [isOpen, contact])
42+
3943
const handleSubmitSiteContact = (): void => {
4044
getApiClient().contactClient.update(contact.id, editedContact as PatchedContact).then(
4145
() => {

0 commit comments

Comments
 (0)