@@ -49,6 +49,20 @@ export const util = { | |||||
postNoAuth: function(obj) { return util.entityNoAuth('POST', obj); }, | postNoAuth: function(obj) { return util.entityNoAuth('POST', obj); }, | ||||
putNoAuth: function(obj) { return util.entityNoAuth('PUT', 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) { | entityWithAuth: function(method, obj) { | ||||
if (typeof obj === 'undefined' || obj === null || obj === 'undefined') { | if (typeof obj === 'undefined' || obj === null || obj === 'undefined') { | ||||
return { | return { | ||||
@@ -65,8 +79,10 @@ export const util = { | |||||
}, | }, | ||||
postWithAuth: function(obj) { return util.entityWithAuth('POST', obj); }, | postWithAuth: function(obj) { return util.entityWithAuth('POST', obj); }, | ||||
postJsonWithAuth: function(json) { return util.jsonWithAuth('POST', json); }, | |||||
putWithAuth: function(obj) { return util.entityWithAuth('PUT', obj); }, | putWithAuth: function(obj) { return util.entityWithAuth('PUT', obj); }, | ||||
putJsonWithAuth: function(json) { return util.jsonWithAuth('PUT', json); }, | |||||
deleteWithAuth: function() { | deleteWithAuth: function() { | ||||
return { | return { | ||||
@@ -47,6 +47,21 @@ function search(type, query) { | |||||
.then(config => { return config; }); | .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) { | function loadMessages(group, locale) { | ||||
const requestOptions = util.userLoggedIn() ? util.getWithAuth() : { method: 'GET' }; | const requestOptions = util.userLoggedIn() ? util.getWithAuth() : { method: 'GET' }; | ||||
if (!locale || locale === '') locale = 'detect'; | if (!locale || locale === '') locale = 'detect'; | ||||
@@ -13,7 +13,7 @@ const state = { | |||||
}, | }, | ||||
entityConfigs: {}, | entityConfigs: {}, | ||||
searchResults: [], | searchResults: [], | ||||
status: { activating: false, searching: false }, | |||||
status: { activating: false, searching: false, creatingEntity: false }, | |||||
activated: null, | activated: null, | ||||
error: null, | error: null, | ||||
messages: { | messages: { | ||||
@@ -88,6 +88,14 @@ const actions = { | |||||
error => commit('searchFailure', error) | 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) { | loadMessages({ commit }, group, locale) { | ||||
commit('loadMessagesRequest'); | commit('loadMessagesRequest'); | ||||
systemService.loadMessages(group, locale) | systemService.loadMessages(group, locale) | ||||
@@ -257,6 +265,16 @@ const mutations = { | |||||
state.status.searching = false; | state.status.searching = false; | ||||
state.error = error; | 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) {}, | loadMessagesRequest(state) {}, | ||||
loadMessagesSuccess(state, {group, messages}) { | loadMessagesSuccess(state, {group, messages}) { | ||||
if (state.messageGroupsLoaded.indexOf(group) === -1) state.messageGroupsLoaded.push(group); | if (state.messageGroupsLoaded.indexOf(group) === -1) state.messageGroupsLoaded.push(group); | ||||
@@ -10,7 +10,16 @@ | |||||
</div> | </div> | ||||
</form> | </form> | ||||
<table border="1" v-if="this.ec && this.ec[this.lcType] && this.ec[this.lcType].listFields && this.selectedEntity === null"> | |||||
<div v-if="this.ec && this.ec[this.lcType] && this.ec[this.lcType].createMethod !== 'DISABLED'"> | |||||
<button @click="showingAddDialog = true">{{messages.button_label_add_entity}}</button> | |||||
<div v-if="showingAddDialog"> | |||||
<textarea v-bind="newEntityJson"></textarea> | |||||
<button @click="createNewEntity()">{{messages.button_label_save_add_entity}}</button> | |||||
<button @click="showingAddDialog = false">{{messages.button_label_close_add_entity}}</button> | |||||
</div> | |||||
</div> | |||||
<table border="1" v-if="this.ec && this.ec[this.lcType] && this.ec[this.lcType].listFields && this.selectedEntity === null && !showingAddDialog"> | |||||
<thead> | <thead> | ||||
<tr> | <tr> | ||||
<td v-for="field in this.ec[this.lcType].listFields">{{field}}</td> | <td v-for="field in this.ec[this.lcType].listFields">{{field}}</td> | ||||
@@ -35,6 +44,10 @@ | |||||
<td>{{field}}:</td> | <td>{{field}}:</td> | ||||
<td>{{selectedEntity[field]}}</td> | <td>{{selectedEntity[field]}}</td> | ||||
</tr> | </tr> | ||||
<tr> | |||||
<td>JSON:</td> | |||||
<td>{{JSON.stringify(selectedEntity)}}</td> | |||||
</tr> | |||||
</table> | </table> | ||||
<button @click="closeEntity()">{{messages.button_label_close_view_entity}}</button> | <button @click="closeEntity()">{{messages.button_label_close_view_entity}}</button> | ||||
</div> | </div> | ||||
@@ -56,7 +69,9 @@ | |||||
sort: '' | sort: '' | ||||
}, | }, | ||||
results: [], | results: [], | ||||
selectedEntity: null | |||||
selectedEntity: null, | |||||
showingAddDialog: false, | |||||
newEntityJson: '' | |||||
}; | }; | ||||
}, | }, | ||||
computed: { | computed: { | ||||
@@ -74,7 +89,8 @@ | |||||
methods: { | methods: { | ||||
// ...mapActions('system', ['loadIsActivated', 'activate']), | // ...mapActions('system', ['loadIsActivated', 'activate']), | ||||
...mapActions('system', [ | ...mapActions('system', [ | ||||
'loadIsActivated', 'loadSystemConfigs', 'loadMessages', 'loadTimezones', 'loadEntityConfigs', 'search' | |||||
'loadIsActivated', 'loadSystemConfigs', 'loadMessages', 'loadTimezones', 'loadEntityConfigs', | |||||
'search', 'createEntity' | |||||
]), | ]), | ||||
setupModel(e) { | setupModel(e) { | ||||
console.log('setupModel called'); | console.log('setupModel called'); | ||||
@@ -87,6 +103,9 @@ | |||||
}, | }, | ||||
closeEntity() { | closeEntity() { | ||||
this.selectedEntity = null; | this.selectedEntity = null; | ||||
}, | |||||
createNewEntity() { | |||||
this.createEntity(this.type, this.newEntityJson, this.messages, this.errors); | |||||
} | } | ||||
}, | }, | ||||
watch: { | watch: { | ||||