Skip to content

Commit 93691d2

Browse files
committed
fix(myaccount): better object id handling
1 parent 18887cd commit 93691d2

File tree

2 files changed

+126
-59
lines changed

2 files changed

+126
-59
lines changed

src/js/my-account/my-account.js

Lines changed: 118 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import Sortable from "sortablejs";
2727
import { kml, gpx } from "@tmcw/togeojson";
2828
import { DOMParser } from "@xmldom/xmldom";
2929
import GeoJsonToGpx from "@dwayneparton/geojson-to-gpx";
30+
import { v4 as uuidv4 } from "uuid";
3031

3132
import LineSlice from "@turf/line-slice";
3233
import CleanCoords from "@turf/clean-coords";
@@ -80,76 +81,98 @@ class MyAccount {
8081

8182
// itinéraires
8283
this.routes = [];
84+
this.routesOrder = [];
8385

8486
// points de repère
8587
this.landmarks = [];
88+
this.landmarksOrder = [];
8689
this.compareLandmarks = [];
90+
this.compareLandmarksOrder = [];
8791

8892
// cartes téléchargées
8993
this.offlineMaps = [];
9094

9195
this.#addSourcesAndLayers();
9296

93-
// Identifiant unique pour les itinéraires
94-
this.lastRouteId = 0;
95-
this.lastLandmarkId = 0;
96-
this.lastCompareLandmarkId = 0;
97-
98-
// chargement des enregistrements stockés en local
99-
let fileStoragePromise = fileStorage.list().then( (files) => {
100-
files.forEach( (file) => {
101-
if (file.id.startsWith("route-")) {
102-
this.routes.push(file.data);
103-
} else if (file.id.startsWith("landmark-")) {
104-
this.landmarks.push(file.data);
105-
} else if (file.id.startsWith("comparelandmark-")) {
106-
this.compareLandmarks.push(file.data);
107-
}
108-
});
109-
this.#updateSources();
110-
});
111-
11297
// REMOVEME : rétrocompatibilité des entités enregistrées : migration de préférences à fichier local (post-3.3.35)
11398
// récupération des itinéraires enregistrés en local
11499
let promiseRoutes = Preferences.get( { key: "savedRoutes"} ).then( (resp) => {
115100
if (resp.value) {
116101
var localRoutes = JSON.parse(resp.value);
117-
this.routes = this.routes.concat(localRoutes.filter( route => !route.type));
118-
this.#updateSources();
119102
localRoutes.forEach( (route) => {
120103
fileStorage.save(route, `route-${route.id}`);
121104
});
122105
Preferences.remove({ key: "savedRoutes" });
106+
this.#updateRoutesOrder();
123107
}
124108
});
125109

126110
// récupération des points de repère enregistrés en local
127111
let promiseLandmarks = Preferences.get( { key: "savedLandmarks"} ).then( (resp) => {
128112
if (resp.value) {
129113
var localLandmarks = JSON.parse(resp.value);
130-
this.landmarks = this.landmarks.concat(localLandmarks);
131-
this.#updateSources();
132114
localLandmarks.forEach( (landmark) => {
133115
fileStorage.save(landmark, `landmark-${landmark.id}`);
134116
});
135117
Preferences.remove({ key: "savedLandmarks" });
118+
this.#updateLandmarksOrder();
136119
}
137120
});
138121

139122
// récupération des points de repère comparer enregistrés en local
140123
let promiseCompareLandmarks = Preferences.get( { key: "savedCompareLandmarks"} ).then( (resp) => {
141124
if (resp.value) {
142125
var localCompareLandmarks = JSON.parse(resp.value);
143-
this.compareLandmarks = this.compareLandmarks.concat(localCompareLandmarks);
144-
this.#updateSources();
145126
localCompareLandmarks.forEach( (compareLandmark) => {
146127
fileStorage.save(compareLandmark, `comparelandmark-${compareLandmark.id}`);
147128
});
148129
Preferences.remove({ key: "savedCompareLandmarks" });
130+
this.#updateCompareLandmarksOrder();
149131
}
150132
});
151133
// END REMOVEME
152134

135+
let fileStoragePromise;
136+
let routeOrderStoragePromise;
137+
let landmarkOrderStoragePromise;
138+
let compareLandmarkOrderStoragePromise;
139+
140+
// REMOVEME
141+
Promise.all([promiseCompareLandmarks, promiseLandmarks, promiseRoutes]).then( () => {
142+
// END REMOVEME
143+
// chargement des enregistrements stockés en local
144+
fileStoragePromise = fileStorage.list().then( (files) => {
145+
files.forEach( (file) => {
146+
if (file.id.startsWith("route-")) {
147+
this.routes.push(file.data);
148+
} else if (file.id.startsWith("landmark-")) {
149+
this.landmarks.push(file.data);
150+
} else if (file.id.startsWith("comparelandmark-")) {
151+
this.compareLandmarks.push(file.data);
152+
}
153+
});
154+
});
155+
156+
// chargement de l'ordre des routes, landmarks et compareLandmarks
157+
routeOrderStoragePromise = Preferences.get( { key: "myaccount_routes_order"} ).then( (resp) => {
158+
if (resp.value) {
159+
this.routesOrder = JSON.parse(resp.value);
160+
}
161+
});
162+
landmarkOrderStoragePromise = Preferences.get( { key: "myaccount_landmarks_order"} ).then( (resp) => {
163+
if (resp.value) {
164+
this.landmarksOrder = JSON.parse(resp.value);
165+
}
166+
});
167+
compareLandmarkOrderStoragePromise = Preferences.get( { key: "myaccount_comparelandmarks_order"} ).then( (resp) => {
168+
if (resp.value) {
169+
this.compareLandmarksOrder = JSON.parse(resp.value);
170+
}
171+
});
172+
// REMOVEME
173+
});
174+
// END REMOVEME
175+
153176
this.map.loadImage(LandmarkIconSaved).then((image) => {
154177
this.map.addImage("landmark-icon-saved", image.data);
155178
});
@@ -176,23 +199,16 @@ class MyAccount {
176199
});
177200

