diff options
author | blizzz <blizzz@arthur-schiwon.de> | 2019-09-10 16:05:17 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-09-10 16:05:17 +0200 |
commit | bfec3715eef9878fe8ba0d27087b104c6b2e75dd (patch) | |
tree | 0fcbc976cf0f03b49e3593e483ee828cc24c1fe9 /apps/workflowengine/src/store.js | |
parent | f889ea83739448bc39aa39cf64c9ddd8548c7701 (diff) | |
parent | 7683208dfa4cf5f0ff196ee0cabdacf8046592eb (diff) | |
download | nextcloud-server-bfec3715eef9878fe8ba0d27087b104c6b2e75dd.tar.gz nextcloud-server-bfec3715eef9878fe8ba0d27087b104c6b2e75dd.zip |
Merge pull request #16706 from nextcloud/workflow-frontend
Workflow frontend overhaul
Diffstat (limited to 'apps/workflowengine/src/store.js')
-rw-r--r-- | apps/workflowengine/src/store.js | 164 |
1 files changed, 164 insertions, 0 deletions
diff --git a/apps/workflowengine/src/store.js b/apps/workflowengine/src/store.js new file mode 100644 index 00000000000..a322c7fb3ea --- /dev/null +++ b/apps/workflowengine/src/store.js @@ -0,0 +1,164 @@ +/* + * @copyright Copyright (c) 2019 Julius Härtl <jus@bitgrid.net> + * + * @author Julius Härtl <jus@bitgrid.net> + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ + +import Vue from 'vue' +import Vuex from 'vuex' +import axios from 'nextcloud-axios' +import { getApiUrl } from './helpers/api' +import confirmPassword from 'nextcloud-password-confirmation' + +Vue.use(Vuex) + +const store = new Vuex.Store({ + state: { + rules: [], + scope: OCP.InitialState.loadState('workflowengine', 'scope'), + operations: OCP.InitialState.loadState('workflowengine', 'operators'), + + plugins: Vue.observable({ + checks: {}, + operators: {} + }), + + entities: OCP.InitialState.loadState('workflowengine', 'entities'), + events: OCP.InitialState.loadState('workflowengine', 'entities') + .map((entity) => entity.events.map(event => { + return { + id: `${entity.id}::${event.eventName}`, + entity, + ...event + } + })).flat(), + checks: OCP.InitialState.loadState('workflowengine', 'checks') + }, + mutations: { + addRule(state, rule) { + state.rules.push({ ...rule, valid: true }) + }, + updateRule(state, rule) { + const index = state.rules.findIndex((item) => rule.id === item.id) + const newRule = Object.assign({}, rule) + Vue.set(state.rules, index, newRule) + }, + removeRule(state, rule) { + const index = state.rules.findIndex((item) => rule.id === item.id) + state.rules.splice(index, 1) + }, + addPluginCheck(state, plugin) { + Vue.set(state.plugins.checks, plugin.class, plugin) + }, + addPluginOperator(state, plugin) { + plugin = Object.assign( + { color: 'var(--color-primary-element)' }, + plugin, state.operations[plugin.id] || {}) + Vue.set(state.operations, plugin.id, plugin) + } + }, + actions: { + async fetchRules(context) { + const { data } = await axios.get(getApiUrl('')) + Object.values(data.ocs.data).flat().forEach((rule) => { + context.commit('addRule', rule) + }) + }, + createNewRule(context, rule) { + let entity = null + let events = [] + if (rule.isComplex === false && rule.fixedEntity === '') { + entity = context.state.entities.find((item) => rule.entities && rule.entities[0] === item.id) + entity = entity || Object.values(context.state.entities)[0] + events = [entity.events[0].eventName] + } + + context.commit('addRule', { + id: -(new Date().getTime()), + class: rule.id, + entity: entity ? entity.id : rule.fixedEntity, + events, + name: '', // unused in the new ui, there for legacy reasons + checks: [], + operation: rule.operation || '' + }) + }, + updateRule(context, rule) { + context.commit('updateRule', { + ...rule, + events: typeof rule.events === 'string' ? JSON.parse(rule.events) : rule.events + }) + }, + removeRule(context, rule) { + context.commit('removeRule', rule) + }, + async pushUpdateRule(context, rule) { + await confirmPassword() + let result + if (rule.id < 0) { + result = await axios.post(getApiUrl(''), rule) + } else { + result = await axios.put(getApiUrl(`/${rule.id}`), rule) + } + Vue.set(rule, 'id', result.data.ocs.data.id) + context.commit('updateRule', rule) + }, + async deleteRule(context, rule) { + await confirmPassword() + await axios.delete(getApiUrl(`/${rule.id}`)) + context.commit('removeRule', rule) + }, + setValid(context, { rule, valid }) { + rule.valid = valid + context.commit('updateRule', rule) + } + }, + getters: { + getRules(state) { + return state.rules.sort((rule1, rule2) => { + return rule1.id - rule2.id || rule2.class - rule1.class + }) + }, + getOperationForRule(state) { + return (rule) => state.operations[rule.class] + }, + getEntityForOperation(state) { + return (operation) => state.entities.find((entity) => operation.fixedEntity === entity.id) + }, + getEventsForOperation(state) { + return (operation) => state.events + }, + /** + * Return all available checker plugins for a given entity class + */ + getChecksForEntity(state) { + return (entity) => { + return state.checks + .filter((check) => check.supportedEntities.indexOf(entity) > -1 || check.supportedEntities.length === 0) + .map((check) => state.plugins.checks[check.id]) + .reduce((obj, item) => { + obj[item.class] = item + return obj + }, {}) + } + } + } +}) + +export default store |