Add ability to import items into food list from JSON #528
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
See #388 (comment)
Import happens from the import/export section of settings. It expects JSON with the following format:
This example is pulled from the script I wrote to scrape the UCLA dining hall menus here. The nutrient names and units are those that waistline uses internally, which are found here.
The version field is there to allow for future changes to the expected import format, because ideally there would also be an export feature that exports to a certain format, to allow backing up the food items.
Notice the uniqueId field in each food item. This is highly recommended to be included in these JSON files because it will prevent duplicate items if the same food items are imported more than once. To accomplish this, the script will take this unique id and prepend "custom_" in front of it (so there will be no collisions with real product bar codes), and use that as the item's barcode.
When importing items, you can select a category to be applied to each item. So in this example, to keep these imported dining hall food items separate from my own entries, I created the category "UCLA" and applied that when importing them.
If the same food item(s) are imported twice (based on the uniqueId), they will be overwritten with the data from the latest import. So for example, if you forget to include a category when importing, or some piece of data is incorrect in your JSON, you can import again to fix it.
Screenshots
Testing
Since file import isn't working when running from the browser, I used the following shim to test it:
Limitations
I couldn't figure out a good way to incorporate the category selection into the UI in a clear way, since I am very unfamiliar with the UI framework. I'm not sure of the idiomatic way to style the category selection for this code base, so it's pretty faint and hard to see this aspect of the feature. I tried putting it into modal dialog box upon clicking the import button, but was not successful. Open to ideas and help.
Importing 40 items from the menus works fine and appears manageable. As a strategy for using custom databases, I'm not sure how well it will work with thousands of foods such as from government databases. Hopefully someone will eventually make a pull request to allow importing custom databases and search from them so we don't have to pollute our food list with entries we don't use.