handler-loop-data.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. import { generateJSON } from '@/utils/data-exporter';
  2. import { getBlockConnection } from '../helper';
  3. function loopData(block) {
  4. return new Promise((resolve, reject) => {
  5. const { data } = block;
  6. const nextBlockId = getBlockConnection(block);
  7. if (this.loopList[data.loopId]) {
  8. this.loopList[data.loopId].index += 1;
  9. let currentLoopData;
  10. if (data.loopThrough === 'numbers') {
  11. currentLoopData = this.loopData[data.loopId] + 1;
  12. } else {
  13. currentLoopData =
  14. this.loopList[data.loopId].data[this.loopList[data.loopId].index];
  15. }
  16. this.loopData[data.loopId] = currentLoopData;
  17. } else {
  18. let currLoopData;
  19. switch (data.loopThrough) {
  20. case 'numbers':
  21. currLoopData = data.fromNumber;
  22. break;
  23. case 'data-columns':
  24. currLoopData = generateJSON(Object.keys(this.data), this.data);
  25. break;
  26. case 'google-sheets':
  27. currLoopData = this.googleSheets[data.referenceKey];
  28. break;
  29. case 'custom-data':
  30. currLoopData = JSON.parse(data.loopData);
  31. break;
  32. default:
  33. }
  34. if (data.loopThrough !== 'number' && !Array.isArray(currLoopData)) {
  35. const error = new Error('invalid-loop-data');
  36. error.nextBlockId = nextBlockId;
  37. reject(error);
  38. return;
  39. }
  40. this.loopList[data.loopId] = {
  41. index: 0,
  42. data: currLoopData,
  43. id: data.loopId,
  44. blockId: block.id,
  45. type: data.loopThrough,
  46. maxLoop:
  47. data.loopThrough === 'numbers'
  48. ? data.toNumber + 1 - data.fromNumber
  49. : data.maxLoop || currLoopData.length,
  50. };
  51. /* eslint-disable-next-line */
  52. this.loopData[data.loopId] = data.loopThrough === 'numbers' ? data.fromNumber : currLoopData[0];
  53. }
  54. resolve({
  55. nextBlockId,
  56. data: this.loopData[data.loopId],
  57. });
  58. });
  59. }
  60. export default loopData;