Skip to content

Commit

Permalink
define endpoints in api.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
jurajmajerik committed Nov 8, 2023
1 parent 1171689 commit 07bfe1e
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 11 deletions.
31 changes: 31 additions & 0 deletions frontend/src/lib/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import {
ExportedAssetType,
FeatureFlagAssociatedRoleType,
FeatureFlagType,
OrganizationFeatureFlags,
OrganizationFeatureFlagsCopyBody,
InsightModel,
IntegrationType,
MediaUploadResponse,
Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -674,6 +690,21 @@ const api = {
},
},

organizationFeatureFlags: {
async get(
orgId: OrganizationType['id'] = getCurrentOrganizationId(),
featureFlagKey: FeatureFlagType['key']
): Promise<OrganizationFeatureFlags> {
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<ActionType> {
return await new ApiRequest().actionsDetail(actionId).get()
Expand Down
24 changes: 13 additions & 11 deletions frontend/src/scenes/feature-flags/featureFlagLogic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -588,9 +588,7 @@ export const featureFlagLogic = kea<featureFlagLogicType>([
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)
Expand All @@ -605,12 +603,16 @@ export const featureFlagLogic = kea<featureFlagLogicType>([
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],
})
}
},
},
})),
Expand Down Expand Up @@ -774,15 +776,15 @@ export const featureFlagLogic = kea<featureFlagLogicType>([
}
},
copyFlagSuccess: ({ featureFlagCopy }) => {
if (featureFlagCopy.success.length) {
if (featureFlagCopy?.success.length) {
const operation = values.projectsWithCurrentFlag.find(
(p) => Number(p.team_id) === values.copyDestinationProject
)
? 'updated'
: '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()
Expand Down
12 changes: 12 additions & 0 deletions frontend/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2251,6 +2251,18 @@ export interface FeatureFlagType extends Omit<FeatureFlagBasicType, 'id' | 'team
has_enriched_analytics?: boolean
}

export interface OrganizationFeatureFlagsCopyBody {
feature_flag_key: FeatureFlagType['key']
from_project: TeamType['id']
target_project_ids: TeamType['id'][]
}

export type OrganizationFeatureFlags = {
flag_id: FeatureFlagType['id']
team_id: TeamType['id']
active: FeatureFlagType['active']
}[]

export interface FeatureFlagRollbackConditions {
threshold: number
threshold_type: string
Expand Down

0 comments on commit 07bfe1e

Please sign in to comment.