@readr-media/react-feedback
Version:
## Installation `yarn install`
116 lines (97 loc) • 4.31 kB
JavaScript
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = useOptions;
var _react = _interopRequireWildcard(require("react"));
var _useUser = _interopRequireDefault(require("./use-user"));
var _api = require("../api");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
/**
* @param {string} formId
* @param {string} fieldId
* @param {string} [identifier]
* @return {import('../../typedef').OptionAmountManager}
*/
function useOptions(formId, fieldId, identifier) {
const [optionSummary, setOptionSummary] = (0, _react.useState)(null);
const originalOptionSummaryRef = (0, _react.useRef)(null);
const {
userId
} = (0, _useUser.default)();
const giveOptions = async (defaultOptions, newOptions) => {
// We use defaultOptions and newOptions to handle options change by following rules:
// Elements in defaultOptions but not in newOptions will make subscription to summary.
// Elements in newOptions but not in defaultOptions will make addition to summary.
// Elements both in or not in don't make any effort.
const originalOptionSummary = originalOptionSummaryRef.current;
const defaultSet = new Set();
const memorySet = new Set();
const filterdOptions = [];
if (newOptions.length > 0) {
const copyOptionSummary = Object.assign({}, originalOptionSummary);
if (defaultOptions.length > 0) {
for (let o of defaultOptions) {
if (defaultSet.has(o) === false) {
copyOptionSummary[o] -= 1;
defaultSet.add(o);
}
}
}
for (let o of newOptions) {
if (o in copyOptionSummary && memorySet.has(o) === false) {
copyOptionSummary[o] += 1;
memorySet.add(o);
filterdOptions.push(o);
}
}
setOptionSummary(copyOptionSummary);
} else {
if (defaultOptions.length > 0) {
const copyOptionSummary = Object.assign({}, originalOptionSummary);
for (let o of defaultOptions) {
copyOptionSummary[o] -= 1;
setOptionSummary(copyOptionSummary);
}
} else {
setOptionSummary(originalOptionSummary);
}
}
if (!userId) return; // send request without error handle
try {
const result = await (0, _api.giveOptions)({
// eslint-disable-line
name: userId,
form: formId,
field: fieldId,
identifier: identifier,
responseTime: new Date(),
userFeedback: filterdOptions
});
} catch (error) {// do nothing for now
}
};
(0, _react.useEffect)(() => {
const getOptionData = async () => {
try {
const result = await (0, _api.getOptionSummary)({
form: formId,
field: fieldId,
identifier: identifier
});
if (result !== null && result !== void 0 && result.data) {
originalOptionSummaryRef.current = result.data;
setOptionSummary(result.data);
}
} catch (error) {// do nothing for now
}
};
getOptionData();
}, []);
return {
optionSummary,
giveOptions
};
}
;