Skip to content

Instantly share code, notes, and snippets.

@bnayae
Created January 12, 2021 17:42
Show Gist options
  • Save bnayae/9e2efc0855371d735bfbba19f42f95c6 to your computer and use it in GitHub Desktop.
Save bnayae/9e2efc0855371d735bfbba19f42f95c6 to your computer and use it in GitHub Desktop.
export const stateOrder = selectorFamily<IOrder, IRecoilId>({
key: 'state-order',
get: (familyKey) => ({ get }) => {
const { color, size, productId, count } = get(
waitForAll({
productId: stateProductId(familyKey),
color: stateColor(familyKey),
size: stateSize(familyKey),
count: stateCount(familyKey),
})
);
return {id: familyKey.id, count, size, color, productId};
},
set: (familyKey) => ({ set, reset }, value) => {
const { journey, id } = familyKey;
// reset (when recoil's value is empty, will discuss later)
if (guardRecoilDefaultValue(value)) {
reset(stateProductId(familyKey));
reset(stateColor(familyKey));
reset(stateSize(familyKey));
reset(stateCount(familyKey));
// remove from tracking
set(stateTracking(journey),
(prv) => [...prv.filter((m) => m !== id)]);
return;
}
// set
set(stateProductId(familyKey), value.productId);
set(stateColor(familyKey), value.color);
set(stateSize(familyKey), value.size);
set(stateCount(familyKey), value.count);
// track
set(stateTracking(journey), (prv) => {
return [...prv, id];
});
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment