Skip to content

Commit 6bce228

Browse files
committed
feat(keppel): transform component functions to components
1 parent 3d2b78a commit 6bce228

File tree

15 files changed

+337
-277
lines changed

15 files changed

+337
-277
lines changed

plugins/keppel/app/javascript/widgets/app/components/modalHelpers/moveOperation.jsx renamed to plugins/keppel/app/javascript/widgets/app/components/componentHelpers/MoveOperation.jsx

File renamed without changes.

plugins/keppel/app/javascript/widgets/app/components/modalHelpers/moveOperation.test.js renamed to plugins/keppel/app/javascript/widgets/app/components/componentHelpers/MoveOperation.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React from "react"
22
import { render, fireEvent } from "@testing-library/react"
33
import { screen } from "@testing-library/dom"
4-
import { MoveOperation } from "./moveOperation"
4+
import { MoveOperation } from "./MoveOperation"
55

66
describe("MoveOperation", () => {
77
const onMove = jest.fn()
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import React from "react"
2+
3+
export const SelectBox = ({ testID = "", options, value, isEditable, onChange }) => {
4+
const current = options.find((o) => o.value == value)
5+
const trimEllipsis = (str) => (str.substr(-3) === "..." ? str.substr(0, str.length - 3) : str)
6+
7+
if (!isEditable) {
8+
return current ? trimEllipsis(current.label) : ""
9+
}
10+
return (
11+
<select data-testid={testID} value={value} className="form-control select" onChange={onChange}>
12+
{!current && (
13+
<option key="unknown" value={value}>
14+
-- Please select --
15+
</option>
16+
)}
17+
{options.map((o) => (
18+
<option key={o.value} value={o.value}>
19+
{o.label}
20+
</option>
21+
))}
22+
</select>
23+
)
24+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import React from "react"
2+
import { SelectBox } from "./SelectBox"
3+
import { render, screen } from "@testing-library/react"
4+
5+
const options = [
6+
{ value: "1", label: "Option 1" },
7+
{ value: "2", label: "Option 2" },
8+
{ value: "3", label: "Option 3" },
9+
]
10+
describe("SelectBox", () => {
11+
it("displays correct label when isEditable is true", () => {
12+
render(<SelectBox testID="select-box" options={options} value={"0"} isEditable={true} onChange={() => {}} />)
13+
expect(screen.getByText("-- Please select --")).not.toBeNull()
14+
})
15+
it("displays correct label when isEditable is false", () => {
16+
render(<SelectBox testID="select-box" options={options} value={"2"} isEditable={false} onChange={() => {}} />)
17+
expect(screen.queryByText("Option 1")).toBeNull()
18+
expect(screen.getByText("Option 2")).not.toBeNull()
19+
})
20+
})
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import React from "react"
2+
3+
export const TextInput = ({ testid = "", value, isEditable, onChange }) => {
4+
value = value || ""
5+
if (!isEditable) {
6+
if (value === "") {
7+
return <em>Any</em>
8+
}
9+
return <code>{value || ""}</code>
10+
}
11+
return <input data-testid={testid} type="text" className="form-control" onChange={(e) => onChange(e)} />
12+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import React from "react"
2+
import { render, fireEvent, screen } from "@testing-library/react"
3+
import { TextInput } from "./TextInput" // Adjust the import according to your file structure
4+
5+
describe("TextInput", () => {
6+
it("renders input element when isEditable is true", () => {
7+
const mockOnChange = jest.fn()
8+
render(<TextInput testid="input" value="" isEditable={true} onChange={mockOnChange} />)
9+
const inputElement = screen.getByTestId("input")
10+
expect(inputElement).not.toBeNull()
11+
fireEvent.change(inputElement, { target: { value: "New Value" } })
12+
expect(mockOnChange).toHaveBeenCalledTimes(1)
13+
})
14+
15+
it('renders input element when isEditable is false', () => {
16+
render(<TextInput value="" isEditable={false} onChange={() => {}} />)
17+
expect(screen.getByText("Any")).not.toBeNull()
18+
})
19+
})

plugins/keppel/app/javascript/widgets/app/components/gc_policies/edit.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import React from "react"
77
import GCPoliciesEditRow from "./row"
88
import { validatePolicy } from "./utils"
99
import { apiStateIsDeleting } from "../utils"
10-
import { moveItems } from "../modalHelpers/moveOperation"
10+
import { moveItems } from "../componentHelpers/MoveOperation"
1111

1212
export default class GCPoliciesEditModal extends React.Component {
1313
state = {

plugins/keppel/app/javascript/widgets/app/components/gc_policies/row.jsx

Lines changed: 86 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
import { MoveOperation } from "../modalHelpers/moveOperation"
2-
import { makeSelectBox } from "../utils"
3-
import { validatePolicy } from "./utils"
41
import React from "react"
2+
import { MoveOperation } from "../componentHelpers/MoveOperation"
3+
import { SelectBox } from "../componentHelpers/SelectBox"
4+
import { TextInput } from "../componentHelpers/TextInput"
5+
import { validatePolicy } from "./utils"
56

67
const actionOptions = [
78
{ value: "protect", label: "Protect image" },
@@ -45,24 +46,6 @@ const GCPoliciesEditRow = ({
4546
setPolicyAttribute,
4647
removePolicy,
4748
}) => {
48-
const makeTextInput = (attr, value) => {
49-
value = value || ""
50-
if (!isEditable) {
51-
if (value === "") {
52-
return <em>Any</em>
53-
}
54-
return <code>{value || ""}</code>
55-
}
56-
return (
57-
<input
58-
type="text"
59-
value={value}
60-
className="form-control"
61-
onChange={(e) => setPolicyAttribute(index, attr, e.target.value)}
62-
/>
63-
)
64-
}
65-
6649
const makeNumberInput = (attr, value) => {
6750
value = value || 0
6851
if (!isEditable) {
@@ -74,130 +57,147 @@ const GCPoliciesEditRow = ({
7457
value={value.toString()}
7558
className="form-control"
7659
min="1"
77-
onChange={(e) =>
78-
setPolicyAttribute(index, attr, parseInt(e.target.value, 10))
79-
}
60+
onChange={(e) => setPolicyAttribute(index, attr, parseInt(e.target.value, 10))}
8061
/>
8162
)
8263
}
8364

8465
const timeConstraint = policy.time_constraint || {}
8566
const currentTimestampOption = timeConstraint.on || "off"
86-
const currentTimeConstraintOption = timeConstraintOptions
87-
.map((o) => o.value)
88-
.find((key) => key in timeConstraint)
67+
const currentTimeConstraintOption = timeConstraintOptions.map((o) => o.value).find((key) => key in timeConstraint)
8968

9069
const validationError = validatePolicy(policy)
9170

9271
return (
9372
<tr>
9473
{isEditable ? (
9574
<td key="order" className="policy-order-buttons">
96-
<MoveOperation
97-
index={index}
98-
itemCount={policyCount}
99-
onMove={movePolicy}
100-
/>
75+
<MoveOperation index={index} itemCount={policyCount} onMove={movePolicy} />
10176
</td>
10277
) : (
10378
<td key="order" className="policy-order-buttons"></td>
10479
)}
10580
<td>
106-
{makeSelectBox({
107-
isEditable,
108-
options: actionOptions,
109-
value: policy.action,
110-
onChange: (e) => setPolicyAttribute(index, "action", e.target.value),
111-
})}
81+
{
82+
<SelectBox
83+
isEditable={isEditable}
84+
options={actionOptions}
85+
value={policy.action}
86+
onChange={(e) => setPolicyAttribute(index, "action", e.target.value)}
87+
/>
88+
}
11289
</td>
11390
<td className="form-inline">
11491
<div className="policy-matching-rule-line">
115-
{makeSelectBox({
116-
isEditable,
117-
options: repoFilterOptions,
118-
value: policy.ui_hints.repo_filter,
119-
onChange: (e) =>
120-
setPolicyAttribute(index, "repo_filter", e.target.value),
121-
})}
92+
{
93+
<SelectBox
94+
isEditable={isEditable}
95+
options={repoFilterOptions}
96+
value={policy.ui_hints.repo_filter}
97+
onChange={(e) => setPolicyAttribute(index, "repo_filter", e.target.value)}
98+
/>
99+
}
122100
{policy.ui_hints.repo_filter === "on" && (
123101
<>
124102
{" regex "}
125-
{makeTextInput("match_repository", policy.match_repository)}
103+
<TextInput
104+
value={policy.match_repository}
105+
isEditable={isEditable}
106+
onChange={(e) => {
107+
setPolicyAttribute(index, "match_repository", e.target.value)
108+
}}
109+
/>
126110
{(isEditable || policy.except_repository) && (
127111
<>
128112
{" but not regex "}
129-
{makeTextInput("except_repository", policy.except_repository)}
113+
<TextInput
114+
value={policy.except_repository}
115+
isEditable={isEditable}
116+
onChange={(e) => {
117+
setPolicyAttribute(index, "except_repository", e.target.value)
118+
}}
119+
/>
130120
</>
131121
)}
132122
</>
133123
)}
134124
</div>
135125
<div className="policy-matching-rule-line">
136-
{makeSelectBox({
137-
isEditable,
138-
options: tagFilterOptions,
139-
value: policy.ui_hints.tag_filter,
140-
onChange: (e) =>
141-
setPolicyAttribute(index, "tag_filter", e.target.value),
142-
})}
126+
{
127+
<SelectBox
128+
isEditable={isEditable}
129+
options={tagFilterOptions}
130+
value={policy.ui_hints.tag_filter}
131+
onChange={(e) => setPolicyAttribute(index, "tag_filter", e.target.value)}
132+
/>
133+
}
143134
{policy.ui_hints.tag_filter === "on" && (
144135
<>
145136
{" regex "}
146-
{makeTextInput("match_tag", policy.match_tag)}
137+
<TextInput
138+
value={policy.match_tag}
139+
isEditable={isEditable}
140+
onChange={(e) => {
141+
setPolicyAttribute(index, "match_tag", e.target.value)
142+
}}
143+
/>
147144
{(isEditable || policy.except_tag) && (
148145
<>
149146
{" but not regex "}
150-
{makeTextInput("except_tag", policy.except_tag)}
147+
<TextInput
148+
value={policy.match_tag}
149+
isEditable={isEditable}
150+
onChange={(e) => {
151+
setPolicyAttribute(index, "except_tag", e.target.value)
152+
}}
153+
/>
151154
</>
152155
)}
153156
</>
154157
)}
155158
</div>
156159
<div className="policy-matching-rule-line">
157-
{makeSelectBox({
158-
isEditable,
159-
options: timestampOptions,
160-
value: currentTimestampOption,
161-
onChange: (e) =>
162-
setPolicyAttribute(index, "timestamp", e.target.value),
163-
})}
160+
{
161+
<SelectBox
162+
isEditable={isEditable}
163+
options={timestampOptions}
164+
value={currentTimestampOption}
165+
onChange={(e) => setPolicyAttribute(index, "timestamp", e.target.value)}
166+
/>
167+
}
164168
{currentTimestampOption !== "off" && (
165169
<>
166170
{" "}
167-
{makeSelectBox({
168-
isEditable,
169-
options: timeConstraintOptions,
170-
value: currentTimeConstraintOption,
171-
onChange: (e) =>
172-
setPolicyAttribute(index, "time_constraint", e.target.value),
173-
})}
174-
{(currentTimeConstraintOption === "oldest" ||
175-
currentTimeConstraintOption == "newest") && (
171+
{
172+
<SelectBox
173+
isEditable={isEditable}
174+
options={timeConstraintOptions}
175+
value={currentTimeConstraintOption}
176+
onChange={(e) => setPolicyAttribute(index, "time_constraint", e.target.value)}
177+
/>
178+
}
179+
{(currentTimeConstraintOption === "oldest" || currentTimeConstraintOption == "newest") && (
176180
<>
177181
{" "}
178-
{makeNumberInput(
179-
currentTimeConstraintOption,
180-
policy.time_constraint[currentTimeConstraintOption]
181-
)}
182+
{makeNumberInput(currentTimeConstraintOption, policy.time_constraint[currentTimeConstraintOption])}
182183
{" in this repository"}
183184
</>
184185
)}
185-
{(currentTimeConstraintOption === "older_than" ||
186-
currentTimeConstraintOption == "newer_than") && (
186+
{(currentTimeConstraintOption === "older_than" || currentTimeConstraintOption == "newer_than") && (
187187
<>
188188
{" "}
189189
{makeNumberInput(
190190
currentTimeConstraintOption,
191191
policy.time_constraint[currentTimeConstraintOption].value
192192
)}{" "}
193-
{makeSelectBox({
194-
isEditable,
195-
options: timeUnitOptions,
196-
value:
197-
policy.time_constraint[currentTimeConstraintOption].unit,
198-
onChange: (e) =>
199-
setPolicyAttribute(index, "time_unit", e.target.value),
200-
})}
193+
{
194+
<SelectBox
195+
isEditable={isEditable}
196+
options={timeUnitOptions}
197+
value={policy.time_constraint[currentTimeConstraintOption].unit}
198+
onChange={(e) => setPolicyAttribute(index, "time_unit", e.target.value)}
199+
/>
200+
}
201201
</>
202202
)}
203203
</>

0 commit comments

Comments
 (0)