@geogirafe/lib-geoportal
Version:
GeoGirafe is a flexible application to build online geoportals.
74 lines (73 loc) • 3.2 kB
JavaScript
// SPDX-License-Identifier: Apache-2.0
import GirafeSingleton from '../../base/GirafeSingleton.js';
import TreeViewRootComponent from '../../components/treeview/treeviewroot/component.js';
const HTML_TEMPLATE = '<girafe-feedback-button question="${question}" component-name="${component-name}" margins="${margins}" placement="${placement}"></girafe-feedback-button>';
export var FeedbackResult;
(function (FeedbackResult) {
FeedbackResult["POSITIVE"] = "positive";
FeedbackResult["NEGATIVE"] = "negative";
FeedbackResult["NEUTRAL"] = "neutral";
})(FeedbackResult || (FeedbackResult = {}));
class FeedbackManager extends GirafeSingleton {
get config() {
return this.context.configManager.Config;
}
STORAGE_PATH = 'alreadyGaveFeedbackFor';
gaveFeedbackFor = {};
initializeSingleton() {
super.initializeSingleton();
this.context.componentManager.registerComponentCallback(TreeViewRootComponent, () => {
console.log('FeedbackManager: TreeViewRootComponent registered');
this.initializeFeedbackOnComponents();
});
this.gaveFeedbackFor = this.context.userDataManager.getUserData(this.STORAGE_PATH) || {};
}
resetGivenFeedback() {
this.gaveFeedbackFor = {};
this.context.userDataManager.saveUserData(this.STORAGE_PATH, this.gaveFeedbackFor);
}
async giveFeedback(componentName, feedbackResult) {
const url = this.config.feedback?.url;
if (!url) {
console.error('Feedback URL is not defined in the configuration.');
return;
}
try {
const response = await fetch(url, {
method: this.config.feedback?.method || 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
componentName: componentName,
result: feedbackResult
})
});
if (!response.ok) {
throw new Error(`Response status: ${response.status}`);
}
const result = await response.json();
console.log('Success:', result);
this.gaveFeedbackFor[componentName] = true;
this.context.userDataManager.saveUserData(this.STORAGE_PATH, this.gaveFeedbackFor);
}
catch (error) {
console.error('Error:', error);
}
}
initializeFeedbackOnComponents() {
this.config.feedback?.components?.forEach((component) => {
if (this.gaveFeedbackFor[component.name]) {
return;
}
const componentsByName = this.context.componentManager.getComponentsByName(component.name);
for (const girafeHTMLElement of componentsByName) {
girafeHTMLElement.insertFeedbackTemplate(HTML_TEMPLATE.replace('${component-name}', component.name)
.replace('${question}', component.question)
.replace('${placement}', component.placement)
.replace('${margins}', component.margins ?? ''));
}
});
}
}
export default FeedbackManager;