Skip to content

Commit

Permalink
Solve issue when cloning an environment,\ and requierd metadata exist
Browse files Browse the repository at this point in the history
  • Loading branch information
novakzaballa committed Jun 7, 2024
1 parent 18ae518 commit b7400a9
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 7 deletions.
3 changes: 2 additions & 1 deletion frontend/common/dispatcher/app-actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,12 @@ const AppActions = Object.assign({}, require('./base/_app-actions'), {
pin,
})
},
createEnv(name, projectId, cloneId, description) {
createEnv(name, projectId, cloneId, description, metadata) {
Dispatcher.handleViewAction({
actionType: Actions.CREATE_ENV,
cloneId,
description,
metadata,
name,
projectId,
})
Expand Down
4 changes: 2 additions & 2 deletions frontend/common/providers/ProjectProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ const ProjectProvider = class extends React.Component {
})
}

createEnv = (env, projectId, cloneId, description) => {
AppActions.createEnv(env, projectId, cloneId, description)
createEnv = (env, projectId, cloneId, description, metadata) => {
AppActions.createEnv(env, projectId, cloneId, description, metadata)
}

editEnv = (env) => {
Expand Down
4 changes: 3 additions & 1 deletion frontend/common/stores/project-store.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const BaseStore = require('./base/_store')
const data = require('../data/base/_data')

const controller = {
createEnv: (name, projectId, cloneId, description) => {
createEnv: (name, projectId, cloneId, description, metadata) => {
API.trackEvent(Constants.events.CREATE_ENVIRONMENT)
const req = cloneId
? data.post(`${Project.api}environments/${cloneId}/clone/`, {
Expand All @@ -30,6 +30,7 @@ const controller = {
data
.put(`${Project.api}environments/${res.api_key}/`, {
description,
metadata: metadata || [],
name,
project: projectId,
})
Expand Down Expand Up @@ -239,6 +240,7 @@ store.dispatcherIndex = Dispatcher.register(store, (payload) => {
action.projectId,
action.cloneId,
action.description,
action.metadata,
)
break
case Actions.EDIT_ENVIRONMENT:
Expand Down
4 changes: 3 additions & 1 deletion frontend/web/components/metadata/AddMetadataToEntity.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export type CustomMetadataField = MetadataField & {
type CustomMetadata = (Metadata & CustomMetadataField) | null

type AddMetadataToEntityType = {
isCloningEnvironment?: boolean
organisationId: string
projectId: string | number
entityContentType: number
Expand All @@ -42,6 +43,7 @@ const AddMetadataToEntity: FC<AddMetadataToEntityType> = ({
entityContentType,
entityId,
envName,
isCloningEnvironment,
onChange,
organisationId,
projectId,
Expand Down Expand Up @@ -249,7 +251,7 @@ const AddMetadataToEntity: FC<AddMetadataToEntityType> = ({
</FormGroup>
}
/>
{entity === 'environment' && (
{entity === 'environment' && !isCloningEnvironment && (
<div className='text-right'>
<Button
theme='primary'
Expand Down
60 changes: 58 additions & 2 deletions frontend/web/components/pages/CreateEnvironmentPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,19 @@ import Constants from 'common/constants'
import ErrorMessage from 'components/ErrorMessage'
import PageTitle from 'components/PageTitle'
import CondensedRow from 'components/CondensedRow'
import AddMetadataToEntity from 'components/metadata/AddMetadataToEntity'
import { getSupportedContentType } from 'common/services/useSupportedContentType'
import { getStore } from 'common/store'

const CreateEnvironmentPage = class extends Component {
static displayName = 'CreateEnvironmentPage'

constructor(props, context) {
super(props, context)
this.state = {}
this.state = {
envContentType: {},
metadata: [],
}
}

static contextTypes = {
Expand All @@ -27,6 +33,19 @@ const CreateEnvironmentPage = class extends Component {
componentDidMount = () => {
API.trackPage(Constants.pages.CREATE_ENVIRONMENT)

if (Utils.getFlagsmithHasFeature('enable_metadata')) {
getSupportedContentType(getStore(), {
organisation_id: AccountStore.getOrganisation().id,
}).then((res) => {
const envContentType = Utils.getContentType(
res.data,
'model',
'environment',
)
this.setState({ envContentType: envContentType })
})
}

this.focusTimeout = setTimeout(() => {
this.input.focus()
this.focusTimeout = null
Expand All @@ -40,7 +59,7 @@ const CreateEnvironmentPage = class extends Component {
}

render() {
const { name } = this.state
const { envContentType, metadata, name } = this.state
return (
<div className='app-container container'>
<PageTitle title={'Create Environment'}>
Expand Down Expand Up @@ -84,6 +103,7 @@ const CreateEnvironmentPage = class extends Component {
this.state.selectedEnv &&
this.state.selectedEnv.api_key,
this.state.description,
metadata,
)
}}
>
Expand Down Expand Up @@ -171,6 +191,42 @@ const CreateEnvironmentPage = class extends Component {
)}
</CondensedRow>
</div>
{Utils.getFlagsmithHasFeature('enable_metadata') &&
envContentType?.id && (
<CondensedRow>
<FormGroup className='mt-5 setting'>
<InputGroup
title={'Metadata'}
tooltip={
'You need to add a value to the metadata if it is required to successfully clone the environment'
}
tooltipPlace='right'
component={
<AddMetadataToEntity
organisationId={
AccountStore.getOrganisation().id
}
projectId={
this.props.match.params.projectId
}
entityId={
this.state.selectedEnv.api_key
}
envName={name}
entityContentType={envContentType.id}
entity={envContentType.model}
isCloningEnvironment
onChange={(m) => {
this.setState({
metadata: m,
})
}}
/>
}
/>
</FormGroup>
</CondensedRow>
)}

{error && <ErrorMessage error={error} />}
<CondensedRow>
Expand Down

0 comments on commit b7400a9

Please sign in to comment.