Files
formbricks-formbricks/packages/lib/surveyState.ts
tyjkerr a35d573884 feat: add single use survey links (#742)
* add uniqueResponseId to link survey

* add uniqueResponseId to survey response

* add singUseId to database and link survey

* add singleUseId to api

* add single use option in survey response options

* add single use to getSurvey

* add getResponseBySingleUseId

* add ZSurveySingleUse schema to survey schema

* add logic to check if link with suid has response

* pass singleUseId as props, revert SWR changes

* generation of single-use url in LinkSurveyModal

* add singleUseId to SingleResponseCard

* update SurveyInactive for invalid link

* add suId to ZResponse schema

* fix typo in SurveyInactive

* update ResponseOptionCard

* add suId to response select

* add default message for SurveyLinkUsed

* update logic to render SurveyLinkUsed

* add comment for suId in prisma schema

* fix types

* refresh server component on save survey

* update logic

* fix build errors

* fix prisma schema

* add db migration

* update wording

* add singleUseId to localstorage

* fix survey link used over thank you

* add suid to people responses

* fix preview and copy link on surveys page.

* update text and icon for link survey modal

* check survey not finished before setting question

* update show surveylink used logic

* add zodtype to prisma

* fix logic to render last question answered/stored

* add better comments

* update default message for single use surveys

* add LinkSingleUseSurveyModal

* add guard before getting response with suid

* fix build error

* add default message for link used page

* add key and group imports

* add suId encryption and validation

* make survey url encryption optional

* fix build errors

* move singleUseId to server side in surveyList

* added validation to getResponseBySingleUseId service

* restored env var names

* import FORMBRICKS_ENCRYPTION_KEY from constants

* check if encryption environment variable is set, add length validation for env variable

---------

Co-authored-by: Ty Kerr <tykerr@Tys-MacBook-Pro.local>
Co-authored-by: Johannes <johannes@formbricks.com>
Co-authored-by: Matthias Nannt <mail@matthiasnannt.com>
Co-authored-by: Dhruwang <dhruwangjariwala18@gmail.com>
2023-10-03 18:26:13 +02:00

82 lines
1.9 KiB
TypeScript

import { TResponseUpdate } from "@formbricks/types/v1/responses";
export class SurveyState {
responseId: string | null = null;
displayId: string | null = null;
surveyId: string;
responseAcc: TResponseUpdate = { finished: false, data: {} };
singleUseId: string | null;
constructor(surveyId: string, singleUseId?: string, responseId?: string) {
this.surveyId = surveyId;
this.singleUseId = singleUseId ?? null;
this.responseId = responseId ?? null;
}
/**
* Set the current survey ID
* @param id - The survey ID
*/
setSurveyId(id: string) {
this.surveyId = id;
this.clear(); // Reset the state when setting a new surveyId
}
/**
* Get a copy of the current state
*/
copy() {
const copyInstance = new SurveyState(
this.surveyId,
this.singleUseId ?? undefined,
this.responseId ?? undefined
);
copyInstance.responseId = this.responseId;
copyInstance.responseAcc = this.responseAcc;
return copyInstance;
}
/**
* Update the response ID after a successful response creation
* @param id - The response ID
*/
updateResponseId(id: string) {
this.responseId = id;
}
/**
* Update the response ID after a successful response creation
* @param id - The response ID
*/
updateDisplayId(id: string) {
this.displayId = id;
}
/**
* Accumulate the responses
* @param responseUpdate - The new response data to add
*/
accumulateResponse(responseUpdate: TResponseUpdate) {
this.responseAcc = {
finished: responseUpdate.finished,
data: { ...this.responseAcc.data, ...responseUpdate.data },
};
}
/**
* Check if the current accumulated response is finished
*/
isResponseFinished() {
return this.responseAcc.finished;
}
/**
* Clear the current state
*/
clear() {
this.responseId = null;
this.responseAcc = { finished: false, data: {} };
}
}
export default SurveyState;