Skip to content

Commit

Permalink
Handle negative credit in UI (#10814)
Browse files Browse the repository at this point in the history
* Handle create connection feature

* Handle sync feature

* Add create and sync connection features to service

* Add unit tests

* Add allowSync property to deps

Co-authored-by: Artem Astapenko <[email protected]>
  • Loading branch information
juliachvyrova and jamakase authored Mar 30, 2022
1 parent 39107bf commit df6c8c7
Show file tree
Hide file tree
Showing 13 changed files with 212 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { Button, DropDownRow, H3, H5 } from "components";
import { Popout } from "components/base/Popout/Popout";
import { ReleaseStageBadge } from "components/ReleaseStageBadge";
import { ReleaseStage } from "core/domain/connector";
import { FeatureItem, useFeatureService } from "hooks/services/Feature";

type IProps = {
type: "source" | "destination";
Expand Down Expand Up @@ -53,6 +54,8 @@ const TableItemTitle: React.FC<IProps> = ({
entityIcon,
releaseStage,
}) => {
const { hasFeature } = useFeatureService();
const allowCreateConnection = hasFeature(FeatureItem.AllowCreateConnection);
const formatMessage = useIntl().formatMessage;
const options = [
{
Expand Down Expand Up @@ -94,7 +97,7 @@ const TableItemTitle: React.FC<IProps> = ({
}}
onChange={onSelect}
targetComponent={({ onOpen }) => (
<Button onClick={onOpen}>
<Button onClick={onOpen} disabled={!allowCreateConnection}>
<FormattedMessage id={`tables.${type}Add`} />
</Button>
)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import StatusCell from "./components/StatusCell";
import ConnectionSettingsCell from "./components/ConnectionSettingsCell";
import { ITableDataItem, SortOrderEnum } from "./types";
import useRouter from "hooks/useRouter";
import { FeatureItem, useFeatureService } from "hooks/services/Feature";

const Content = styled.div`
margin: 0 32px 0 27px;
Expand All @@ -36,6 +37,8 @@ const ConnectionTable: React.FC<IProps> = ({
onSync,
}) => {
const { query, push } = useRouter();
const { hasFeature } = useFeatureService();
const allowSync = hasFeature(FeatureItem.AllowSync);

const sortBy = query.sortBy || "entity";
const sortOrder = query.order || SortOrderEnum.ASC;
Expand Down Expand Up @@ -165,6 +168,7 @@ const ConnectionTable: React.FC<IProps> = ({
isManual={!row.original.schedule}
onChangeStatus={onChangeStatus}
onSync={onSync}
allowSync={allowSync}
/>
),
},
Expand All @@ -177,7 +181,7 @@ const ConnectionTable: React.FC<IProps> = ({
),
},
],
[entity, onChangeStatus, onSync, onSortClick, sortBy, sortOrder]
[allowSync, entity, onChangeStatus, onSync, onSortClick, sortBy, sortOrder]
);

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { useAsyncFn } from "react-use";
import { LoadingButton, Toggle } from "components";

type IProps = {
allowSync?: boolean;
enabled?: boolean;
isSyncing?: boolean;
isManual?: boolean;
Expand All @@ -29,6 +30,7 @@ const StatusCell: React.FC<IProps> = ({
onChangeStatus,
isSyncing,
onSync,
allowSync,
}) => {
const [{ loading }, OnLaunch] = useAsyncFn(
async (event: React.SyntheticEvent) => {
Expand All @@ -44,7 +46,13 @@ const StatusCell: React.FC<IProps> = ({
};

if (!isManual) {
return <Toggle checked={enabled} onChange={OnToggleClick} />;
return (
<Toggle
checked={enabled}
onChange={OnToggleClick}
disabled={!allowSync}
/>
);
}

if (isSyncing) {
Expand All @@ -56,7 +64,7 @@ const StatusCell: React.FC<IProps> = ({
}

return (
<SmallButton onClick={OnLaunch} isLoading={loading}>
<SmallButton onClick={OnLaunch} isLoading={loading} disabled={!allowSync}>
<FormattedMessage id="tables.launch" />
</SmallButton>
);
Expand Down
2 changes: 1 addition & 1 deletion airbyte-webapp/src/config/ConfigServiceProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { LoadingPage } from "components";
import { Config, ValueProvider } from "./types";
import { applyProviders } from "./configProviders";

type ConfigContext<T extends Config = Config> = {
export type ConfigContext<T extends Config = Config> = {
config: T;
};

Expand Down
6 changes: 6 additions & 0 deletions airbyte-webapp/src/config/defaultConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ const features: Feature[] = [
{
id: FeatureItem.AllowUpdateConnectors,
},
{
id: FeatureItem.AllowCreateConnection,
},
{
id: FeatureItem.AllowSync,
},
];

const defaultConfig: Config = {
Expand Down
93 changes: 93 additions & 0 deletions airbyte-webapp/src/hooks/services/Feature/FeatureService.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import React from "react";
import { act, renderHook } from "@testing-library/react-hooks";
import {
FeatureService,
useFeatureRegisterValues,
useFeatureService,
} from "./FeatureService";
import { FeatureItem } from "./types";
import { TestWrapper } from "utils/testutils";
import { ConfigContext, configContext } from "config";

const predefinedFeatures = [
{
id: FeatureItem.AllowCustomDBT,
},
];

const wrapper: React.FC = ({ children }) => (
<TestWrapper>
<configContext.Provider
value={
({
config: { features: predefinedFeatures },
} as unknown) as ConfigContext
}
>
<FeatureService>{children}</FeatureService>
</configContext.Provider>
</TestWrapper>
);

describe("FeatureService", () => {
test("should register and unregister features", async () => {
const { result } = renderHook(() => useFeatureService(), {
wrapper,
});

expect(result.current.features).toEqual(predefinedFeatures);

act(() => {
result.current.registerFeature([
{
id: FeatureItem.AllowCreateConnection,
},
]);
});

expect(result.current.features).toEqual([
...predefinedFeatures,
{
id: FeatureItem.AllowCreateConnection,
},
]);

act(() => {
result.current.unregisterFeature([FeatureItem.AllowCreateConnection]);
});

expect(result.current.features).toEqual(predefinedFeatures);
});
});

describe("useFeatureRegisterValues", () => {
test("should register more than 1 feature", async () => {
const { result } = renderHook(
() => {
useFeatureRegisterValues([{ id: FeatureItem.AllowCreateConnection }]);
useFeatureRegisterValues([{ id: FeatureItem.AllowSync }]);

return useFeatureService();
},
{
initialProps: { initialValue: 0 },
wrapper,
}
);

expect(result.current.features).toEqual([
...predefinedFeatures,
{ id: FeatureItem.AllowCreateConnection },
{ id: FeatureItem.AllowSync },
]);

act(() => {
result.current.unregisterFeature([FeatureItem.AllowCreateConnection]);
});

expect(result.current.features).toEqual([
...predefinedFeatures,
{ id: FeatureItem.AllowSync },
]);
});
});
52 changes: 43 additions & 9 deletions airbyte-webapp/src/hooks/services/Feature/FeatureService.tsx
Original file line number Diff line number Diff line change
@@ -1,25 +1,43 @@
import React, { useContext, useMemo } from "react";
import React, { useContext, useMemo, useState } from "react";
import { Feature, FeatureItem, FeatureServiceApi } from "./types";
import { useConfig } from "config";
import { useDeepCompareEffect } from "react-use";

const featureServiceContext = React.createContext<FeatureServiceApi | null>(
null
);

export function FeatureService({
children,
}: {
children: React.ReactNode;
features?: Feature[];
}) {
const { features } = useConfig();
export function FeatureService({ children }: { children: React.ReactNode }) {
const [additionFeatures, setAdditionFeatures] = useState<Feature[]>([]);
const { features: instanceWideFeatures } = useConfig();

const featureMethods = useMemo(() => {
return {
registerFeature: (newFeatures: Feature[]): void =>
setAdditionFeatures((oldFeatures) => [...oldFeatures, ...newFeatures]),
unregisterFeature: (unregisteredFeatures: FeatureItem[]): void => {
setAdditionFeatures((oldFeatures) =>
oldFeatures.filter(
(feature) => !unregisteredFeatures.includes(feature.id)
)
);
},
};
}, []);

const features = useMemo(
() => [...instanceWideFeatures, ...additionFeatures],
[instanceWideFeatures, additionFeatures]
);

const featureService = useMemo(
() => ({
features,
hasFeature: (featureId: FeatureItem): boolean =>
!!features.find((feature) => feature.id === featureId),
...featureMethods,
}),
[features]
[features, featureMethods]
);

return (
Expand All @@ -44,3 +62,19 @@ export const WithFeature: React.FC<{ featureId: FeatureItem }> = ({
const { hasFeature } = useFeatureService();
return hasFeature(featureId) ? <>{children}</> : null;
};

export const useFeatureRegisterValues = (props?: Feature[] | null): void => {
const { registerFeature, unregisterFeature } = useFeatureService();

useDeepCompareEffect(() => {
if (props) {
registerFeature(props);

return () =>
unregisterFeature(props.map((feature: Feature) => feature.id));
}

return;
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [props]);
};
4 changes: 4 additions & 0 deletions airbyte-webapp/src/hooks/services/Feature/types.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ export enum FeatureItem {
AllowCustomDBT = "ALLOW_CUSTOM_DBT",
AllowUpdateConnectors = "ALLOW_UPDATE_CONNECTORS",
AllowOAuthConnector = "ALLOW_OAUTH_CONNECTOR",
AllowCreateConnection = "ALLOW_CREATE_CONNECTION",
AllowSync = "ALLOW_SYNC",
}

type Feature = {
Expand All @@ -11,6 +13,8 @@ type Feature = {

type FeatureServiceApi = {
features: Feature[];
registerFeature: (props: Feature[]) => void;
unregisterFeature: (props: FeatureItem[]) => void;
hasFeature: (featureId: FeatureItem) => boolean;
};

Expand Down
19 changes: 19 additions & 0 deletions airbyte-webapp/src/packages/cloud/cloudRoutes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ import { storeUtmFromQuery } from "utils/utmStorage";
import { DefaultView } from "./views/DefaultView";
import { hasFromState } from "utils/stateUtils";
import { RoutePaths } from "../../pages/routePaths";
import { FeatureItem, useFeatureRegisterValues } from "hooks/services/Feature";
import { useGetCloudWorkspace } from "./services/workspaces/WorkspacesService";
import { CreditStatus } from "./lib/domain/cloudWorkspaces/types";

export const CloudRoutes = {
Root: "/",
Expand All @@ -60,6 +63,7 @@ export const CloudRoutes = {

const MainRoutes: React.FC = () => {
const workspace = useCurrentWorkspace();
const cloudWorkspace = useGetCloudWorkspace(workspace.workspaceId);

const analyticsContext = useMemo(
() => ({
Expand All @@ -74,6 +78,21 @@ const MainRoutes: React.FC = () => {
? RoutePaths.Onboarding
: RoutePaths.Connections;

const features = useMemo(
() =>
cloudWorkspace.creditStatus !==
CreditStatus.NEGATIVE_BEYOND_GRACE_PERIOD &&
cloudWorkspace.creditStatus !== CreditStatus.NEGATIVE_MAX_THRESHOLD
? [
{ id: FeatureItem.AllowCreateConnection },
{ id: FeatureItem.AllowSync },
]
: null,
[cloudWorkspace]
);

useFeatureRegisterValues(features);

return (
<Routes>
<Route
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import useRouter from "hooks/useRouter";
import HeadTitle from "components/HeadTitle";
import Placeholder, { ResourceTypes } from "components/Placeholder";
import useWorkspace from "hooks/services/useWorkspace";
import { FeatureItem, useFeatureService } from "hooks/services/Feature";
import { RoutePaths } from "../../../routePaths";

const AllConnectionsPage: React.FC = () => {
Expand All @@ -18,6 +19,8 @@ const AllConnectionsPage: React.FC = () => {
const { connections } = useResource(ConnectionResource.listShape(), {
workspaceId: workspace.workspaceId,
});
const { hasFeature } = useFeatureService();
const allowCreateConnection = hasFeature(FeatureItem.AllowCreateConnection);

const onClick = () => push(`${RoutePaths.ConnectionNew}`);

Expand All @@ -28,7 +31,7 @@ const AllConnectionsPage: React.FC = () => {
<PageTitle
title={<FormattedMessage id="sidebar.connections" />}
endComponent={
<Button onClick={onClick}>
<Button onClick={onClick} disabled={!allowCreateConnection}>
<FormattedMessage id="connection.newConnection" />
</Button>
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,15 @@ type IProps = {
frequencyText?: string;
destinationDefinition?: DestinationDefinition;
sourceDefinition?: SourceDefinition;
allowSync?: boolean;
};

const StatusMainInfo: React.FC<IProps> = ({
connection,
frequencyText,
destinationDefinition,
sourceDefinition,
allowSync,
}) => {
return (
<MainInfo>
Expand Down Expand Up @@ -72,6 +74,7 @@ const StatusMainInfo: React.FC<IProps> = ({
<Cell>{frequencyText}</Cell>
<EnabledCell flex={1.1}>
<EnabledControl
disabled={!allowSync}
connection={connection}
frequencyText={frequencyText}
/>
Expand Down
Loading

0 comments on commit df6c8c7

Please sign in to comment.