Skip to content

Commit

Permalink
Export utils from LinkToEdit component
Browse files Browse the repository at this point in the history
  • Loading branch information
alexlipovka committed Feb 4, 2025
1 parent bc63c9f commit a80f591
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 11 deletions.
34 changes: 27 additions & 7 deletions src/components/LinkToEdit/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,39 @@ interface LinkToEditProps {
resource: Resource;
provenanceList?: Provenance[];
to?: string;
customPathTo?: (props: {
provenance: Provenance;
customEntityResourceExtract: (provenance: Provenance) => Resource | undefined;
}) => string;
customEntityResourceExtract?: (provenance?: Provenance) => Resource | undefined;
}

export function LinkToEdit(props: LinkToEditProps) {
const { name, resource, to } = props;
const { name, resource, to, customPathTo, customEntityResourceExtract } = props;
const location = useLocation();
const provenance = useLinkToEdit({ resourceId: resource.id });

if (provenance) {
const pathname = location.pathname.split('/').slice(0, 3).join('/');
const resource = getSourceFromProvenance(provenance);
const qrId = resource?.id;
return <Link to={to ? `${to}/${qrId}` : `${pathname}/documents/${qrId}`}>{name}</Link>;
if (!provenance) {
return <>{name}</>;
}

return <>{name}</>;
if (customPathTo) {
return (
<Link
to={customPathTo({
provenance,
customEntityResourceExtract: customEntityResourceExtract
? customEntityResourceExtract
: getSourceFromProvenance,
})}
>
{name}
</Link>
);
}

const pathname = location.pathname.split('/').slice(0, 2).join('/');
const entityResource = getSourceFromProvenance(provenance);
const qrId = entityResource?.id;
return <Link to={to ? `${to}/${qrId}` : `${pathname}/documents/${qrId}`}>{name}</Link>;
}
8 changes: 4 additions & 4 deletions src/components/LinkToEdit/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ import { fromFHIRReference } from 'sdc-qrf';

import { compileAsFirst } from 'src/utils';

const getProvenanceEntityUriByRole = (role: string) =>
export const getProvenanceEntityUriByRole = (role: string) =>
compileAsFirst<Provenance, string>(`Provenance.entity.where(role='${role}').what.uri`);

const getProvenanceEntityReferenceByRole = (role: string) =>
export const getProvenanceEntityReferenceByRole = (role: string) =>
compileAsFirst<Provenance, Reference>(`Provenance.entity.where(role='${role}').what`);

const getProvenanceEntityUriSource = getProvenanceEntityUriByRole('source');
const getProvenanceEntityReferenceSource = getProvenanceEntityReferenceByRole('source');
export const getProvenanceEntityUriSource = getProvenanceEntityUriByRole('source');
export const getProvenanceEntityReferenceSource = getProvenanceEntityReferenceByRole('source');

export function getSourceFromProvenance(provenance?: Provenance): Resource | undefined {
if (!provenance) {
Expand Down

0 comments on commit a80f591

Please sign in to comment.