Form Module
The Form module handles all form-related functionality for creating and editing reviews. It manages form submission, data collection, validation, and coordinates with other modules to persist review data.Overview
The Form module is responsible for:- Capturing form submission events
- Collecting review data from form inputs
- Managing loading states during submission
- Coordinating with StarRating module for rating values
- Saving or updating reviews through DataStore
- Error handling and user feedback
src/js/modules/form.js
Public Methods
init()
Initializes the form module by attaching event listeners to the review form.
This method takes no parameters
void
Usage:
- Finds the
reviewFormelement by ID - Attaches a submit event listener that calls
handleSubmit() - Called during application initialization
handleSubmit(e)
Handles the form submission event, collects review data, and saves it to the database.
The form submission event object
Promise<void>
Usage:
- Prevent Default: Prevents the default form submission behavior
- Loading State: Disables submit button and shows loading spinner
- Data Collection: Gathers data from all form inputs
- Reviewer Selection: Only includes data for active reviewers (checked checkboxes)
- Date Handling: Uses current date if visit date is not specified
- Save/Update: Creates new review or updates existing one based on
editId - Close Modal: Closes the add/edit modal on success
- Error Handling: Shows alert on error and logs to console
- Restore State: Re-enables submit button regardless of outcome
Form Data Structure
The form collects the following data structure:Form Fields
Basic Information
Name of the restaurant (input with autocomplete dropdown)
Name of the dish being reviewed
URL to the dish photo
Date of visit in YYYY-MM-DD format. If empty, current date is used.
Reviewer Data
Checkbox to include/exclude Gian’s review. Default: checked
Gian’s rating (1-10), managed by StarRating module
Gian’s review text
Checkbox to include/exclude Yami’s review. Default: checked
Yami’s rating (1-10), managed by StarRating module
Yami’s review text
Edit Mode
The form supports editing existing reviews through a special data attribute:form.dataset.editId is present:
- Form calls
DataStore.updateReview(id, data)instead ofaddReview() - Existing review data is pre-populated in form fields
- Submit button text may change to “Update” instead of “Save”
Dependencies
The Form module depends on:- StarRating: To get rating values (
StarRating.ratings.gian,StarRating.ratings.yami) - DataStore: To save/update reviews (
DataStore.addReview(),DataStore.updateReview()) - Utils: For date formatting (
Utils.formatDate()) - Modal: To close the form modal after submission (
Modal.toggleAddModal())
Loading States
During form submission:Error Handling
The module handles errors gracefully:Validation
Current validation is handled by HTML5 form validation:- Restaurant, dish, and photo fields are marked as
required - Photo field has
type="url"for URL validation - Visit date uses
type="date"for date picker
Usage Example
Related Modules
- StarRating - Manages the rating input
- DataStore - Persists review data
- Modal - Controls form visibility
- Utils - Date formatting utilities