Skip to content

Commit

Permalink
feat(SchemaConfig): add link to FieldFormatter VisualEditor
Browse files Browse the repository at this point in the history
  • Loading branch information
maxpatiiuk committed Jul 7, 2024
1 parent bfdd53f commit f5b2b65
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
53 changes: 53 additions & 0 deletions specifyweb/frontend/js_src/lib/components/SchemaConfig/Format.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { LoadingContext, ReadOnlyContext } from '../Core/Contexts';
import { getField } from '../DataModel/helpers';
import type { SerializedResource } from '../DataModel/helperTypes';
import type { LiteralField, Relationship } from '../DataModel/specifyField';
import type { SpecifyTable } from '../DataModel/specifyTable';
import { tables } from '../DataModel/tables';
import type { SpLocaleContainerItem } from '../DataModel/types';
import { ResourceLink } from '../Molecules/ResourceLink';
Expand Down Expand Up @@ -62,6 +63,13 @@ export function SchemaConfigFormat({
/>
<FormatterLine
{...lineProps}
extraComponents={
<FieldFormatterEditing
schemaData={schemaData}
table={field.table}
value={item.format}
/>
}
label={schemaText.formatted()}
name="formatted"
value={item.format}
Expand Down Expand Up @@ -314,3 +322,48 @@ function WebLinkEditing({
</>
) : null;
}

function FieldFormatterEditing({
table,
value,
schemaData,
}: {
readonly table: SpecifyTable;
readonly value: string | null;
readonly schemaData: SchemaData;
}): JSX.Element | null {
const index = schemaData.uiFormatters
.filter((formatter) => formatter.tableName === table.name)
.findIndex(({ name }) => name === value);
const resourceId = appResourceIds.UIFormatters;
const navigate = useNavigate();
if (resourceId === undefined) return null;

const baseUrl = `/specify/resources/app-resource/${resourceId}/field-formatters/${table.name}/`;
return (
<>
{typeof index === 'number' && (
<Link.Icon
className={className.dataEntryEdit}
href={`${baseUrl}${index}/`}
icon="pencil"
title={commonText.edit()}
onClick={(event): void => {
event.preventDefault();
navigate(`${baseUrl}${index}/`);
}}
/>
)}
<Link.Icon
className={className.dataEntryAdd}
href={baseUrl}
icon="plus"
title={commonText.add()}
onClick={(event): void => {
event.preventDefault();
navigate(baseUrl);
}}
/>
</>
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ type SimpleFieldFormatter = {
readonly name: string;
readonly isSystem: boolean;
readonly value: string;
readonly tableName: keyof Tables | undefined;
};

export const fetchSchemaData = async (): Promise<RawSchemaData> =>
Expand All @@ -66,6 +67,7 @@ export const fetchSchemaData = async (): Promise<RawSchemaData> =>
name,
isSystem: formatter.isSystem,
value: formatter.defaultValue,
tableName: formatter.table?.name,
}))
.filter(({ value }) => value)
),
Expand Down

0 comments on commit f5b2b65

Please sign in to comment.