Skip to content

Instantly share code, notes, and snippets.

@wilcorrea
Created August 24, 2020 11:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wilcorrea/eb0036c684b8d9d2ebc304da0579b3ec to your computer and use it in GitHub Desktop.
Save wilcorrea/eb0036c684b8d9d2ebc304da0579b3ec to your computer and use it in GitHub Desktop.
import Schema from '@devitools/Agnostic/Schema'
import Service from './InviteService'
import { domain } from '../settings'
import { Component, Context, SchemaTable } from '@devitools/Agnostic/Helper/interfaces'
import { positions, scopes, SCOPES } from '@devitools/Agnostic/enum'
import { unique } from '@devitools/Util/general'
import { RULES } from 'src/settings/schema'
import { replacement } from '@devitools/Util/string'
import { inviteHistoryMap } from '../resources/helper'
/**
* @type {InviteSchema}
*/
export default class InviteSchema extends Schema {
/**
* @type {string}
*/
static domain = domain
/**
* @type {string}
*/
static displayKey = 'code'
/**
* @type {Http}
*/
service = Service
/**
* @param {Component} $component
* @param {Record<string, unknown>} dependencies
* Call schema builder method
*/
construct ($component?: Component, dependencies?: Record<string, unknown>): void {
this.getField('createdAt')
.fieldTableShow()
.fieldIsDate()
this.addField('link')
.fieldScopes([SCOPES.SCOPE_VIEW, SCOPES.SCOPE_EDIT])
.fieldFormDisabled()
.fieldAvoid()
.fieldOn('click', function ({ $event }: { $event: Event }) {
const target = $event.target as HTMLTextAreaElement
target.select()
})
this.addField('code')
.fieldTableShow()
.fieldFormDisabled(true, { [SCOPES.SCOPE_ADD]: false })
.validationRequired()
.fieldOn('click', function ({ $event }: { $event: Event }) {
const target = $event.target as HTMLTextAreaElement
target.select()
})
this.addField('type')
.fieldIsRadio()
.fieldTableShow()
.fieldFormWidth(50)
.validationRequired()
this.addField('active')
.fieldIsToggle()
.fieldTableShow()
.fieldFormWidth(50)
.fieldFormDefaultValue(true)
this.addField('notes')
.fieldIsText()
this.addField('last')
.fieldIsDatetime()
.fieldTableShow()
.fieldScopes([SCOPES.SCOPE_INDEX])
this.addField('history')
.fieldScopes([SCOPES.SCOPE_VIEW, SCOPES.SCOPE_EDIT])
.fieldIs('AppTimeline')
.fieldAppendAttrs({ map: inviteHistoryMap })
.fieldFormHidden(true)
.fieldAvoid()
.fieldFormErrorHide()
this.addAction('actionLink')
.actionIcon('link')
.actionLevels([RULES.LEVEL_INDEX])
.actionScopes(scopes())
.actionPositions(positions())
this.addHook('fetch:record', function (this: Component) {
const code = this.$getField('code').$getValue()
const template = String(this.$lang('actions.actionLink.link'))
const replaces = { code }
this.$getField('link').$setValue(replacement(template, replaces))
const history = this.$getField('history').$getValue()
if (!Array.isArray(history)) {
return
}
this.$getField('history').$fieldFormHidden(history.length === 0)
})
}
/**
* @param {Record<string, Record<string, unknown>>} payload
*/
actionLink (this: SchemaTable, payload: Record<string, Context>) {
const { context } = payload
const success = (record: Record<string, unknown>) => {
const template = String(this.$lang('actions.actionLink.template'))
const replaces = { code: record.code }
const title = String(this.$lang('actions.actionLink.title'))
this.$alert(replacement(template, replaces), { title })
}
this.withRecord(context, success)
}
/**
* @param {InviteSchema} schema
*/
createdHook (this: Component, schema: Schema) {
if (this.scope !== SCOPES.SCOPE_ADD) {
return
}
this.$getField('code').$setValue(unique())
}
/**
* @param {Record<string, *>} options
* @returns {*}
*/
static provideRemote (options: Record<string, unknown> = {}) {
const provide = super.provideRemote(options)
return { ...provide, uppercase: false }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment