Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(j-s): Use lawyer registry from api #17434

Merged
merged 15 commits into from
Jan 17, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions apps/judicial-system/web/messages/Core/errors.ts
Original file line number Diff line number Diff line change
@@ -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',

This file was deleted.

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
@@ -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))
@@ -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,
@@ -44,5 +61,10 @@ export const useGetLawyer = (
},
)

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

return data
}