mirror of
https://github.com/formbricks/formbricks.git
synced 2025-12-22 06:00:51 -06:00
Compare commits
5 Commits
feat-reset
...
feature/fi
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f7165272f1 | ||
|
|
cd523daa73 | ||
|
|
d646f82a4a | ||
|
|
bacabca29f | ||
|
|
646247024a |
@@ -48,7 +48,7 @@ Initialize the Formbricks JS Client for surveys. When used in a web app, pass a
|
|||||||
<CodeGroup title="Initialize Formbricks">
|
<CodeGroup title="Initialize Formbricks">
|
||||||
|
|
||||||
```javascript
|
```javascript
|
||||||
import formbricks from "@formbricks/js/app";
|
import formbricks from "@formbricks/js";
|
||||||
|
|
||||||
formbricks.init({
|
formbricks.init({
|
||||||
environmentId: "<your-environment-id>", // required
|
environmentId: "<your-environment-id>", // required
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
import { prisma } from "@formbricks/database";
|
import { prisma } from "@formbricks/database";
|
||||||
import { attributeCache } from "@formbricks/lib/attribute/cache";
|
import { attributeCache } from "@formbricks/lib/attribute/cache";
|
||||||
import { getAttributesByUserId } from "@formbricks/lib/attribute/service";
|
|
||||||
import { cache } from "@formbricks/lib/cache";
|
import { cache } from "@formbricks/lib/cache";
|
||||||
import { IS_FORMBRICKS_CLOUD } from "@formbricks/lib/constants";
|
import { IS_FORMBRICKS_CLOUD } from "@formbricks/lib/constants";
|
||||||
import { displayCache } from "@formbricks/lib/display/cache";
|
import { displayCache } from "@formbricks/lib/display/cache";
|
||||||
@@ -71,7 +70,6 @@ export const getPersonState = async ({
|
|||||||
const personResponses = await getResponsesByUserId(environmentId, userId);
|
const personResponses = await getResponsesByUserId(environmentId, userId);
|
||||||
const personDisplays = await getDisplaysByUserId(environmentId, userId);
|
const personDisplays = await getDisplaysByUserId(environmentId, userId);
|
||||||
const segments = await getPersonSegmentIds(environmentId, person, device);
|
const segments = await getPersonSegmentIds(environmentId, person, device);
|
||||||
const attributes = await getAttributesByUserId(environmentId, userId);
|
|
||||||
|
|
||||||
// If the person exists, return the persons's state
|
// If the person exists, return the persons's state
|
||||||
const userState: TJsPersonState["data"] = {
|
const userState: TJsPersonState["data"] = {
|
||||||
@@ -81,7 +79,6 @@ export const getPersonState = async ({
|
|||||||
personDisplays?.map((display) => ({ surveyId: display.surveyId, createdAt: display.createdAt })) ??
|
personDisplays?.map((display) => ({ surveyId: display.surveyId, createdAt: display.createdAt })) ??
|
||||||
[],
|
[],
|
||||||
responses: personResponses?.map((response) => response.surveyId) ?? [],
|
responses: personResponses?.map((response) => response.surveyId) ?? [],
|
||||||
attributes,
|
|
||||||
lastDisplayAt:
|
lastDisplayAt:
|
||||||
personDisplays.length > 0
|
personDisplays.length > 0
|
||||||
? personDisplays.sort((a, b) => b.createdAt.getTime() - a.createdAt.getTime())[0].createdAt
|
? personDisplays.sort((a, b) => b.createdAt.getTime() - a.createdAt.getTime())[0].createdAt
|
||||||
|
|||||||
@@ -55,8 +55,7 @@ export const updateAttribute = async (
|
|||||||
}
|
}
|
||||||
return err({
|
return err({
|
||||||
code: "network_error",
|
code: "network_error",
|
||||||
// @ts-expect-error
|
status: 500,
|
||||||
status: res.error.status ?? 500,
|
|
||||||
message: res.error.message ?? `Error updating person with userId ${userId}`,
|
message: res.error.message ?? `Error updating person with userId ${userId}`,
|
||||||
url: `${config.get().apiHost}/api/v1/client/${environmentId}/people/${userId}/attributes`,
|
url: `${config.get().apiHost}/api/v1/client/${environmentId}/people/${userId}/attributes`,
|
||||||
responseMessage: res.error.message,
|
responseMessage: res.error.message,
|
||||||
@@ -93,7 +92,7 @@ export const updateAttributes = async (
|
|||||||
const updatedAttributes = { ...attributes };
|
const updatedAttributes = { ...attributes };
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const existingAttributes = config.get().personState.data.attributes;
|
const existingAttributes = config.get().attributes;
|
||||||
if (existingAttributes) {
|
if (existingAttributes) {
|
||||||
for (const [key, value] of Object.entries(existingAttributes)) {
|
for (const [key, value] of Object.entries(existingAttributes)) {
|
||||||
if (updatedAttributes[key] === value) {
|
if (updatedAttributes[key] === value) {
|
||||||
@@ -140,7 +139,7 @@ export const updateAttributes = async (
|
|||||||
};
|
};
|
||||||
|
|
||||||
export const isExistingAttribute = (key: string, value: string): boolean => {
|
export const isExistingAttribute = (key: string, value: string): boolean => {
|
||||||
if (config.get().personState.data.attributes[key] === value) {
|
if (config.get().attributes[key] === value) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -239,6 +239,7 @@ export const initialize = async (
|
|||||||
environmentState,
|
environmentState,
|
||||||
personState,
|
personState,
|
||||||
filteredSurveys,
|
filteredSurveys,
|
||||||
|
attributes: configInput.attributes || {},
|
||||||
});
|
});
|
||||||
|
|
||||||
const surveyNames = filteredSurveys.map((s) => s.name);
|
const surveyNames = filteredSurveys.map((s) => s.name);
|
||||||
@@ -280,6 +281,7 @@ export const initialize = async (
|
|||||||
personState,
|
personState,
|
||||||
environmentState,
|
environmentState,
|
||||||
filteredSurveys,
|
filteredSurveys,
|
||||||
|
attributes: configInput.attributes || {},
|
||||||
});
|
});
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
handleErrorOnFirstInit();
|
handleErrorOnFirstInit();
|
||||||
@@ -293,16 +295,10 @@ export const initialize = async (
|
|||||||
if (updatedAttributes && Object.keys(updatedAttributes).length > 0) {
|
if (updatedAttributes && Object.keys(updatedAttributes).length > 0) {
|
||||||
config.update({
|
config.update({
|
||||||
...config.get(),
|
...config.get(),
|
||||||
personState: {
|
|
||||||
...config.get().personState,
|
|
||||||
data: {
|
|
||||||
...config.get().personState.data,
|
|
||||||
attributes: {
|
attributes: {
|
||||||
...config.get().personState.data.attributes,
|
...config.get().attributes,
|
||||||
...updatedAttributes,
|
...updatedAttributes,
|
||||||
},
|
},
|
||||||
},
|
|
||||||
},
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -22,7 +22,7 @@ export const resetPerson = async (): Promise<Result<void, NetworkError>> => {
|
|||||||
environmentId: config.get().environmentId,
|
environmentId: config.get().environmentId,
|
||||||
apiHost: config.get().apiHost,
|
apiHost: config.get().apiHost,
|
||||||
...(userId && { userId }),
|
...(userId && { userId }),
|
||||||
attributes: config.get().personState.data.attributes,
|
attributes: config.get().attributes,
|
||||||
};
|
};
|
||||||
await logoutPerson();
|
await logoutPerson();
|
||||||
try {
|
try {
|
||||||
|
|||||||
@@ -15,7 +15,6 @@ export const DEFAULT_PERSON_STATE_NO_USER_ID: TJsPersonState = {
|
|||||||
segments: [],
|
segments: [],
|
||||||
displays: [],
|
displays: [],
|
||||||
responses: [],
|
responses: [],
|
||||||
attributes: {},
|
|
||||||
lastDisplayAt: null,
|
lastDisplayAt: null,
|
||||||
},
|
},
|
||||||
} as const;
|
} as const;
|
||||||
@@ -68,7 +67,6 @@ export const fetchPersonState = async (
|
|||||||
segments: [],
|
segments: [],
|
||||||
displays: [],
|
displays: [],
|
||||||
responses: [],
|
responses: [],
|
||||||
attributes: {},
|
|
||||||
lastDisplayAt: null,
|
lastDisplayAt: null,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -65,7 +65,7 @@ const renderWidget = async (
|
|||||||
}
|
}
|
||||||
|
|
||||||
const { product } = config.get().environmentState.data ?? {};
|
const { product } = config.get().environmentState.data ?? {};
|
||||||
const { attributes } = config.get().personState.data ?? {};
|
const { attributes } = config.get() ?? {};
|
||||||
|
|
||||||
const isMultiLanguageSurvey = survey.languages.length > 1;
|
const isMultiLanguageSurvey = survey.languages.length > 1;
|
||||||
let languageCode = "default";
|
let languageCode = "default";
|
||||||
|
|||||||
@@ -126,7 +126,6 @@ export const ZJsPersonState = z.object({
|
|||||||
})
|
})
|
||||||
),
|
),
|
||||||
responses: z.array(ZId), // responded survey ids
|
responses: z.array(ZId), // responded survey ids
|
||||||
attributes: ZAttributes,
|
|
||||||
lastDisplayAt: z.date().nullable(),
|
lastDisplayAt: z.date().nullable(),
|
||||||
}),
|
}),
|
||||||
});
|
});
|
||||||
@@ -146,6 +145,7 @@ export const ZJsConfig = z.object({
|
|||||||
environmentState: ZJsEnvironmentState,
|
environmentState: ZJsEnvironmentState,
|
||||||
personState: ZJsPersonState,
|
personState: ZJsPersonState,
|
||||||
filteredSurveys: z.array(ZSurvey).default([]),
|
filteredSurveys: z.array(ZSurvey).default([]),
|
||||||
|
attributes: z.record(z.string()),
|
||||||
status: z.object({
|
status: z.object({
|
||||||
value: z.enum(["success", "error"]),
|
value: z.enum(["success", "error"]),
|
||||||
expiresAt: z.date().nullable(),
|
expiresAt: z.date().nullable(),
|
||||||
|
|||||||
Reference in New Issue
Block a user