diff --git a/src/views/FeatureType/FeatureTypeDetail.vue b/src/views/FeatureType/FeatureTypeDetail.vue index 22c6b22cb6ce59c775ad090d7885aafedb26a416..6d9e72bf2405713e0a6bde9319d9418def5ec981 100644 --- a/src/views/FeatureType/FeatureTypeDetail.vue +++ b/src/views/FeatureType/FeatureTypeDetail.vue @@ -1,6 +1,6 @@ <template> <div - v-if="structure" + v-if="feature_type" id="feature-type-detail" > <div class="ui stackable grid"> @@ -8,42 +8,42 @@ <div class="ui attached secondary segment"> <h1 class="ui center aligned header ellipsis"> <img - v-if="structure.geom_type === 'point'" + v-if="feature_type.geom_type === 'point'" class="ui medium image" alt="Géométrie point" src="@/assets/img/marker.png" > <img - v-if="structure.geom_type === 'linestring'" + v-if="feature_type.geom_type === 'linestring'" class="ui medium image" alt="Géométrie ligne" src="@/assets/img/line.png" > <img - v-if="structure.geom_type === 'polygon'" + v-if="feature_type.geom_type === 'polygon'" class="ui medium image" alt="Géométrie polygone" src="@/assets/img/polygon.png" > <img - v-if="structure.geom_type === 'multipoint'" + v-if="feature_type.geom_type === 'multipoint'" class="ui medium image" alt="Géométrie point" src="@/assets/img/multimarker.png" > <img - v-if="structure.geom_type === 'multilinestring'" + v-if="feature_type.geom_type === 'multilinestring'" class="ui medium image" alt="Géométrie ligne" src="@/assets/img/multiline.png" > <img - v-if="structure.geom_type === 'multipolygon'" + v-if="feature_type.geom_type === 'multipolygon'" class="ui medium image" alt="Géométrie polygone" src="@/assets/img/multipolygon.png" > - {{ structure.title }} + {{ feature_type.title }} </h1> </div> <div class="ui attached segment"> @@ -125,7 +125,7 @@ </div> <div - v-if="structure.geom_type === 'point' || structure.geom_type === 'none'" + v-if="feature_type.geom_type === 'point' || feature_type.geom_type === 'none'" class="field" > <label @@ -222,7 +222,7 @@ GeoJSON </option> <option - v-if="structure.geom_type === 'point' || structure.geom_type === 'none'" + v-if="feature_type.geom_type === 'point' || feature_type.geom_type === 'none'" value="CSV" > CSV @@ -355,10 +355,10 @@ Voir tous les signalements </router-link> <router-link - v-if="permissions.can_create_feature && structure.geom_type && !structure.geom_type.includes('multi')" + v-if="permissions.can_create_feature && feature_type.geom_type && !feature_type.geom_type.includes('multi')" :to="{ name: 'ajouter-signalement', - params: { slug_type_signal: structure.slug }, + params: { slug_type_signal: feature_type.slug }, }" class="ui icon button button-hover-green margin-25" > @@ -442,10 +442,16 @@ export default { exportFormat: 'GeoJSON', exportLoading: false, lastFeatures: [], - featuresCount: 0 + featuresCount: 0, }; }, + watch: { + feature_type(newValue) { + this.toggleJsonUploadOption(newValue); + } + }, + computed: { ...mapGetters([ 'permissions', @@ -453,6 +459,9 @@ export default { ...mapGetters('projects', [ 'project' ]), + ...mapGetters('feature-type', [ + 'feature_type' + ]), ...mapState([ 'reloadIntervalId', 'configuration', @@ -478,20 +487,9 @@ export default { IDGO() { return this.$store.state.configuration.VUE_APP_IDGO; }, - structure: function () { - if (Object.keys(this.feature_types).length) { - const st = this.feature_types.find( - (el) => el.slug === this.featureTypeSlug - ); - if (st) { - return st; - } - } - return {}; - }, orderedCustomFields() { - if (Object.keys(this.structure).length) { - return [...this.structure.customfield_set].sort( + if (this.feature_type) { + return [...this.feature_type.customfield_set].sort( (a, b) => a.position - b.position ); } @@ -509,7 +507,7 @@ export default { ); this.$store.dispatch('feature-type/GET_IMPORTS', { project_slug: this.$route.params.slug, - feature_type: this.$route.params.feature_type_slug + feature_type: this.featureTypeSlug }); this.getLastFeatures(); if (this.$route.params.type === 'external-geojson') { @@ -517,6 +515,8 @@ export default { } // empty prerecorded lists in case the list has been previously loaded with a limit in other component like FeatureExtraForm this.SET_PRERECORDED_LISTS([]); + // This function is already called by watcher at this stage, but to be safe in edge case + this.toggleJsonUploadOption(this.feature_type); }, @@ -559,6 +559,17 @@ export default { this.showImport = !this.showImport; }, + /** + * In the case of a non geographical feature type, replace geoJSON by JSON in file to upload options + * + * @param {Object} featureType - The current featureType. + */ + toggleJsonUploadOption(featureType) { + if (featureType && featureType.geom_type === 'none') { + this.geojsonFileToImport.name = 'Sélectionner un fichier JSON ...'; + } + }, + async checkPreRecordedValue(fieldValue, listName) { const fieldLabel = fieldValue.label || fieldValue; // encode special characters like apostrophe or white space @@ -571,7 +582,7 @@ export default { async isValidTypes(features) { this.importError = ''; - const fields = this.structure.customfield_set.map((el) => { + const fields = this.feature_type.customfield_set.map((el) => { return { name: el.name, field_type: el.field_type, @@ -681,7 +692,7 @@ export default { // Extract headers const headers = rows.shift(); - if (this.structure.geom_type !== 'none') { + if (this.feature_type.geom_type !== 'none') { // Check for required fields 'lat' and 'lon' in headers if (!headers.includes('lat') || !headers.includes('lon')) { this.importError = 'Les champs obligatoires "lat" et "lon" sont absents des headers.'; @@ -765,13 +776,14 @@ export default { jsonValidity = true; } - // If the GeoJSON is valid, update the component state with the file + // If the GeoJSON is valid, update the component state with the file and set the file in store if (jsonValidity) { - this.geojsonFileToImport = files[0]; // TODO: Remove this value from state as it is stored (first attempt didn't work) + this.geojsonFileToImport = files[0]; this.SET_FILE_TO_IMPORT(this.geojsonFileToImport); } else { // Clear any previously selected geojson file to disable import button this.geojsonFileToImport = geojsonFileToImport; + this.toggleJsonUploadOption(this.feature_type); } // Stop the loading process @@ -797,6 +809,7 @@ export default { // Clear any previously selected geojson file to avoid confusion this.geojsonFileToImport = geojsonFileToImport; + this.toggleJsonUploadOption(this.feature_type); // Retrieve the files from the event const files = e.target.files || e.dataTransfer.files; @@ -901,7 +914,7 @@ export default { if (blob) { const link = document.createElement('a'); link.href = URL.createObjectURL(blob); - link.download = `${this.project.title}-${this.structure.title}.${this.exportFormat === 'GeoJSON' ? 'json' : 'csv'}`; + link.download = `${this.project.title}-${this.feature_type.title}.${this.exportFormat === 'GeoJSON' ? 'json' : 'csv'}`; link.click(); setTimeout(function(){ URL.revokeObjectURL(link.href);