Skip to content

Commit

Permalink
Minor fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
cbeauchesne committed Mar 25, 2021
1 parent b8da0c5 commit 35677ca
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 34 deletions.
35 changes: 13 additions & 22 deletions src/js/apis/c2c/DocumentService.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,37 +7,28 @@ DocumentService.prototype.getAll = function (params) {
return this.api.get('/' + this.documentType + 's', { params });
};

DocumentService.prototype.fullDownload = function (params, limit, progress) {
DocumentService.prototype.fullDownload = function (params, limit, onProgress) {
// will load the ENTIRE list of document. Limited to 2000 docs
const this_ = this;

limit = limit || 2000;
params = { ...params }; // clone
const MAX_SIZE = 2000;
const API_MAX_LIMIT = 100;

limit = limit || MAX_SIZE;
limit = limit > MAX_SIZE ? MAX_SIZE : limit;

return new Promise((resolve, reject) => {
const result = [];

params.limit = 100;

const download = function (offset) {
params.offset = offset;

this_
.getAll(params)
.then((response) => {
for (const document of response.data.documents) {
const download = (offset = 0) => {
this.getAll({ ...params, offset: offset, limit: API_MAX_LIMIT })
.then(({ data }) => {
for (const document of data.documents) {
result.push(document);
}

if (progress) {
progress(result.length, response.data.total);
}
onProgress?.(result.length, data.total);

if (response.data.documents.length === 0) {
resolve(result);
} else if (result.length === response.data.total) {
resolve(result);
} else if (result.length >= limit) {
if (data.documents.length === 0 || result.length === data.total || result.length >= limit) {
resolve(result);
} else {
download(offset + 100);
Expand All @@ -48,7 +39,7 @@ DocumentService.prototype.fullDownload = function (params, limit, progress) {
});
};

download(0);
download();
});
};

Expand Down
2 changes: 1 addition & 1 deletion src/views/document/utils/OutingsDownloader.vue
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export default {
})
.catch((error) => {
this.isLoading = false;
window.alert(error.response.data.errors[0].description);
window.alert(error.response?.data?.errors?.[0]?.description);
});
},
},
Expand Down
4 changes: 4 additions & 0 deletions src/views/documents/utils/QueryItems.vue
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,10 @@ export default {
},
documentType() {
// route name are like outings, routes ...
// always the document type with a tail "s".
// the `.slice(0, -1)` removes this "s"
// it also can be outings-stats => the `.split('-')[0]` remove "-stats"
return this.$route.name.split('-')[0].slice(0, -1);
},
Expand Down
4 changes: 2 additions & 2 deletions src/views/portals/outings-stats/OutingsStatsPart.vue
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ const getOutingMonth = function (outing) {
const formatLengthInMeter = function (length) {
if (length < 9999) {
return `${length}m`;
return `${length} m`;
} else {
length = Math.round(length / 1000);
return `${length}km`;
return `${length} km`;
}
};
Expand Down
13 changes: 4 additions & 9 deletions src/views/portals/outings-stats/OutingsStatsView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ import c2c from '@/js/apis/c2c';
import constants from '@/js/constants';
import QueryItems from '@/views/documents/utils/QueryItems';
const LIST_MAX_LENGTH = 2000;
export default {
components: {
OutingsStatsPart,
Expand All @@ -55,11 +57,11 @@ export default {
methods: {
load() {
c2c.outing.fullDownload(this.$route.query, 2000, this.progress).then(this.compute);
c2c.outing.fullDownload(this.$route.query, LIST_MAX_LENGTH, this.progress).then(this.compute);
},
progress(current, total) {
this.loadingPercentage = current / Math.min(total, 2000);
this.loadingPercentage = current / Math.min(total, LIST_MAX_LENGTH);
},
compute(outings) {
Expand Down Expand Up @@ -88,11 +90,4 @@ export default {
padding-bottom: 0.5rem;
clear: both;
}
.result-section {
margin-top: 0.5rem;
border-top: 1px solid lightgrey;
padding-top: 0.5rem;
clear: both;
}
</style>

0 comments on commit 35677ca

Please sign in to comment.