Skip to content
Snippets Groups Projects
Commit 8351816e authored by Timothee P's avatar Timothee P :sunflower:
Browse files

use order from api & simplify filters code

parent c57332b5
No related branches found
No related tags found
2 merge requests!2132.3.2-rc1,!198REDMINE_ISSUE-12567
...@@ -75,7 +75,7 @@ ...@@ -75,7 +75,7 @@
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
<tr v-for="(feature, index) in sortedFeatures" :key="index"> <tr v-for="(feature, index) in paginatedFeatures" :key="index">
<td class="center"> <td class="center">
<div <div
class="ui checkbox" class="ui checkbox"
...@@ -240,18 +240,9 @@ export default { ...@@ -240,18 +240,9 @@ export default {
"checkedFeatures", "checkedFeatures",
"featuresCount", "featuresCount",
"pagination", "pagination",
"pageNumbers", "sort",
], ],
data() {
return {
sort: {
column: "",
ascending: true,
},
};
},
computed: { computed: {
...mapState(["user"]), ...mapState(["user"]),
...mapGetters(["project", "permissions"]), ...mapGetters(["project", "permissions"]),
...@@ -260,50 +251,6 @@ export default { ...@@ -260,50 +251,6 @@ export default {
return this.permissions.is_project_administrator; return this.permissions.is_project_administrator;
}, },
sortedFeatures() {
let sortedFeatures = [...this.paginatedFeatures];
// Ajout du tri
if (this.sort.column !== "") {
sortedFeatures = sortedFeatures.sort((a, b) => {
let aProp = this.getFeatureDisplayName(a);
let bProp = this.getFeatureDisplayName(b);
if (this.sort.column === "status") {
aProp = a.properties.status.value;
bProp = b.properties.status.value;
} else if (this.sort.column === "feature_type") {
aProp = a.properties.feature_type.title;
bProp = b.properties.feature_type.title;
} else if (this.sort.column === "updated_on") {
aProp = a.properties.updated_on;
bProp = b.properties.updated_on;
} else if (this.sort.column === "display_creator") {
aProp = a.properties.display_creator;
bProp = b.properties.display_creator;
}
//ascending
if (this.sort.ascending) {
if (aProp < bProp) {
return -1;
}
if (aProp > bProp) {
return 1;
}
return 0;
} else {
//descending
if (aProp < bProp) {
return 1;
}
if (aProp > bProp) {
return -1;
}
return 0;
}
});
}
return sortedFeatures;
},
checked: { checked: {
get() { get() {
return this.checkedFeatures; return this.checkedFeatures;
...@@ -313,6 +260,16 @@ export default { ...@@ -313,6 +260,16 @@ export default {
}, },
}, },
pageNumbers() {
const totalPages = Math.ceil(
this.featuresCount / this.pagination.pagesize
);
return [...Array(totalPages).keys()].map((pageNumb) => {
++pageNumb;
return pageNumb;
});
},
displayedPageEnd() { displayedPageEnd() {
return this.featuresCount <= this.pagination.end return this.featuresCount <= this.pagination.end
? this.featuresCount ? this.featuresCount
...@@ -341,15 +298,18 @@ export default { ...@@ -341,15 +298,18 @@ export default {
changeSort(column) { changeSort(column) {
if (this.sort.column === column) { if (this.sort.column === column) {
//changer order //changer order
this.sort.ascending = !this.sort.ascending; this.$emit(
"update:sort",
{ column: this.sort.column, ascending: !this.sort.ascending }
);
} else { } else {
this.sort.column = column; this.sort.column = column;
this.sort.ascending = true; this.sort.ascending = true;
this.$emit(
"update:sort",
{ column, ascending: true }
);
} }
this.$emit(
"update:sort",
`${this.sort.ascending ? "-" : ""}${this.sort.column}`
);
}, },
}, },
}; };
......
...@@ -86,7 +86,6 @@ ...@@ -86,7 +86,6 @@
<div class="field wide four column no-margin-mobile"> <div class="field wide four column no-margin-mobile">
<label>Type</label> <label>Type</label>
<Dropdown <Dropdown
v-on:update:selection="updateTypeFeatures"
:options="featureTypeChoices" :options="featureTypeChoices"
:selected="form.type.selected" :selected="form.type.selected"
:selection.sync="form.type.selected" :selection.sync="form.type.selected"
...@@ -98,7 +97,6 @@ ...@@ -98,7 +97,6 @@
<label>Statut</label> <label>Statut</label>
<!-- //* giving an object mapped on key name --> <!-- //* giving an object mapped on key name -->
<Dropdown <Dropdown
v-on:update:selection="updateStatusFeatures"
:options="statusChoices" :options="statusChoices"
:selected="form.status.selected.name" :selected="form.status.selected.name"
:selection.sync="form.status.selected" :selection.sync="form.status.selected"
...@@ -146,7 +144,7 @@ ...@@ -146,7 +144,7 @@
:checkedFeatures.sync="checkedFeatures" :checkedFeatures.sync="checkedFeatures"
:featuresCount="featuresCount" :featuresCount="featuresCount"
:pagination="pagination" :pagination="pagination"
:pageNumbers="pageNumbers" :sort="sort"
/> />
<!-- MODAL ALL DELETE FEATURE TYPE --> <!-- MODAL ALL DELETE FEATURE TYPE -->
...@@ -202,17 +200,12 @@ export default { ...@@ -202,17 +200,12 @@ export default {
data() { data() {
return { return {
modalAllDeleteOpen: false,
form: { form: {
type: { type: {
selected: null, selected: "",
choices: [],
}, },
status: { status: {
selected: { selected: "",
name: null,
value: null,
},
choices: [ choices: [
{ {
name: "Brouillon", name: "Brouillon",
...@@ -234,33 +227,40 @@ export default { ...@@ -234,33 +227,40 @@ export default {
}, },
title: null, title: null,
}, },
filters: {
type: "",
value: "",
},
filtersBis: {},
paginatedFeatures: [],
baseUrl: this.$store.state.configuration.BASE_URL, baseUrl: this.$store.state.configuration.BASE_URL,
modalAllDeleteOpen: false,
map: null, map: null,
zoom: null, zoom: null,
lat: null, lat: null,
lng: null, lng: null,
featuresCount: 0, featuresCount: 0,
next: null, paginatedFeatures: [],
previous: null,
paginateUrl: null,
pagination: { pagination: {
currentPage: 1, currentPage: 1,
pagesize: 15, pagesize: 15,
start: 0, start: 0,
end: 15, end: 15,
}, },
//showMap: true, previous: null,
showMap: false, next: null,
sort: {
column: "",
ascending: true,
},
showMap: true,
showAddFeature: false, showAddFeature: false,
}; };
}, },
watch: {
"form.type.selected"() {
this.fetchPagedFeatures();
},
"form.status.selected.value"() {
this.fetchPagedFeatures();
},
},
computed: { computed: {
...mapGetters(["project", "permissions"]), ...mapGetters(["project", "permissions"]),
...mapState("feature", ["checkedFeatures"]), ...mapState("feature", ["checkedFeatures"]),
...@@ -281,16 +281,6 @@ export default { ...@@ -281,16 +281,6 @@ export default {
featureTypeChoices() { featureTypeChoices() {
return this.feature_types.map((el) => el.title); return this.feature_types.map((el) => el.title);
}, },
pageNumbers() {
const totalPages = Math.ceil(
this.featuresCount / this.pagination.pagesize
);
return [...Array(totalPages).keys()].map((pageNumb) => {
++pageNumb;
return pageNumb;
});
},
}, },
methods: { methods: {
...@@ -357,7 +347,7 @@ export default { ...@@ -357,7 +347,7 @@ export default {
mapDefaultViewZoom, mapDefaultViewZoom,
}); });
this.getBbox2FIt(); this.fetchBboxNfit();
document.addEventListener("change-layers-order", (event) => { document.addEventListener("change-layers-order", (event) => {
// Reverse is done because the first layer in order has to be added in the map in last. // Reverse is done because the first layer in order has to be added in the map in last.
...@@ -380,7 +370,7 @@ export default { ...@@ -380,7 +370,7 @@ export default {
}, 1000); }, 1000);
}, },
getBbox2FIt(queryParams) { fetchBboxNfit(queryParams) {
featureAPI featureAPI
.getFeaturesBbox(this.project.slug, queryParams) .getFeaturesBbox(this.project.slug, queryParams)
.then((bbox) => { .then((bbox) => {
...@@ -396,153 +386,42 @@ export default { ...@@ -396,153 +386,42 @@ export default {
return featureType ? featureType.slug : null; return featureType ? featureType.slug : null;
}, },
buildFilterParams(params) { getAvalaibleField(orderField) {
const { filterType, filterValue } = params; let result = orderField;
let urlParams = ""; if (orderField === "display_creator") {
let typeFilter, statusFilter; result = "creator";
console.log(filterType, filterValue); } else if (orderField === "display_last_editor") {
//*** feature type ***// result = "last_editor";
if (filterType === "featureType") {
if (filterValue === "" && !this.form.type.selected) {
//* s'il y n'avait pas de filtre et qu'il a été supprimé --> ne pas mettre à jour les features
return "abort";
} else if (filterValue !== undefined && filterValue !== null) {
//* s'il y a un nouveau filtre --> ajouter une params
typeFilter = this.getFeatureTypeSlug(filterValue);
} //* sinon il n'y a pas de param ajouté, ce qui supprime la query
//*** status ***//
} else if (filterType === "status") {
if (filterValue === "" && !this.form.status.selected.value) {
return "abort";
} else if (filterValue !== undefined && filterValue !== null) {
statusFilter = filterValue;
//statusFilter = filterValue.value;
}
}
//* after possibilities of aborting features fetch, empty geojson to make sure even no result would update
if (
(filterType === undefined || filterType === "status") &&
this.form.type.selected
) {
//* s'il y a déjà un filtre sélectionné, maintenir le params
typeFilter = this.getFeatureTypeSlug(this.form.type.selected);
}
if (
(filterType === undefined || filterType === "featureType") &&
this.form.status.selected.value
) {
statusFilter = this.form.status.selected.value;
} }
return result;
if (typeFilter) {
let typeParams = `&feature_type_slug=${typeFilter}`;
urlParams += typeParams;
}
if (statusFilter) {
let statusParams = `&status__value=${statusFilter}`;
urlParams += statusParams;
}
//*** title ***//
if (this.form.title) {
urlParams += `&title=${this.form.title}`;
}
this.getBbox2FIt(urlParams);
return urlParams;
}, },
/* buildFilterParamsBis() { buildQueryString() {
let params = ""; let urlParams = "";
let typeFilter, statusFilter; let typeFilter = this.getFeatureTypeSlug(this.form.type.selected);
let statusFilter = this.form.status.selected.value;
if ("featureType") {
if (filterValue === "" && !this.form.type.selected) { if (typeFilter) urlParams += `&feature_type_slug=${typeFilter}`;
//* s'il y n'avait pas de filtre et qu'il a été supprimé --> ne pas mettre à jour les features if (statusFilter) urlParams += `&status__value=${statusFilter}`;
return "abort"; if (this.form.title) urlParams += `&title=${this.form.title}`;
} else if (filterValue !== undefined && filterValue !== null) { if (this.sort.column) {
//* s'il y a un nouveau filtre --> ajouter une params urlParams += `&ordering=${
typeFilter = this.getFeatureTypeSlug(filterValue); this.sort.ascending ? "-" : ""
} //* sinon il n'y a pas de param ajouté, ce qui supprime la query }${this.getAvalaibleField(this.sort.column)}`;
} else if (filterType === "status") {
if (filterValue === "" && !this.form.status.selected.value) {
return "abort";
} else if (filterValue !== undefined && filterValue !== null) {
statusFilter = filterValue.value;
}
}
//* after possibilities of aborting features fetch, empty geojson to make sure even no result would update
if (
(filterType === undefined || filterType === "status") &&
this.form.type.selected
) {
//* s'il y a déjà un filtre sélectionné, maintenir le params
typeFilter = this.getFeatureTypeSlug(this.form.type.selected);
}
if (
(filterType === undefined || filterType === "featureType") &&
this.form.status.selected.value
) {
statusFilter = this.form.status.selected.value;
}
if (typeFilter) {
let typeParams = `&feature_type_slug=${typeFilter}`;
params += typeParams;
}
if (statusFilter) {
let statusParams = `&status__value=${statusFilter}`;
params += statusParams;
}
if (this.form.title) {
params += `&title=${this.form.title}`;
} }
this.getBbox2FIt(params); return urlParams;
return params;
}, */
updateTypeFeatures(filterValue) {
//* only update:selection custom event can trigger the filter update,
//* but it happens before the value is updated, thus using selected value from event to update query
this.fetchPagedFeatures({ filterType: "featureType", filterValue });
//this.filters = { type: "featureType", value };
//this.filtersBis["featureType"] = value;
//this.fetchPagedFeatures();
},
updateStatusFeatures(filterValue) {
this.fetchPagedFeatures({
filterType: "status",
filterValue: filterValue.value,
});
//this.filtersBis["status"] = value;
//this.filters = { type: "status", value };
//this.fetchPagedFeatures();
}, },
fetchPagedFeatures(params) { fetchPagedFeatures(newUrl) {
this.onFilterChange(); //* temporary, use paginated event to watch change in filters, to modify geojson on map this.onFilterChange(); //* use paginated event to watch change in filters and modify features on map
let url = `${this.API_BASE_URL}projects/${this.$route.params.slug}/feature-paginated/?output=geojson&limit=${this.pagination.pagesize}&offset=${this.pagination.start}`; let url = `${this.API_BASE_URL}projects/${this.$route.params.slug}/feature-paginated/?output=geojson&limit=${this.pagination.pagesize}&offset=${this.pagination.start}`;
//console.log(this.buildFilterParamsBis(params)) if (newUrl && typeof newUrl === "string") {
if (typeof params === "string") { //* for receiving next & previous url //* if receiving next & previous url
//console.error("ONLY FOR DEV !!!!!!!!!!!!!"); //newUrl = newUrl.replace("8000", "8010"); //* for dev uncomment to use proxy link
//params = params.replace("8000", "8010"); //* for dev uncomment to use proxy link url = newUrl;
url = params;
} else if (typeof params === "object") {
const filterParams = this.buildFilterParams(params);
if (filterParams === "abort") return; // ? to adapt ???
url += filterParams;
console.log("filterParams", filterParams);
} }
// this.paginateUrl = url; //* store to reuse with sorting const queryString = this.buildQueryString();
url += queryString;
this.$store.commit( this.$store.commit(
"DISPLAY_LOADER", "DISPLAY_LOADER",
...@@ -555,6 +434,8 @@ export default { ...@@ -555,6 +434,8 @@ export default {
this.next = data.next; this.next = data.next;
this.paginatedFeatures = data.results.features; this.paginatedFeatures = data.results.features;
} }
//* bbox needs to be updated with the same filters
if (queryString) this.fetchBboxNfit(queryString);
this.$store.commit("DISCARD_LOADER"); this.$store.commit("DISCARD_LOADER");
}); });
}, },
...@@ -572,9 +453,12 @@ export default { ...@@ -572,9 +453,12 @@ export default {
} }
}, },
handleSortChange(ordering) { handleSortChange(sort) {
console.log("ORDERING", ordering); this.sort = sort;
this.fetchPagedFeatures({filterType: undefined, filterValue: undefined}) this.fetchPagedFeatures({
filterType: undefined,
filterValue: undefined,
});
}, },
toPage(pageNumber) { toPage(pageNumber) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment