Skip to content

Commit

Permalink
Add loading state to button to create subpoena
Browse files Browse the repository at this point in the history
  • Loading branch information
oddsson committed Oct 29, 2024
1 parent 574ef99 commit 9f48c3c
Showing 1 changed file with 13 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import { FC, useCallback, useContext, useState } from 'react'
import { useIntl } from 'react-intl'
import router from 'next/router'

import { Box, Button } from '@island.is/island-ui/core'
import { Box, Button, toast } from '@island.is/island-ui/core'
import * as constants from '@island.is/judicial-system/consts'
import { formatDate } from '@island.is/judicial-system/formatters'
import { titles } from '@island.is/judicial-system-web/messages'
import { errors, titles } from '@island.is/judicial-system-web/messages'
import {
CourtArrangements,
CourtCaseInfo,
Expand All @@ -32,6 +32,7 @@ const Subpoena: FC = () => {
useContext(FormContext)
const [navigateTo, setNavigateTo] = useState<keyof stepValidationsType>()
const [newSubpoenas, setNewSubpoenas] = useState<string[]>([])
const [isCreatingSubpoena, setIsCreatingSubpoena] = useState<boolean>(false)
const { updateDefendantState, updateDefendant } = useDefendants()
const { formatMessage } = useIntl()
const {
Expand All @@ -51,6 +52,8 @@ const Subpoena: FC = () => {

const handleNavigationTo = useCallback(
async (destination: keyof stepValidationsType) => {
setIsCreatingSubpoena(true)

if (!isSchedulingArraignmentDate) {
router.push(`${destination}/${workingCase.id}`)
return
Expand All @@ -74,6 +77,9 @@ const Subpoena: FC = () => {
const allDefendantsUpdated = await Promise.all(promises)

if (!allDefendantsUpdated.every((result) => result)) {
toast.error(formatMessage(errors.updateDefendant))
setIsCreatingSubpoena(false)

return
}

Expand All @@ -96,6 +102,9 @@ const Subpoena: FC = () => {
const courtDateUpdated = await sendCourtDateToServer(clearedConclusion)

if (!courtDateUpdated) {
toast.error(formatMessage(errors.updateCase))
setIsCreatingSubpoena(false)

return
}

Expand All @@ -109,6 +118,7 @@ const Subpoena: FC = () => {
isArraignmentScheduled,
sendCourtDateToServer,
updateDefendant,
formatMessage,
],
)

Expand Down Expand Up @@ -254,7 +264,7 @@ const Subpoena: FC = () => {
}}
primaryButtonText={formatMessage(strings.modalPrimaryButtonText)}
secondaryButtonText={formatMessage(strings.modalSecondaryButtonText)}
isPrimaryButtonLoading={false}
isPrimaryButtonLoading={isCreatingSubpoena}
/>
)}
</PageLayout>
Expand Down

0 comments on commit 9f48c3c

Please sign in to comment.