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

RemoteEdit: start to use react-hook-form #116

Merged
merged 1 commit into from
Dec 12, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
16 changes: 16 additions & 0 deletions src/main/webui/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src/main/webui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"react": "^18.2.0",
"react-bootstrap": "^2.9.1",
"react-dom": "^18.2.0",
"react-hook-form": "^7.48.2",
"react-json-pretty": "^1.7.9",
"react-router": "^6.16.0",
"react-router-bootstrap": "^0.26.2",
Expand Down
9 changes: 8 additions & 1 deletion src/main/webui/src/app/components/ComponentConstants.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,12 @@ const hostedOptionLegend = [

const STORE_API_BASE_URL = "/api/admin/stores";

const PATTERNS={
URL: /^https?:\/\/(?:www\.)?[-a-zA-Z0-9@:%._+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b(?:[-a-zA-Z0-9()@:%_+.~#?&/=]*)$/u
};

export {remoteOptionLegend, hostedOptionLegend, STORE_API_BASE_URL};

export {remoteOptionLegend,
hostedOptionLegend,
STORE_API_BASE_URL,
PATTERNS};
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@ import {PropTypes} from 'prop-types';
import {jsonRest} from '#utils/RestClient.js';
import {Utils} from '#utils/AppUtils';

export const PackageTypeSelect = ({packageType,vauleChangeHandler}) =>{
export const PackageTypeSelect = ({register, formErrors}) =>{
const [state, setState] = useState({
pkgTypes: []
});
const [selected, setSelected] = useState();

useEffect(()=>{
const fetchPkgTypes = async () =>{
Expand All @@ -37,18 +38,23 @@ export const PackageTypeSelect = ({packageType,vauleChangeHandler}) =>{
fetchPkgTypes();
}, []);

const selectedValue = packageType || "maven";
const onChangeHandler = vauleChangeHandler || (()=>{});
let registered = {};
if(register){
registered = register("packageType", {required: true});
}
return <span>
<select value={selectedValue} onChange={onChangeHandler}>
<select role="pkgTypeSel" value={selected}
onChange={e => setSelected(e.target.value)} {...registered}>
<option value=""></option>
{
state.pkgTypes.map(type => <option key={`pkgType:${type}`} value={type}>{type}</option>)
}
</select>
</select>{' '}
{formErrors && formErrors.packageType?.type === "required" && <span className="alert">Package Type is required</span>}
</span>;
};

PackageTypeSelect.propTypes = {
packageType: PropTypes.string,
vauleChangeHandler: PropTypes.func
register: PropTypes.func,
formErrors: PropTypes.object
};
Original file line number Diff line number Diff line change
Expand Up @@ -41,36 +41,36 @@ describe('PackageTypeSelect tests', () => {
expect(screen.getByRole("option", {name: "npm"})).toBeInTheDocument();
expect(screen.getByRole("option", {name: "generic-http"})).toBeInTheDocument();

expect(screen.getByRole("option", {name: "maven"}).selected).toBe(true);
expect(screen.getByRole("combobox")).toHaveValue("maven");
expect(screen.getByRole("option", {name: ""}).selected).toBe(true);
expect(screen.getByRole("pkgTypeSel")).toHaveValue("");
});
});

it("Verify PackageTypeSelect for npm selected", async ()=>{
render(<PackageTypeSelect packageType="npm" />);
await waitFor(()=>{
expect(screen.getByRole("option", {name: "maven"})).toBeInTheDocument();
expect(screen.getByRole("option", {name: "npm"})).toBeInTheDocument();
expect(screen.getByRole("option", {name: "generic-http"})).toBeInTheDocument();
// it("Verify PackageTypeSelect for npm selected", async ()=>{
// render(<PackageTypeSelect packageType="npm" />);
// await waitFor(()=>{
// expect(screen.getByRole("option", {name: "maven"})).toBeInTheDocument();
// expect(screen.getByRole("option", {name: "npm"})).toBeInTheDocument();
// expect(screen.getByRole("option", {name: "generic-http"})).toBeInTheDocument();

expect(screen.getByRole("combobox")).toHaveValue("npm");
expect(screen.getByRole("option", {name: "npm"}).selected).toBe(true);
});
});
// const pkgSelect = screen.getByRole("pkgTypeSel");
// expect(pkgSelect).toHaveValue("npm");
// expect(screen.getByRole("option", {name: "npm"}).selected).toBe(true);
// });
// });

it("Verify PackageTypeSelect for value change", async ()=>{
const {selectOptions} = userEvent.setup();
let value = "";
const vauleChangeHandler = e => {
value = e.target.value;
};
render(<PackageTypeSelect vauleChangeHandler={vauleChangeHandler}/>);
render(<PackageTypeSelect />);
expect(value).toBe("");
await waitFor(() => {
expect(screen.getByRole("option", {name: "maven"}).selected).toBe(true);
expect(screen.getByRole("pkgTypeSel")).toHaveValue("");
});
await waitFor(() => {
expect(screen.getByRole("option", {name: "npm"})).toBeInTheDocument();
selectOptions(screen.getByRole("combobox"), "npm");
expect(value).toBe("npm");
selectOptions(screen.getByRole("pkgTypeSel"), "npm");
expect(screen.getByRole("pkgTypeSel")).toHaveValue("npm");
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@ StoreViewControlPanel.propTypes={
store: PropTypes.object
};

const StoreEditControlPanel = ({mode, store}) =>{
const StoreEditControlPanel = ({mode, store, handleSubmit}) =>{
const navigate = useNavigate();
const handleSave = () => {
const save = () => {
const saveUrl = `${STORE_API_BASE_URL}/${store.packageType}/${store.type}/${store.name}`;
const saveStore = async () => {
let response = {};
Expand Down Expand Up @@ -115,6 +115,16 @@ const StoreEditControlPanel = ({mode, store}) =>{
}
};

let handleSave = () => save();
if(handleSubmit && typeof handleSubmit === 'function'){
// console.log(handleSubmit);
handleSave = handleSubmit(data=>{
data.disabled = !data.enabled;
data.enabled = undefined;
Utils.rewriteTargetObject(data, store);
save();
});
}
return <div className="cp-row">
<button name="save" onClick={handleSave} className="cp-button">Save</button>{' '}
<button name="cancel" onClick={handleCancel} className="cp-button">Cancel</button>{' '}
Expand All @@ -127,7 +137,8 @@ const StoreEditControlPanel = ({mode, store}) =>{
};
StoreEditControlPanel.propTypes={
mode: PropTypes.string,
store: PropTypes.object
store: PropTypes.object,
handleSubmit: PropTypes.func
};

export {StoreViewControlPanel, StoreEditControlPanel};
Loading
Loading