Last active
January 7, 2021 21:05
-
-
Save tfiechowski/f4af93c04b2cd142275e520d5317a436 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 isUpdateNeeded(original, itemUpdate) { | |
const fieldsToOmit = [LOCKED_FLAG_KEY]; | |
const keysToCompare = Object.keys(itemUpdate).filter( | |
(key) => ![...fieldsToOmit, "id"].includes(key) | |
); | |
return !isEqual( | |
pick(original, keysToCompare), | |
pick(itemUpdate, keysToCompare) | |
); | |
} | |
export function usePhotos({}) { | |
// ... | |
const getItemsToResetAndUpdate = useCallback((itemsUpdates, originalPhotos) => { | |
const originalPhotosLookup = fromPairs( | |
originalPhotos.map((originalPhoto) => [originalPhoto.id, originalPhoto]) | |
); | |
function getOriginalPhoto(id) { | |
return originalPhotosLookup[id]; | |
} | |
const [toUpdate, toReset] = partition(itemsUpdates, (itemUpdate) => { | |
const originalPhoto = getOriginalPhoto(itemUpdate.id); | |
return isUpdateNeeded(originalPhoto, itemUpdate); | |
}); | |
return { | |
toReset: toReset.map((item) => item.id), | |
toUpdate: fromPairs(toUpdate.map((item) => [item.id, item])), | |
}; | |
}, []); | |
// ... | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment