Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(docgen-sidebar): add polling to tags loading in dogen sidebar #3858

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions src/elements/content-sidebar/DocGenSidebar/DocGenSidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,20 @@ const DocGenSidebar = ({ getDocGenTags }: Props) => {
return jsonPathsMap;
};

const loadTags = async () => {

const loadTags = async (attempts = 10) => {
if(attempts <= 0){
return
}
setIsLoading(true);
try {
const response: DocGenTemplateTagsResponse = await getDocGenTags();
if (response && !!response.data) {
if(response.message){
abdel-ships-it marked this conversation as resolved.
Show resolved Hide resolved
loadTags(attempts - 1);
rustam-e marked this conversation as resolved.
Show resolved Hide resolved
} else if (response && !!response.data) {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore

const { data } = response || [];
rustam-e marked this conversation as resolved.
Show resolved Hide resolved

// anything that is not an image tag for this view is treated as a text tag
Expand All @@ -107,20 +114,23 @@ const DocGenSidebar = ({ getDocGenTags }: Props) => {
imageTree: tagsToJsonPaths(imageTags),
});
setHasError(false);
setIsLoading(false);
} else {
setHasError(true);
setIsLoading(false);
abdel-ships-it marked this conversation as resolved.
Show resolved Hide resolved
}
} catch (error) {
setHasError(true);
setIsLoading(false);
}
setIsLoading(false);
};

}

React.useEffect(() => {
loadTags();
loadTags(10);
rustam-e marked this conversation as resolved.
Show resolved Hide resolved
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);


const isEmpty = tags.image.length + tags.text.length === 0;

return (
Expand Down
1 change: 1 addition & 0 deletions src/elements/content-sidebar/DocGenSidebar/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export type DocGenTag = {

export type DocGenTemplateTagsResponse = {
data?: DocGenTag[];
message?: string;
pagination?: {
previousMarker?: string;
nextMarker?: string;
Expand Down
14 changes: 13 additions & 1 deletion src/elements/content-sidebar/__tests__/DocGenSidebar.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ const docGenSidebarProps = {
};

const noTagsMock = jest.fn().mockReturnValue(Promise.resolve({ data: [] }));
const processingTagsMock = jest.fn().mockReturnValue(Promise.resolve({
"message": "Processing tags for this file."
}));
const errorTagsMock = jest.fn().mockRejectedValue([]);
const noDataMock = jest.fn().mockReturnValue(Promise.resolve({}));

Expand Down Expand Up @@ -77,6 +80,15 @@ describe('elements/content-sidebar/DocGenSidebar', () => {
});
});


test('should re-trigger loadTags if the tempalte is still processing', async () => {
renderComponent({
getDocGenTags: processingTagsMock,
});

await waitFor(() => expect(processingTagsMock).toHaveBeenCalledTimes(10));
});

test('should re-trigger getDocGenTags on click on refresh button', async () => {
renderComponent({
getDocGenTags: errorTagsMock,
Expand All @@ -88,7 +100,7 @@ describe('elements/content-sidebar/DocGenSidebar', () => {
const refreshButton = screen.getByRole('button', { name: 'Process document' });
fireEvent.click(refreshButton);

await waitFor(() => expect(errorTagsMock).toBeCalledTimes(2));
await waitFor(() => expect(errorTagsMock).toHaveBeenCalledTimes(2));
});

test('should handle undefined data', async () => {
Expand Down
Loading