Skip to content

Commit

Permalink
UserAddonForm: Add validation that required addons are enabled
Browse files Browse the repository at this point in the history
  • Loading branch information
jlandowner committed Jul 1, 2024
1 parent dd991ab commit 1921979
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 24 deletions.
49 changes: 37 additions & 12 deletions web/dashboard-ui/src/views/organisms/UserActionDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,7 @@ export const UserCreateConfirmDialog: React.VFC<{
title="Create?"
user={user}
defaultOpenUserAddon={true}
onClose={onClose}
actions={
<DialogActions>
<Button onClick={onClose} color="primary">
Expand Down Expand Up @@ -404,19 +405,43 @@ export const UserCreateDialog: React.VFC<{ onClose: () => void }> = ({
<form
onSubmit={handleSubmit((inp: Inputs) => {
console.log(inp);
const userAddons = inp.addons
.filter((v) => v.enable || v.template.isDefaultUserAddon)
.map((inpAddon) => {
const vars: { [key: string]: string } = {};
inpAddon.vars.forEach((v, i) => {
vars[inpAddon.template.requiredVars?.[i].varName!] = v;
});
return {
template: inpAddon.template.name,
vars: vars,
clusterScoped: inpAddon.template.isClusterScope,
};

const enabled = inp.addons.filter(
(v) => v.enable || v.template.isDefaultUserAddon
);

// check required addons are enabled
for (const hasDeps of enabled.filter(
(v) => v.template.requiredUseraddons.length > 0
)) {
for (const req of hasDeps.template.requiredUseraddons) {
if (enabled.findIndex((v) => v.template.name === req) < 0) {
setError(
`addons.${inp.addons.findIndex(
(v) => v.template.name === hasDeps.template.name
)}.enable`,
{
message: `Required: ${hasDeps.template.requiredUseraddons.join(
", "
)}`,
}
);
return;
}
}
}

const userAddons = enabled.map((inpAddon) => {
const vars: { [key: string]: string } = {};
inpAddon.vars.forEach((v, i) => {
vars[inpAddon.template.requiredVars?.[i].varName!] = v;
});
return {
template: inpAddon.template.name,
vars: vars,
clusterScoped: inpAddon.template.isClusterScope,
};
});
const protoUserAddons = userAddons.map((ua) => new UserAddon(ua));
console.log("protoUserAddons", protoUserAddons);

Expand Down
49 changes: 37 additions & 12 deletions web/dashboard-ui/src/views/organisms/UserAddonsChangeDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ export const UserAddonChangeDialog: React.FC<{
watch,
control,
formState: { errors },
setError,
} = useForm<Inputs>({
defaultValues: {},
});
Expand Down Expand Up @@ -84,19 +85,43 @@ export const UserAddonChangeDialog: React.FC<{
<form
onSubmit={handleSubmit(async (inp: Inputs) => {
console.log(inp);
const userAddons = inp.addons
.filter((v) => v.enable || v.template.isDefaultUserAddon)
.map((inpAddon) => {
const vars: { [key: string]: string } = {};
inpAddon.vars.forEach((v, i) => {
vars[inpAddon.template.requiredVars?.[i].varName!] = v;
});
return {
template: inpAddon.template.name,
vars: vars,
clusterScoped: inpAddon.template.isClusterScope,
};

const enabled = inp.addons.filter(
(v) => v.enable || v.template.isDefaultUserAddon
);

// check required addons are enabled
for (const hasDeps of enabled.filter(
(v) => v.template.requiredUseraddons.length > 0
)) {
for (const req of hasDeps.template.requiredUseraddons) {
if (enabled.findIndex((v) => v.template.name === req) < 0) {
setError(
`addons.${inp.addons.findIndex(
(v) => v.template.name === hasDeps.template.name
)}.enable`,
{
message: `Required: ${hasDeps.template.requiredUseraddons.join(
", "
)}`,
}
);
return;
}
}
}

const userAddons = enabled.map((inpAddon) => {
const vars: { [key: string]: string } = {};
inpAddon.vars.forEach((v, i) => {
vars[inpAddon.template.requiredVars?.[i].varName!] = v;
});
return {
template: inpAddon.template.name,
vars: vars,
clusterScoped: inpAddon.template.isClusterScope,
};
});
const protoUserAddons = userAddons.map((ua) => new UserAddon(ua));
console.log("protoUserAddons", protoUserAddons);

Expand Down

0 comments on commit 1921979

Please sign in to comment.