mirror of
https://github.com/formbricks/formbricks.git
synced 2026-04-22 11:29:22 -05:00
fix sdk bug on initialization
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@formbricks/js",
|
||||
"version": "0.1.11",
|
||||
"version": "0.1.12",
|
||||
"description": "Formbricks-js allows you to connect your app to Formbricks, display surveys and trigger events.",
|
||||
"keywords": [
|
||||
"Formbricks",
|
||||
|
||||
@@ -28,7 +28,6 @@ export default function SurveyView({ config, survey, close, brandColor }: Survey
|
||||
const displayId = await createDisplay({ surveyId: survey.id, personId: config.person.id }, config);
|
||||
setDisplayId(displayId.id);
|
||||
}
|
||||
console.log(survey);
|
||||
}, [config, survey]);
|
||||
|
||||
useEffect(() => {
|
||||
|
||||
@@ -29,7 +29,7 @@ export class Config {
|
||||
|
||||
private loadFromLocalStorage(): JsConfig {
|
||||
if (typeof window !== "undefined") {
|
||||
const savedConfig = localStorage.getItem("config");
|
||||
const savedConfig = localStorage.getItem("formbricksConfig");
|
||||
if (savedConfig) {
|
||||
return JSON.parse(savedConfig);
|
||||
}
|
||||
@@ -41,6 +41,6 @@ export class Config {
|
||||
}
|
||||
|
||||
private saveToLocalStorage(): void {
|
||||
localStorage.setItem("config", JSON.stringify(this.config));
|
||||
localStorage.setItem("formbricksConfig", JSON.stringify(this.config));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -36,21 +36,30 @@ export const initialize = async (c: InitConfig): Promise<void> => {
|
||||
}
|
||||
addWidgetContainer();
|
||||
addStylesToDom();
|
||||
if (!config || config.get().environmentId !== c.environmentId || config.get().apiHost !== c.apiHost) {
|
||||
if (
|
||||
config.get().session &&
|
||||
config.get().environmentId === c.environmentId &&
|
||||
config.get().apiHost === c.apiHost
|
||||
) {
|
||||
logger.debug("Found existing configuration. Checking session.");
|
||||
const existingSession = config.get().session;
|
||||
if (isExpired(existingSession)) {
|
||||
logger.debug("Session expired. Creating new session.");
|
||||
const { session, settings } = await createSession();
|
||||
config.update({ session: extendSession(session), settings });
|
||||
trackEvent("New Session");
|
||||
} else {
|
||||
logger.debug("Session valid. Extending session.");
|
||||
config.update({ session: extendSession(existingSession) });
|
||||
}
|
||||
} else {
|
||||
logger.debug("No valid session found. Creating new config.");
|
||||
// we need new config
|
||||
config.update({ environmentId: c.environmentId, apiHost: c.apiHost });
|
||||
// get person, session and settings from server
|
||||
const { person, session, settings } = await createPerson();
|
||||
config.update({ person, session: extendSession(session), settings });
|
||||
trackEvent("New Session");
|
||||
} else if (config.get().session && isExpired(config.get().session)) {
|
||||
// we need new session
|
||||
const { session, settings } = await createSession();
|
||||
config.update({ session: extendSession(session), settings });
|
||||
trackEvent("New Session");
|
||||
} else if (!config.get().session) {
|
||||
logger.error("Unable to initialize. No session found");
|
||||
return;
|
||||
}
|
||||
addSessionEventListeners();
|
||||
addPageUrlEventListeners();
|
||||
|
||||
@@ -29,13 +29,13 @@ export const createSession = async (): Promise<{ session: Session; settings: Set
|
||||
};
|
||||
|
||||
export const extendSession = (session: Session): Session => {
|
||||
logger.debug("Session expired. Creating new session.");
|
||||
const updatedSession = { ...session };
|
||||
updatedSession.expiresAt = Date.now() + 1000 * 60 * 60; // extend session for 60 minutes
|
||||
return updatedSession;
|
||||
};
|
||||
|
||||
export const isExpired = (session: Session): boolean => {
|
||||
if (!session) return true;
|
||||
return session.expiresAt <= Date.now();
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user