diff --git a/src/_helpers/util.js b/src/_helpers/util.js index 5694a98..6c625b6 100644 --- a/src/_helpers/util.js +++ b/src/_helpers/util.js @@ -49,6 +49,20 @@ export const util = { postNoAuth: function(obj) { return util.entityNoAuth('POST', obj); }, putNoAuth: function(obj) { return util.entityNoAuth('PUT', obj); }, + jsonWithAuth: function(method, json) { + if (typeof json === 'undefined' || json === null || json === 'undefined') { + return { + method: method, + headers: { ...util.authHeader(), 'Content-Type': 'application/json' } + }; + } else { + return { + method: method, + headers: { ...util.authHeader(), 'Content-Type': 'application/json' }, + body: json + }; + } + }, entityWithAuth: function(method, obj) { if (typeof obj === 'undefined' || obj === null || obj === 'undefined') { return { @@ -65,8 +79,10 @@ export const util = { }, postWithAuth: function(obj) { return util.entityWithAuth('POST', obj); }, + postJsonWithAuth: function(json) { return util.jsonWithAuth('POST', json); }, putWithAuth: function(obj) { return util.entityWithAuth('PUT', obj); }, + putJsonWithAuth: function(json) { return util.jsonWithAuth('PUT', json); }, deleteWithAuth: function() { return { diff --git a/src/_services/system.service.js b/src/_services/system.service.js index 338f6b5..22f965a 100644 --- a/src/_services/system.service.js +++ b/src/_services/system.service.js @@ -47,6 +47,21 @@ function search(type, query) { .then(config => { return config; }); } +function createEntity(config, json, messages, errors) { + let requestOptions = null; + if (config.createMethod) { + if (config.createMethod === 'PUT') { + requestOptions = util.putJsonWithAuth(json); + } else if (config.createMethod === 'POST') { + requestOptions = util.putJsonWithAuth(json); + } + } + if (requestOptions === null) requestOptions = util.putJsonWithAuth(json); + return fetch(`${config.apiUrl}/${config.createUri}`, requestOptions) + .then(util.handleCrudResponse(messages, errors)) + .then(config => { return config; }); +} + function loadMessages(group, locale) { const requestOptions = util.userLoggedIn() ? util.getWithAuth() : { method: 'GET' }; if (!locale || locale === '') locale = 'detect'; diff --git a/src/_store/system.module.js b/src/_store/system.module.js index cb69a9c..b04e6b5 100644 --- a/src/_store/system.module.js +++ b/src/_store/system.module.js @@ -13,7 +13,7 @@ const state = { }, entityConfigs: {}, searchResults: [], - status: { activating: false, searching: false }, + status: { activating: false, searching: false, creatingEntity: false }, activated: null, error: null, messages: { @@ -88,6 +88,14 @@ const actions = { error => commit('searchFailure', error) ); }, + createEntity({ commit }, type, json, messages, errors) { + commit('createEntityRequest'); + systemService.createEntity(state.entityConfigs[type], json, messages, errors) + .then( + entity => commit('createEntitySuccess', entity), + error => commit('createEntityFailure', error) + ); + }, loadMessages({ commit }, group, locale) { commit('loadMessagesRequest'); systemService.loadMessages(group, locale) @@ -257,6 +265,16 @@ const mutations = { state.status.searching = false; state.error = error; }, + createEntityRequest(state) { + state.status.creatingEntity = true; + }, + createEntitySuccess(state, {entity}) { + state.status.creatingEntity = false; + }, + createEntityFailure(state, error) { + state.status.creatingEntity = false; + state.error = error; + }, loadMessagesRequest(state) {}, loadMessagesSuccess(state, {group, messages}) { if (state.messageGroupsLoaded.indexOf(group) === -1) state.messageGroupsLoaded.push(group); diff --git a/src/admin/ModelSetupPage.vue b/src/admin/ModelSetupPage.vue index 13bfeb4..c77f9de 100644 --- a/src/admin/ModelSetupPage.vue +++ b/src/admin/ModelSetupPage.vue @@ -10,7 +10,16 @@ -
{{field}} | @@ -35,6 +44,10 @@{{field}}: | {{selectedEntity[field]}} |
JSON: | +{{JSON.stringify(selectedEntity)}} | +