Skip to content
This repository has been archived by the owner on Jun 14, 2019. It is now read-only.

Refactoring extension-related code to use hooks #419

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
107 changes: 0 additions & 107 deletions app/ui-react/packages/api/src/WithExtensionHelpers.tsx

This file was deleted.

2 changes: 1 addition & 1 deletion app/ui-react/packages/api/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export * from './Stream';
export * from './SyndesisFetch';
export * from './useApiConnector';
export * from './useApiProvider';
export * from './useExtensionHelpers';
export * from './useIntegrationHelpers';
export * from './WithActionDescriptor';
export * from './WithActivities';
Expand All @@ -25,7 +26,6 @@ export * from './WithConnectors';
export * from './WithEnvironmentHelpers';
export * from './WithEnvironments';
export * from './WithExtension';
export * from './WithExtensionHelpers';
export * from './WithExtensionIntegrations';
export * from './WithExtensions';
export * from './WithFilterOptions';
Expand Down
34 changes: 34 additions & 0 deletions app/ui-react/packages/api/src/useExtension.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import * as React from 'react';
import { ApiContext } from './ApiContext';
import { callFetch } from './callFetch';
import { ServerEventsContext } from './ServerEventsContext';

export function useExtensionIntegrations(extensionId: string) {
const apiContext = React.useContext(ApiContext);
const serverEventsContext = React.useContext(ServerEventsContext);
const [loading, setLoading] = React.useState(true);
const [error, setError] = React.useState<false | Error>(false);
const [integrations, setIntegrations] = React.useState([]);

React.useEffect(() => {
const fetchIntegrations = async () => {
setLoading(true);
try {
const response = await callFetch({
headers: apiContext.headers,
method: 'GET',
url: `${apiContext.apiUri}/extensions/${extensionId}/integrations`,
});
const result = await response.json();
setIntegrations(result);
} catch (e) {
setError(e as Error);
} finally {
setLoading(false);
}
};
fetchIntegrations();
}, [integrations, loading, error]);

return { integrations, loading, error };
}
82 changes: 82 additions & 0 deletions app/ui-react/packages/api/src/useExtensionHelpers.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import { Extension } from '@syndesis/models';
import * as React from 'react';
import { ApiContext } from './ApiContext';
import { callFetch } from './callFetch';

export const useExtensionHelpers = () => {
const apiContext = React.useContext(ApiContext);

/**
* Deletes the extension with the specified identifier.
* @param extensionId the ID of the extension being deleted
*/
const deleteExtension = async (extensionId: string): Promise<void> => {
const response = await callFetch({
headers: apiContext.headers,
method: 'DELETE',
url: `${apiContext.apiUri}/extensions/${extensionId}`,
});

if (!response.ok) {
throw new Error(response.statusText);
}

return Promise.resolve();
};

/**
* Imports the extension with the specified identifier.
* @param extensionId the ID of the extension being imported
*/
const importExtension = async (extensionId: string): Promise<void> => {
const response = await callFetch({
headers: apiContext.headers,
method: 'POST',
url: `${apiContext.apiUri}/extensions/${extensionId}/install`,
});

if (!response.ok) {
throw new Error(response.statusText);
}

return Promise.resolve();
};

/**
* Updates the extension with the specified identifier or creates a new extension if there is no identifier.
* @param extensionId the ID of the extension being uploaded
*/
const uploadExtension = async (
file: File,
extensionId?: string
): Promise<Extension> => {
const data = new FormData();
data.append('file', file, file.name);
const url = `${apiContext.apiUri}/extensions`;
const {
Accept,
['Content-Type']: contentType,
...rest
} = apiContext.headers;
const response = await callFetch({
body: data,
headers: { ...rest },
includeAccept: false,
includeContentType: false,
includeReferrerPolicy: false,
method: 'POST',
url: extensionId ? `${url}?updatedId=${extensionId}` : url,
});
if (!response.ok) {
throw new Error(response.statusText);
}

return (await response.json()) as Extension;
};

return {
deleteExtension,
importExtension,
uploadExtension,
};
};
Loading