This repository has been archived by the owner on Apr 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Externalize cache updates to object (#104)
Follows new approach from aerogear/aerogear-js-sdk#292 Requires to link js-SDK for the moment. ticket: https://issues.jboss.org/browse/AEROGEAR-8333 ## Verification steps 1. Add new item when offline 2. Restart mobile app (do not work in website version) 3. See item that was added. 4. Restore network - see that item is still there
- Loading branch information
Showing
4 changed files
with
81 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import { GET_TASKS } from './graphql.queries'; | ||
|
||
export const taskCacheUpdates = { | ||
createTask: (cache, { data }) => { | ||
data = data.createTask; | ||
try { | ||
let { allTasks } = cache.readQuery({ query: GET_TASKS }); | ||
if (allTasks) { | ||
if (!allTasks.find((task) => task.id === data.id)) { | ||
allTasks.push(data); | ||
} | ||
} else { | ||
allTasks = [data]; | ||
} | ||
cache.writeQuery({ | ||
query: GET_TASKS, | ||
data: { | ||
'allTasks': allTasks | ||
} | ||
}); | ||
} catch (e) { | ||
// ignore as cache may not be populated at this point | ||
} | ||
}, | ||
|
||
updateTask: (cache, { data }) => { | ||
data = data.updateTask; | ||
try { | ||
const { allTasks } = cache.readQuery({ query: GET_TASKS }); | ||
if (allTasks) { | ||
const index = allTasks.findIndex((task) => { | ||
return data.id === task.id; | ||
}); | ||
allTasks[index] = data; | ||
} | ||
cache.writeQuery({ | ||
query: GET_TASKS, | ||
data: { | ||
'allTasks': allTasks | ||
} | ||
}); | ||
} catch (e) { | ||
// ignore as cache may not be populated at this point | ||
} | ||
}, | ||
|
||
deleteTask: (cache, { data }) => { | ||
data = data.deleteTask; | ||
let deletedId; | ||
if (data.optimisticResponse) { | ||
// Map optimistic response field | ||
deletedId = data.id; | ||
} else { | ||
deletedId = data; | ||
} | ||
try { | ||
const { allTasks } = cache.readQuery({ query: GET_TASKS, }); | ||
const newData = allTasks.filter((item) => { | ||
return deletedId !== item.id; | ||
}); | ||
cache.writeQuery({ | ||
query: GET_TASKS, | ||
data: { | ||
'allTasks': newData | ||
} | ||
}); | ||
} catch (e) { | ||
// ignore as cache may not be populated at this point | ||
} | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters