How can I get mapped list to bind to data?

Summary

I’m working on a side-drawer to display an edit form for an object that has nested properties. My manager does not want us to have individual calls to the API to get the nested properties, so this page is designed to get the parent object and display the nested properties to be edited.

We are using the open-source version of React Admin v4.16

The problem I’m having is getting a nested collection of <Insert> objects to render with data and have blank forms, in numerical order of the property Insert.hopper.

The collection in the data layer is 0 to many, but for the display we need 6 Insert edit forms, whether they exist or not.

The object Insert is of type IInsert:

IInsert extends RaRecord {
    id: Identifier,
    inventoryItemId: number,
    hopper: number,
    cameraEnabled: boolean,
    isActive: boolean,
    expiryDate: Date | undefined,
}

I have tried using React-Admin’s <ArrayInput> with the <SimpleFormIterator>, but I have been unsuccessful to get any existing data to bind while adding static blank forms for new <Insert> objects.

Attempt 1

This is a snippet of my code for my first attempt using an <ArrayInput> with the <SimpleFormIterator>. insertsList is an array of IInsert objects with the hopper value defined as 1-6.

I was expecting the <SimpleFormIterator> to automatically bind the data using the source property, but this did not work.

      <VerticalAccordianDetails>
       <ArrayInput source="inserts" label="">
        <SimpleFormIterator
         fullWidth
         disableReordering
         sx={{
          [`& .RaSimpleFormIterator-line`]: { marginTop: '10px' },
         }}>
          {insertsList.map((insert: IInsert) => 
           <Fragment key={insert.hopper}>
            <SelectInput 
             label={`Hopper ${insert.hopper}`}
             name={`hopper-${insert.hopper}`}
             source="inventoryItemId"
             choices={insertsData}
             optionText="sku"
             optionValue="id"
             resettable
            />
            <BooleanInput source="cameraEnabled" label="Camera Enabled" />
            <BooleanInput source="isActive" label="Is Active" defaultValue={true} />
            <DateInput source="expiryDate" label="Expiry Date" />
           </Fragment>
          )}
        </SimpleFormIterator>
       </ArrayInput>
      </VerticalAccordianDetails>

Attempt 2

My second attempt was to write a function to iterate through the list of inserts received from the parent object when the page first loads. This also did not work as expected because the data is still not binding.

Function

  let insertsListIndexes = [0,1,2,3,4,5]
  let insertsList: IInsert[] = [];
  if (documentConfigurationData && documentConfigurationData.inserts) {
    console.log("Entering function to iterate through inserts...")
    console.log(`Inserts list received: ${JSON.stringify(documentConfigurationData.inserts)}`)
    insertsListIndexes.forEach(function(index) {
      let hopper = index + 1

      console.log(`Working on hopper ${hopper}...`)

      let insert = documentConfigurationData.inserts.filter( (insert: any) => insert.hopper === hopper)[0]
      if (insert) {
        console.log(`Found existing insert for hopper ${hopper} = ${JSON.stringify(insert)}`)

        insertsList.push(insert)
      } else {
        console.log(`No existing insert found for hopper ${hopper}. Adding new one...`)
        let newInsert = {
          id: '',
          inventoryItemId: 0,
          hopper: hopper,
          cameraEnabled: false,
          isActive: true,
          expiryDate: undefined
        }

        insertsList.push(newInsert)
        console.log(`New insert added: ${JSON.stringify(newInsert)}`)
      }

      console.log(`Finished iterating through inserts. Final list: ${JSON.stringify(insertsList)}`)
    })
  }

Console Output

Finished iterating through inserts. Final list: 
[{"id":"","inventoryItemId":0,"hopper":1,"cameraEnabled":false,"isActive":true},
{"id":"","inventoryItemId":0,"hopper":2,"cameraEnabled":false,"isActive":true},
{"id":"","inventoryItemId":0,"hopper":3,"cameraEnabled":false,"isActive":true},
{"id":"","inventoryItemId":0,"hopper":4,"cameraEnabled":false,"isActive":true},
{"id":1,"documentConfigurationId":1,"inventoryItemId":2,"hopper":5,"cameraEnabled":false,"isActive":false,"expiryDate":"2024-07-21"},
{"id":"","inventoryItemId":0,"hopper":6,"cameraEnabled":false,"isActive":true}]

Component

      <VerticalAccordianDetails>
       {insertsList.length === 6 && insertsList.map((insert: IInsert) => 
        <Fragment key={insert.hopper}>
         <SelectInput 
          label={`Hopper ${insert.hopper}`}
          name={`hopper-${insert.hopper}`}
          source="inventoryItemId"
          value={insert.inventoryItemId}
          choices={insertsData}
          optionText="sku"
          optionValue="id"
          resettable
         />
         <BooleanInput source={`${insert.cameraEnabled}`} label="Camera Enabled" />
         <BooleanInput source={`${insert.isActive}`} label="Is Active" defaultValue={true} />
         <DateInput source={`${insert.expiryDate}`} label="Expiry Date" />
         <hr />
        </Fragment>
       )}
      </VerticalAccordianDetails>

Additional Concerns

My concern with this approach is that I will be breaking the auto-bind functionality of react-admin and complicate the data further down when it comes to saving. Any advice for this is also welcome.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật