-
Notifications
You must be signed in to change notification settings - Fork 15
Description
Feature
Currently the code for submitting the Harvest form is contained in the App.vue file. All other FarmData2 forms factor the code for doing a submission out into a separate Javascript file (lib.js) the Harvest form should be refactored to work similarly. The code in the lib.js file will use the farmosUtil.runTransaction function to create the quantities and logs in farmOS, and clean up if errors occur.
Rationale
Factoring the code for the submit out of the Harvest form App.vue file will make the behavior of the Harvest form consistent with other FarmData2 forms. In addition, this structure allows the data collection by the form and the submission of that data to farmOS to be tested independently.
The use of the farmosUtil.runTransaction function simplifies the process of handling errors (network, server, etc) that may occur during submission. For example, imagine that a quantity has been created, but then an error occurs during the creation of the associated harvest log. Without proper error handling, the quantity would exist in farmOS but the log that was to refer to it would not exist. Using the farmosUtil.runTransaction function provides a straight forward way to attempt to clean up situations like this when errors occur during the submit process.
In addition:
- Unit tests should be written for the
lib.jsfile to ensure that the correct records have been created in farmOS. - An E2E test should be create for the Harvest form that confirms that the correct data is being passed to the
lib.jsfor submission.
Alternatives
None. All FarmData2 forms use a lib.js and the runTransaction function to perform and test the submission.
Additional context
The farmosUtil.runTransaction function is documented in the FarmData2 documentation.
- Start the documentation server.
- Navigate to the "FarmData2 Component and Library Documentation" and the
farmosUtillibrary. - Find the "transaction" section, read the documentation and study the example code that uses the
runTransactionfunction.
Use the Tray Seeding form as an example of how to perform the submit using the runTransaction function. The TraySeeding form can be found in modules/farm_fd2/src/entrypoints/tray_seeding. Some important points:
- The definition of the form is in the
App.vuefile.- This file imports the
lib.jsfile that contains the code for doing the submission. - The
submitmethod in this file contains code that passes the form data to thesubmitFormfunction in thelib.jsfile. Thesubmitmethod also handles the display of the progress bar and/or any error messages that come back from thesubmitFormfunction. - The Vue
datacontains aformattribute that collects all of the attributes that are needed to make the submission.- The Harvest form has all of the necessary fields, but they are not currently collected into a
formattribute. - Changing the Harvest form to collect all of the relevant form data into a
formattribute, without making any other changes, and confirming that the form still works correctly would be a good first step.
- The Harvest form has all of the necessary fields, but they are not currently collected into a
- This file imports the
- The
lib.jsfile contains thesubmitFormfunction that does the submission using therunTransactionfunction.- It creates a transaction object with
doandundofunctions for each of the quantities and logs required, collects them into an array and passes that array to therunTransactionfunction.
- It creates a transaction object with
The Tray Seeding form also contains code that will serve as good examples for the the unit tests and the E2E test that are to be created.
- The
lib.submit.unit.cy.jsfile contains the unit tests that check that the correct quantities and attributes are created in farmOS.- The unit tests for the Tray Seeding
lib.jsare run with the command:test.bash --unit --fd2 --glob="modules/farm_fd2/**/tray_seeding/lib.*.unit.cy.js" - To run the test for
lib.jsin the Harvest form:test.bash --unit --fd2 --glob="modules/farm_fd2/**/harvest/lib.*.unit.cy.js"
- The unit tests for the Tray Seeding
- The
trayseeding.submission.e2e.cy.jsfile contains the test that checks that the data passed tosubmitFormis correct.- Note: The code below appears in the
createdlifecycle hook in the Tray SeedingApp.vueand will need to be added to thecreatedlifecycle hook in the Harvest formApp.vuebefore you write the E2E test.if (window.Cypress) { document.defaultView.lib = lib; }
- Note: The code below appears in the