Skip to content

Commit

Permalink
Parse discussion response for top comments (#10374)
Browse files Browse the repository at this point in the history
* feat(discussion): validate top comments

* fix(discussion): proper type for `topPicks` mock
  • Loading branch information
mxdvl authored Jan 29, 2024
1 parent 50d8c24 commit a1b4a69
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 15 deletions.
6 changes: 4 additions & 2 deletions dotcom-rendering/fixtures/manual/topPicks.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import type { GetDiscussionSuccess } from '../../src/types/discussion';

export const topPicks = {
status: 'ok',
page: 1,
currentPage: 1,
pages: 1,
pageSize: 50,
orderBy: 'oldest',
Expand Down Expand Up @@ -65,4 +67,4 @@ export const topPicks = {
},
],
},
};
} satisfies GetDiscussionSuccess;
13 changes: 6 additions & 7 deletions dotcom-rendering/src/components/Discussion/Comments.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -153,15 +153,14 @@ export const Comments = ({
} else return;
}, [expanded, comments.length]);

//todo: parse this properly
useEffect(() => {
const fetchPicks = async () => {
const json = await getPicks(shortUrl);
if (json !== undefined) {
setPicks(json);
void getPicks(shortUrl).then((result) => {
if (result.kind === 'error') {
console.error(result.error);
return;
}
};
void fetchPicks();
setPicks(result.value);
});
}, [shortUrl]);

/**
Expand Down
19 changes: 13 additions & 6 deletions dotcom-rendering/src/lib/discussionApi.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -231,24 +231,31 @@ export const reply =
return parseCommentResponse(jsonResult.value);
};

//todo: come back and parse the response properly and set a proper return type for the error case
export const getPicks = async (
shortUrl: string,
): Promise<CommentType[] | undefined> => {
): Promise<Result<GetDiscussionError, CommentType[]>> => {
const url =
joinUrl(options.baseUrl, 'discussion', shortUrl, 'topcomments') +
objAsParams(defaultParams);

const resp = await fetch(url, {
const jsonResult = await fetchJSON(url, {
headers: {
...options.headers,
},
});

const json = await resp.json();
if (jsonResult.kind === 'error') return jsonResult;

// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access -- YOLO
return json.discussion.comments;
const result = safeParse(discussionApiResponseSchema, jsonResult.value);

if (!result.success) {
return { kind: 'error', error: 'ParsingError' };
}
if (result.output.status === 'error') {
return { kind: 'error', error: 'ApiError' };
}

return { kind: 'ok', value: result.output.discussion.comments };
};

export const reportAbuse = async ({
Expand Down

0 comments on commit a1b4a69

Please sign in to comment.