Skip to content

Commit

Permalink
Set interval ref to null and clean up (#1796)
Browse files Browse the repository at this point in the history
* Return boolean on duplicate stored

* Use local storage

* Set interval ref to null and clean up
  • Loading branch information
ssylver93 authored Apr 17, 2024
1 parent 420af85 commit 03f0139
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { CommonUtilityService } from '@app/services/common-utility.service';
import { ReportOfFirePage } from '@app/components/report-of-fire/report-of-fire.component';
import { App } from '@capacitor/app';
import { BackgroundTask } from '@capawesome/capacitor-background-task';
import { interval, timer } from 'rxjs';
import { interval } from 'rxjs';
import { ReportOfFireService } from '@app/services/report-of-fire-service';

@Component({
Expand Down Expand Up @@ -92,9 +92,11 @@ export class RoFTitlePage extends RoFPage implements OnInit {
await this.commonUtilityService.checkOnlineStatus().then(async (result) => {
if (result) {
await this.reportOfFireService.syncDataWithServer().then(response => {
if(response) self?.intervalRef?.unsubscribe();
});

if(response) {
self?.intervalRef?.unsubscribe();
self.intervalRef = null;
}
});
};
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export class ReportOfFireService {
private appConfigService: AppConfigService,
private commonUtilityService: CommonUtilityService,
private storageService: LocalStorageService
) {}
) { }

async saveReportOfFire(
reportOfFire: ReportOfFireType,
Expand All @@ -57,7 +57,7 @@ export class ReportOfFireService {
if (this.commonUtilityService.hasSQLKeywords(resource)) {
console.error("JSON blob contains SQL keywords. Potential SQL injection attempt.");
return;
}
}
// if the device's location is not populated use the fire location to set image GPS coordinates
if (reportOfFire?.deviceLocation) {
this.latitude = reportOfFire.deviceLocation[0];
Expand All @@ -72,14 +72,14 @@ export class ReportOfFireService {
formData.append('resource', resource);

if (image1) {
formData.append('image1', await this.convertToBase64(image1));
}
formData.append('image1', await this.convertToBase64(image1));
}
if (image2) {
formData.append('image2', await this.convertToBase64(image2));
}
formData.append('image2', await this.convertToBase64(image2));
}
if (image3) {
formData.append('image3', await this.convertToBase64(image3));
}
formData.append('image3', await this.convertToBase64(image3));
}
this.formData = formData
// if the device is offline save RoF in storage
try {
Expand All @@ -95,8 +95,8 @@ formData.append('image3', await this.convertToBase64(image3));
}

if (this.submittedOffline) {
return;
}
return;
}

let storedOfflineReportData;
try {
Expand Down Expand Up @@ -180,25 +180,25 @@ return;

// Filesystem.readFile returns just the content of the base64 string. Detect mimeType from content
const identifier = content.charAt(0)
switch(identifier) {
case '/':
switch (identifier) {
case '/':
mimeType = 'jpg';
break;
case 'i':
case 'i':
mimeType = 'png';
break;
case 'R':
case 'R':
mimeType = 'gif';
break;
case 'U':
case 'U':
mimeType = 'webp';
break;
default:
default:
mimeType = 'jpg';
break;
}

base64 = 'data:image/' + mimeType + ';base64,' + content;
base64 = 'data:image/' + mimeType + ';base64,' + content;
}

// if the webPath is already a base64 string, return it
Expand Down Expand Up @@ -233,7 +233,7 @@ return;
}

async submitOfflineReportToServer(offlineReport?): Promise<any> {
// retrive the offline RoF from the device's storage and convert to FormData for submission
// retrieve the offline RoF from the device's storage and convert to FormData for submission
// images will already to converted to base64 string from initial submission
const rofUrl = this.appConfigService.getConfig().rest['fire-report-api'];
const rofJson = JSON.parse(offlineReport);
Expand All @@ -244,53 +244,53 @@ return;

const formData = new FormData();
if (resource) {
formData.append('resource', resource);
}
formData.append('resource', resource);
}

if (image1) {
formData.append('image1', image1);
}
formData.append('image1', image1);
}
if (image2) {
formData.append('image2', image2);
}
formData.append('image2', image2);
}
if (image3) {
formData.append('image3', image3);
}
formData.append('image3', image3);
}

try {
// Make an HTTP POST request to your server's API endpoint
const response = await fetch(rofUrl, {
method: 'POST',
body: formData,
});

try {
// Make an HTTP POST request to your server's API endpoint
const response = await fetch(rofUrl, {
method: 'POST',
body: formData,
});

if (response.ok || response.status == 200) {
// Remove the locally stored data if sync is successful
this.storageService.removeData('offlineReportData');
App.removeAllListeners();
// The server successfully processed the report
return { success: true, message: 'Report submitted successfully' };
} else {
// The server encountered an error
return { success: false, message: JSON.stringify(response) };
if (response.ok || response.status == 200) {
// Remove the locally stored data if sync is successful
this.storageService.removeData('offlineReportData');
App.removeAllListeners();
// The server successfully processed the report
return { success: true, message: 'Report submitted successfully' };
} else {
// The server encountered an error
return { success: false, message: JSON.stringify(response) };
}
} catch (error) {
// An error occurred during the HTTP request
return {
success: false,
message: 'An error occurred while submitting the report',
};
}
}
} catch (error) {
// An error occurred during the HTTP request
return {
success: false,
message: 'An error occurred while submitting the report',
};
}
}

async submitToStorage(formData: FormData) {
const object = {};
formData.forEach((value, key) => (object[key] = value));
const json = JSON.stringify(object);
const data = this.storageService.getData('offlineReportData')
if (data == json){
return;
}else this.storageService.saveData('offlineReportData', json);
if (data == json) {
return;
} else this.storageService.saveData('offlineReportData', json);
}

// could not seem to get this to work for non-JPEG, those will be handled in notifications api.
Expand Down Expand Up @@ -330,11 +330,11 @@ try {
// Fetch and submit locally stored data
offlineReport = this.storageService.getData('offlineReportData')
submissionIdList = this.storageService.getData('submissionIDList')

if (offlineReport) {
// Check for duplicate, reject if submissionID has already been stored
// Check for duplicate, reject if submissionID has already been stored
const offlineJson = JSON.parse(offlineReport)
if(offlineJson?.resource) {
if (offlineJson?.resource) {
const resourceJson = JSON.parse(offlineJson.resource)
submissionID = resourceJson?.submissionID
if (submissionID && submissionIdList?.includes(submissionID)) {
Expand All @@ -343,7 +343,7 @@ try {
}

// Reject duplicate if submissionID has already been stored
if(duplicateStored) return true;
if (duplicateStored) return true;

// Send the report to the server
const response =
Expand All @@ -352,9 +352,11 @@ try {
dataSynced = true;
// Remove the locally stored data if sync is successful
this.storageService.removeData('offlineReportData');
const rof = this.storageService.getData('offlineReportData')
console.log('rof: ' + rof)
// store submissionID for duplicate check
if(submissionID) {
submissionIdList = submissionIdList ? submissionIdList + ", " + submissionID : submissionID;
if (submissionID) {
submissionIdList = submissionIdList ? submissionIdList + ", " + submissionID : submissionID;
this.storageService.saveData('submissionIDList', submissionIdList)
}
App.removeAllListeners();
Expand Down

0 comments on commit 03f0139

Please sign in to comment.