From 0a7ea2f93fe31607dc2a578fda4f611bad106afc Mon Sep 17 00:00:00 2001 From: minnakt Date: Sat, 16 Sep 2023 14:30:58 -0400 Subject: [PATCH] Surface types --- src/gql/generated/types.ts | 1 + .../tabs/ProviderTab/transformerUtils.ts | 147 +++++------------- .../tabs/ProviderTab/transformers.ts | 15 +- 3 files changed, 48 insertions(+), 115 deletions(-) diff --git a/src/gql/generated/types.ts b/src/gql/generated/types.ts index 30c7da284e..948f7979db 100644 --- a/src/gql/generated/types.ts +++ b/src/gql/generated/types.ts @@ -1531,6 +1531,7 @@ export type Permissions = { __typename?: "Permissions"; canCreateDistro: Scalars["Boolean"]["output"]; canCreateProject: Scalars["Boolean"]["output"]; + canEditAdminSettings: Scalars["Boolean"]["output"]; distroPermissions: DistroPermissions; userId: Scalars["String"]["output"]; }; diff --git a/src/pages/distroSettings/tabs/ProviderTab/transformerUtils.ts b/src/pages/distroSettings/tabs/ProviderTab/transformerUtils.ts index af09741ce5..6a4fa7366a 100644 --- a/src/pages/distroSettings/tabs/ProviderTab/transformerUtils.ts +++ b/src/pages/distroSettings/tabs/ProviderTab/transformerUtils.ts @@ -1,119 +1,52 @@ -import { BuildType } from "./types"; - -type FieldGetter = (providerSettings: Record) => { - form: Record; - gql: Record; -}; - -const getUserData = ((providerSettings) => ({ - form: { +import { BuildType, ProviderFormState } from "./types"; + +interface ProviderSettingsList { + user_data: string; + merge_user_data_parts: boolean; + security_group_ids: string[]; + image_url: string; + build_type: string; + docker_registry_user: string; + docker_registry_pw: string; +} + +export const formProviderSettings = ( + providerSettings: Partial +) => ({ + staticProviderSettings: { userData: providerSettings.user_data ?? "", - }, - gql: { - user_data: providerSettings.userData, - }, -})) satisfies FieldGetter; - -const getMergeUserData = ((providerSettings) => ({ - form: { mergeUserData: providerSettings.merge_user_data_parts ?? false, - }, - gql: { - merge_user_data_parts: providerSettings.mergeUserData, - }, -})) satisfies FieldGetter; - -const getSecurityGroups = ((providerSettings) => ({ - form: { securityGroups: providerSettings.security_group_ids ?? [], }, - gql: { - security_group_ids: providerSettings.securityGroups, - }, -})) satisfies FieldGetter; - -const getImageUrl = ((providerSettings) => ({ - form: { + dockerProviderSettings: { + userData: providerSettings.user_data ?? "", + mergeUserData: providerSettings.merge_user_data_parts ?? false, + securityGroups: providerSettings.security_group_ids ?? [], imageUrl: providerSettings.image_url ?? "", - }, - gql: { - image_url: providerSettings.imageUrl, - }, -})) satisfies FieldGetter; - -const getBuildType = ((providerSettings) => ({ - form: { buildType: (providerSettings.build_type ?? "") as BuildType, - }, - gql: { - build_type: providerSettings.buildType, - }, -})) satisfies FieldGetter; - -const getRegistryUsername = ((providerSettings) => ({ - form: { registryUsername: providerSettings.docker_registry_user ?? "", + registryPassword: providerSettings.docker_registry_pw ?? "", }, - gql: { - docker_registry_user: providerSettings.registryUsername, - }, -})) satisfies FieldGetter; +}); -const getRegistryPassword = ((providerSettings) => ({ - form: { - registryPassword: providerSettings.docker_registry_pw ?? "", +type ProviderSettings = ProviderFormState["staticProviderSettings"] & + ProviderFormState["dockerProviderSettings"]; + +export const gqlProviderSettings = ( + providerSettings: Partial +) => ({ + staticProviderSettings: { + user_data: providerSettings.userData, + merge_user_data_parts: providerSettings.mergeUserData, + security_group_ids: providerSettings.securityGroups, }, - gql: { + dockerProviderSettings: { + user_data: providerSettings.userData, + merge_user_data_parts: providerSettings.mergeUserData, + security_group_ids: providerSettings.securityGroups, + image_url: providerSettings.imageUrl, + build_type: providerSettings.buildType, + docker_registry_user: providerSettings.registryUsername, docker_registry_pw: providerSettings.registryPassword, }, -})) satisfies FieldGetter; - -export const staticProviderSettings = ((providerSettings = {}) => { - const userData = getUserData(providerSettings); - const mergeUserData = getMergeUserData(providerSettings); - const securityGroups = getSecurityGroups(providerSettings); - - return { - form: { - ...userData.form, - ...mergeUserData.form, - ...securityGroups.form, - }, - gql: { - ...userData.gql, - ...mergeUserData.gql, - ...securityGroups.gql, - }, - }; -}) satisfies FieldGetter; - -export const dockerProviderSettings = ((providerSettings = {}) => { - const imageUrl = getImageUrl(providerSettings); - const buildType = getBuildType(providerSettings); - const registryUser = getRegistryUsername(providerSettings); - const registryPassword = getRegistryPassword(providerSettings); - const userData = getUserData(providerSettings); - const mergeUserData = getMergeUserData(providerSettings); - const securityGroups = getSecurityGroups(providerSettings); - - return { - form: { - ...imageUrl.form, - ...buildType.form, - ...registryUser.form, - ...registryPassword.form, - ...userData.form, - ...mergeUserData.form, - ...securityGroups.form, - }, - gql: { - ...imageUrl.gql, - ...buildType.gql, - ...registryUser.gql, - ...registryPassword.gql, - ...userData.gql, - ...mergeUserData.gql, - ...securityGroups.gql, - }, - }; -}) satisfies FieldGetter; +}); diff --git a/src/pages/distroSettings/tabs/ProviderTab/transformers.ts b/src/pages/distroSettings/tabs/ProviderTab/transformers.ts index 304ebbbd62..ffba811162 100644 --- a/src/pages/distroSettings/tabs/ProviderTab/transformers.ts +++ b/src/pages/distroSettings/tabs/ProviderTab/transformers.ts @@ -1,10 +1,7 @@ import { DistroSettingsTabRoutes } from "constants/routes"; import { Provider } from "gql/generated/types"; import { FormToGqlFunction, GqlToFormFunction } from "../types"; -import { - staticProviderSettings, - dockerProviderSettings, -} from "./transformerUtils"; +import { gqlProviderSettings, formProviderSettings } from "./transformerUtils"; type Tab = DistroSettingsTabRoutes.Provider; @@ -18,10 +15,10 @@ export const gqlToForm = ((data) => { providerName: provider, }, staticProviderSettings: { - ...staticProviderSettings(providerSettingsList[0]).form, + ...formProviderSettings(providerSettingsList[0]).staticProviderSettings, }, dockerProviderSettings: { - ...dockerProviderSettings(providerSettingsList[0]).form, + ...formProviderSettings(providerSettingsList[0]).dockerProviderSettings, containerPoolId: containerPool, poolMappingInfo: "", }, @@ -40,7 +37,8 @@ export const formToGql = ((data, distro) => { provider: Provider.Static, providerSettingsList: [ { - ...staticProviderSettings(data.staticProviderSettings).gql, + ...gqlProviderSettings(data.staticProviderSettings) + .staticProviderSettings, }, ], containerPool: "", @@ -51,7 +49,8 @@ export const formToGql = ((data, distro) => { provider: Provider.Docker, providerSettingsList: [ { - ...dockerProviderSettings(data.dockerProviderSettings).gql, + ...gqlProviderSettings(data.dockerProviderSettings) + .dockerProviderSettings, }, ], containerPool: data.dockerProviderSettings.containerPoolId,