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.
54 lines
2.0 KiB
54 lines
2.0 KiB
import { location_service } from './Location.service.js'
|
|
import { resource_service } from './Resource.service.js'
|
|
|
|
class ActionService {
|
|
async perform({ text = '', action, ...args }) {
|
|
if ( action === 'redirect' ) {
|
|
if ( args.next ) {
|
|
return location_service.redirect(args.next, args.delay || 0)
|
|
}
|
|
} else if ( action === 'back' ) {
|
|
return location_service.back()
|
|
} else if ( args.type === 'resource' ) {
|
|
const { resource } = args
|
|
if ( action === 'insert' ) {
|
|
return location_service.redirect(`/dash/c/form/${resource}`, 0)
|
|
} else if ( action === 'update' ) {
|
|
const { id } = args
|
|
return location_service.redirect(`/dash/c/form/${resource}?id=${id}`, 0)
|
|
} else if ( action === 'delete' ) {
|
|
const { id } = args
|
|
const rsc = await resource_service.get(resource)
|
|
await rsc.delete(id)
|
|
} else if ( action === 'list' ) {
|
|
return location_service.redirect(`/dash/c/listing/${resource}`, 0)
|
|
}
|
|
} else if ( action === 'post' ) {
|
|
const inputs = []
|
|
|
|
if ( args.params ) {
|
|
for (const param in args.params) {
|
|
if ( !args.params.hasOwnProperty(param) ) continue
|
|
inputs.push(`<input type="hidden" name="${param}" value="${args.params[param]}"/>`)
|
|
}
|
|
}
|
|
|
|
const form_attrs = ['method="POST"']
|
|
if ( args.destination ) {
|
|
form_attrs.push(`action="${args.destination}"`)
|
|
}
|
|
|
|
$(`
|
|
<form ${form_attrs.join(' ')}>
|
|
${inputs.join('\n')}
|
|
</form>
|
|
`).appendTo('body').submit()
|
|
} else {
|
|
throw new TypeError(`Unknown action type: ${action}`)
|
|
}
|
|
}
|
|
}
|
|
|
|
const action_service = new ActionService()
|
|
export { action_service }
|