Commit fb30154c authored by Zamir Martins Filho's avatar Zamir Martins Filho

Preserve enviroment when editing networkpolicies

Editing page will provide environment id from the
controller which will set the current environment
id to the current state while reflecting on the
environment picker
parent d930f7f4
......@@ -9,6 +9,7 @@ export default () => {
networkPoliciesEndpoint,
threatMonitoringPath,
policy,
environmentId,
} = el.dataset;
const store = createStore();
......@@ -19,6 +20,10 @@ export default () => {
networkPoliciesEndpoint,
});
if (environmentId !== undefined) {
store.dispatch('threatMonitoring/setCurrentEnvironmentId', parseInt(environmentId, 10));
}
const props = { threatMonitoringPath };
if (policy) {
props.existingPolicy = JSON.parse(policy);
......
......@@ -12,7 +12,8 @@ export default {
state.environments = payload;
state.isLoadingEnvironments = false;
state.errorLoadingEnvironments = false;
if (payload.length > 0) state.currentEnvironmentId = payload[0].id;
if (payload.length > 0 && state.currentEnvironmentId === -1)
state.currentEnvironmentId = payload[0].id;
},
[types.RECEIVE_ENVIRONMENTS_ERROR](state) {
state.isLoadingEnvironments = false;
......
......@@ -9,11 +9,11 @@ module Projects
end
def edit
environment = project.environments.find(params[:environment_id])
@environment = project.environments.find(params[:environment_id])
@policy_name = params[:id]
response = NetworkPolicies::FindResourceService.new(
resource_name: @policy_name,
environment: environment,
environment: @environment,
kind: Gitlab::Kubernetes::CiliumNetworkPolicy::KIND
).execute
......
......@@ -6,4 +6,5 @@
environments_endpoint: project_environments_path(@project),
threat_monitoring_path: project_threat_monitoring_path(@project),
policy: @policy.to_json,
environment_id: @environment.id,
} }
---
title: This change preserves the environment when editing network policies
merge_request: 42148
author:
type: fixed
......@@ -5,7 +5,9 @@ describe('Threat Monitoring mutations', () => {
let state;
beforeEach(() => {
state = {};
state = {
currentEnvironmentId: -1,
};
});
describe(types.SET_ENDPOINT, () => {
......@@ -52,6 +54,29 @@ describe('Threat Monitoring mutations', () => {
it('sets currentEnvironmentId to 1', () => {
expect(state.currentEnvironmentId).toEqual(1);
});
describe('without payload', () => {
beforeEach(() => {
state.currentEnvironmentId = 1;
mutations[types.RECEIVE_ENVIRONMENTS_SUCCESS](state, []);
});
it('does not update currentEnvironmentId', () => {
expect(state.currentEnvironmentId).toBe(1);
});
});
describe('with currentEnvironmentId set', () => {
beforeEach(() => {
state.currentEnvironmentId = 1;
environments = [{ id: 2, name: 'production' }];
mutations[types.RECEIVE_ENVIRONMENTS_SUCCESS](state, environments);
});
it('does not update currentEnvironmentId', () => {
expect(state.currentEnvironmentId).toBe(1);
});
});
});
describe(types.RECEIVE_ENVIRONMENTS_ERROR, () => {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment