Skip to content

Commit ae1187d

Browse files
committed
Fix bug which prevented non-multiple relations.
Signed-off-by: Steve Cassidy <steve.cassidy@mq.edu.au>
1 parent 22c49ff commit ae1187d

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

src/gui/components/record/relationships/create_links/create_record_link.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ export function CreateRecordLink(props: CreateRecordLinkProps) {
289289
>
290290
<Typography variant="caption">
291291
{' '}
292-
To enable Add record or Link, remove link firstly
292+
Remove existing link to enable Add record or Link
293293
</Typography>
294294
</Grid>
295295
)}

src/gui/fields/RelatedRecordSelector.tsx

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,23 +113,37 @@ function get_default_relation_label(
113113
return [];
114114
}
115115

116+
/**
117+
* Remove any records from the array that are already linked to the current record.
118+
* @param multiple - do we allow multiple relations?
119+
* @param value - current value of the field
120+
* @param all_records - array of records that are available to link to
121+
* @returns a reduced array of records for linking
122+
*/
116123
function excludes_related_record(
117124
multiple: boolean,
118125
value: any,
119126
all_records: RecordReference[]
120127
) {
121128
const relations: string[] = multiple ? [] : [value?.record_id];
122129
const records: RecordReference[] = [];
130+
131+
// if we allow multiple links and we have a current value then extract
132+
// the record_ids from the current value and add them to the relations array
123133
if (multiple && value)
124134
value.map((record: RecordReference) =>
125135
record !== null ? relations.push(record.record_id) : record
126136
);
127137

138+
// filter the all_records array to remove any records that are already linked
139+
// if we don't allow multiple values OR we have no current value
140+
// then all records would be included
128141
all_records.map((record: RecordReference) =>
129142
relations.includes(record.record_id) ? record : records.push(record)
130143
);
131144
return records;
132145
}
146+
133147
type DisplayChildProps = {
134148
handleUnlink: Function;
135149
handleReset: Function;
@@ -239,12 +253,18 @@ export function RelatedRecordSelector(props: FieldProps & Props) {
239253
let mounted = true;
240254
(async () => {
241255
if (project_id !== undefined && mounted && props.isconflict !== true) {
256+
// need to enable the field for multiple=false since it defaults to disabled
257+
// enable if there is no existing value or if the existing value doesn't have
258+
// a record id (why would it not have a record ID?)
242259
if (
243260
!multiple &&
244261
props.form.values[field_name] &&
245262
props.form.values[field_name]['record_id'] === undefined
246263
)
247264
setIs_enabled(true);
265+
// or just no existing value
266+
if (!multiple && !props.form.values[field_name]) setIs_enabled(true);
267+
248268
if (!multiple) {
249269
if (
250270
props.form.values[field_name] &&
@@ -607,8 +627,7 @@ export function RelatedRecordSelector(props: FieldProps & Props) {
607627
</Typography>
608628
{is_enabled && (
609629
<Typography variant="caption">
610-
It's a single related, to enable Add record or Link, remove
611-
link firstly
630+
Remove existing link to enable Add record or Link
612631
</Typography>
613632
)}
614633
</Grid>

0 commit comments

Comments
 (0)