Last active
January 7, 2021 21:04
-
-
Save tfiechowski/467fcfac319ba3330570ec2122f2dfa9 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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