-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(apps/manage-frontend): auto-save of user inputs in element edit …
…modal (#4474)
- Loading branch information
1 parent
49da9f6
commit 367e5c2
Showing
12 changed files
with
540 additions
and
32 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
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
45 changes: 45 additions & 0 deletions
45
apps/frontend-manage/src/components/questions/manipulation/AutoSaveMonitor.ts
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,45 @@ | ||
import { Dispatch, SetStateAction, useCallback, useEffect, useRef } from 'react' | ||
import { ElementFormTypes } from './types' | ||
|
||
function AutoSaveMonitor({ | ||
values, | ||
initialValuesString, | ||
setAutoSavedElement, | ||
}: { | ||
values: ElementFormTypes | ||
initialValuesString: string | ||
setAutoSavedElement: Dispatch<SetStateAction<ElementFormTypes>> | ||
}) { | ||
// create a call-back function that will save the editor's content every 2 seconds | ||
// (if not actively typing -> do not disturb other state updates) | ||
const savingTimeout = useRef<NodeJS.Timeout | null>(null) | ||
const autoSaveContent = useCallback( | ||
({ values }: { values: ElementFormTypes }) => { | ||
if (savingTimeout.current) { | ||
clearTimeout(savingTimeout.current as NodeJS.Timeout) | ||
} | ||
|
||
savingTimeout.current = setTimeout(async () => { | ||
// only update the stored content if it has changed | ||
if (JSON.stringify(values) !== initialValuesString) { | ||
setAutoSavedElement(values) | ||
} | ||
}, 2000) | ||
}, | ||
[setAutoSavedElement, initialValuesString] | ||
) | ||
|
||
useEffect(() => { | ||
autoSaveContent({ values }) | ||
|
||
return () => { | ||
if (savingTimeout.current) { | ||
clearTimeout(savingTimeout.current as NodeJS.Timeout) | ||
} | ||
} | ||
}, [values]) | ||
|
||
return null | ||
} | ||
|
||
export default AutoSaveMonitor |
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
63 changes: 63 additions & 0 deletions
63
apps/frontend-manage/src/components/questions/manipulation/RecoveryPrompt.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,63 @@ | ||
import { faArrowsRotate, faBan } from '@fortawesome/free-solid-svg-icons' | ||
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome' | ||
import { Button, Modal, UserNotification } from '@uzh-bf/design-system' | ||
import { useTranslations } from 'next-intl' | ||
|
||
function RecoveryPrompt({ | ||
open, | ||
onRecovery, | ||
onDiscard, | ||
editMode, | ||
}: { | ||
open: boolean | ||
onRecovery: () => void | ||
onDiscard: () => void | ||
editMode: boolean | ||
}) { | ||
const t = useTranslations() | ||
|
||
return ( | ||
<Modal | ||
hideCloseButton | ||
escapeDisabled | ||
open={open} | ||
onClose={() => null} | ||
title={t('manage.questionForms.recoverData')} | ||
className={{ content: 'gap-1' }} | ||
> | ||
<UserNotification | ||
type="warning" | ||
message={ | ||
editMode | ||
? t('manage.questionForms.temporaryStorageEditing') | ||
: t('manage.questionForms.temporaryStorageCreation') | ||
} | ||
className={{ root: 'text-base' }} | ||
/> | ||
<div className="mt-2 flex flex-row justify-between"> | ||
<Button | ||
onClick={onDiscard} | ||
className={{ | ||
root: 'border-2 border-red-600 hover:border-red-600 hover:text-red-600', | ||
}} | ||
data={{ cy: 'discard-recovered-element-data' }} | ||
> | ||
<FontAwesomeIcon icon={faBan} /> | ||
<div>{t('manage.questionForms.discard')}</div> | ||
</Button> | ||
<Button | ||
onClick={onRecovery} | ||
className={{ | ||
root: 'border-primary-80 hover:border-primary-80 border-2', | ||
}} | ||
data={{ cy: 'load-recovered-element-data' }} | ||
> | ||
<FontAwesomeIcon icon={faArrowsRotate} /> | ||
<div>{t('manage.questionForms.loadData')}</div> | ||
</Button> | ||
</div> | ||
</Modal> | ||
) | ||
} | ||
|
||
export default RecoveryPrompt |
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.