avantifellows/plio-frontend

Inconsistent ordering while adding questions in between other questions

deepansh96 opened this issue · 0 comments

When adding a new item in between any other two items, the ordering of itemTimestamps and items was not consistent.

Let's say we have three items - [ item1, item2, item3 ] with timings [ 3, 6, 12 ]. If the user adds another item itemX between item2 and item3 at time 9, the items array becomes - [ item1, item2, itemX, item3 ].

All good until this step. Now the issue was that although the items array gets sorted properly based on it's time, the itemTimestamps array still remains as [ 3, 6, 12, 9] whereas it should've also gotten sorted to [3, 6, 9, 12].

Why does this happen?

  • addNewItem() method is called
  • A new item is pushed into this.items[] array. (The items array looks like this currently - [ item1, item2, item3, itemX ]
  • updateItemTimestamps() is called. This updates the array itemTimestamps to this - [3, 6, 12, 9]
  • itemTimestamps() watcher is triggered -> sortAndFixItemsOrder() method is called -> Sorts the items array
  • Now the items array is sorted properly and looks like this - [ item1, item2, itemX, item3 ]
  • But now, there are no more calls made to updateItemTimestamps. So itemTimestamps remains like this [3, 6, 12, 9]
  • Earlier this issue never occurred because we had a watcher attached to items and whenever that was updated, itemTimestamps were updated and got sorted.

Solution -
Before updating itemTimestamps, make sure you sort the array so itemTimestamps, when created/updated, is also sorted.