From 82a95f0793448e855a3449d1ab638cec8659c1d9 Mon Sep 17 00:00:00 2001
From: Luke Barnard <luke@matrix.org>
Date: Wed, 6 Dec 2017 11:22:06 +0000
Subject: [PATCH] Simplify order_tag in TagOrderStore

such that:
 - it takes a targetTag to be replaced instead the previous tag to insert after
 - it optionally displaces the targetTag before or after the inserted tag
---
 src/stores/TagOrderStore.js | 15 ++++++++++-----
 1 file changed, 10 insertions(+), 5 deletions(-)

diff --git a/src/stores/TagOrderStore.js b/src/stores/TagOrderStore.js
index ce150a60b0..4364ec2683 100644
--- a/src/stores/TagOrderStore.js
+++ b/src/stores/TagOrderStore.js
@@ -53,19 +53,24 @@ class TagOrderStore extends Store {
                 this._setState({allTags: payload.tags});
             break;
             case 'order_tag': {
-                // Puts payload.tag below payload.prevTag in the orderedTags state
+                if (!payload.tag || !payload.targetTag || payload.tag === payload.targetTag) break;
+
+                // Puts payload.tag at payload.targetTag, placing the targetTag before or after the tag
                 const tags = SettingsStore.getValue("TagOrderStore.orderedTags") || this._state.allTags;
+
                 let orderedTags = tags.filter((t) => t !== payload.tag);
-                const tagPrevIx = orderedTags.indexOf(payload.prevTag);
+                const newIndex = orderedTags.indexOf(payload.targetTag) + (payload.after ? 1 : 0);
                 orderedTags = [
-                    ...orderedTags.slice(0, tagPrevIx + 1),
+                    ...orderedTags.slice(0, newIndex),
                     payload.tag,
-                    ...orderedTags.slice(tagPrevIx + 1),
+                    ...orderedTags.slice(newIndex),
                 ];
                 this._setState({orderedTags});
-                SettingsStore.setValue("TagOrderStore.orderedTags", null, "account", orderedTags);
             }
             break;
+            case 'commit_tags':
+                SettingsStore.setValue("TagOrderStore.orderedTags", null, "account", this._state.orderedTags);
+            break;
         }
     }