|
| 1 | +// Copyright (c) 2021 Uber Technologies Inc. |
| 2 | +// |
| 3 | +// |
| 4 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 | +// of this software and associated documentation files (the "Software"), to deal |
| 6 | +// in the Software without restriction, including without limitation the rights |
| 7 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 8 | +// copies of the Software, and to permit persons to whom the Software is |
| 9 | +// furnished to do so, subject to the following conditions: |
| 10 | +// |
| 11 | +// The above copyright notice and this permission notice shall be included in |
| 12 | +// all copies or substantial portions of the Software. |
| 13 | +// |
| 14 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 19 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 20 | +// THE SOFTWARE. |
| 21 | + |
| 22 | +import { |
| 23 | + DOMAIN_RESET_STATE, |
| 24 | + DOMAIN_SET_DOMAIN, |
| 25 | + DOMAIN_SET_ERROR, |
| 26 | +} from './mutation-types'; |
| 27 | + |
| 28 | +const mutations = { |
| 29 | + [DOMAIN_RESET_STATE]: (state, domainName) => { |
| 30 | + state.domain.domainHash = { |
| 31 | + ...state.domain.domainHash, |
| 32 | + [domainName]: {}, |
| 33 | + }; |
| 34 | + }, |
| 35 | + [DOMAIN_SET_DOMAIN]: (state, domain) => { |
| 36 | + const { |
| 37 | + domainInfo: { name: domainName }, |
| 38 | + isGlobalDomain, |
| 39 | + replicationConfiguration: { activeClusterName }, |
| 40 | + } = domain; |
| 41 | + |
| 42 | + if (isGlobalDomain) { |
| 43 | + return (state.domain.domainHash = { |
| 44 | + ...state.domain.domainHash, |
| 45 | + [domainName]: { |
| 46 | + global: domain, |
| 47 | + }, |
| 48 | + }); |
| 49 | + } |
| 50 | + |
| 51 | + if (!state.domain.domainHash[domainName].local) { |
| 52 | + return (state.domain.domainHash = { |
| 53 | + ...state.domain.domainHash, |
| 54 | + [domainName]: { |
| 55 | + local: [domain], |
| 56 | + }, |
| 57 | + }); |
| 58 | + } |
| 59 | + |
| 60 | + const matchIndex = state.domain.domainHash[domainName].local.findIndex( |
| 61 | + ({ |
| 62 | + replicationConfiguration: { activeClusterName: matchActiveClusterName }, |
| 63 | + }) => matchActiveClusterName === activeClusterName |
| 64 | + ); |
| 65 | + |
| 66 | + if (matchIndex === -1) { |
| 67 | + return (state.domain.domainHash = { |
| 68 | + ...state.domain.domainHash, |
| 69 | + [domainName]: { |
| 70 | + local: [...state.domain.domainHash[domainName].local, domain], |
| 71 | + }, |
| 72 | + }); |
| 73 | + } |
| 74 | + |
| 75 | + state.domain.domainHash[domainName].local.splice(matchIndex, 1, domain); |
| 76 | + |
| 77 | + state.domain.domainHash = { |
| 78 | + ...state.domain.domainHash, |
| 79 | + [domainName]: { |
| 80 | + local: [...state.domain.domainHash[domainName].local], |
| 81 | + }, |
| 82 | + }; |
| 83 | + }, |
| 84 | + [DOMAIN_SET_ERROR]: (state, { domainName, error }) => { |
| 85 | + state.domain.domainHash = { |
| 86 | + ...state.domain.domainHash, |
| 87 | + [domainName]: { |
| 88 | + error, |
| 89 | + }, |
| 90 | + }; |
| 91 | + }, |
| 92 | +}; |
| 93 | + |
| 94 | +export default mutations; |
0 commit comments