Skip to content

Commit

Permalink
Merge pull request #1068 from FAIMS/fix-related-record-init-bug
Browse files Browse the repository at this point in the history
Fix-related-record-init-bug
  • Loading branch information
stevecassidy authored Jun 5, 2024
2 parents aca8524 + 0b421c9 commit be92e94
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 33 deletions.
55 changes: 28 additions & 27 deletions src/gui/components/record/relationships/RelatedInformation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -339,20 +339,18 @@ export async function get_RelatedFields_for_field(
relationLabel[0] === ''
)
relationLabel = relation_type_vocabPair;
const hrid = values['hrid' + form_type] ?? record_id;
const hrid = getHRIDValue(record_id, values);
try {
const {latest_record, revision_id} = await getRecordInformation(
child_record
);

if (latest_record !== null)
child_record['record_label'] =
latest_record.data['hrid' + related_type];
if (
child_record['record_label'] === undefined ||
child_record['record_label'] === ''
)
child_record['record_label'] = child_record['record_id'];
child_record['record_label'] = getHRIDValue(
child_record['record_id'],
latest_record.data
);

if (revision_id !== undefined) {
const child = generate_RecordLink(
child_record,
Expand Down Expand Up @@ -491,14 +489,10 @@ async function get_field_RelatedFields(
fields[index]['value']
);
if (latest_record !== null && revision_id !== undefined) {
child_record['record_label'] =
latest_record.data['hrid' + related_type];

if (
child_record['record_label'] === undefined ||
child_record['record_label'] === ''
)
child_record['record_label'] = child_record['record_id'];
child_record['record_label'] = getHRIDValue(
child_record['record_id'],
latest_record.data
);

const linked_vocab =
child_record['relation_type_vocabPair'] !== null &&
Expand Down Expand Up @@ -603,12 +597,8 @@ export async function addLinkedRecord(
? parent_link['relation_type_vocabPair']
: ['is related to', 'is related to'];
let type = latest_record?.type;
let hrid = parent_link.record_id;
if (
latest_record !== null &&
latest_record.data['hrid' + type] !== undefined
)
hrid = latest_record?.data['hrid' + type] ?? parent_link.record_id;
const hrid = getHRIDValue(parent_link.record_id, latest_record?.data);

if (type !== undefined) type = ui_specification.viewsets[type]['label'];
const {section, section_label} = get_section(
ui_specification,
Expand Down Expand Up @@ -731,10 +721,8 @@ export async function getParentPersistenceData(
)
);

// const data: Array<{[field_name: string]: any}> = [];
const parent_hrid = getHRIDValue(record_id, latest_record?.data);

let parent_hrid = latest_record?.data['hrid' + type] ?? record_id;
if (parent_hrid === ' ') parent_hrid = record_id;
if (
latest_record !== null &&
type !== undefined &&
Expand Down Expand Up @@ -767,6 +755,20 @@ export async function getParentPersistenceData(
return parentRecords;
}

function getHRIDValue(
record_id: string,
values: {[field_name: string]: any} | undefined
) {
if (values === undefined) return record_id;

const possibleHRIDFields = Object.getOwnPropertyNames(values).filter(
(f: string) => f.startsWith('hrid')
);

if (possibleHRIDFields.length === 1) return values[possibleHRIDFields[0]];
else return record_id;
}

export async function getDetailRelatedInformation(
ui_specification: ProjectUIModel,
form_type: string,
Expand All @@ -784,8 +786,7 @@ export async function getDetailRelatedInformation(
values
);

let hrid = values['hrid' + form_type] ?? record_id;
if (hrid === ' ') hrid = record_id;
const hrid = getHRIDValue(record_id, values);

if (record_to_field_links !== null) {
// get field child records
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,23 +109,26 @@ export function DataGridNoLink(props: {
renderCell: (params: GridCellParams) =>
params.value ? <>{props.relation_preferred_label}</> : <></>,
});
// remove any invalid entries in links (due to a bug elsewhere)
const links = props.links.filter(link => link.record_id);
return props.links !== null && props.links.length > 0 ? (
<DataGrid
autoHeight
density={'compact'}
rowCount={5}
pageSizeOptions={[100]} // 100 here to disable an error thrown by MUI
pageSizeOptions={[5, 10, 20]} // 100 here to disable an error thrown by MUI
disableRowSelectionOnClick
componentsProps={{
slotProps={{
filterPanel: {sx: {maxWidth: '96vw'}},
}}
columns={columns}
initialState={{
sorting: {
sortModel: [{field: 'lastUpdatedBy', sort: 'desc'}],
},
pagination: {paginationModel: {pageSize: 5}},
}}
rows={props.links}
rows={links}
getRowId={r => r.record_id}
/>
) : (
Expand Down
2 changes: 1 addition & 1 deletion src/gui/components/record/relationships/record_links.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ export default function RecordLinkComponent(props: RecordLinksComponentProps) {
autoHeight
density={'compact'}
rowCount={5}
pageSizeOptions={[100]} // 100 here to disable an error thrown by MUI
pageSizeOptions={[5, 10, 20]} // 100 here to disable an error thrown by MUI
disableRowSelectionOnClick
columns={
linkKey === 'links_to_record'
Expand Down
6 changes: 4 additions & 2 deletions src/gui/fields/RelatedRecordSelector.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,6 @@ function DisplayChild(props: DisplayChildProps) {
is_values = false;

if (!is_values) return <></>;

if (props.recordsInformation === null) {
if (is_values)
return (
Expand Down Expand Up @@ -406,9 +405,12 @@ export function RelatedRecordSelector(props: FieldProps & Props) {
let newValue = props.form.values[field_name];
if (multiple) {
// edge case: existing value could be a singleton if schema was changed
// but first make sure it's a relation record rather than eg. empty string
if (Array.isArray(newValue))
newValue = [...(newValue ?? []), new_child_record];
else newValue = [newValue, new_child_record];
else if (newValue.record_id !== undefined)
newValue = [newValue, new_child_record];
else newValue = [new_child_record];
} else newValue = new_child_record;
props.form.setFieldValue(props.field.name, newValue);
return new_record_id;
Expand Down

0 comments on commit be92e94

Please sign in to comment.