@crazy-web/feedback
Version:
A feedback form handler package that shows a popup based on allowed URLs.
112 lines (111 loc) • 5.2 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.frequencyTypes = exports.localStorageName = exports.SessionStorageName = void 0;
exports.init = init;
exports.publicFeedback = publicFeedback;
const Storage_1 = require("./core/repositories/Storage");
const fetchOrganizationData_1 = require("./core/usecases/fetchOrganizationData");
const getMatchedUrlItem_1 = require("./core/usecases/getMatchedUrlItem");
const feedbackSlider_1 = require("./ui/feedbackSlider");
const popup_1 = require("./ui/popup");
exports.SessionStorageName = "prodio-feedback";
exports.localStorageName = "prodio-feedback-local";
exports.frequencyTypes = {
// oneTime: "One Time",
everySession: "Every Session",
everyTime: "Every Time"
};
/**
* Initializes the popup handler by saving dummy organization data in sessionStorage.
* @param {Object} options
* @param {string} options.organizationId - The ID of the organization.
*/
function init(_a) {
return __awaiter(this, arguments, void 0, function* ({ organizationId, websiteId, properties }) {
var _b;
if (!organizationId) {
console.error("Error: organizationId is required.");
return;
}
if (!websiteId) {
console.error("Error: websiteId is required.");
return;
}
const requiredFields = ["name", "phone_number", "id", "job_title", "email"];
const missingFields = requiredFields.filter(field => !properties[field]);
if (missingFields.length > 0) {
console.error(`Error: Missing the following fields: [${missingFields.join(", ")}]`);
return;
}
const existingData = (0, Storage_1.getFromSessionStorage)(exports.SessionStorageName);
if (((_b = existingData === null || existingData === void 0 ? void 0 : existingData.userData) === null || _b === void 0 ? void 0 : _b.id) !== properties.id) {
// Find the organization data based on the provided ID
const orgData = yield (0, fetchOrganizationData_1.fetchOrganizationData)({ organizationId, websiteId });
// if (!orgData.length) {
// console.error("Error: Organization not found for ID", organizationId);
// return;
// }
// Save data in sessionStorage
(0, Storage_1.saveToSessionStorage)(exports.SessionStorageName, { organizationId, websiteId, userData: properties, orgData, newFeedbackCount: 5 });
}
// Check if the current URL matches allowed URLs
checkAndShowPopup();
});
}
function publicFeedback({ form, config }) {
const orgData = (0, Storage_1.getFromSessionStorage)(exports.SessionStorageName);
if (!orgData) {
console.error("Error: OrganizationId not found");
return;
}
;
const openFeedbackData = {
orgData,
form,
config
};
(0, feedbackSlider_1.openFeedbackSlider)(openFeedbackData);
}
/**
* Checks if the current page URL is allowed and triggers the popup if applicable.
*/
function checkAndShowPopup() {
var _a, _b;
// const currentPath = window.location.pathname; // Get current path like "/login", "/", "/about"
const currentPath = window.location.href; // Get full URL including hostname, e.g., "https://example.com/login"
const orgData = (0, Storage_1.getFromSessionStorage)(exports.SessionStorageName);
if (!orgData)
return;
// Check if any allowed URL matches the current path
// const matchedOrg = (orgData.orgData ?? []).find((org:any) =>
// org.allowed_url?.includes(currentPath)
// );
const result = (_a = orgData.orgData) !== null && _a !== void 0 ? _a : [];
const matchedOrg = (0, getMatchedUrlItem_1.getMatchedUrlItem)(currentPath, result);
if (matchedOrg) {
const { id, metadata } = matchedOrg;
// const { frequency } = metadata
// const data = getFromLocalStorage(localStorageName)
const forms = (_b = orgData === null || orgData === void 0 ? void 0 : orgData.submittedForms) !== null && _b !== void 0 ? _b : [];
// || (forms.includes(id) && frequency === frequencyTypes.everyTime )
if (!forms.includes(id)) {
(0, popup_1.showPopup)(matchedOrg);
}
else {
console.log("Popup already submitted and frequency check not met.");
}
// showPopup(matchedOrg);
}
else {
console.log("No matching allowed URLs found for this page.");
}
}