Automation.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import { useEffect, useState } from "react";
  2. import { Button, Col, Icon, Row, Steps } from "tiny-ui";
  3. const { Step } = Steps;
  4. const Automation = ({
  5. automation,
  6. presets,
  7. clients,
  8. names,
  9. onPreset,
  10. ...props
  11. }) => {
  12. const [current, setCurrent] = useState(-1);
  13. const [total, setTotal] = useState();
  14. useEffect(() => {
  15. setTotal((automation && Object.keys(automation)) || 0);
  16. }, [automation]);
  17. useEffect(() => {
  18. if (current > -1) {
  19. let indx = current + 1;
  20. let payload = {
  21. clients: automation?.[indx] || [],
  22. value: indx
  23. };
  24. onPreset(payload);
  25. }
  26. }, [current]);
  27. const getTitle = (indx) => presets?.[indx]?.name || indx;
  28. const getName = (v) => {
  29. let indx = clients?.findIndex((o) => o === v);
  30. return indx > -1 ? names?.[indx] : v;
  31. };
  32. const getDescription = (indx) => {
  33. return automation?.[indx]?.map((o) => getName(o))?.join(", ");
  34. };
  35. let steps = Object.keys(automation).map((o, i) => {
  36. return <Step key={i} title={getTitle(o)} description={getDescription(o)} />;
  37. });
  38. return (
  39. <Row>
  40. <Col span="12">
  41. <Steps
  42. current={current}
  43. direction="vertical"
  44. onChange={(v) => setCurrent(v)}
  45. >
  46. {steps}
  47. </Steps>
  48. </Col>
  49. <Col span="12">
  50. <Button.Group size="lg" round>
  51. <Button
  52. title="Previous"
  53. icon={<Icon name="arrow-left" />}
  54. onClick={() => {
  55. let next = current - 1;
  56. if (next < 0) next = 0;
  57. setCurrent(next);
  58. }}
  59. />
  60. <Button
  61. title="Next"
  62. icon={<Icon name="arrow-right" />}
  63. onClick={() => {
  64. let next = current + 1;
  65. if (next > total) next = 0;
  66. setCurrent(next);
  67. }}
  68. />
  69. </Button.Group>
  70. </Col>
  71. </Row>
  72. );
  73. };
  74. export default Automation;