Skip to content

Commit

Permalink
Implement notification about 64k post limit when uploading to surveyj… (
Browse files Browse the repository at this point in the history
#8155)

* Implement notification about 64k post limit when uploading to surveyjs.io fix #8131

* Fix the tests #8131

* Fix unit test #8131

* Add localization strings #8131
  • Loading branch information
andrewtelnov committed Apr 22, 2024
1 parent c754243 commit a03860c
Show file tree
Hide file tree
Showing 53 changed files with 346 additions and 118 deletions.
91 changes: 43 additions & 48 deletions src/dxSurveyService.ts
@@ -1,23 +1,25 @@
import { settings } from "./settings";
import { surveyLocalization } from "./surveyStrings";

const surveyIOSite = "surveyjs.io";
const surveyIOMaxPostSize = 65536;
/**
* The class contains methods to work with api.surveyjs.io service.
*/
export class dxSurveyService {
public locale: string;
public static get serviceUrl(): string {
return settings.web.surveyServiceUrl;
}
public static set serviceUrl(val: string) {
settings.web.surveyServiceUrl = val;
}
constructor() {}
public loadSurvey(
surveyId: string,
onLoad: (success: boolean, result: string, response: any) => void
) {
public loadSurvey(surveyId: string,
onLoad: (success: boolean, result: string, response: any) => void): void {
var xhr = new XMLHttpRequest();
xhr.open(
"GET",
dxSurveyService.serviceUrl + "/getSurvey?surveyId=" + surveyId
this.serviceUrl + "/getSurvey?surveyId=" + surveyId
);
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhr.onload = function () {
Expand All @@ -26,20 +28,12 @@ export class dxSurveyService {
};
xhr.send();
}
public getSurveyJsonAndIsCompleted(
surveyId: string,
clientId: string,
onLoad: (
success: boolean,
surveyJson: any,
result: string,
response: any
) => void
) {
public getSurveyJsonAndIsCompleted(surveyId: string, clientId: string,
onLoad: (success: boolean, surveyJson: any, result: string, response: any) => void): void {
var xhr = new XMLHttpRequest();
xhr.open(
"GET",
dxSurveyService.serviceUrl +
this.serviceUrl +
"/getSurveyAndIsCompleted?surveyId=" +
surveyId +
"&clientId=" +
Expand All @@ -54,56 +48,57 @@ export class dxSurveyService {
};
xhr.send();
}
public sendResult(
postId: string,
result: JSON,
public canSendResult(result: JSON): boolean {
if(!this.isSurveJSIOService) return true;
const str = JSON.stringify(result);
return str.length < surveyIOMaxPostSize;
}
public get isSurveJSIOService(): boolean {
return this.serviceUrl.indexOf(surveyIOSite) >= 0;
}
public sendResult(postId: string, result: JSON,
onSendResult: (success: boolean, response: any, request?: any) => void,
clientId: string = null, isPartialCompleted: boolean = false): void {
if(!this.canSendResult(result)) {
onSendResult(false, surveyLocalization.getString("savingExceedSize", this.locale), undefined);
} else {
this.sendResultCore(postId, result, onSendResult, clientId, isPartialCompleted);
}
}
protected sendResultCore(postId: string, result: JSON,
onSendResult: (success: boolean, response: any, request?: any) => void,
clientId: string = null,
isPartialCompleted: boolean = false
) {
clientId: string = null, isPartialCompleted: boolean = false): void {
var xhr = new XMLHttpRequest();
xhr.open("POST", dxSurveyService.serviceUrl + "/post/");
xhr.open("POST", this.serviceUrl + "/post/");
xhr.setRequestHeader("Content-Type", "application/json; charset=utf-8");
var data = { postId: postId, surveyResult: JSON.stringify(result) };
if (clientId) (<any>data)["clientId"] = clientId;
if (isPartialCompleted) (<any>data)["isPartialCompleted"] = true;
var dataStringify: string = JSON.stringify(data);
var self = this;
xhr.onload = xhr.onerror = function () {
if (!onSendResult) return;
onSendResult(xhr.status === 200, xhr.response, xhr);
};
xhr.send(dataStringify);
}
public sendFile(
postId: string,
file: File,
onSendFile: (success: boolean, response: any) => void
) {
public sendFile(postId: string, file: File,
onSendFile: (success: boolean, response: any) => void): void {
var xhr = new XMLHttpRequest();
xhr.onload = xhr.onerror = function () {
if (!onSendFile) return;
onSendFile(xhr.status == 200, JSON.parse(xhr.response));
};
xhr.open("POST", dxSurveyService.serviceUrl + "/upload/", true);
xhr.open("POST", this.serviceUrl + "/upload/", true);
var formData = new FormData();
formData.append("file", file);
formData.append("postId", postId);
xhr.send(formData);
}
public getResult(
resultId: string,
name: string,
onGetResult: (
success: boolean,
data: any,
dataList: Array<any>,
response: any
) => void
) {
public getResult(resultId: string, name: string,
onGetResult: (success: boolean, data: any, dataList: Array<any>, response: any) => void): void {
var xhr = new XMLHttpRequest();
var data = "resultId=" + resultId + "&name=" + name;
xhr.open("GET", dxSurveyService.serviceUrl + "/getResult?" + data);
xhr.open("GET", this.serviceUrl + "/getResult?" + data);
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
var self = this;
xhr.onload = function () {
Expand All @@ -121,14 +116,11 @@ export class dxSurveyService {
};
xhr.send();
}
public isCompleted(
resultId: string,
clientId: string,
onIsCompleted: (success: boolean, result: string, response: any) => void
) {
public isCompleted(resultId: string, clientId: string,
onIsCompleted: (success: boolean, result: string, response: any) => void): void {
var xhr = new XMLHttpRequest();
var data = "resultId=" + resultId + "&clientId=" + clientId;
xhr.open("GET", dxSurveyService.serviceUrl + "/isCompleted?" + data);
xhr.open("GET", this.serviceUrl + "/isCompleted?" + data);
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
var self = this;
xhr.onload = function () {
Expand All @@ -140,4 +132,7 @@ export class dxSurveyService {
};
xhr.send();
}
private get serviceUrl(): string {
return dxSurveyService.serviceUrl || "";
}
}
5 changes: 4 additions & 1 deletion src/localization/arabic.ts
Expand Up @@ -69,6 +69,7 @@ export var arabicSurveyStrings = {
savingData: "يتم حفظ النتائج على الخادم ...",
savingDataError: "حدث خطأ ولم نتمكن من حفظ النتائج.",
savingDataSuccess: "تم حفظ النتائج بنجاح!",
savingExceedSize: "ردك يتجاوز 64 كيلوبايت. يرجى تقليل حجم الملف (الملفات) والمحاولة مرة أخرى أو الاتصال بمالك الاستطلاع.",
saveAgainButton: "حاول مجددا",
timerMin: "دقيقة",
timerSec: "ثانية",
Expand All @@ -80,6 +81,7 @@ export var arabicSurveyStrings = {
timerLimitSurvey: "لقد أنفقت {0} من إجمالي {1}.",
clearCaption: "واضح",
signaturePlaceHolder: "وقع هنا",
signaturePlaceHolderReadOnly: "لا يوجد توقيع",
chooseFileCaption: "اختر ملف",
takePhotoCaption: "التقاط صورة",
photoPlaceholder: "انقر فوق الزر أدناه لالتقاط صورة باستخدام الكاميرا.",
Expand Down Expand Up @@ -138,4 +140,5 @@ surveyLocalization.localeNames["ar"] = "العربية";
// ok: "OK" => "موافق"
// cancel: "Cancel" => "إلغاء الأمر"
// refuseItemText: "Refuse to answer" => "رفض الإجابة"
// dontKnowItemText: "Don't know" => "لا أعرف"
// dontKnowItemText: "Don't know" => "لا أعرف"// savingExceedSize: "Your response exceeds 64KB. Please reduce the size of your file(s) and try again or contact a survey owner." => "ردك يتجاوز 64 كيلوبايت. يرجى تقليل حجم الملف (الملفات) والمحاولة مرة أخرى أو الاتصال بمالك الاستطلاع."
// signaturePlaceHolderReadOnly: "No signature" => "لا يوجد توقيع"
5 changes: 4 additions & 1 deletion src/localization/basque.ts
Expand Up @@ -69,6 +69,7 @@ export var basqueSurveyStrings = {
savingData: "Erantzunak zerbitzarian gordetzen ari dira...",
savingDataError: "Erroreren bat gertatu eta erantzunak ez dira zerbitzarian gorde ahal izan.",
savingDataSuccess: "Erantzunak egoki gorde dira!",
savingExceedSize: "Erantzuna 64 KB-tik gorakoa da. Murriztu artxiboaren tamaina, eta berriro saiatu edo jarri harremanetan inkesta baten jabearekin.",
saveAgainButton: "Berriro saiatu.",
timerMin: "min",
timerSec: "seg",
Expand All @@ -80,6 +81,7 @@ export var basqueSurveyStrings = {
timerLimitSurvey: "Zuk orotara {0} gastatu duzu {1}-(e)tik.",
clearCaption: "Hustu",
signaturePlaceHolder: "Sinatu hemen",
signaturePlaceHolderReadOnly: "Sinadurarik gabe",
chooseFileCaption: "Fitxategia hautatu",
takePhotoCaption: "Argazkia hartu",
photoPlaceholder: "Egin klik beheko botoian, kamerarekin argazki bat hartzeko.",
Expand Down Expand Up @@ -131,4 +133,5 @@ surveyLocalization.localeNames["eu"] = "Euskara";
// ok: "OK" => "Ados"
// cancel: "Cancel" => "Ezeztatu"
// refuseItemText: "Refuse to answer" => "Erantzuteari uko egin"
// dontKnowItemText: "Don't know" => "Ez dakit"
// dontKnowItemText: "Don't know" => "Ez dakit"// savingExceedSize: "Your response exceeds 64KB. Please reduce the size of your file(s) and try again or contact a survey owner." => "Erantzuna 64 KB-tik gorakoa da. Murriztu artxiboaren tamaina, eta berriro saiatu edo jarri harremanetan inkesta baten jabearekin."
// signaturePlaceHolderReadOnly: "No signature" => "Sinadurarik gabe"
5 changes: 4 additions & 1 deletion src/localization/bulgarian.ts
Expand Up @@ -69,6 +69,7 @@ export var bulgarianStrings = {
savingData: "Резултатите се запазват на сървъра...",
savingDataError: "Поради възникнала грешка резултатите не можаха да бъдат запазени.",
savingDataSuccess: "Резултатите бяха запазени успешно!",
savingExceedSize: "Вашият отговор надхвърля 64KB. Намалете размера на вашите файлове и опитайте отново или се свържете със собственика на проучването.",
saveAgainButton: "Нов опит",
timerMin: "мин",
timerSec: "сек",
Expand All @@ -80,6 +81,7 @@ export var bulgarianStrings = {
timerLimitSurvey: "Вие използвахте общо {0} от {1}.",
clearCaption: "Начално състояние",
signaturePlaceHolder: "Подпишете тук",
signaturePlaceHolderReadOnly: "Няма подпис",
chooseFileCaption: "Изберете файл",
takePhotoCaption: "Направете снимка",
photoPlaceholder: "Кликнете върху бутона по-долу, за да направите снимка с помощта на камерата.",
Expand Down Expand Up @@ -138,4 +140,5 @@ surveyLocalization.localeNames["bg"] = "български";
// ok: "OK" => "Добре"
// cancel: "Cancel" => "Отмени"
// refuseItemText: "Refuse to answer" => "Отказва да отговори"
// dontKnowItemText: "Don't know" => "Не знам"
// dontKnowItemText: "Don't know" => "Не знам"// savingExceedSize: "Your response exceeds 64KB. Please reduce the size of your file(s) and try again or contact a survey owner." => "Вашият отговор надхвърля 64KB. Намалете размера на вашите файлове и опитайте отново или се свържете със собственика на проучването."
// signaturePlaceHolderReadOnly: "No signature" => "Няма подпис"
5 changes: 4 additions & 1 deletion src/localization/catalan.ts
Expand Up @@ -69,6 +69,7 @@ export var catalanSurveyStrings = {
savingData: "Els resultats s'estan guardant al servidor...",
savingDataError: "S'ha produït un error i no hem pogut guardar els resultats.",
savingDataSuccess: "Els resultats es van salvar amb èxit!",
savingExceedSize: "La teva resposta supera els 64KB. Reduïu la mida dels fitxers i torneu-ho a provar o poseu-vos en contacte amb el propietari de l'enquesta.",
saveAgainButton: "Prova una altra vegada",
timerMin: "min",
timerSec: "Seg",
Expand All @@ -80,6 +81,7 @@ export var catalanSurveyStrings = {
timerLimitSurvey: "Has gastat {0} d'{1} en total.",
clearCaption: "Clar",
signaturePlaceHolder: "Inscriu-te aquí",
signaturePlaceHolderReadOnly: "Sense signatura",
chooseFileCaption: "Tria un fitxer",
takePhotoCaption: "Fer foto",
photoPlaceholder: "Feu clic al botó següent per fer una foto amb la càmera.",
Expand Down Expand Up @@ -178,4 +180,5 @@ surveyLocalization.localeNames["ca"] = "català";
// ok: "OK" => "D'ACORD"
// cancel: "Cancel" => "Cancel·lar"
// refuseItemText: "Refuse to answer" => "Negar-se a respondre"
// dontKnowItemText: "Don't know" => "No sé"
// dontKnowItemText: "Don't know" => "No sé"// savingExceedSize: "Your response exceeds 64KB. Please reduce the size of your file(s) and try again or contact a survey owner." => "La teva resposta supera els 64KB. Reduïu la mida dels fitxers i torneu-ho a provar o poseu-vos en contacte amb el propietari de l'enquesta."
// signaturePlaceHolderReadOnly: "No signature" => "Sense signatura"
5 changes: 4 additions & 1 deletion src/localization/croatian.ts
Expand Up @@ -69,6 +69,7 @@ export var croatianStrings = {
savingData: "Rezultati se spremaju na poslužitelju...",
savingDataError: "Došlo je do pogreške i nismo mogli spremiti rezultate.",
savingDataSuccess: "Rezultati su uspješno spremljeni!",
savingExceedSize: "Vaš odgovor premašuje 64KB. Smanjite veličinu datoteka i pokušajte ponovno ili se obratite vlasniku upitnika.",
saveAgainButton: "Pokušaj ponovo",
timerMin: "min",
timerSec: "sec",
Expand All @@ -80,6 +81,7 @@ export var croatianStrings = {
timerLimitSurvey: "Ukupno ste potrošili {0} od {1}.",
clearCaption: "Očistiti",
signaturePlaceHolder: "Potpiši ovdje",
signaturePlaceHolderReadOnly: "Bez potpisa",
chooseFileCaption: "Odaberite datoteku",
takePhotoCaption: "Snimi fotografiju",
photoPlaceholder: "Kliknite donji gumb da biste snimili fotografiju pomoću kamere.",
Expand Down Expand Up @@ -136,4 +138,5 @@ surveyLocalization.localeNames["hr"] = "hrvatski";
// ok: "OK" => "OK"
// cancel: "Cancel" => "Otkazati"
// refuseItemText: "Refuse to answer" => "Odbijte odgovoriti"
// dontKnowItemText: "Don't know" => "Ne znam"
// dontKnowItemText: "Don't know" => "Ne znam"// savingExceedSize: "Your response exceeds 64KB. Please reduce the size of your file(s) and try again or contact a survey owner." => "Vaš odgovor premašuje 64KB. Smanjite veličinu datoteka i pokušajte ponovno ili se obratite vlasniku upitnika."
// signaturePlaceHolderReadOnly: "No signature" => "Bez potpisa"
5 changes: 4 additions & 1 deletion src/localization/czech.ts
Expand Up @@ -69,6 +69,7 @@ export var czechSurveyStrings = {
savingData: "Výsledky se ukládají na server...",
savingDataError: "Došlo k chybě a výsledky jsme nemohli uložit.",
savingDataSuccess: "Výsledky byly úspěšně uloženy!",
savingExceedSize: "Vaše odpověď překračuje 64 kB. Zmenšete prosím velikost svých souborů a zkuste to znovu nebo kontaktujte vlastníka průzkumu.",
saveAgainButton: "Zkuste to znovu",
timerMin: "min",
timerSec: "sek",
Expand All @@ -80,6 +81,7 @@ export var czechSurveyStrings = {
timerLimitSurvey: "Celkově jste strávil/a {0} z {1}.",
clearCaption: "Vymazat",
signaturePlaceHolder: "Podepište se zde",
signaturePlaceHolderReadOnly: "Bez podpisu",
chooseFileCaption: "Vyberte soubor",
takePhotoCaption: "Pořídit fotografii",
photoPlaceholder: "Kliknutím na tlačítko níže pořídíte fotografii pomocí fotoaparátu.",
Expand Down Expand Up @@ -138,4 +140,5 @@ surveyLocalization.localeNames["cs"] = "čeština";
// ok: "OK" => "OK"
// cancel: "Cancel" => "Zrušit"
// refuseItemText: "Refuse to answer" => "Odmítnout odpovědět"
// dontKnowItemText: "Don't know" => "Nevím"
// dontKnowItemText: "Don't know" => "Nevím"// savingExceedSize: "Your response exceeds 64KB. Please reduce the size of your file(s) and try again or contact a survey owner." => "Vaše odpověď překračuje 64 kB. Zmenšete prosím velikost svých souborů a zkuste to znovu nebo kontaktujte vlastníka průzkumu."
// signaturePlaceHolderReadOnly: "No signature" => "Bez podpisu"
5 changes: 4 additions & 1 deletion src/localization/danish.ts
Expand Up @@ -69,6 +69,7 @@ export var danishSurveyStrings = {
savingData: "Resultaterne bliver gemt på serveren...",
savingDataError: "Der opstod en fejl og vi kunne ikke gemme resultatet.",
savingDataSuccess: "Resultatet blev gemt!",
savingExceedSize: "Dit svar overstiger 64 KB. Reducer størrelsen på din(e) fil(er), og prøv igen, eller kontakt en undersøgelsesejer.",
saveAgainButton: "Prøv igen",
timerMin: "min",
timerSec: "sek",
Expand All @@ -80,6 +81,7 @@ export var danishSurveyStrings = {
timerLimitSurvey: "Du har brugt {0} af {1} i alt.",
clearCaption: "Fjern",
signaturePlaceHolder: "Tilmeld dig her",
signaturePlaceHolderReadOnly: "Ingen underskrift",
chooseFileCaption: "Vælg fil",
takePhotoCaption: "Tag billede",
photoPlaceholder: "Klik på knappen nedenfor for at tage et billede med kameraet.",
Expand Down Expand Up @@ -138,4 +140,5 @@ surveyLocalization.localeNames["da"] = "dansk";
// ok: "OK" => "OK"
// cancel: "Cancel" => "Aflyse"
// refuseItemText: "Refuse to answer" => "Nægt at svare"
// dontKnowItemText: "Don't know" => "Ved ikke"
// dontKnowItemText: "Don't know" => "Ved ikke"// savingExceedSize: "Your response exceeds 64KB. Please reduce the size of your file(s) and try again or contact a survey owner." => "Dit svar overstiger 64 KB. Reducer størrelsen på din(e) fil(er), og prøv igen, eller kontakt en undersøgelsesejer."
// signaturePlaceHolderReadOnly: "No signature" => "Ingen underskrift"
5 changes: 4 additions & 1 deletion src/localization/dutch.ts
Expand Up @@ -69,6 +69,7 @@ export var dutchSurveyStrings = {
savingData: "De resultaten worden bewaard op de server...",
savingDataError: "Er was een probleem en we konden de resultaten niet bewaren.",
savingDataSuccess: "De resultaten werden succesvol bewaard!",
savingExceedSize: "Uw antwoord is groter dan 64 kB. Verklein de grootte van uw bestand(en) en probeer het opnieuw of neem contact op met een enquête-eigenaar.",
saveAgainButton: "Probeer opnieuw",
timerMin: "minimum",
timerSec: "sec",
Expand All @@ -80,6 +81,7 @@ export var dutchSurveyStrings = {
timerLimitSurvey: "U heeft {0} van {1} in het totaal.",
clearCaption: "Verwijder",
signaturePlaceHolder: "Hier tekenen",
signaturePlaceHolderReadOnly: "Geen handtekening",
chooseFileCaption: "Gekozen bestand",
takePhotoCaption: "Foto maken",
photoPlaceholder: "Klik op de onderstaande knop om een foto te maken met de camera.",
Expand Down Expand Up @@ -136,4 +138,5 @@ surveyLocalization.localeNames["nl"] = "nederlands";
// ok: "OK" => "OK"
// cancel: "Cancel" => "Annuleren"
// refuseItemText: "Refuse to answer" => "Weiger te antwoorden"
// dontKnowItemText: "Don't know" => "Weet niet"
// dontKnowItemText: "Don't know" => "Weet niet"// savingExceedSize: "Your response exceeds 64KB. Please reduce the size of your file(s) and try again or contact a survey owner." => "Uw antwoord is groter dan 64 kB. Verklein de grootte van uw bestand(en) en probeer het opnieuw of neem contact op met een enquête-eigenaar."
// signaturePlaceHolderReadOnly: "No signature" => "Geen handtekening"
1 change: 1 addition & 0 deletions src/localization/english.ts
Expand Up @@ -74,6 +74,7 @@ export var englishStrings = {
savingData: "The results are being saved on the server...",
savingDataError: "An error occurred and we could not save the results.",
savingDataSuccess: "The results were saved successfully!",
savingExceedSize: "Your response exceeds 64KB. Please reduce the size of your file(s) and try again or contact a survey owner.",
saveAgainButton: "Try again",
timerMin: "min",
timerSec: "sec",
Expand Down

0 comments on commit a03860c

Please sign in to comment.