File size: 2,229 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# Reader Hooks
This directory contains React hooks for managing the list of recommend feeds from the current user.
## Overview
### `useFeedRecommendationsMutation`
A custom hook for managing recommended sites state with optimistic updates and automatic error recovery. Note that sites are actually "feeds."
## Real-World Example
```typescript
const SiteSubscriptionRow = ( { feed_ID: feedId, /* other props */ } ) => {
const { isRecommended, toggleRecommended } = useFeedRecommendationsMutation( Number( feedId ) );
return (
<div className="subscription-row">
{/* Site info */}
<Toggle
checked={ isRecommended }
onChange={ toggleRecommended }
label="Recommended blog"
/>
</div>
);
};
```
## API Reference
### `useFeedRecommendationsMutation(feedId: number)`
**Parameters:**
- `feedId: number` - The feed ID to manage recommendations for
**Returns:** `useFeedRecommendationsMutationResult`
```typescript
interface useFeedRecommendationsMutationResult {
isRecommended: boolean; // Current recommendation state (from Redux)
isUpdating: boolean; // Whether operation is in progress
canToggle: boolean; // Whether toggle is allowed
toggleRecommended: () => void; // Function to toggle state
}
```
## Error Recovery Flow
The implementation uses the established WordPress.com `bypassDataLayer` pattern:
### Successful Operation
```
1. User toggles → READER_LIST_ITEM_ADD_FEED dispatched
2. Reducer immediately adds feed (optimistic update)
3. API succeeds → READER_LIST_ITEM_ADD_FEED_RECEIVE ensures feed is in list
4. UI shows new state ✅
```
### Failed Operation with Automatic Recovery
```
1. User toggles → READER_LIST_ITEM_ADD_FEED dispatched
2. Reducer immediately adds feed (optimistic update)
3. API fails → Data layer dispatches bypassDataLayer(READER_LIST_ITEM_DELETE_FEED)
4. Reducer removes feed, reverting to original state
5. UI automatically reverts toggle to previous position ✅
6. Error notice shown to user
```
## Testing
### Running Tests
```bash
yarn test-client client/data/reader/use-recommended-site/index.test.ts
yarn test-client client/state/reader/lists/test/reducer.js
```
## Related Files
See `client/state/reader/lists/`
|