diff --git a/package-lock.json b/package-lock.json index e07bcf24b..90b6a1ae9 100644 --- a/package-lock.json +++ b/package-lock.json @@ -8,7 +8,7 @@ "name": "ecospheres-front", "version": "0.3.0", "dependencies": { - "@etalab/data.gouv.fr-components": "^1.11.8", + "@etalab/data.gouv.fr-components": "^1.11.9", "@gouvminint/vue-dsfr": "^5.3.1", "@vueform/multiselect": "^2.6.2", "axios": "^1.6.2", @@ -806,9 +806,9 @@ } }, "node_modules/@etalab/data.gouv.fr-components": { - "version": "1.11.8", - "resolved": "https://registry.npmjs.org/@etalab/data.gouv.fr-components/-/data.gouv.fr-components-1.11.8.tgz", - "integrity": "sha512-EMYRuMKcco8imCCD/Pu9UZqCE8M0tNx0veUsOHoYUK38INaNPoi6mJUKd8J13KXMTPcg0uYfhivw+zdVvBUBNg==", + "version": "1.11.9", + "resolved": "https://registry.npmjs.org/@etalab/data.gouv.fr-components/-/data.gouv.fr-components-1.11.9.tgz", + "integrity": "sha512-P7l3bV4rYLOdrL2Kz2qB5KH+Ir9X2o1uKwACt/E814YqVhPZBBdAitya015UolWjOgDujJeYZbdgufSnrN7QAQ==", "dependencies": { "vue": "^3.3.8" }, diff --git a/package.json b/package.json index 9c7dbdc67..659563bc2 100644 --- a/package.json +++ b/package.json @@ -18,7 +18,7 @@ "prepare": "husky install" }, "dependencies": { - "@etalab/data.gouv.fr-components": "^1.11.8", + "@etalab/data.gouv.fr-components": "^1.11.9", "@gouvminint/vue-dsfr": "^5.3.1", "@vueform/multiselect": "^2.6.2", "axios": "^1.6.2", diff --git a/src/model/resource.ts b/src/model/resource.ts new file mode 100644 index 000000000..f10b0aeaa --- /dev/null +++ b/src/model/resource.ts @@ -0,0 +1,16 @@ +import type { Resource } from '@etalab/data.gouv.fr-components' + +export interface ResourceType { + id: string + label: string +} + +export interface ResourceData { + currentPage: number + resources: Resource[] + total: number + totalWithoutFilter: number + type: ResourceType +} + +export type { Resource } diff --git a/src/store/DatasetStore.js b/src/store/DatasetStore.js index 2e51505cb..30adeed6e 100644 --- a/src/store/DatasetStore.js +++ b/src/store/DatasetStore.js @@ -147,46 +147,6 @@ export const useDatasetStore = defineStore('dataset', { } return datasets }, - /** - * Load resources from the API via a HATEOAS rel - * - * @param {Object} rel - HATEOAS rel for datasets - * @param {string} pageSize - page size - * @returns {Promise, total: number}>>} - */ - async loadResources(rel, pageSize) { - if (this.resourceTypes.length === 0) { - this.resourceTypes = await datasetsApi.get('resource_types') - } - const resources = [] - for (const type of this.resourceTypes) { - const url = new URL(rel.href) - url.searchParams.set('page_size', pageSize) - url.searchParams.set('type', type.id) - const updatedUrl = url.toString() - const response = await datasetsApiv2.request(updatedUrl) - resources.push({ - currentPage: 1, - resources: response.data, - total: response.total, - typeId: type.id, - typeLabel: type.label - }) - } - return resources - }, - - async fetchDatasetResources(datasetId, type, page, pageSize, query) { - const response = await datasetsApiv2.get(`${datasetId}/resources/`, { - params: { - page, - page_size: pageSize, - type, - q: query - } - }) - return { data: response.data, total: response.total } - }, async getLicense(license) { const response = await datasetsApi.get('licenses') diff --git a/src/store/ResourceStore.ts b/src/store/ResourceStore.ts new file mode 100644 index 000000000..e7a8d071a --- /dev/null +++ b/src/store/ResourceStore.ts @@ -0,0 +1,69 @@ +import type { Rel } from '@etalab/data.gouv.fr-components' +import { defineStore } from 'pinia' + +import config from '@/config' + +import type { Resource, ResourceData, ResourceType } from '../model/resource' +import DatasetsAPI from '../services/api/resources/DatasetsAPI' + +const datasetsApi = new DatasetsAPI() +const datasetsApiv2 = new DatasetsAPI({ version: 2 }) +const pageSize: number = config.website.pagination_sizes.files_list + +export interface RootState { + data: Record + resourceTypes: ResourceType[] +} + +export const useResourceStore = defineStore('resource', { + state: (): RootState => ({ + data: {}, + resourceTypes: [] + }), + actions: { + /** + * Load resources from the API via a HATEOAS rel + * + */ + async loadResources(datasetId: string, rel: Rel): Promise { + if (datasetId in this.data) { + return this.data[datasetId] + } + if (this.resourceTypes.length === 0) { + this.resourceTypes = await datasetsApi.get('resource_types', {}) + } + this.data[datasetId] = [] + for (const type of this.resourceTypes) { + const url = new URL(rel.href) + url.searchParams.set('page_size', pageSize.toFixed(0)) + url.searchParams.set('type', type.id) + const response = await datasetsApi.request(url.toString()) + this.data[datasetId].push({ + currentPage: 1, + resources: response.data, + total: response.total, + totalWithoutFilter: response.total, + type + }) + } + return this.data[datasetId] + }, + + async fetchDatasetResources( + datasetId: string, + typeId: string, + page: number, + q = '' + ): Promise<{ data: Resource[]; total: number }> { + const response = await datasetsApiv2.get(`${datasetId}/resources`, { + params: { + page, + page_size: pageSize, + type: typeId, + q + } + }) + return { data: response.data, total: response.total } + } + } +}) diff --git a/src/views/datasets/DatasetDetailView.vue b/src/views/datasets/DatasetDetailView.vue index 82b6a8164..f746cc6a9 100644 --- a/src/views/datasets/DatasetDetailView.vue +++ b/src/views/datasets/DatasetDetailView.vue @@ -1,4 +1,4 @@ -