Skip to content

Commit 72fefa5

Browse files
committed
move modal to new component, fix tests, better user feedback on comment
1 parent ee8cfc7 commit 72fefa5

File tree

3 files changed

+212
-150
lines changed

3 files changed

+212
-150
lines changed
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
import type React from "react";
2+
import { useCallback, useState } from "react";
3+
4+
import { Text, Modal, Button, Input } from "@cortexapps/plugin-core/components";
5+
import "../baseStyles.css";
6+
7+
interface SonarQubeCommentModalProps {
8+
issueForComment: Record<string, any>;
9+
isModalOpen: boolean;
10+
setIsModalOpen: React.Dispatch<React.SetStateAction<boolean>>;
11+
sonarqubeApiBaseUrl: string;
12+
}
13+
14+
const SonarQubeCommentModal: React.FC<SonarQubeCommentModalProps> = ({
15+
issueForComment,
16+
isModalOpen,
17+
setIsModalOpen,
18+
sonarqubeApiBaseUrl,
19+
}) => {
20+
const [commentText, setCommentText] = useState("");
21+
const [isLoading, setIsLoading] = useState(false);
22+
23+
const sendComment = useCallback(async () => {
24+
const url = `${sonarqubeApiBaseUrl}/api/issues/add_comment`;
25+
26+
const params = new URLSearchParams();
27+
params.append("issue", issueForComment.key);
28+
params.append("text", commentText);
29+
30+
setIsLoading(true);
31+
try {
32+
const response = await fetch(url, {
33+
method: "POST",
34+
headers: {
35+
"Content-Type": "application/x-www-form-urlencoded",
36+
},
37+
body: params.toString(),
38+
});
39+
40+
if (!response.ok) {
41+
console.error("Failed to add comment", response);
42+
}
43+
} catch (err) {
44+
console.error("Failed to add comment", err);
45+
}
46+
setIsLoading(false);
47+
}, [issueForComment, commentText, sonarqubeApiBaseUrl]);
48+
49+
if (!issueForComment || !sonarqubeApiBaseUrl) {
50+
return null;
51+
}
52+
53+
return (
54+
<Modal
55+
isOpen={isModalOpen}
56+
toggleModal={(): void => {
57+
setCommentText("");
58+
setIsModalOpen((prev) => !prev);
59+
}}
60+
title="Comment"
61+
>
62+
<>
63+
<Text>
64+
Commenting on issue &nbsp;
65+
<i>{issueForComment.message}</i>
66+
</Text>
67+
<br />
68+
<Input
69+
placeholder="Comment"
70+
value={commentText}
71+
disabled={isLoading}
72+
onChange={(event: React.ChangeEvent<HTMLInputElement>) => {
73+
setCommentText(event.target.value);
74+
}}
75+
/>
76+
<br />
77+
<Button
78+
disabled={!commentText || isLoading}
79+
onClick={(): void => {
80+
void sendComment().then(() => {
81+
setCommentText("");
82+
setIsModalOpen(false);
83+
});
84+
}}
85+
>
86+
Submit
87+
</Button>
88+
{isLoading && (
89+
<>
90+
<br />
91+
<Text>
92+
Adding comment to issue <i>{issueForComment?.key || ""}</i>...
93+
</Text>
94+
</>
95+
)}
96+
</>
97+
</Modal>
98+
);
99+
};
100+
101+
export default SonarQubeCommentModal;

plugins/sonarqube-issues/src/components/SonarqubeIssues.test.tsx

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,6 @@ describe("Issues", () => {
182182
await waitFor(() => {
183183
expect(screen.queryByText("Loading")).not.toBeInTheDocument();
184184
});
185-
// expect(screen.queryByText("useless")).toBeInTheDocument();
186185
const majorElements = screen.queryAllByText("major");
187186
expect(majorElements.length).toEqual(3);
188187
const commentElements = screen.queryAllByText("Comment");
@@ -196,16 +195,13 @@ describe("Issues", () => {
196195
return await Promise.resolve(JSON.stringify({ issues: [] }));
197196
}
198197
);
198+
199199
render(<SonarqubeIssues entityYaml={serviceYaml} />);
200-
expect(screen.queryByText("Loading")).toBeInTheDocument();
201200

202201
await waitFor(() => {
203-
expect(screen.queryByText("Loading")).not.toBeInTheDocument();
202+
expect(
203+
screen.queryByText(/We could not find any Sonarqube issues/)
204+
).toBeInTheDocument();
204205
});
205-
expect(
206-
screen.queryByText(
207-
"We could not find any Sonarqube issues associated with this entity"
208-
)
209-
).toBeInTheDocument();
210206
});
211207
});

0 commit comments

Comments
 (0)