Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redux and Calendar repeating events

What should be the proper way of storing / handling repeating events in the redux store ?

Problem: Let's say that we have a backend API that generates repeating events trough a complicated business logic.Some of the events might have the same ID. Lets say that generated output looks this way :

[
  {
    "id": 1,
    "title": "Weekly meeting",
    "all_day": true,
    "starts_at": "2017-09-12",
    "ends_at": "2017-09-12"
  },
  {
    "id": 3,
    "title": "Daily meeting1",
    "all_day": false,
    "starts_at": "2017-09-12",
    "ends_at": "2017-09-12",
  },
  {
    "id": 3,
    "title": "Daily meeting1",
    "all_day": false,
    "starts_at": "2017-09-13",
    "ends_at": "2017-09-13",
  },
  {
    "id": 3,
    "title": "Daily meeting1",
    "all_day": false,
    "starts_at": "2017-09-14",
    "ends_at": "2017-09-14",
  }
]

Possible solution would be: generate unique ID by having additional property uid composed like this: id + # + starts_at. This way we could identify each occurrence uniquely. (I'm using this right now)

Example:

[
  {
    "id": 1,
    "uid": "1#2017-09-12",
    "title": "Weekly meeting",
    "all_day": true,
    "starts_at": "2017-09-12",
    "ends_at": "2017-09-12"
  }
]

I'm wondering is there some other way, maybe more elegant than having composed unique id ?

like image 334
Goran.it Avatar asked Oct 17 '22 07:10

Goran.it


2 Answers

There is a possible pitfall with your current solution. What will happen if id and start_id of two events will be the same? Is it possible scenario in your domain?

Because of that I usually use this nice lib in such cases. It produces really short unique ids, which have some nice properties, like guaranties not to intersect, to be unpredictable and so on.

Also ask yourself if you actually need unique ids in your case. Looks like your back-end have no chance to distinguish events anyways, so why bother? Redux store will happily keep your events event without uid.

like image 71
fkulikov Avatar answered Oct 21 '22 01:10

fkulikov


Maybe not much of an improvement (if at all) but just using JSON.stringify to check for duplicates could make unique id's obsolete.

const existingEvents = [
  {
    "id": 3,
    "title": "Daily meeting1",
    "all_day": false,
    "starts_at": "2017-09-14",
    "ends_at": "2017-09-14",
  }
];

const duplicate = {
    "id": 3,
    "title": "Daily meeting1",
    "all_day": false,
    "starts_at": "2017-09-14",
    "ends_at": "2017-09-14",
};

const eventIsDuplicate = (existingEvents, newEvent) => {
    const duplicate = 
    existingEvents.find(event => JSON.stringify(event) == JSON.stringify(newEvent));
    return typeof duplicate != 'undefined';
};

console.log(eventIsDuplicate(existingEvents, duplicate)); // true

I guess this would only be preferable to your existing solution if, for some reason, you'd want to keep all the uniqueness logic on the client side.

like image 20
jonahe Avatar answered Oct 21 '22 00:10

jonahe