Skip to content
18 changes: 10 additions & 8 deletions ui-cra/src/components/Templates/Form/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ const toPayload = (
infraCredential: any,
templateName: string,
templateNamespace: string,
templateKind: string,
updatedProfiles: ProfilesIndex,
): CreatePullRequestRequest => {
const { parameterValues } = formData;
Expand All @@ -238,10 +239,11 @@ const toPayload = (
commitMessage: formData.commitMessage,
credentials: infraCredential,
templateName,
templateNamespace: templateNamespace,
templateNamespace,
parameterValues,
kustomizations: getKustomizations(formData),
values: encodedProfiles(updatedProfiles),
templateKind,
};
};

Expand All @@ -254,7 +256,7 @@ interface ResourceFormProps {
const ResourceForm: FC<ResourceFormProps> = ({ template, resource }) => {
const callbackState = useCallbackState();
const classes = useStyles();
const { renderTemplate, addCluster } = useTemplates();
const { renderTemplate, addResource } = useTemplates();
const { data } = useListConfig();
const repositoryURL = data?.repositoryURL || '';
const random = useMemo(() => Math.random().toString(36).substring(7), []);
Expand Down Expand Up @@ -392,19 +394,19 @@ const ResourceForm: FC<ResourceFormProps> = ({ template, resource }) => {
setNotifications,
]);

const handleAddCluster = useCallback(() => {
const handleAddResource = useCallback(() => {
const payload = toPayload(
formData,
infraCredential,
template.name,
template.namespace!,
template.templateKind,
updatedProfiles,
);
setLoading(true);
return addCluster(
return addResource(
payload,
getProviderToken(formData.provider as GitProvider),
template.templateKind,
)
.then(response => {
setPRPreview(null);
Expand Down Expand Up @@ -438,7 +440,7 @@ const ResourceForm: FC<ResourceFormProps> = ({ template, resource }) => {
.finally(() => setLoading(false));
}, [
updatedProfiles,
addCluster,
addResource,
formData,
infraCredential,
setPRPreview,
Expand Down Expand Up @@ -563,7 +565,7 @@ const ResourceForm: FC<ResourceFormProps> = ({ template, resource }) => {
) : (
<div className="create-cta">
<Button
onClick={event => validateFormData(event, handleAddCluster)}
onClick={event => validateFormData(event, handleAddResource)}
disabled={!enableCreatePR}
>
CREATE PULL REQUEST
Expand All @@ -587,7 +589,7 @@ const ResourceForm: FC<ResourceFormProps> = ({ template, resource }) => {
showAuthDialog,
setUpdatedProfiles,
handlePRPreview,
handleAddCluster,
handleAddResource,
updatedProfiles,
previewLoading,
loading,
Expand Down
25 changes: 8 additions & 17 deletions ui-cra/src/hooks/templates.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,29 +36,20 @@ const useTemplates = () => {

const renderTemplate = api.RenderTemplate;

const addCluster = useCallback(
({ ...data }, token: string, templateKind: string) => {
setLoading(true);
return request(
'POST',
templateKind === 'GitOpsTemplate'
? '/v1/tfcontrollers'
: '/v1/clusters',
{
body: JSON.stringify(data),
headers: new Headers({ 'Git-Provider-Token': `token ${token}` }),
},
).finally(() => setLoading(false));
},
[],
);
const addResource = useCallback(({ ...data }, token: string) => {
setLoading(true);
return request('POST', '/v1/clusters', {
body: JSON.stringify(data),
headers: new Headers({ 'Git-Provider-Token': `token ${token}` }),
}).finally(() => setLoading(false));
}, []);

return {
isLoading,
templates,
loading,
getTemplate,
addCluster,
addResource,
renderTemplate,
};
};
Expand Down