Skip to content
Middle+5 views

How would you split into files a form that fetches several reference datasets from the backend, combines them, and calculates a calorie allowance?

Practice

AI answer

I would separate responsibilities into layers: data retrieval, combining and calculations, form state, and presentation. For example:

  • api/ — request functions for each reference data source;
  • services/ — loading and combining reference data, error handling, and data transformation;
  • domain/ or utils/ — a pure function for calculating the calorie requirement;
  • components/ — the form and individual fields;
  • hooks/ or store/ — form state and loading status.

Requests to independent reference data sources can be run in parallel with Promise.all. It is best to implement the calorie calculation as a pure function and test it separately. The form should account for loading, error, and empty-data states, as well as user input validation.