CustomParams.tsx 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. import React from 'react';
  2. import { omit, reject, reduce, remove } from 'lodash';
  3. import { v4 } from 'uuid';
  4. import {
  5. TopicFormCustomParams,
  6. TopicConfigByName,
  7. TopicConfigParams,
  8. } from 'redux/interfaces';
  9. import CustomParamButton from './CustomParamButton';
  10. import CustomParamField from './CustomParamField';
  11. export const INDEX_PREFIX = 'customParams';
  12. interface Props {
  13. isSubmitting: boolean;
  14. config?: TopicConfigByName;
  15. }
  16. const existingFields: string[] = [];
  17. const CustomParams: React.FC<Props> = ({ isSubmitting, config }) => {
  18. const byIndex = config
  19. ? reduce(
  20. config.byName,
  21. (result: TopicConfigParams, param, paramName) => ({
  22. ...result,
  23. [`${INDEX_PREFIX}.${new Date().getTime()}ts`]: {
  24. name: paramName,
  25. value: param.value,
  26. id: v4(),
  27. },
  28. }),
  29. {}
  30. )
  31. : {};
  32. const [
  33. formCustomParams,
  34. setFormCustomParams,
  35. ] = React.useState<TopicFormCustomParams>({
  36. byIndex,
  37. allIndexes: Object.keys(byIndex),
  38. });
  39. const onAdd = (event: React.MouseEvent<HTMLButtonElement>) => {
  40. event.preventDefault();
  41. const newIndex = `${INDEX_PREFIX}.${new Date().getTime()}ts`;
  42. setFormCustomParams({
  43. ...formCustomParams,
  44. byIndex: {
  45. ...formCustomParams.byIndex,
  46. [newIndex]: { name: '', value: '', id: v4() },
  47. },
  48. allIndexes: [newIndex, ...formCustomParams.allIndexes],
  49. });
  50. };
  51. const onRemove = (index: string) => {
  52. const fieldName = formCustomParams.byIndex[index].name;
  53. remove(existingFields, (el) => el === fieldName);
  54. setFormCustomParams({
  55. ...formCustomParams,
  56. byIndex: omit(formCustomParams.byIndex, index),
  57. allIndexes: reject(formCustomParams.allIndexes, (i) => i === index),
  58. });
  59. };
  60. const onFieldNameChange = (index: string, name: string) => {
  61. formCustomParams.byIndex[index].name = name;
  62. existingFields.push(name);
  63. };
  64. return (
  65. <>
  66. <div className="columns">
  67. <div className="column">
  68. <CustomParamButton
  69. className="is-success"
  70. type="fa-plus"
  71. onClick={onAdd}
  72. btnText="Add Custom Parameter"
  73. />
  74. </div>
  75. </div>
  76. {formCustomParams.allIndexes.map((index) => (
  77. <CustomParamField
  78. key={formCustomParams.byIndex[index].name}
  79. index={index}
  80. isDisabled={isSubmitting}
  81. name={formCustomParams.byIndex[index].name}
  82. defaultValue={formCustomParams.byIndex[index].value}
  83. existingFields={existingFields}
  84. onNameChange={onFieldNameChange}
  85. onRemove={onRemove}
  86. />
  87. ))}
  88. </>
  89. );
  90. };
  91. export default CustomParams;