@@ -6,28 +6,23 @@ import { useLiveState } from '../../hooks/useLiveState';
6
6
import { commonText } from '../../localization/common' ;
7
7
import { interactionsText } from '../../localization/interactions' ;
8
8
import type { RA } from '../../utils/types' ;
9
- import { filterArray } from '../../utils/types' ;
9
+ import { defined , filterArray } from '../../utils/types' ;
10
10
import { group , replaceItem } from '../../utils/utils' ;
11
11
import { Button } from '../Atoms/Button' ;
12
12
import { Form , Input , Label } from '../Atoms/Form' ;
13
13
import { Submit } from '../Atoms/Submit' ;
14
14
import { ReadOnlyContext } from '../Core/Contexts' ;
15
15
import { getField , toTable } from '../DataModel/helpers' ;
16
+ import type { AnyInteractionPreparation } from '../DataModel/helperTypes' ;
16
17
import type { SpecifyResource } from '../DataModel/legacyTypes' ;
17
18
import { getResourceApiUrl , getResourceViewUrl } from '../DataModel/resource' ;
18
19
import { serializeResource } from '../DataModel/serializers' ;
19
20
import type { Collection , SpecifyTable } from '../DataModel/specifyTable' ;
20
- import { strictGetTable , tables } from '../DataModel/tables' ;
21
- import type {
22
- Disposal ,
23
- DisposalPreparation ,
24
- Gift ,
25
- GiftPreparation ,
26
- Loan ,
27
- LoanPreparation ,
28
- } from '../DataModel/types' ;
21
+ import { tables } from '../DataModel/tables' ;
22
+ import type { ExchangeOut , ExchangeOutPrep } from '../DataModel/types' ;
29
23
import { Dialog } from '../Molecules/Dialog' ;
30
- import type { PreparationData } from './helpers' ;
24
+ import type { InteractionWithPreps , PreparationData } from './helpers' ;
25
+ import { interactionPrepTables } from './helpers' ;
31
26
import { PrepDialogRow } from './PrepDialogRow' ;
32
27
33
28
export function PrepDialog ( {
@@ -38,10 +33,8 @@ export function PrepDialog({
38
33
} : {
39
34
readonly onClose : ( ) => void ;
40
35
readonly preparations : RA < PreparationData > ;
41
- readonly table : SpecifyTable < Disposal | Gift | Loan > ;
42
- readonly itemCollection ?: Collection <
43
- DisposalPreparation | GiftPreparation | LoanPreparation
44
- > ;
36
+ readonly table : SpecifyTable < InteractionWithPreps > ;
37
+ readonly itemCollection ?: Collection < AnyInteractionPreparation > ;
45
38
} ) : JSX . Element {
46
39
const preparations = React . useMemo ( ( ) => {
47
40
if ( itemCollection === undefined ) return rawPreparations ;
@@ -152,11 +145,16 @@ export function PrepDialog({
152
145
< Form
153
146
id = { id ( 'form' ) }
154
147
onSubmit = { ( ) : void => {
155
- const itemTable = strictGetTable (
156
- `${ table . name } Preparation`
157
- ) as SpecifyTable <
158
- DisposalPreparation | GiftPreparation | LoanPreparation
159
- > ;
148
+ const itemTable = defined (
149
+ table . relationships . find ( ( relationship ) =>
150
+ interactionPrepTables . includes (
151
+ (
152
+ relationship . relatedTable as SpecifyTable < AnyInteractionPreparation >
153
+ ) . name
154
+ )
155
+ ) ?. relatedTable
156
+ ) as SpecifyTable < AnyInteractionPreparation > ;
157
+
160
158
const items = filterArray (
161
159
preparations . map ( ( preparation , index ) => {
162
160
if ( selected [ index ] === 0 ) return undefined ;
@@ -178,20 +176,10 @@ export function PrepDialog({
178
176
handleClose ( ) ;
179
177
} else {
180
178
const interaction = new table . Resource ( ) ;
179
+ setPreparationItems ( interaction , items ) ;
180
+
181
181
const loan = toTable ( interaction , 'Loan' ) ;
182
- loan ?. set (
183
- 'loanPreparations' ,
184
- items as RA < SpecifyResource < LoanPreparation > >
185
- ) ;
186
182
loan ?. set ( 'isClosed' , false ) ;
187
- toTable ( interaction , 'Gift' ) ?. set (
188
- 'giftPreparations' ,
189
- items as RA < SpecifyResource < GiftPreparation > >
190
- ) ;
191
- toTable ( interaction , 'Disposal' ) ?. set (
192
- 'disposalPreparations' ,
193
- items as RA < SpecifyResource < DisposalPreparation > >
194
- ) ;
195
183
navigate ( getResourceViewUrl ( table . name , undefined ) , {
196
184
state : {
197
185
type : 'RecordSet' ,
@@ -238,3 +226,23 @@ export function PrepDialog({
238
226
</ Dialog >
239
227
) ;
240
228
}
229
+
230
+ function setPreparationItems (
231
+ interaction : SpecifyResource < InteractionWithPreps > ,
232
+ items : RA < SpecifyResource < AnyInteractionPreparation > >
233
+ ) : void {
234
+ const preparationRelationship = defined (
235
+ interaction . specifyTable . relationships . find ( ( relationship ) =>
236
+ interactionPrepTables . includes (
237
+ ( relationship . relatedTable as SpecifyTable < AnyInteractionPreparation > )
238
+ . name
239
+ )
240
+ )
241
+ ) ;
242
+
243
+ // Typecast as a single case because the relatiships do not exist in the union type.
244
+ ( interaction as SpecifyResource < ExchangeOut > ) . set (
245
+ preparationRelationship . name as 'exchangeOutPreps' ,
246
+ items as RA < SpecifyResource < ExchangeOutPrep > >
247
+ ) ;
248
+ }
0 commit comments