@open-tender/utils
Version:
A library of utils for use with Open Tender applications that utilize our cloud-based Order API.
36 lines (35 loc) • 1.37 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.useOrderRatingForm = void 0;
const react_1 = require("react");
const useOrderRatingForm = (orderId, orderRating, updateRating, callback) => {
const submitRef = (0, react_1.useRef)(null);
const [rating, setRating] = (0, react_1.useState)((orderRating === null || orderRating === void 0 ? void 0 : orderRating.rating) || 0);
const [comments, setComments] = (0, react_1.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
};
};
exports.useOrderRatingForm = useOrderRatingForm;