Files
formbricks/packages/lib/surveyState.ts
T
Shubham Palriwala f9fb2c6a99 fix: build error due to survey status becoming optional (#1026)
* fix: build error due to survey status becoming optional

* made refactors

* fix: use universal ID over newly created ID

* remove build step from github actions workflows

---------

Co-authored-by: Dhruwang <dhruwangjariwala18@gmail.com>
Co-authored-by: Matti Nannt <mail@matthiasnannt.com>
2023-10-09 12:44:01 +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;