Advertisement
claukiller

Untitled

May 29th, 2020
495
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.46 KB | None | 0 0
  1. Probar;
  2. com;
  3. muchos;
  4. campos;
  5.  
  6. import Vue from 'vue';
  7. import { mapActions, mapGetters } from 'vuex';
  8.  
  9. import { ApiEnum, CrudDataTypeEnum, FilterTypeEnum } from '../enums';
  10.  
  11. import {
  12. ApiError,
  13. Dictionary,
  14. Filter,
  15. RegularFilter,
  16. RegularFilterConfig,
  17. RegularFilterSelectedValues,
  18. RegularFilterValueConfig,
  19. ToolConfig
  20. } from '../models';
  21.  
  22. import { ROUTER_CONSTS } from '../router';
  23.  
  24. import { CONFIG_CONSTS } from '../store/config/config.consts';
  25. import { CONTEXT_CONSTS } from '../store/context/context.consts';
  26. import { CRUD_CONSTS } from '../store/crud/crud.consts';
  27.  
  28. export const CrudFormMixin = {
  29. computed: {
  30. ...mapGetters(CONFIG_CONSTS.moduleName, {
  31. environmentConfig: CONFIG_CONSTS.getters.currentEnvironment
  32. }),
  33. ...mapGetters(CONTEXT_CONSTS.moduleName, {
  34. context: CONTEXT_CONSTS.getters.context
  35. }),
  36. ...mapGetters(CRUD_CONSTS.moduleName, {
  37. state: CRUD_CONSTS.getters.state
  38. }),
  39. toolConfig: function (): ToolConfig {
  40. const toolsConfig = this.environmentConfig.tools;
  41. return Object.assign({ pivotIds: [] }, (!!toolsConfig && toolsConfig.default), (!!toolsConfig && toolsConfig[this.dataType]));
  42. },
  43. pivots: function (): Dictionary<RegularFilter> {
  44. return (Object.values(this.context.filters).reduce((pivots: Dictionary<RegularFilter>, filter: Filter) => {
  45. if (this.toolConfig.pivotIds.includes(filter.config.id)) {
  46. const filterConfig = (filter.config as RegularFilterConfig);
  47. pivots[filter.config.id] = {
  48. config: {
  49. ...filterConfig,
  50. mandatory: true
  51. },
  52. selectedValues: this.getFilterSelectedValues(filterConfig)
  53. };
  54. }
  55. return pivots;
  56. }, {}) as Dictionary<RegularFilter>);
  57. },
  58. pivots: function () {
  59. return {
  60. lang: {
  61. config: {
  62. id: 'lang',
  63. type: FilterTypeEnum.REGULAR,
  64. mandatory: true,
  65. values: [
  66. {
  67. id: 'en'
  68. }
  69. ]
  70. },
  71. selectedValues: []
  72. },
  73. division2: {
  74. config: {
  75. id: 'section',
  76. prettyName: 'Division',
  77. type: FilterTypeEnum.REGULAR,
  78. mandatory: true,
  79. values: [
  80. {
  81. id: '011'
  82. },
  83. {
  84. id: '014'
  85. },
  86. {
  87. id: '016'
  88. },
  89. {
  90. id: '018'
  91. },
  92. {
  93. id: '021'
  94. },
  95. {
  96. id: '024'
  97. },
  98. {
  99. id: '025'
  100. },
  101. {
  102. id: '026'
  103. },
  104. {
  105. id: '029'
  106. },
  107. {
  108. id: '034'
  109. },
  110. {
  111. id: '035'
  112. },
  113. {
  114. id: '090'
  115. },
  116. {
  117. id: '105'
  118. },
  119. {
  120. id: '531'
  121. },
  122. {
  123. id: '534'
  124. },
  125. {
  126. id: '615'
  127. },
  128. {
  129. id: '620'
  130. },
  131. {
  132. id: '660'
  133. },
  134. {
  135. id: '701'
  136. },
  137. {
  138. id: '703'
  139. },
  140. {
  141. id: '704'
  142. },
  143. {
  144. id: '705'
  145. },
  146. {
  147. id: '706'
  148. },
  149. {
  150. id: '708'
  151. }
  152. ]
  153. },
  154. selectedValues: []
  155. },
  156. division3: {
  157. config: {
  158. id: 'section',
  159. prettyName: 'Division',
  160. type: FilterTypeEnum.REGULAR,
  161. mandatory: true,
  162. values: [
  163. {
  164. id: '011'
  165. },
  166. {
  167. id: '014'
  168. },
  169. {
  170. id: '016'
  171. },
  172. {
  173. id: '018'
  174. },
  175. {
  176. id: '021'
  177. },
  178. {
  179. id: '024'
  180. },
  181. {
  182. id: '025'
  183. },
  184. {
  185. id: '026'
  186. },
  187. {
  188. id: '029'
  189. },
  190. {
  191. id: '034'
  192. },
  193. {
  194. id: '035'
  195. },
  196. {
  197. id: '090'
  198. },
  199. {
  200. id: '105'
  201. },
  202. {
  203. id: '531'
  204. },
  205. {
  206. id: '534'
  207. },
  208. {
  209. id: '615'
  210. },
  211. {
  212. id: '620'
  213. },
  214. {
  215. id: '660'
  216. },
  217. {
  218. id: '701'
  219. },
  220. {
  221. id: '703'
  222. },
  223. {
  224. id: '704'
  225. },
  226. {
  227. id: '705'
  228. },
  229. {
  230. id: '706'
  231. },
  232. {
  233. id: '708'
  234. }
  235. ]
  236. },
  237. selectedValues: []
  238. },
  239. division5: {
  240. config: {
  241. id: 'section',
  242. prettyName: 'Division',
  243. type: FilterTypeEnum.REGULAR,
  244. mandatory: true,
  245. values: [
  246. {
  247. id: '011'
  248. },
  249. {
  250. id: '014'
  251. },
  252. {
  253. id: '016'
  254. },
  255. {
  256. id: '018'
  257. },
  258. {
  259. id: '021'
  260. },
  261. {
  262. id: '024'
  263. },
  264. {
  265. id: '025'
  266. },
  267. {
  268. id: '026'
  269. },
  270. {
  271. id: '029'
  272. },
  273. {
  274. id: '034'
  275. },
  276. {
  277. id: '035'
  278. },
  279. {
  280. id: '090'
  281. },
  282. {
  283. id: '105'
  284. },
  285. {
  286. id: '531'
  287. },
  288. {
  289. id: '534'
  290. },
  291. {
  292. id: '615'
  293. },
  294. {
  295. id: '620'
  296. },
  297. {
  298. id: '660'
  299. },
  300. {
  301. id: '701'
  302. },
  303. {
  304. id: '703'
  305. },
  306. {
  307. id: '704'
  308. },
  309. {
  310. id: '705'
  311. },
  312. {
  313. id: '706'
  314. },
  315. {
  316. id: '708'
  317. }
  318. ]
  319. },
  320. selectedValues: []
  321. },
  322. division: {
  323. config: {
  324. id: 'section',
  325. prettyName: 'Division',
  326. type: FilterTypeEnum.REGULAR,
  327. mandatory: true,
  328. values: [
  329. {
  330. id: '011'
  331. },
  332. {
  333. id: '014'
  334. },
  335. {
  336. id: '016'
  337. },
  338. {
  339. id: '018'
  340. },
  341. {
  342. id: '021'
  343. },
  344. {
  345. id: '024'
  346. },
  347. {
  348. id: '025'
  349. },
  350. {
  351. id: '026'
  352. },
  353. {
  354. id: '029'
  355. },
  356. {
  357. id: '034'
  358. },
  359. {
  360. id: '035'
  361. },
  362. {
  363. id: '090'
  364. },
  365. {
  366. id: '105'
  367. },
  368. {
  369. id: '531'
  370. },
  371. {
  372. id: '534'
  373. },
  374. {
  375. id: '615'
  376. },
  377. {
  378. id: '620'
  379. },
  380. {
  381. id: '660'
  382. },
  383. {
  384. id: '701'
  385. },
  386. {
  387. id: '703'
  388. },
  389. {
  390. id: '704'
  391. },
  392. {
  393. id: '705'
  394. },
  395. {
  396. id: '706'
  397. },
  398. {
  399. id: '708'
  400. }
  401. ]
  402. },
  403. selectedValues: []
  404. }
  405. };
  406. },
  407. visiblePivots: function (): RegularFilter[] {
  408. return Object.values(this.pivots).filter((pivot: RegularFilter) => pivot.config.values.length > 1);
  409. },
  410. filters: function (): Dictionary<RegularFilter> {
  411. return (Object.values(this.context.filters).reduce((filters: Dictionary<RegularFilter>, filter: Filter) => {
  412. if ((filter.config.type === FilterTypeEnum.REGULAR) && !this.toolConfig.pivotIds.includes(filter.config.id)) {
  413. const filterConfig = (filter.config as RegularFilterConfig);
  414. filters[filter.config.id] = {
  415. config: filterConfig,
  416. selectedValues: this.getFilterSelectedValues(filterConfig)
  417. };
  418. }
  419. return filters;
  420. }, {}) as Dictionary<RegularFilter>);
  421. },
  422. valid: function (): boolean {
  423. return !Object.values(this.pivots).concat(Object.values(this.filters)).some((filter: RegularFilter) =>
  424. (filter.config.mandatory && !filter.selectedValues.length));
  425. }
  426. },
  427. data: function () {
  428. return {
  429. dataType: undefined,
  430. item: {},
  431. updateMode: false,
  432. serviceErrorsFields: [],
  433. files: undefined,
  434. messages: {
  435. create: {
  436. success: undefined
  437. },
  438. update: {
  439. success: undefined
  440. }
  441. }
  442. };
  443. },
  444. props: {
  445. loading: Object
  446. },
  447. methods: {
  448. ...mapActions(CRUD_CONSTS.moduleName, {
  449. read: CRUD_CONSTS.actions.read,
  450. create: CRUD_CONSTS.actions.create,
  451. update: CRUD_CONSTS.actions.update,
  452. delete: CRUD_CONSTS.actions.delete
  453. }),
  454. onItemLoad: function (): void {
  455. Object.values(this.pivots).concat(Object.values(this.filters)).forEach((filter: RegularFilter) => {
  456. if (filter.config.mandatory && !filter.selectedValues.length) {
  457. const filterDefaultValueId = (filter.config.defaultValueId ||
  458. ((filter.config.values.length === 1) && filter.config.values[0].id));
  459. if (!!filterDefaultValueId) {
  460. this.onFilterChange(filter, [filterDefaultValueId]);
  461. }
  462. }
  463. });
  464. },
  465. getFilterSelectedValues: function (filterConfig: RegularFilterConfig): RegularFilterSelectedValues {
  466. const filterId = (filterConfig.taggingId || filterConfig.id);
  467. let itemFilterValues: (string[] | string);
  468. if (this.toolConfig.pivotIds.includes(filterConfig.id)) {
  469. itemFilterValues = this.item.context.pivots[filterId];
  470. } else if (!!this.item.filters) {
  471. itemFilterValues = this.item.filters[filterId];
  472. }
  473. if (!Array.isArray(itemFilterValues)) {
  474. itemFilterValues = (!!itemFilterValues ? [itemFilterValues] : []);
  475. }
  476. return itemFilterValues.map((itemFilterValue: string) => {
  477. const itemFilterValueConfig = filterConfig.values.find((valueConfig: RegularFilterValueConfig) =>
  478. ((valueConfig.taggingId || valueConfig.id) === itemFilterValue));
  479. return ((!!itemFilterValueConfig && itemFilterValueConfig.id) || itemFilterValue);
  480. });
  481. },
  482. onFilterChange: function (filter: RegularFilter, selectedValues: RegularFilterSelectedValues): void {
  483. let itemFilterValues: (string[] | string) = selectedValues.map((selectedValue: string) => {
  484. const selectedValueConfig = filter.config.values.find((valueConfig: RegularFilterValueConfig) =>
  485. (valueConfig.id === selectedValue));
  486. return (selectedValueConfig.taggingId || selectedValueConfig.id);
  487. });
  488. if (filter.config.multiselectDisabled) {
  489. itemFilterValues = itemFilterValues[0];
  490. }
  491. const filterId = (filter.config.taggingId || filter.config.id);
  492. let itemFiltersObject;
  493. if (this.toolConfig.pivotIds.includes(filter.config.id)) {
  494. itemFiltersObject = this.item.context.pivots;
  495. } else {
  496. itemFiltersObject = this.item.filters;
  497. }
  498. Vue.set(itemFiltersObject, filterId, itemFilterValues);
  499. },
  500. onAccept: async function (): Promise<void> {
  501. this.$emit('updateLoading', { visible: true });
  502. try {
  503. if (!this.updateMode) {
  504. await this.create({ dataType: this.dataType, items: [this.item], files: this.files });
  505. this.$emit('showSnackbar', this.messages.create.success);
  506. } else {
  507. await this.update({ dataType: this.dataType, items: [this.item], files: this.files });
  508. this.$emit('showSnackbar', this.messages.update.success);
  509. }
  510. this.$emit('updateLoading', { visible: false });
  511. this.$router.push({
  512. name: this.$route.meta.parentRouteName
  513. });
  514. } catch (error) {
  515. const apiEnum = this.getApiEnum();
  516. const errorResponse: ApiError = this.$epb.utils.decodeErrorResponse(error, apiEnum);
  517. if (!!errorResponse.details && (errorResponse.status === 400)) {
  518. this.serviceErrorsFields = errorResponse.details.fields;
  519. } else {
  520. this.$emit('showError', this.$epb.utils.decodeErrorMessage(error, apiEnum));
  521. }
  522. this.$emit('updateLoading', { visible: false });
  523. }
  524. },
  525. getApiEnum: function (): ApiEnum {
  526. if (this.dataType === CrudDataTypeEnum.USERS) {
  527. return ApiEnum.USERS;
  528. }
  529. return ApiEnum.PLAY;
  530. },
  531. onCancel: function (): void {
  532. this.$router.push({
  533. name: this.$route.meta.parentRouteName
  534. });
  535. },
  536. onDisableConflict: async function (item): Promise<void> {
  537. this.$emit('updateLoading', { visible: true });
  538. try {
  539. await this.update({
  540. dataType: this.dataType, items: [
  541. {
  542. ...item,
  543. enabled: false
  544. }
  545. ]
  546. });
  547. this.$emit('showSnackbar', this.messages.conflicts.disable.success);
  548. this.removeConflictFromErrorsFields(item);
  549. } catch (error) {
  550. this.$emit('showError', this.$epb.utils.decodeErrorMessage(error, this.getApiEnum()));
  551. } finally {
  552. this.$emit('updateLoading', { visible: false });
  553. }
  554. },
  555. onDeleteConflict: async function (item): Promise<void> {
  556. this.$emit('updateLoading', { visible: true });
  557. try {
  558. await this.delete({ dataType: this.dataType, ids: [item.id] });
  559. this.$emit('showSnackbar', this.messages.conflicts.delete.success);
  560. this.removeConflictFromErrorsFields(item);
  561. } catch (error) {
  562. this.$emit('showError', this.$epb.utils.decodeErrorMessage(error, this.getApiEnum()));
  563. } finally {
  564. this.$emit('updateLoading', { visible: false });
  565. }
  566. },
  567. removeConflictFromErrorsFields: function (item): void {}
  568. },
  569. created: async function (): Promise<void> {
  570. const id = this.$route.params[ROUTER_CONSTS.params.id];
  571. if (!!id) {
  572. let item = this.state.data.find((dataItem: any) => (dataItem.id === id));
  573. if (!item || (this.state.dataType !== this.dataType)) {
  574. this.$emit('updateLoading', { visible: true });
  575. try {
  576. await this.read({ dataType: this.dataType, ids: [id] });
  577. item = this.state.data.find((dataItem: any) => (dataItem.id === id));
  578. this.$emit('updateLoading', { visible: false });
  579. } catch (error) {
  580. this.$emit('showError', this.$epb.utils.decodeErrorMessage(error, this.getApiEnum()));
  581. this.$emit('updateLoading', { visible: false });
  582. }
  583. }
  584. if (!!item) {
  585. this.item = Object.assign({}, item);
  586. }
  587. }
  588. this.onItemLoad();
  589. }
  590. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement