You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

66 lines
2.4 KiB

import {Session} from './Session.service.js'
import {Resource} from '../Resource.js'
import {Message} from './Message.service.js'
export class ActionService {
static get() {
if ( !this.instance ) {
this.instance = new ActionService()
}
return this.instance
}
async perform(action, data, onComplete = () => {}) {
if ( action.type === 'back' ) this.goBack()
if ( action.type === 'resource' ) await this.handleResourceAction(action, data, onComplete)
}
async handleResourceAction(action, data, onComplete) {
const resource = await Resource.get(action.resource)
if ( action.action === 'insert' ) this.launchResourceForm(action.resource)
if ( action.action === 'update' ) this.launchResourceForm(action.resource, data[resource.configuration.primaryKey])
if ( action.action === 'list' ) this.launchResourceListing(action.resource)
if ( action.action === 'delete' ) {
await Message.get()
.modal({
title: `Delete ${resource.singular()}?`,
message: `Are you sure you want to delete this ${resource.singular().toLowerCase()}? This action cannot be undone.`,
buttons: [
{
type: 'close',
text: 'Keep it',
},
{
class: ['btn', 'btn-danger'],
type: 'close',
text: 'Delete it',
onClick: async () => {
await resource.delete(data[resource.configuration.primaryKey])
await Message.get()
.alert({
message: `The ${resource.singular().toLowerCase()} was deleted successfully.`,
timeout: 7000,
})
await onComplete()
},
}
],
})
}
}
launchResourceListing(key) {
location.assign(Session.get().url(`dash/cobalt/listing/${key}`))
}
launchResourceForm(key, id) {
location.assign(Session.get().url(`dash/cobalt/form/${key}${id ? '/' + id : ''}`))
}
goBack() {
history.back()
}
}