Skip to content

Commit

Permalink
fix: handling no network
Browse files Browse the repository at this point in the history
  • Loading branch information
69pmb committed Feb 25, 2024
1 parent c297444 commit 1200737
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 30 deletions.
42 changes: 20 additions & 22 deletions src/app/services/data.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,13 @@ export class DataService<T extends Composition | Fichier> {
private toast: ToastService
) {}

loadsList(table: Table<T>, file: Table<File>, dropboxFile: string): void {
loadsList(
table: Table<T>,
fileTable: Table<File>,
dropboxFile: string
): void {
forkJoin([
from(file.get(1)),
from(fileTable.get(1)),
this.dropboxService.files.obs$.pipe(take(1)),
]).subscribe(([storedName, filesList]) => {
const fileNameToDownload = DataService.findsFileNameToDownload(
Expand All @@ -53,7 +57,7 @@ export class DataService<T extends Composition | Fichier> {
} else if (fileNameToDownload && !storedName?.filename) {
this.downloadsList(
table,
file,
fileTable,
fileNameToDownload,
`Download ${dropboxFile}`
).subscribe();
Expand All @@ -67,7 +71,7 @@ export class DataService<T extends Composition | Fichier> {
) {
this.downloadsList(
table,
file,
fileTable,
fileNameToDownload ?? '',
`Update ${dropboxFile}`
).subscribe();
Expand Down Expand Up @@ -228,30 +232,24 @@ export class DataService<T extends Composition | Fichier> {

private static findsFileNameToDownload(
dropboxFile: string,
filesList: files.ListFolderResult
filesList?: files.ListFolderResult
): string | undefined {
const names = filesList.entries
.map(f => f.name)
.filter(name => DataService.isCorrectFileName(name, dropboxFile));
const count = names.length;
if (count === 0) {
const names =
filesList?.entries
.map(f => f.name)
.filter(name => DataService.isCorrectFileName(name, dropboxFile)) ?? [];
if (names.length === 0) {
return undefined;
} else if (count === 1) {
return names.find(name =>
DataService.isCorrectFileName(name, dropboxFile)
);
} else if (names.length === 1) {
return names[0];
} else {
const dateArray: DateTime[] = [];
names.map(name => {
if (DataService.isCorrectFileName(name, dropboxFile)) {
dateArray.push(DataService.extractDateFromFilename(name));
}
});
const dateArray = names.map(name =>
DataService.extractDateFromFilename(name)
);
const lastDate = dateArray.reduce((d1, d2) => (d1 > d2 ? d1 : d2));
const fileToDownload = names.find(name =>
return names.find(name =>
name.includes(lastDate.toFormat(DataService.dateFormat))
);
return fileToDownload;
}
}

Expand Down
23 changes: 16 additions & 7 deletions src/app/services/dropbox.service.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import {Injectable} from '@angular/core';
import {Dropbox, files} from 'dropbox';
import {Dropbox, DropboxResponseError, files} from 'dropbox';
import {UtilsService} from './utils.service';
import {Dropbox as DropboxConstant} from '@utils/dropbox';
import {Observable, catchError, from, map, switchMap} from 'rxjs';
import {Observable, catchError, from, map, of, switchMap} from 'rxjs';
import {Reactive} from '../utils/reactive';

@Injectable({providedIn: 'root'})
export class DropboxService {
files = new Reactive<files.ListFolderResult>();
files = new Reactive<files.ListFolderResult | undefined>();

constructor(private serviceUtils: UtilsService) {
this.listFiles().subscribe(f => this.files.set(f));
Expand All @@ -23,16 +23,25 @@ export class DropboxService {
return DropboxConstant.DROPBOX_FOLDER + fileName;
}

private listFiles(): Observable<files.ListFolderResult> {
private listFiles(): Observable<files.ListFolderResult | undefined> {
return from(
DropboxService.getDbx().filesListFolder({
path: DropboxConstant.DROPBOX_FOLDER,
})
).pipe(
map(response => response.result),
catchError(err =>
this.serviceUtils.handleError(err, 'Error when listing files')
)
catchError((err: unknown) => {
if (
(err instanceof DropboxResponseError &&
[0, 504].includes(err.status)) ||
err instanceof TypeError
) {
// No internet connection
return of(undefined);
} else {
return this.serviceUtils.handleError(err, 'Error when listing files');
}
})
);
}

Expand Down
2 changes: 1 addition & 1 deletion src/app/services/utils.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export class UtilsService {
}

handleError(error: unknown, message: string): Observable<never> {
console.error('handleError', error);
console.error('An error occured:', error);
this.toast.open(UtilsService.getErrorMessage(error));
return throwError(() => message);
}
Expand Down

0 comments on commit 1200737

Please sign in to comment.