@@ -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 { | |||
@@ -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'; | |||
@@ -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); | |||
@@ -10,7 +10,16 @@ | |||
</div> | |||
</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> | |||
<tr> | |||
<td v-for="field in this.ec[this.lcType].listFields">{{field}}</td> | |||
@@ -35,6 +44,10 @@ | |||
<td>{{field}}:</td> | |||
<td>{{selectedEntity[field]}}</td> | |||
</tr> | |||
<tr> | |||
<td>JSON:</td> | |||
<td>{{JSON.stringify(selectedEntity)}}</td> | |||
</tr> | |||
</table> | |||
<button @click="closeEntity()">{{messages.button_label_close_view_entity}}</button> | |||
</div> | |||
@@ -56,7 +69,9 @@ | |||
sort: '' | |||
}, | |||
results: [], | |||
selectedEntity: null | |||
selectedEntity: null, | |||
showingAddDialog: false, | |||
newEntityJson: '' | |||
}; | |||
}, | |||
computed: { | |||
@@ -74,7 +89,8 @@ | |||
methods: { | |||
// ...mapActions('system', ['loadIsActivated', 'activate']), | |||
...mapActions('system', [ | |||
'loadIsActivated', 'loadSystemConfigs', 'loadMessages', 'loadTimezones', 'loadEntityConfigs', 'search' | |||
'loadIsActivated', 'loadSystemConfigs', 'loadMessages', 'loadTimezones', 'loadEntityConfigs', | |||
'search', 'createEntity' | |||
]), | |||
setupModel(e) { | |||
console.log('setupModel called'); | |||
@@ -87,6 +103,9 @@ | |||
}, | |||
closeEntity() { | |||
this.selectedEntity = null; | |||
}, | |||
createNewEntity() { | |||
this.createEntity(this.type, this.newEntityJson, this.messages, this.errors); | |||
} | |||
}, | |||
watch: { | |||