Skip to content

Instantly share code, notes, and snippets.

@tfiechowski
Last active January 7, 2021 21:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tfiechowski/467fcfac319ba3330570ec2122f2dfa9 to your computer and use it in GitHub Desktop.
Save tfiechowski/467fcfac319ba3330570ec2122f2dfa9 to your computer and use it in GitHub Desktop.
export function usePhotos({}) {
// ...
const removePhotosLockedFlag = useCallback(
(_pendingUpdates) =>
setPhotos((_photos) =>
_photos.map((photo) => {
const updatedItem = _pendingUpdates[photo.id];
if (updatedItem) {
return Object.assign({}, photo, {
[LOCKED_FLAG_KEY]: false,
});
}
return photo;
})
),
[setPhotos]
);
const revertPhotosToOriginalState = useCallback(
(originalPhotos) =>
setPhotos((_photos) =>
_photos.map((item) => {
const originalItem = originalPhotos.find((photo) => photo.id === item.id) || item;
return Object.assign({}, originalItem, { [LOCKED_FLAG_KEY]: false });
})
),
[setPhotos]
);
const applyUpdatesToPhotos = useCallback(
(_pendingUpdates) =>
setPhotos((_photos) =>
_photos.map((photo) => {
const batchUpdateItem = _pendingUpdates[photo.id];
if (batchUpdateItem) {
// Locked flag will indicate that item is being processed now
// (sent in an API request and waiting for a response)
return Object.assign({}, photo, batchUpdateItem, {
[LOCKED_FLAG_KEY]: true,
});
}
return photo;
})
),
[setPhotos]
);
const clearPendingUpdates = useCallback(() => setPendingUpdates({}), [setPendingUpdates]);
// ...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment