Skip to content

Commit 02ad65b

Browse files
fix: uploading files from default value
1 parent 63eaacc commit 02ad65b

File tree

2 files changed

+37
-4
lines changed

2 files changed

+37
-4
lines changed

libs/safe/src/lib/components/form/form.component.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import { TranslateService } from '@ngx-translate/core';
3030
import { SafeUnsubscribeComponent } from '../utils/unsubscribe/unsubscribe.component';
3131
import { SafeFormHelpersService } from '../../services/form-helper/form-helper.service';
3232
import { SnackbarService } from '@oort-front/ui';
33+
import { cloneDeep } from 'lodash';
3334

3435
/**
3536
* This component is used to display forms
@@ -223,9 +224,19 @@ export class SafeFormComponent
223224
* Handles the value change event when the user completes the survey
224225
*/
225226
public valueChange(): void {
227+
// Cache the survey data, but remove the files from it
228+
// to avoid hitting the local storage limit
229+
const data = cloneDeep(this.survey.data);
230+
Object.keys(data).forEach((key) => {
231+
const question = this.survey.getQuestionByName(key);
232+
if (question && question.getType() === 'file') {
233+
delete data[key];
234+
}
235+
});
236+
226237
localStorage.setItem(
227238
this.storageId,
228-
JSON.stringify({ data: this.survey.data, date: new Date() })
239+
JSON.stringify({ data, date: new Date() })
229240
);
230241
}
231242

libs/safe/src/lib/services/form-helper/form-helper.service.ts

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
import { DialogRef } from '@angular/cdk/dialog';
1414
import { SnackbarService } from '@oort-front/ui';
1515
import localForage from 'localforage';
16+
import { cloneDeep } from 'lodash';
1617

1718
/**
1819
* Shared survey helper service.
@@ -94,7 +95,7 @@ export class SafeFormHelpersService {
9495
* @param formId Form where to upload the files
9596
*/
9697
async uploadFiles(
97-
survey: any,
98+
survey: Survey.SurveyModel,
9899
temporaryFilesStorage: any,
99100
formId: string | undefined
100101
): Promise<void> {
@@ -119,11 +120,32 @@ export class SafeFormHelpersService {
119120
this.snackBar.openSnackBar(res.errors[0].message, { error: true });
120121
return;
121122
} else {
122-
data[name][index].content = res.data?.uploadFile;
123+
const uploadedFileID = res.data?.uploadFile;
124+
const fileContent = data[name][index].content;
125+
data[name][index].content = uploadedFileID;
126+
127+
// Check if any other question is using the same file
128+
survey.getAllQuestions().forEach((question) => {
129+
const questionType = question.getType();
130+
if (
131+
questionType !== 'file' ||
132+
// Only change files that are not in the temporary storage
133+
// meaning their values came from the default values
134+
!!temporaryFilesStorage[question.name]
135+
)
136+
return;
137+
138+
const files = data[question.name] ?? [];
139+
files.forEach((file: any) => {
140+
if (file && file.content === fileContent) {
141+
file.content = uploadedFileID;
142+
}
143+
});
144+
});
123145
}
124146
}
125147
}
126-
survey.data = data;
148+
survey.data = cloneDeep(data);
127149
}
128150

129151
/**

0 commit comments

Comments
 (0)