Skip to main content
Glama
useCreateResource.ts1.69 kB
// SPDX-FileCopyrightText: Copyright Orangebot, Inc. and Medplum contributors // SPDX-License-Identifier: Apache-2.0 import { showNotification } from '@mantine/notifications'; import { normalizeErrorString, normalizeOperationOutcome } from '@medplum/core'; import type { OperationOutcome, Resource } from '@medplum/fhirtypes'; import { useMedplum } from '@medplum/react'; import { useNavigate } from 'react-router'; /** * React Hook providing helpers to create a FHIR resource. * @param resourceType - The FHIR resource type. * @param setOutcome - Optional callback to set the OperationOutcome. * @returns An object with `defaultValue` to seed a creation view and `handleSubmit` * to create the new resource on submission. */ export function useCreateResource<T extends Resource>( resourceType: string | undefined, setOutcome?: (outcome: OperationOutcome | undefined) => void ): { defaultValue: T; handleSubmit: (newResource: T) => void; } { const medplum = useMedplum(); const navigate = useNavigate(); const defaultValue = { resourceType } as T; const handleSubmit = (newResource: T): void => { if (setOutcome) { setOutcome(undefined); } medplum .createResource(newResource) .then((result) => navigate('/' + result.resourceType + '/' + result.id)) .catch((err) => { if (setOutcome) { setOutcome(normalizeOperationOutcome(err)); } showNotification({ color: 'red', message: normalizeErrorString(err), autoClose: false, styles: { description: { whiteSpace: 'pre-line' } }, }); }); }; return { defaultValue, handleSubmit, }; }

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/medplum/medplum'

If you have feedback or need assistance with the MCP directory API, please join our Discord server