Skip to content

Commit

Permalink
Refresh Service Bindings list after restoring a secret
Browse files Browse the repository at this point in the history
  • Loading branch information
szwedm committed Sep 2, 2024
1 parent a788f70 commit 39e1746
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 65 deletions.
4 changes: 2 additions & 2 deletions ui/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "smuiii",
"version": "0.1.0",
"name": "btp-manager-ui",
"version": "0.0.1",
"private": true,
"dependencies": {
"@reduxjs/toolkit": "^2.2.3",
Expand Down
143 changes: 83 additions & 60 deletions ui/src/components/CreateBindingForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,71 +36,94 @@ function CreateBindingForm(props: any) {
createdBinding.service_instance_id = props.instanceId ?? ""

setLoading(true)
if (props.buttonPressed) {
axios
.put(api("service-bindings") + "/" + createdBinding.id, {
name: createdBinding.name,
service_instance_id: createdBinding.service_instance_id,
secret_name: createdBinding.secret_name,
secret_namespace: createdBinding.secret_namespace
}, {
params: {
sm_secret_name: props.secret.name,
sm_secret_namespace: props.secret.namespace
axios
.post<ServiceInstanceBinding>(api("service-bindings"), {
name: createdBinding.name,
service_instance_id: createdBinding.service_instance_id,
secret_name: createdBinding.secret_name,
secret_namespace: createdBinding.secret_namespace
}, {
params:
{
sm_secret_name: props.secret!!.name,
sm_secret_namespace: props.secret!!.namespace
}
})
.then((response) => {
props.onCreate(response.data);
setError(undefined);
setLoading(false);
})
.catch((error: ApiError) => {
setLoading(false);
setError(error);
setSuccess("");
});
} else {
axios
.post<ServiceInstanceBinding>(api("service-bindings"), {
name: createdBinding.name,
service_instance_id: createdBinding.service_instance_id,
secret_name: createdBinding.secret_name,
secret_namespace: createdBinding.secret_namespace
}, {
params:
{
sm_secret_name: props.secret!!.name,
sm_secret_namespace: props.secret!!.namespace
}
})
.then((response) => {

// propagate the created binding
props.onCreate(response.data);

// reset binding
const binding = new ServiceInstanceBinding()
binding.name = props.instanceName
binding.secret_name = props.instanceName + suffix
binding.secret_namespace = "default"

setSuccess("Item with id " + response.data.name + " created.");
setCreatedBinding(binding);
setError(undefined);
setLoading(false);
})
.catch((error: ApiError) => {
setLoading(false);
setError(error);
setSuccess("");
});
}
})
.then((response) => {

// propagate the created binding
props.onCreate(response.data);

// reset binding
const binding = new ServiceInstanceBinding()
binding.name = props.instanceName
binding.secret_name = props.instanceName + suffix
binding.secret_namespace = "default"

setSuccess("Item with id " + response.data.name + " created.");
setCreatedBinding(binding);
setError(undefined);
setLoading(false);
})
.catch((error: ApiError) => {
setLoading(false);
setError(error);
setSuccess("");
});

e.preventDefault();
e.stopPropagation();
return false;
}

const handleSecretRestore = (e: any): boolean => {
e.preventDefault();

if (e.nativeEvent.submitter.localName === "ui5-multi-input") {
e.preventDefault()
return false;
}

createdBinding.service_instance_id = props.instanceId ?? ""

setLoading(true)
if (props.buttonPressed) {
axios
.put(api("service-bindings") + "/" + createdBinding.id, {
name: createdBinding.name,
service_instance_id: createdBinding.service_instance_id,
secret_name: createdBinding.secret_name,
secret_namespace: createdBinding.secret_namespace
}, {
params: {
sm_secret_name: props.secret.name,
sm_secret_namespace: props.secret.namespace
}
})
.then((response) => {
const binding = new ServiceInstanceBinding()
binding.name = props.instanceName
binding.secret_name = props.instanceName + suffix
binding.secret_namespace = "default"

props.onSecretRestore();

setSuccess("Item with id " + response.data.name + " updated.");
setCreatedBinding(binding);
setError(undefined);
setLoading(false);
})
.catch((error: ApiError) => {
setLoading(false);
setError(error);
setSuccess("");
});
}

e.preventDefault();
return false;
}

useEffect(() => {
setLoading(true)
if (!Ok(props.instanceId)) {
Expand Down Expand Up @@ -131,7 +154,7 @@ function CreateBindingForm(props: any) {
setCreatedBinding(createdBinding)
}

}, [createdBinding, props.instanceId, props.instanceName, props.onCreate, props.secret]);
}, [createdBinding, suffix, props.instanceId, props.instanceName, props.onCreate, props.secret, props.binding, props.buttonPressed]);

const renderData = () => {

Expand All @@ -146,7 +169,7 @@ function CreateBindingForm(props: any) {
if (props.buttonPressed) {
return (
<ui5.Form
onSubmit={handleCreate}>
onSubmit={handleSecretRestore}>
<ui5.FormItem>
<StatusMessage error={error ?? undefined} success={success} />
</ui5.FormItem>
Expand Down
9 changes: 8 additions & 1 deletion ui/src/components/ServiceBindingsList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,14 @@ const ServiceBindingsList = forwardRef((props: any, ref) => {
const newbindings = new ServiceInstanceBindings();
newbindings.items = bindings?.items ?? [];
setServiceInstanceBindings(newbindings);
}
},

refresh() {
console.log(bindings)
const newbindings = new ServiceInstanceBindings();
newbindings.items = bindings?.items ?? [];
setServiceInstanceBindings(newbindings);
}

}));

Expand Down
9 changes: 7 additions & 2 deletions ui/src/components/ServiceInstancesDetailsView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const ServiceInstancesDetailsView = forwardRef((props: any, ref) => {
const [error] = useState<ApiError>();

const [instance, setInstance] = useState<ServiceInstance>();
const [binding, setBinding] = useState<ServiceInstanceBinding>();
const [binding, setBinding] = useState<ServiceInstanceBinding>(new ServiceInstanceBinding());
const [secretRestoreButtonPressed, setSecretRestoreButtonPressed] = useState(false);
const dialogRef = useRef(null);
const listRef = useRef(null);
Expand All @@ -35,6 +35,11 @@ const ServiceInstancesDetailsView = forwardRef((props: any, ref) => {
setSecretRestoreButtonPressed(pressed)
}

function onSecretRestore() {
// @ts-ignore
listRef.current.refresh()
}

useEffect(() => {
setLoading(true);
if (!Ok(props.instance)) {
Expand Down Expand Up @@ -86,7 +91,7 @@ const ServiceInstancesDetailsView = forwardRef((props: any, ref) => {
</ui5.Panel>

<ui5.Panel headerLevel="H2" headerText="Create Binding">
<CreateBindingForm secret={secret} binding={binding} onCreate={(binding: ServiceInstanceBinding) => onBindingAdded(binding)} instanceId={props.instance.id} instanceName={props.instance.name} buttonPressed={secretRestoreButtonPressed}/>
<CreateBindingForm secret={secret} binding={binding} onCreate={(binding: ServiceInstanceBinding) => onBindingAdded(binding)} instanceId={props.instance.id} instanceName={props.instance.name} buttonPressed={secretRestoreButtonPressed} onSecretRestore={onSecretRestore} />
</ui5.Panel>
</>

Expand Down

0 comments on commit 39e1746

Please sign in to comment.