Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { comunicadoServiceName } from './comunicado.service'
- import moment from 'moment'
- import { COMUNICADO_BASE_STATE as COMUNICADO_STATE } from './comunicado.states'
- function ComunicadoController (ComunicadoService, $commons, promiseTracker) {
- this.tracker = {
- carregando: promiseTracker(),
- salvando: promiseTracker()
- }
- this._comunicadoService = ComunicadoService
- this.COMUNICADO_BASE_STATE = COMUNICADO_STATE
- this.colors = [
- { hex: '#C3E6F1', name: 'Info' },
- { hex: '#96CAA3', name: 'Success' },
- { hex: '#F7DC9A', name: 'Warning' },
- { hex: '#F0BAB7', name: 'Danger' }
- ]
- this.init = async () => {
- const buscarPromise = this._comunicadoService.getComunicado()
- this.tracker.carregando.addPromise(buscarPromise)
- const comunicadoData = await buscarPromise
- this.comunicado = comunicadoData
- this.comunicado ? this.changeModelo() : (this.comunicado = {})
- }
- this.changeModelo = () => {
- const modelo = this.comunicado.modelo
- switch (modelo) {
- case '#C3E6F1':
- this.comunicado.modelo = 'INFO'
- break
- case '#96CAA3':
- this.comunicado.modelo = 'SUCCESS'
- break
- case '#F7DC9A':
- this.comunicado.modelo = 'WARNING'
- break
- case '#F0BAB7':
- this.comunicado.modelo = 'DANGER'
- break
- case 'INFO':
- this.comunicado.modelo = '#C3E6F1'
- break
- case 'SUCCESS':
- this.comunicado.modelo = '#96CAA3'
- break
- case 'WARNING':
- this.comunicado.modelo = '#F7DC9A'
- break
- case 'DANGER':
- this.comunicado.modelo = '#F0BAB7'
- break
- default:
- this.comunicado.modelo = null
- }
- }
- this.salvar = async () => {
- this.changeModelo()
- !this.comunicado.ativo ? (this.comunicado.ativo = false) : (this.comunicado.ativo = true)
- try {
- const salvarPromiseComunicado = this._comunicadoService.saveComunicado(this.comunicado)
- this.tracker.addPromise(salvarPromiseComunicado)
- const comunicado = await salvarPromiseComunicado
- $commons.$notification.publish('Registro salvo com sucesso.', 'success')
- close(comunicado)
- } catch (err) {
- console.log(err)
- }
- }
- this.excluir = async () => {
- try {
- const removePromiseComunicado = this._comunicadoService.removeComunicado(this.comunicado.id)
- this.tracker.addPromise(removePromiseComunicado)
- const comunicado = await removePromiseComunicado
- $commons.$notification.publish('Registro removido com sucesso.', 'success')
- close(comunicado)
- } catch (err) {
- console.log(err)
- }
- }
- this.formataData = data => {
- return (
- data.getFullYear().toString() +
- '-' +
- ((data.getMonth() + 1).toString().length === 2
- ? (data.getMonth() + 1).toString()
- : '0' + (data.getMonth() + 1).toString()) +
- '-' +
- (data.getDate().toString().length === 2 ? data.getDate().toString() : '0' + data.getDate().toString())
- )
- }
- this.dataInicialConfig = {
- minDate: moment(this.formataData(new Date()), 'YYYY-MM-DD').toDate()
- }
- this.dataFinalConfig = {
- minDate: moment(this.formataData(new Date()), 'YYYY-MM-DD').toDate()
- }
- this.changeConfigLast = () => {
- this.dataFinalConfig = {
- minDate: moment(this.comunicado.dataInicial).toDate()
- }
- }
- this.changeConfigFirst = () => {
- this.dataInicialConfig = {
- minDate: moment().toDate(),
- maxDate: moment(this.comunicado.dataFinal).toDate()
- }
- }
- this.init()
- }
- ComunicadoController.$inject = [comunicadoServiceName, '$commons', 'promiseTracker']
- export const comunicadoControllerName =
- 'minhacidade.administrando.portal.comunicado.ComunicadoController'
- export default ComunicadoController
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement