Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 | import { takeLatest, takeEvery, put, call } from 'redux-saga/effects';
import { hackernewsAPI } from '@/services/hackernews';
import {
fetchComments,
fetchCommentsSuccess,
fetchCommentsFailure,
loadCommentItem,
updateCommentItem,
} from '@/store/reducers/comments/comments-actions';
import type { HNItemId } from '@/types/hackernews';
import { getErrorMessage } from '@/helpers/errorHelper';
function* handleFetchComments(action: ReturnType<typeof fetchComments>): Generator {
try {
const parentId = action.payload;
if (!parentId) {
yield put(fetchCommentsSuccess([]));
return;
}
const parentItem = yield call(hackernewsAPI.getItem, parentId);
if (!parentItem || !parentItem.kids || parentItem.kids.length === 0) {
yield put(fetchCommentsSuccess([]));
return;
}
const commentIds = parentItem.kids;
const initialComments = commentIds.map((id: HNItemId) => ({ id }));
yield put(fetchCommentsSuccess(initialComments));
for (const commentId of commentIds) {
yield put(loadCommentItem(commentId));
}
} catch (error) {
const errorMessage = getErrorMessage(error);
yield put(fetchCommentsFailure(errorMessage));
}
}
function* handleLoadCommentItem(action: ReturnType<typeof loadCommentItem>): Generator {
try {
const itemId = action.payload;
if (!itemId) {
return;
}
const item = yield call(hackernewsAPI.getItem, itemId);
if (item) {
yield put(updateCommentItem(item));
if (item.kids && item.kids.length > 0) {
for (const childId of item.kids) {
yield put(loadCommentItem(childId));
}
}
}
} catch (error) {
console.error('Error loading comment item:', error);
}
}
export default function* commentsSaga() {
yield takeLatest(fetchComments.type, handleFetchComments);
yield takeEvery(loadCommentItem.type, handleLoadCommentItem);
}
|