fix(sign): allow signing without selectedMark - no currentUserMarks

This commit is contained in:
enes 2024-12-23 17:44:33 +01:00
parent 8df5084703
commit 92f23bab91
2 changed files with 24 additions and 23 deletions

View File

@ -17,7 +17,7 @@ interface MarkFormFieldProps {
handleCurrentUserMarkChange: (mark: CurrentUserMark) => void handleCurrentUserMarkChange: (mark: CurrentUserMark) => void
handleSelectedMarkValueChange: (value: string) => void handleSelectedMarkValueChange: (value: string) => void
handleSubmit: (event: React.MouseEvent<HTMLButtonElement>) => void handleSubmit: (event: React.MouseEvent<HTMLButtonElement>) => void
selectedMark: CurrentUserMark selectedMark: CurrentUserMark | null
selectedMarkValue: string selectedMarkValue: string
} }
@ -34,26 +34,23 @@ const MarkFormField = ({
}: MarkFormFieldProps) => { }: MarkFormFieldProps) => {
const [displayActions, setDisplayActions] = useState(true) const [displayActions, setDisplayActions] = useState(true)
const [complete, setComplete] = useState(false) const [complete, setComplete] = useState(false)
const isReadyToSign = () => const isReadyToSign = () =>
isCurrentUserMarksComplete(currentUserMarks) || isCurrentUserMarksComplete(currentUserMarks) ||
isCurrentValueLast(currentUserMarks, selectedMark, selectedMarkValue) isCurrentValueLast(currentUserMarks, selectedMark, selectedMarkValue)
const isCurrent = (currentMark: CurrentUserMark) => const isCurrent = (currentMark: CurrentUserMark) =>
currentMark.id === selectedMark.id && !complete currentMark.id === selectedMark?.id && !complete
const isDone = (currentMark: CurrentUserMark) => const isDone = (currentMark: CurrentUserMark) =>
isCurrent(currentMark) ? !!selectedMarkValue : currentMark.isCompleted isCurrent(currentMark) ? !!selectedMarkValue : currentMark.isCompleted
const findNext = () => { const findNext = () => {
return ( return (
currentUserMarks[selectedMark.id] || currentUserMarks[selectedMark!.id] ||
findNextIncompleteCurrentUserMark(currentUserMarks) findNextIncompleteCurrentUserMark(currentUserMarks)
) )
} }
const handleFormSubmit = (event: React.FormEvent<HTMLFormElement>) => { const handleFormSubmit = (event: React.FormEvent<HTMLFormElement>) => {
event.preventDefault() event.preventDefault()
console.log('handle form submit runs...')
// Without this line, we lose mark values when switching // Without this line, we lose mark values when switching
handleCurrentUserMarkChange(selectedMark) handleCurrentUserMarkChange(selectedMark!)
if (!complete) { if (!complete) {
isReadyToSign() isReadyToSign()
@ -63,15 +60,16 @@ const MarkFormField = ({
} }
const toggleActions = () => setDisplayActions(!displayActions) const toggleActions = () => setDisplayActions(!displayActions)
const markLabel = getToolboxLabelByMarkType(selectedMark.mark.type) const markLabel = selectedMark
? getToolboxLabelByMarkType(selectedMark.mark.type)
: ''
const handleCurrentUserMarkClick = (mark: CurrentUserMark) => { const handleCurrentUserMarkClick = (mark: CurrentUserMark) => {
setComplete(false) setComplete(false)
handleCurrentUserMarkChange(mark) handleCurrentUserMarkChange(mark)
} }
const handleSelectCompleteMark = () => { const handleSelectCompleteMark = () => {
handleCurrentUserMarkChange(selectedMark) if (currentUserMarks.length) handleCurrentUserMarkChange(selectedMark!)
setComplete(true) setComplete(true)
} }
@ -106,14 +104,15 @@ const MarkFormField = ({
<div className={styles.actionsWrapper}> <div className={styles.actionsWrapper}>
<div className={styles.actionsTop}> <div className={styles.actionsTop}>
<div className={styles.actionsTopInfo}> <div className={styles.actionsTopInfo}>
{!complete && ( {!complete && selectedMark ? (
<p className={styles.actionsTopInfoText}>Add {markLabel}</p> <p className={styles.actionsTopInfoText}>Add {markLabel}</p>
) : (
<p className={styles.actionsTopInfoText}>Finish</p>
)} )}
{complete && <p className={styles.actionsTopInfoText}>Finish</p>}
</div> </div>
</div> </div>
<div className={styles.inputWrapper}> <div className={styles.inputWrapper}>
{!complete && ( {!complete && selectedMark ? (
<form onSubmit={(e) => handleFormSubmit(e)}> <form onSubmit={(e) => handleFormSubmit(e)}>
<MarkInput <MarkInput
markType={selectedMark.mark.type} markType={selectedMark.mark.type}
@ -129,9 +128,7 @@ const MarkFormField = ({
</Button> </Button>
</div> </div>
</form> </form>
)} ) : (
{complete && (
<div className={styles.actionsBottom}> <div className={styles.actionsBottom}>
<Button <Button
onClick={handleSignAndComplete} onClick={handleSignAndComplete}

View File

@ -123,15 +123,19 @@ const isLast = <T>(index: number, arr: T[]) => index === arr.length - 1
const isCurrentValueLast = ( const isCurrentValueLast = (
currentUserMarks: CurrentUserMark[], currentUserMarks: CurrentUserMark[],
selectedMark: CurrentUserMark, selectedMark: CurrentUserMark | null,
selectedMarkValue: string selectedMarkValue: string
) => { ) => {
const filteredMarks = currentUserMarks.filter( if (selectedMark && currentUserMarks.length > 0) {
(mark) => mark.id !== selectedMark.id const filteredMarks = currentUserMarks.filter(
) (mark) => mark.id !== selectedMark.id
return ( )
isCurrentUserMarksComplete(filteredMarks) && selectedMarkValue.length > 0 return (
) isCurrentUserMarksComplete(filteredMarks) && selectedMarkValue.length > 0
)
}
return true
} }
const getUpdatedMark = ( const getUpdatedMark = (