diff --git a/src/components/Extension/PrivateExtensions/actions/IntakeScheduling/IntakeScheduling.tsx b/src/components/Extension/PrivateExtensions/actions/IntakeScheduling/IntakeScheduling.tsx index 82a2ee8..1dcc327 100644 --- a/src/components/Extension/PrivateExtensions/actions/IntakeScheduling/IntakeScheduling.tsx +++ b/src/components/Extension/PrivateExtensions/actions/IntakeScheduling/IntakeScheduling.tsx @@ -28,6 +28,23 @@ export const IntakeScheduling: FC = ({ const [provider, setProvider] = useState(undefined) const [date, setDate] = useState(undefined) const [slot, setSlot] = useState(undefined) + const [providerPreferences, setProviderPreferences] = useState<{ + age?: string + gender?: string | undefined + ethnicity?: string | undefined + clinicalFocus?: string[] | undefined + deliveryMethod?: string | undefined + locationState?: string | undefined + therapeuticModality?: string | undefined + }>({ + age: undefined, + gender: undefined, + ethnicity: undefined, + clinicalFocus: undefined, + deliveryMethod: undefined, + locationState: undefined, + therapeuticModality: undefined, + }) const { activity_id, fields } = activityDetails const { onSubmit } = useIntakeScheduling() @@ -35,7 +52,7 @@ export const IntakeScheduling: FC = ({ const { providerId, patientName, - agePreference, + age, genderPreference, ethnicityPreference, clinicalFocusPreference, @@ -51,12 +68,41 @@ export const IntakeScheduling: FC = ({ // Reset to default mode on unmount resetLayoutMode() } - }, []) + }) + + useEffect(() => { + // Set the initial provider preferences from the mapped fields + setProviderPreferences({ + age: age ? String(age) : undefined, + gender: genderPreference, + ethnicity: ethnicityPreference, + clinicalFocus: clinicalFocusPreference?.split(','), + therapeuticModality: therapeuticModalityPreference, + locationState: locationStatePreference, + }) + }, [ + age, + genderPreference, + ethnicityPreference, + clinicalFocusPreference, + therapeuticModalityPreference, + locationStatePreference, + ]) + + const handleProviderPreferencesChange = useCallback( + (key: string, value: any) => { + setProviderPreferences((prev) => ({ + ...prev, + [key]: value, + })) + }, + [] + ) const fetchProvidersFn = useCallback( () => fetchProviders({ - age: agePreference ? String(agePreference) : undefined, + age: age ? String(age) : undefined, gender: genderPreference, ethnicity: ethnicityPreference, therapeuticModality: therapeuticModalityPreference, @@ -90,7 +136,7 @@ export const IntakeScheduling: FC = ({ }, }), [ - agePreference, + age, genderPreference, ethnicityPreference, clinicalFocusPreference, @@ -144,6 +190,8 @@ export const IntakeScheduling: FC = ({ opts={{ allowSchedulingInThePast: false, }} + providerPreferences={providerPreferences} + onProviderPreferencesChange={handleProviderPreferencesChange} /> ) } diff --git a/src/components/Extension/PrivateExtensions/actions/IntakeScheduling/api.service.ts b/src/components/Extension/PrivateExtensions/actions/IntakeScheduling/api.service.ts index 30c23f8..ee4f1c4 100644 --- a/src/components/Extension/PrivateExtensions/actions/IntakeScheduling/api.service.ts +++ b/src/components/Extension/PrivateExtensions/actions/IntakeScheduling/api.service.ts @@ -8,6 +8,7 @@ import { GetAvailabilitiesResponseSchema, GetProvidersResponseSchema, } from '@awell-health/sol-scheduling' +import { SolApiResponseError } from './helpers/error' import { log } from '../../../../../utils/logging' export const fetchProviders = async ( @@ -25,30 +26,32 @@ export const fetchProviders = async ( }) if (!response.ok) { + const errorMessage = `Failed to fetch providers: ${response.statusText}` log( { - message: `${basicMessage}: failed`, + message: `${basicMessage}: ${errorMessage}`, data: { input, response }, }, 'ERROR', '' ) - throw new Error('Failed to fetch providers') + throw new SolApiResponseError(errorMessage) } const jsonRes = await response.json() const result = GetProvidersResponseSchema.safeParse(jsonRes) if (!result.success) { + const errorMessage = 'SOL API response does not match expected schema' log( { - message: `${basicMessage}: zod parsing error`, + message: `${basicMessage}: ${errorMessage}`, data: { input, response, result }, }, 'ERROR', '' ) - throw new Error('Zod error', result.error) + throw new SolApiResponseError(errorMessage, result.error.issues) } log( { @@ -85,32 +88,32 @@ export const fetchAvailability = async ( ) if (!response.ok) { + const errorMessage = `Failed to fetch availability for provider ${input.providerId[0]}: ${response.statusText}` log( { - message: `${basicMessage}: failed`, + message: `${basicMessage}: ${errorMessage}`, data: { input, response }, }, 'ERROR', '' ) - throw new Error( - `Failed to fetch availability for provider ${input.providerId[0]}` - ) + throw new SolApiResponseError(errorMessage) } const jsonRes = await response.json() const result = GetAvailabilitiesResponseSchema.safeParse(jsonRes) if (!result.success) { + const errorMessage = 'SOL API response does not match expected schema' log( { - message: `${basicMessage}: failed with zod error`, + message: `${basicMessage}: ${errorMessage}`, data: { input, result }, }, 'ERROR', '' ) - throw new Error('Zod error', result.error) + throw new SolApiResponseError(errorMessage, result.error.issues) } log( @@ -144,12 +147,13 @@ export const bookAppointment = async ( body: JSON.stringify(input), }) if (!response.ok) { + const errorMessage = `Failed to book appointment: ${response.statusText}` log( { message: `${basicMessage}: failed`, data: { input, response } }, 'ERROR', - 'Failed to book appointment' + errorMessage ) - throw new Error(`Failed to book appointment`) + throw new SolApiResponseError(errorMessage) } const jsonRes = (await response.json()) as BookAppointmentResponseType diff --git a/src/components/Extension/PrivateExtensions/actions/IntakeScheduling/helpers/error.ts b/src/components/Extension/PrivateExtensions/actions/IntakeScheduling/helpers/error.ts new file mode 100644 index 0000000..21900c9 --- /dev/null +++ b/src/components/Extension/PrivateExtensions/actions/IntakeScheduling/helpers/error.ts @@ -0,0 +1,11 @@ +import { ZodError } from '@awell-health/sol-scheduling' + +export class SolApiResponseError extends Error { + public issues: ZodError['issues'] + + constructor(message: string, issues: ZodError['issues'] = []) { + super(message) + this.name = 'SolApiResponseError' + this.issues = issues + } +} diff --git a/src/components/Extension/PrivateExtensions/actions/IntakeScheduling/types.ts b/src/components/Extension/PrivateExtensions/actions/IntakeScheduling/types.ts index 39aa87e..107bf95 100644 --- a/src/components/Extension/PrivateExtensions/actions/IntakeScheduling/types.ts +++ b/src/components/Extension/PrivateExtensions/actions/IntakeScheduling/types.ts @@ -6,7 +6,7 @@ export { export type ActionFields = { providerId?: string patientName: string - agePreference?: number + age?: string genderPreference?: 'M' | 'F' | 'Non-binary/non-conforming' ethnicityPreference?: | 'Asian'