Advertisement
Guest User

Untitled

a guest
Feb 18th, 2020
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.80 KB | None | 0 0
  1. import { comunicadoServiceName } from './comunicado.service'
  2. import moment from 'moment'
  3. import { COMUNICADO_BASE_STATE as COMUNICADO_STATE } from './comunicado.states'
  4.  
  5. function ComunicadoController (ComunicadoService, $commons, promiseTracker) {
  6. this.tracker = {
  7. carregando: promiseTracker(),
  8. salvando: promiseTracker()
  9. }
  10. this._comunicadoService = ComunicadoService
  11.  
  12. this.COMUNICADO_BASE_STATE = COMUNICADO_STATE
  13.  
  14. this.colors = [
  15. { hex: '#C3E6F1', name: 'Info' },
  16. { hex: '#96CAA3', name: 'Success' },
  17. { hex: '#F7DC9A', name: 'Warning' },
  18. { hex: '#F0BAB7', name: 'Danger' }
  19. ]
  20.  
  21. this.init = async () => {
  22. const buscarPromise = this._comunicadoService.getComunicado()
  23. this.tracker.carregando.addPromise(buscarPromise)
  24. const comunicadoData = await buscarPromise
  25. this.comunicado = comunicadoData
  26. this.comunicado ? this.changeModelo() : (this.comunicado = {})
  27. }
  28.  
  29. this.changeModelo = () => {
  30. const modelo = this.comunicado.modelo
  31. switch (modelo) {
  32. case '#C3E6F1':
  33. this.comunicado.modelo = 'INFO'
  34. break
  35. case '#96CAA3':
  36. this.comunicado.modelo = 'SUCCESS'
  37. break
  38. case '#F7DC9A':
  39. this.comunicado.modelo = 'WARNING'
  40. break
  41. case '#F0BAB7':
  42. this.comunicado.modelo = 'DANGER'
  43. break
  44. case 'INFO':
  45. this.comunicado.modelo = '#C3E6F1'
  46. break
  47. case 'SUCCESS':
  48. this.comunicado.modelo = '#96CAA3'
  49. break
  50. case 'WARNING':
  51. this.comunicado.modelo = '#F7DC9A'
  52. break
  53. case 'DANGER':
  54. this.comunicado.modelo = '#F0BAB7'
  55. break
  56. default:
  57. this.comunicado.modelo = null
  58. }
  59. }
  60.  
  61. this.salvar = async () => {
  62. this.changeModelo()
  63. !this.comunicado.ativo ? (this.comunicado.ativo = false) : (this.comunicado.ativo = true)
  64. try {
  65. const salvarPromiseComunicado = this._comunicadoService.saveComunicado(this.comunicado)
  66. this.tracker.addPromise(salvarPromiseComunicado)
  67. const comunicado = await salvarPromiseComunicado
  68. $commons.$notification.publish('Registro salvo com sucesso.', 'success')
  69. close(comunicado)
  70. } catch (err) {
  71. console.log(err)
  72. }
  73. }
  74.  
  75. this.excluir = async () => {
  76. try {
  77. const removePromiseComunicado = this._comunicadoService.removeComunicado(this.comunicado.id)
  78. this.tracker.addPromise(removePromiseComunicado)
  79. const comunicado = await removePromiseComunicado
  80. $commons.$notification.publish('Registro removido com sucesso.', 'success')
  81. close(comunicado)
  82. } catch (err) {
  83. console.log(err)
  84. }
  85. }
  86.  
  87. this.formataData = data => {
  88. return (
  89. data.getFullYear().toString() +
  90. '-' +
  91. ((data.getMonth() + 1).toString().length === 2
  92. ? (data.getMonth() + 1).toString()
  93. : '0' + (data.getMonth() + 1).toString()) +
  94. '-' +
  95. (data.getDate().toString().length === 2 ? data.getDate().toString() : '0' + data.getDate().toString())
  96. )
  97. }
  98.  
  99. this.dataInicialConfig = {
  100. minDate: moment(this.formataData(new Date()), 'YYYY-MM-DD').toDate()
  101. }
  102.  
  103. this.dataFinalConfig = {
  104. minDate: moment(this.formataData(new Date()), 'YYYY-MM-DD').toDate()
  105. }
  106.  
  107. this.changeConfigLast = () => {
  108. this.dataFinalConfig = {
  109. minDate: moment(this.comunicado.dataInicial).toDate()
  110. }
  111. }
  112.  
  113. this.changeConfigFirst = () => {
  114. this.dataInicialConfig = {
  115. minDate: moment().toDate(),
  116. maxDate: moment(this.comunicado.dataFinal).toDate()
  117. }
  118. }
  119.  
  120. this.init()
  121. }
  122.  
  123. ComunicadoController.$inject = [comunicadoServiceName, '$commons', 'promiseTracker']
  124.  
  125. export const comunicadoControllerName =
  126. 'minhacidade.administrando.portal.comunicado.ComunicadoController'
  127.  
  128. export default ComunicadoController
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement