DocSpace-client/packages/asc-web-common/utils/firebase.js

106 lines
2.6 KiB
JavaScript
Raw Normal View History

2021-06-22 10:41:11 +00:00
import firebase from "firebase/app";
import "firebase/remote-config";
2021-08-24 18:52:01 +00:00
import "firebase/storage";
2021-06-22 10:41:11 +00:00
class FirebaseHelper {
remoteConfig = null;
firebaseConfig = null;
2021-08-24 18:52:01 +00:00
firebaseStorage = null;
constructor(settings) {
this.firebaseConfig = settings;
2021-06-22 10:41:11 +00:00
if (!this.isEnabled) return;
2021-07-27 15:04:42 +00:00
if (!firebase.apps.length) {
firebase.initializeApp(this.config);
} else {
firebase.app();
}
2021-06-22 10:41:11 +00:00
2021-08-24 18:52:01 +00:00
this.firebaseStorage = firebase.storage();
2021-06-22 10:41:11 +00:00
this.remoteConfig = firebase.remoteConfig();
this.remoteConfig.settings = {
2022-01-19 13:16:01 +00:00
fetchTimeMillis: 3600000,
minimumFetchIntervalMillis: 3600000,
2021-06-22 10:41:11 +00:00
};
this.remoteConfig.defaultConfig = {
maintenance: null,
};
this.remoteConfig
.ensureInitialized()
.then(() => {
console.log("Firebase Remote Config is initialized");
})
.catch((err) => {
console.error("Firebase Remote Config failed to initialize", err);
});
}
get config() {
return this.firebaseConfig;
2021-06-22 10:41:11 +00:00
}
get isEnabled() {
return (
this.config &&
this.config["apiKey"] &&
this.config["authDomain"] &&
this.config["projectId"] &&
this.config["storageBucket"] &&
this.config["messagingSenderId"] &&
2021-10-21 13:44:29 +00:00
this.config["appId"] /*&&
this.config["measurementId"]*/
2021-06-22 10:41:11 +00:00
);
}
async checkMaintenance() {
if (!this.isEnabled) return Promise.reject("Not enabled");
const res = await this.remoteConfig.fetchAndActivate();
//console.log("fetchAndActivate", res);
const maintenance = this.remoteConfig.getValue("maintenance");
if (!maintenance) {
return Promise.resolve(null);
}
return await Promise.resolve(JSON.parse(maintenance.asString()));
}
async checkCampaigns() {
if (!this.isEnabled) return Promise.reject("Not enabled");
const res = await this.remoteConfig.fetchAndActivate();
2021-07-05 14:59:58 +00:00
const campaignsValue = this.remoteConfig.getValue("campaigns");
const campaignsString = campaignsValue && campaignsValue.asString();
if (!campaignsValue || !campaignsString) {
return Promise.resolve([]);
}
2021-07-05 14:59:58 +00:00
const list = JSON.parse(campaignsString);
if (!list || !(list instanceof Array)) return Promise.resolve([]);
const campaigns = list.filter((element) => {
return typeof element === "string" && element.length > 0;
});
return await Promise.resolve(campaigns);
}
2021-08-24 18:52:01 +00:00
async getCampaignsImages(banner) {
return `https://${this.config["authDomain"]}/images/campaigns.${banner}.png`;
2021-08-24 18:52:01 +00:00
}
async getCampaignsTranslations(banner, lng) {
return `https://${this.config["authDomain"]}/locales/${lng}/CampaignPersonal${banner}.json`;
}
2021-06-22 10:41:11 +00:00
}
export default FirebaseHelper;