From 651fc8489158a11b9cfdab22e0bfbfb81e1706cb Mon Sep 17 00:00:00 2001 From: Juraj Majerik Date: Wed, 8 Nov 2023 14:19:54 +0100 Subject: [PATCH] define endpoints in api.ts --- frontend/src/lib/api.ts | 31 +++++++++++++++++++ .../scenes/feature-flags/featureFlagLogic.ts | 24 +++++++------- frontend/src/types.ts | 12 +++++++ 3 files changed, 56 insertions(+), 11 deletions(-) diff --git a/frontend/src/lib/api.ts b/frontend/src/lib/api.ts index 63e4d08fd2d8f..805c8c3262ca5 100644 --- a/frontend/src/lib/api.ts +++ b/frontend/src/lib/api.ts @@ -19,6 +19,8 @@ import { ExportedAssetType, FeatureFlagAssociatedRoleType, FeatureFlagType, + OrganizationFeatureFlags, + OrganizationFeatureFlagsCopyBody, InsightModel, IntegrationType, MediaUploadResponse, @@ -178,6 +180,20 @@ class ApiRequest { return this.organizationResourceAccess().addPathComponent(id) } + public organizationFeatureFlags(orgId: OrganizationType['id'], featureFlagKey: FeatureFlagType['key']): ApiRequest { + return this.organizations() + .addPathComponent(orgId) + .addPathComponent('feature_flags') + .addPathComponent(featureFlagKey) + } + + public copyOrganizationFeatureFlags(orgId: OrganizationType['id']): ApiRequest { + return this.organizations() + .addPathComponent(orgId) + .addPathComponent('feature_flags') + .addPathComponent('copy_flags') + } + // # Projects public projects(): ApiRequest { return this.addPathComponent('projects') @@ -674,6 +690,21 @@ const api = { }, }, + organizationFeatureFlags: { + async get( + orgId: OrganizationType['id'] = getCurrentOrganizationId(), + featureFlagKey: FeatureFlagType['key'] + ): Promise { + return await new ApiRequest().organizationFeatureFlags(orgId, featureFlagKey).get() + }, + async copy( + orgId: OrganizationType['id'] = getCurrentOrganizationId(), + data: OrganizationFeatureFlagsCopyBody + ): Promise<{ success: FeatureFlagType[]; failed: any }> { + return await new ApiRequest().copyOrganizationFeatureFlags(orgId).create({ data }) + }, + }, + actions: { async get(actionId: ActionType['id']): Promise { return await new ApiRequest().actionsDetail(actionId).get() diff --git a/frontend/src/scenes/feature-flags/featureFlagLogic.ts b/frontend/src/scenes/feature-flags/featureFlagLogic.ts index 59e5708992e37..a9aee748e6430 100644 --- a/frontend/src/scenes/feature-flags/featureFlagLogic.ts +++ b/frontend/src/scenes/feature-flags/featureFlagLogic.ts @@ -588,9 +588,7 @@ export const featureFlagLogic = kea([ const orgId = values.currentOrganization?.id const flagKey = values.featureFlag.key - const projects: ProjectsWithCurrentFlagResponse = await api.get( - `api/organizations/${orgId}/feature_flags/${flagKey}` - ) + const projects = await api.organizationFeatureFlags.get(orgId, flagKey) // Put current project first const currentProjectIdx = projects.findIndex((p) => p.team_id === values.currentTeamId) @@ -605,12 +603,16 @@ export const featureFlagLogic = kea([ featureFlagCopy: { copyFlag: async () => { const orgId = values.currentOrganization?.id - - return await api.create(`api/organizations/${orgId}/feature_flags/copy_flags`, { - feature_flag_key: values.featureFlag.key, - from_project: values.currentTeamId, - target_project_ids: [values.copyDestinationProject], - }) + const featureFlagKey = values.featureFlag.key + const { copyDestinationProject, currentTeamId } = values + + if (currentTeamId && copyDestinationProject) { + return await api.organizationFeatureFlags.copy(orgId, { + feature_flag_key: featureFlagKey, + from_project: currentTeamId, + target_project_ids: [copyDestinationProject], + }) + } }, }, })), @@ -774,7 +776,7 @@ export const featureFlagLogic = kea([ } }, copyFlagSuccess: ({ featureFlagCopy }) => { - if (featureFlagCopy.success.length) { + if (featureFlagCopy?.success.length) { const operation = values.projectsWithCurrentFlag.find( (p) => Number(p.team_id) === values.copyDestinationProject ) @@ -782,7 +784,7 @@ export const featureFlagLogic = kea([ : 'copied' lemonToast.success(`Feature flag ${operation} successfully!`) } else { - lemonToast.error(`Error while saving feature flag: ${featureFlagCopy.failed || featureFlagCopy}`) + lemonToast.error(`Error while saving feature flag: ${featureFlagCopy?.failed || featureFlagCopy}`) } actions.loadProjectsWithCurrentFlag() diff --git a/frontend/src/types.ts b/frontend/src/types.ts index c515f2e22af3d..fa7bcc592299c 100644 --- a/frontend/src/types.ts +++ b/frontend/src/types.ts @@ -2251,6 +2251,18 @@ export interface FeatureFlagType extends Omit