@open-tender/utils
Version:
A library of utils for use with Open Tender applications that utilize our cloud-based Order API.
32 lines (31 loc) • 1.18 kB
JavaScript
import { useRef, useState } from 'react';
export const useOrderRatingForm = (orderId, orderRating, updateRating, callback) => {
const submitRef = useRef(null);
const [rating, setRating] = useState((orderRating === null || orderRating === void 0 ? void 0 : orderRating.rating) || 0);
const [comments, setComments] = useState((orderRating === null || orderRating === void 0 ? void 0 : orderRating.comments) || '');
const stars = [1, 2, 3, 4, 5];
const handleRating = (star) => {
setRating(star);
};
const handleComments = (value) => {
setComments(value);
};
const handleSubmit = (evt) => {
var _a, _b;
evt === null || evt === void 0 ? void 0 : evt.preventDefault();
const data = { rating, comments };
updateRating(orderId, data);
if (callback)
callback();
((_a = submitRef.current) === null || _a === void 0 ? void 0 : _a.blur) && ((_b = submitRef.current) === null || _b === void 0 ? void 0 : _b.blur());
};
return {
submitRef,
stars,
rating,
handleRating,
comments,
handleComments,
handleSubmit
};
};