178201
// récupération des infos et rendu graphique
179-
// REMOVE promiseCompareLandmarks, promiseLandmarks, promiseRoutes
180-
Promise.all([this.compute(), fileStoragePromise, promiseCompareLandmarks, promiseLandmarks, promiseRoutes, Globals.offlineMaps.loadPromise]).then(() => {
181-
// Ajout d'identifiant unique aux routes
182-
this.routes.forEach((route) => {
183-
route.id = this.lastRouteId;
184-
this.lastRouteId++;
185-
});
186-
// Ajout d'identifiant unique aux landmarks
187-
this.landmarks.forEach((landmark) => {
188-
landmark.id = this.lastLandmarkId;
189-
this.lastLandmarkId++;
190-
});
191-
// Ajout d'identifiant unique aux compareLandmarks
192-
this.compareLandmarks.forEach((compareLandmark) => {
193-
compareLandmark.id = this.lastCompareLandmarkId;
194-
this.lastCompareLandmarkId++;
195-
});
202+
Promise.all([
203+
this.compute(), fileStoragePromise,
204+
routeOrderStoragePromise, landmarkOrderStoragePromise, compareLandmarkOrderStoragePromise,
205+
Globals.offlineMaps.loadPromise,
206+
]).then(() => {
207+
// Mise en ordre des routes, landmarks et compareLandmarks
208+
jsUtils.sortArrayByAnotherArray(this.routes, this.routesOrder, "id");
209+
jsUtils.sortArrayByAnotherArray(this.landmarks, this.landmarksOrder, "id");
210+
jsUtils.sortArrayByAnotherArray(this.compareLandmarks, this.compareLandmarksOrder, "id");
211+
196212
this.render();
197213
this.#listeners();
198214
this.#updateSources();
@@ -626,17 +642,16 @@ class MyAccount {
626642
* @param {*} drawRouteSaveOptions
627643
*/
628644
addRoute(drawRouteSaveOptions) {
629-
if (typeof drawRouteSaveOptions.id !== "undefined" && drawRouteSaveOptions.id >= 0) {
645+
if (typeof drawRouteSaveOptions.id === "undefined" || drawRouteSaveOptions.id < 0) {
646+
drawRouteSaveOptions.id = uuidv4();
647+
this.routes.unshift(drawRouteSaveOptions);
648+
} else {
630649
for (let i = 0; i < this.routes.length; i++) {
631650
if (this.routes[i].id === drawRouteSaveOptions.id){
632651
this.routes[i] = drawRouteSaveOptions;
633652
break;
634653
}
635654
}
636-
} else {
637-
drawRouteSaveOptions.id = this.lastRouteId;
638-
this.lastRouteId++;
639-
this.routes.unshift(drawRouteSaveOptions);
640655
}
641656
fileStorage.save(drawRouteSaveOptions, `route-${drawRouteSaveOptions.id}`);
642657
this.__updateAccountRoutesContainerDOMElement(this.routes);
@@ -669,18 +684,17 @@ class MyAccount {
669684
* @param {*} landmarkGeojson
670685
*/
671686
addLandmark(landmarkGeojson) {
672-
if (typeof landmarkGeojson.id !== "undefined" && landmarkGeojson.id >= 0) {
687+
if (typeof landmarkGeojson.id === "undefined" || landmarkGeojson.id < 0) {
688+
landmarkGeojson = JSON.parse(JSON.stringify(landmarkGeojson));
689+
landmarkGeojson.id = uuidv4();
690+
this.landmarks.unshift(landmarkGeojson);
691+
} else {
673692
for (let i = 0; i < this.landmarks.length; i++) {
674693
if (this.landmarks[i].id === landmarkGeojson.id){
675694
this.landmarks[i] = JSON.parse(JSON.stringify(landmarkGeojson));
676695
break;
677696
}
678697
}
679-
} else {
680-
landmarkGeojson = JSON.parse(JSON.stringify(landmarkGeojson));
681-
landmarkGeojson.id = this.lastLandmarkId;
682-
this.lastLandmarkId++;
683-
this.landmarks.unshift(landmarkGeojson);
684698
}
685699
fileStorage.save(landmarkGeojson, `landmark-${landmarkGeojson.id}`);
686700
this.__updateAccountLandmarksContainerDOMElement(this.landmarks);
@@ -692,18 +706,17 @@ class MyAccount {
692706
* @param {*} compareLandmarkGeojson
693707
*/
694708
addCompareLandmark(compareLandmarkGeojson) {
695-
if (typeof compareLandmarkGeojson.id !== "undefined" && compareLandmarkGeojson.id >= 0) {
709+
if (typeof compareLandmarkGeojson.id === "undefined" || compareLandmarkGeojson.id < 0) {
710+
compareLandmarkGeojson = JSON.parse(JSON.stringify(compareLandmarkGeojson));
711+
compareLandmarkGeojson.id = uuidv4();
712+
this.compareLandmarks.unshift(compareLandmarkGeojson);
713+
} else {
696714
for (let i = 0; i < this.compareLandmarks.length; i++) {
697715
if (this.compareLandmarks[i].id === compareLandmarkGeojson.id){
698716
this.compareLandmarks[i] = JSON.parse(JSON.stringify(compareLandmarkGeojson));
699717
break;
700718
}
701719
}
702-
} else {
703-
compareLandmarkGeojson = JSON.parse(JSON.stringify(compareLandmarkGeojson));
704-
compareLandmarkGeojson.id = this.lastCompareLandmarkId;
705-
this.lastCompareLandmarkId++;
706-
this.compareLandmarks.unshift(compareLandmarkGeojson);
707720
}
708721
fileStorage.save(compareLandmarkGeojson, `comparelandmark-${compareLandmarkGeojson.id}`);
709722
this.__updateAccountCompareLandmarksContainerDOMElement(this.compareLandmarks);
@@ -784,6 +797,7 @@ class MyAccount {
784797
const route = this.routes[oldIndex];
785798
this.routes.splice(oldIndex, 1);
786799
this.routes.splice(newIndex, 0, route);
800+
this.#updateRoutesOrder();
787801
this.#updateSources();
788802
}
789803

@@ -796,6 +810,7 @@ class MyAccount {
796810
const landmark = this.landmarks[oldIndex];
797811
this.landmarks.splice(oldIndex, 1);
798812
this.landmarks.splice(newIndex, 0, landmark);
813+
this.#updateLandmarksOrder();
799814
this.#updateSources();
800815
}
801816

@@ -808,6 +823,7 @@ class MyAccount {
808823
const compareLandmark = this.compareLandmarks[oldIndex];
809824
this.compareLandmarks.splice(oldIndex, 1);
810825
this.compareLandmarks.splice(newIndex, 0, compareLandmark);
826+
this.#updateCompareLandmarksOrder();
811827
this.#updateSources();
812828
}
813829

@@ -820,6 +836,36 @@ class MyAccount {
820836
Globals.offlineMaps.changeMapIndex(offlineMapId, oldIndex, newIndex);
821837
}
822838

839+
/**
840+
* Met à jour l'ordre d'affichage des routes
841+
*/
842+
#updateRoutesOrder() {
843+
this.routesOrder = [];
844+
this.routes.forEach((route) => {
845+
this.routesOrder.push(route.id);
846+
});
847+
}
848+
849+
/**
850+
* Met à jour l'ordre d'affichage des points de repère
851+
*/
852+
#updateLandmarksOrder() {
853+
this.landmarksOrder = [];
854+
this.landmarks.forEach((landmark) => {
855+
this.landmarksOrder.push(landmark.id);
856+
});
857+
}
858+
859+
/**
860+
* Met à jour l'ordre d'affichage des points de repère Comparer
861+
*/
862+
#updateCompareLandmarksOrder() {
863+
this.compareLandmarksOrder = [];
864+
this.compareLandmarks.forEach((compareLandmark) => {
865+
this.compareLandmarksOrder.push(compareLandmark.id);
866+
});
867+
}
868+
823869
/**
824870
* Ouvre l'outil de tracé d'itinéraire pour modifier un itinéraire
825871
* @param {*} route
@@ -1886,6 +1932,19 @@ ${props.text}`,
18861932
type: "FeatureCollection",
18871933
features: this.compareLandmarks,
18881934
});
1935+
1936+
Preferences.set({
1937+
key: "myaccount_routes_order",
1938+
value: JSON.stringify(this.routesOrder),
1939+
});
1940+
Preferences.set({
1941+
key: "myaccount_landmarks_order",
1942+
value: JSON.stringify(this.landmarksOrder),
1943+
});
1944+
Preferences.set({
1945+
key: "myaccount_comparelandmarks_order",
1946+
value: JSON.stringify(this.compareLandmarksOrder),
1947+
});
18891948
}
18901949

18911950
/**

src/js/utils/js-utils.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,14 @@ let jsUtils = {
3131
document.body.appendChild(element);
3232
element.click();
3333
document.body.removeChild(element);
34+
},
35+
36+
sortArrayByAnotherArray(arrToSort, arrReference, property) {
37+
arrToSort.sort( (a, b) => {
38+
let indexA = arrReference.findIndex( item => item === a[property] );
39+
let indexB = arrReference.findIndex( item => item === b[property] );
40+
return indexA - indexB;
41+
});
3442
}
3543
};
3644

0 commit comments

Comments
 (0)