in here create a payload and handle submit. To get shiftDates use processesDays.
To handle form submission effectively and create the appropriate payload using the processDays function, you'll need to make several adjustments to your NewShiftWeeklyModal component. Below are the steps and the updated code to achieve this:
Handle Form Submission Properly:
Attach the onSubmit handler to the <form> element using handleSubmit from react-hook-form.
Ensure the submit button triggers the form submission by setting its type to "submit" and removing any unnecessary onClick handlers.
Create the Payload:
In the onSubmit function, use the processDays function to generate the shiftDates.
Combine the form data with additional necessary fields like companyId and companyLocationId.
Reset the Form and Close Modal on Success:
After a successful mutation, reset the form and close the modal to provide feedback to the user.
Ensure All Required Fields Are Included:
Make sure companyId and companyLocationId are correctly obtained, either from props or another part of your application.
Here's the updated code with these changes implemented:
onSubmit Handler: The <form> element now has an onSubmit={handleSubmit(onSubmit)} attribute, which ensures that the onSubmit function is called with validated data when the form is submitted.
Submit Button: The "Plan Shifts" button is set to type="submit" and linked to the form via the form attribute (form="new-shift-weekly-form"), ensuring that clicking this button submits the form.
Discard Button: The "Discard" button now resets the form and closes the modal when clicked.
Payload Creation:
processDays Function: This function generates the shiftDates based on the selected days and times. It checks if the end time is on the next day relative to the start time.
onSubmit Function: This function creates the payload by combining the selected personnel IDs, processed shift dates, and company identifiers. It then triggers the mutation to add the bulk shifts.
Form State Management:
React Hook Form Integration: The form integrates with react-hook-form to manage form state and validation. Errors are displayed using the ErrorMessage component.
Updating Form Values: When personnel are selected, the users field in the form state is updated using setValue, ensuring that validation works correctly.
Mutation Handling:
Success Handling: Upon successful mutation, the form is reset, and the modal is closed to provide feedback to the user.
Error Handling: Any errors during the mutation are logged to the console. You can enhance this by displaying error messages to the user as needed.
Assumptions:
companyId and companyLocationId: It's assumed that these are passed as props to the NewShiftWeeklyModal component. Ensure that when you use this component, you provide these props accordingly.
Error Messages for Shift Dates: Basic error handling is provided for shift dates. Depending on your application's requirements, you might want to enhance this to provide more specific feedback.
Additional Recommendations:
Validation Enhancements: Depending on your requirements, you might want to add more robust validation, such as ensuring that shiftDates align with the selected days.
User Feedback: Implement user-facing notifications (e.g., success messages, error alerts) to provide immediate feedback beyond just closing the modal.
Performance Optimizations: If processing days or other functions become complex, consider optimizing them or memoizing values to enhance performance.
By implementing these changes, your NewShiftWeeklyModal component should effectively handle form submissions, create the necessary payload using processDays, and integrate seamlessly with your backend via mutations.