Skip to content

Commit

Permalink
Merge pull request #6050 from espoon-voltti/fix-child-document-save-loop
Browse files Browse the repository at this point in the history
Korjataan lapsen dokumentin muokkausnäkymän tallennuskäyttäytyminen virhetilanteissa
  • Loading branch information
akheron authored Nov 29, 2024
2 parents fc2d95e + d0c3c2a commit cf3bd82
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ import InfoModal from 'lib-components/molecules/modals/InfoModal'
import { H1, H2 } from 'lib-components/typography'
import { defaultMargins, Gap } from 'lib-components/white-space'
import colors from 'lib-customizations/common'
import { faExclamationTriangle } from 'lib-icons'
import { fasCheckCircle, fasExclamationTriangle } from 'lib-icons'

import { useTranslation } from '../../state/i18n'
Expand Down Expand Up @@ -185,7 +186,7 @@ const ChildDocumentEditViewInner = React.memo(

const [lastSaved, setLastSaved] = useState(HelsinkiDateTime.now())
const [lastSavedContent, setLastSavedContent] = useState(document.content)
const [lockError, setLockError] = useState(false)
const [error, setError] = useState<'lock' | 'other' | null>(null)

const { mutateAsync: updateChildDocumentContent, isPending: submitting } =
useMutationResult(updateChildDocumentContentMutation)
Expand All @@ -212,8 +213,8 @@ const ChildDocumentEditViewInner = React.memo(
setLastSaved(HelsinkiDateTime.now())
setLastSavedContent(content)
} else {
if (result.isFailure && result.errorCode === 'invalid-lock') {
setLockError(true)
if (result.isFailure) {
setError(result.errorCode === 'invalid-lock' ? 'lock' : 'other')
}
}
},
Expand All @@ -232,19 +233,27 @@ const ChildDocumentEditViewInner = React.memo(

useEffect(() => {
if (
!lockError &&
error === null &&
debouncedValidContent !== null &&
!isEqual(lastSavedContent, debouncedValidContent) &&
!submitting
) {
void save(debouncedValidContent)
}
}, [lockError, debouncedValidContent, lastSavedContent, save, submitting])
}, [error, debouncedValidContent, lastSavedContent, save, submitting])

useEffect(() => {
if (error === 'other') {
const handle = setTimeout(() => setError(null), 5000)
return () => clearTimeout(handle)
}
return undefined
}, [error])

const goBack = () =>
navigate(`/child-information/${childIdFromUrl ?? document.child.id}`)

if (lockError) {
if (error === 'lock') {
return (
<ConcurrentEditWarning
documentId={document.id}
Expand Down Expand Up @@ -286,6 +295,15 @@ const ChildDocumentEditViewInner = React.memo(
{!saved && (
<Spinner size={defaultMargins.m} data-qa="saving-spinner" />
)}
{error === 'other' && (
<span>
<FontAwesomeIcon
icon={faExclamationTriangle}
color={colors.status.warning}
/>{' '}
{i18n.childInformation.childDocuments.editor.saveError}
</span>
)}
</FixedSpaceRow>
</FixedSpaceRow>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -847,6 +847,7 @@ export const fi = {
'Toinen käyttäjä muokkaa asiakirjaa. Yritä myöhemmin uudelleen.',
lockedErrorDetailed: (modifiedByName: string, opensAt: string) =>
`Käyttäjä ${modifiedByName} on muokkaamassa asiakirjaa. Asiakirjan lukitus vapautuu ${opensAt} mikäli muokkaamista ei jatketa. Yritä myöhemmin uudelleen.`,
saveError: 'Asiakirjan tallentaminen epäonnistui.',
preview: 'Esikatsele',
publish: 'Julkaise huoltajalle',
publishConfirmTitle: 'Haluatko varmasti julkaista huoltajalle?',
Expand Down

0 comments on commit cf3bd82

Please sign in to comment.