Skip to content

Commit

Permalink
chore(j-s): Use lawyer registry from api (#17434)
Browse files Browse the repository at this point in the history
* Refactor lawyer registry

* Simplify code

* Handle errors from API

* Improve type safety

* Add error handling to get single lawyer

* Refactor

---------

Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
  • Loading branch information
oddsson and kodiakhq[bot] authored Jan 17, 2025
1 parent e2c586b commit 4a71289
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 103 deletions.
6 changes: 6 additions & 0 deletions apps/judicial-system/web/messages/Core/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,12 @@ export const errors = defineMessages({
description:
'Notaður sem villuskilaboð þegar ekki gengur að sækja lögmanna skrá',
},
fetchLawyer: {
id: 'judicial.system.core:errors.fetch_lawyer',
defaultMessage: 'Upp kom villa við að sækja lögmann úr lögmanna skrá',
description:
'Notaður sem villuskilaboð þegar ekki gengur að sækja lögmann úr lögmanna skrá',
},
copyLink: {
id: 'judicial.system.core:errors.copy_link',
defaultMessage: 'Ekki tókst að afrita hlekk',
Expand Down
46 changes: 0 additions & 46 deletions apps/judicial-system/web/pages/api/lawyers/getLawyer/getLawyer.ts

This file was deleted.

3 changes: 0 additions & 3 deletions apps/judicial-system/web/pages/api/lawyers/getLawyer/index.ts

This file was deleted.

This file was deleted.

This file was deleted.

52 changes: 37 additions & 15 deletions apps/judicial-system/web/src/utils/hooks/useLawyers/useLawyers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,25 @@ import { toast } from '@island.is/island-ui/core'
import { type Lawyer } from '@island.is/judicial-system/types'
import { errors as errorMessages } from '@island.is/judicial-system-web/messages'

const LAWYER_REGISTRY_URL = '/api/defender/lawyerRegistry'

export const useGetLawyers = (): Lawyer[] => {
const { formatMessage } = useIntl()
const { data, error } = useSWR<Lawyer[]>(
'/api/lawyers/getLawyers',
(url: string) => fetch(url).then((res) => res.json()),
{
revalidateIfStale: false,
revalidateOnFocus: false,
revalidateOnReconnect: false,
errorRetryCount: 2,
},
)
const fetcher = (url: string): Promise<Lawyer[]> =>
fetch(url).then((res) => {
if (!res.ok) {
throw new Error('Failed to get lawyers from lawyer registry')
}

return res.json()
})

const { data, error } = useSWR<Lawyer[]>(LAWYER_REGISTRY_URL, fetcher, {
revalidateIfStale: false,
revalidateOnFocus: false,
revalidateOnReconnect: false,
errorRetryCount: 2,
})

if (error) {
toast.error(formatMessage(errorMessages.fetchLawyers))
Expand All @@ -30,12 +37,22 @@ export const useGetLawyer = (
nationalId?: string | null,
shouldFetch?: boolean,
): Lawyer | undefined => {
const fetchWithNationalId = (url: string, nationalId: string) =>
fetch(`${url}?nationalId=${nationalId}`).then((res) => res.json())
const { formatMessage } = useIntl()

const fetcher = (url: string): Promise<Lawyer> =>
fetch(url).then((res) => {
if (!res.ok) {
throw new Error(
`Failed to get lawyer with nationalId ${nationalId} from lawyer registry`,
)
}

const { data } = useSWR<Lawyer>(
nationalId && shouldFetch ? [`/api/lawyers/getLawyer`, nationalId] : null,
fetchWithNationalId,
return res.json()
})

const { data, error } = useSWR<Lawyer>(
nationalId && shouldFetch ? `${LAWYER_REGISTRY_URL}/${nationalId}` : null,
fetcher,
{
revalidateIfStale: false,
revalidateOnFocus: false,
Expand All @@ -44,5 +61,10 @@ export const useGetLawyer = (
},
)

if (error) {
toast.error(formatMessage(errorMessages.fetchLawyer))
return undefined
}

return data
}

0 comments on commit 4a71289

Please sign in to comment.