There are basicly 2 scenarious for create:
- Online and everything is successful
- Offline, request failed
First case
- requested action: we generate id (https://www.npmjs.com/package/uuid) and store information under this id
{
posts: {
6c84fb90-12c4-11e1-840d-7b25c5ee775a: {
status: 'requested',
data: { id: '6c84fb90-12c4-11e1-840d-7b25c5ee775a' .....},
......
}
}
}
- success action: we replace object with temporary id, with object from server
{
posts: {
'id from server here': {
data: { 'id': 'id from servere here' .....},
......
}
}
}
Second case So if we look closer into second scenario, I see the following actions should follow:
- requested action: same as success scenario
- fail action:
- we keep local object with random id, and return it to client and add error info
{
posts: {
6c84fb90-12c4-11e1-840d-7b25c5ee775a: {
status: 'error',
data: { id: '6c84fb90-12c4-11e1-840d-7b25c5ee775a' .....},
......
}
}
}
does it mean catch for create/update/delete never fire?
- replay action
- online:
- we save object from server, replacing local,
- we still keeping random id because it can be used by client
- we store server id, for future operations (update/delete), it can't be performed with local random id, we can store id in serverId field
- online:
{
posts: {
6c84fb90-12c4-11e1-840d-7b25c5ee775a: {
data: { id: '6c84fb90-12c4-11e1-840d-7b25c5ee775a' .....},
ref: 'real-id'
......
}
}
}
- offline
- user can do next actions with object like update and delete
- if he updates object then when we become online we can just dispatch one create action with updated object istead of 2
- if he deletes object nothing should be ddispatched
There possible problem with duplicated data, when you have one local object and same object from server with different id. Other solution can be always have one instance of object, but in that case if client is using offline copy of object we need to notify it, that data must be refreshed.