index.tsx 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. import React from 'react';
  2. import Input from 'components/Input';
  3. import Select from 'components/Select';
  4. import TabBar from 'components/TabBar';
  5. import Suggestions from 'components/Ingredient/Suggestions';
  6. import { SubRecipe, _recipe, _subRecipe, _ingredient } from 'types/recipes';
  7. import './styles.scss';
  8. type IngredientFormProps = {
  9. components: SubRecipe[],
  10. updateField: (key: any[]) => (e: any) => void,
  11. addSubrecipe: () => void,
  12. updateSubrecipeName: (subrecipe: number, newValue: string) => void,
  13. removeSubrecipe: (index: number) => void,
  14. updateIngredientUnit: (key: Array<string|number>, newValue: string) => void,
  15. };
  16. type SelectOption = {
  17. label: string,
  18. value: string,
  19. }
  20. type IngredientFormState = {
  21. activeGroup: number,
  22. unitOptions: SelectOption[],
  23. normalizeUnits: any,
  24. }
  25. class IngredientForm extends React.Component<IngredientFormProps, IngredientFormState>{
  26. constructor(props: IngredientFormProps) {
  27. super(props);
  28. this.state = {
  29. unitOptions: [
  30. { label: 'gr', value: 'gr'},
  31. { label: 'cup', value: 'cup'},
  32. { label: 'tbsp', value: 'tbsp'},
  33. { label: 'tsp', value: 'tsp'},
  34. { label: 'stick', value: 'stick'},
  35. { label: 'ml', value: 'ml'},
  36. { label: 'oz', value: 'oz'},
  37. { label: 'lb', value: 'lb'},
  38. ],
  39. activeGroup: 0,
  40. normalizeUnits: {
  41. tablespoon: 'tbsp',
  42. teaspoon: 'tsp',
  43. pound: 'lb',
  44. ounce: 'oz'
  45. }
  46. };
  47. }
  48. handleActiveIndexUpdate = (activeGroup: number) => this.setState({ activeGroup })
  49. normalizeUnits = (unit: string): string => this.state.normalizeUnits[unit] || unit;
  50. onChangeUnit = (key: Array<string|number>) =>
  51. (selection: SelectOption) => this.props.updateIngredientUnit(key, selection.value);
  52. render() {
  53. const {updateField, components, addSubrecipe, updateSubrecipeName, removeSubrecipe } = this.props;
  54. const { activeGroup, unitOptions, normalizeUnits } = this.state;
  55. const tabs = components.map(group => group.name);
  56. return (
  57. <div className='cbk-ingredient-form'>
  58. <div className='cbk-ingredients-subrecipe-list'>
  59. <TabBar
  60. tabs={tabs}
  61. onAddTab={addSubrecipe}
  62. onUpdateTab={updateSubrecipeName}
  63. onDeleteTab={removeSubrecipe}
  64. onSelectTab={this.handleActiveIndexUpdate}
  65. activeIndex={this.state.activeGroup}
  66. />
  67. </div>
  68. <div className='cbk-ingredients-component'>
  69. <div className='cbk-ingredients-component-header'>
  70. <span>Ingredient Name</span>
  71. <span>Quantity</span>
  72. <span>Unit</span>
  73. <span>Notes</span>
  74. <span>Original</span>
  75. </div>
  76. {
  77. components[activeGroup].ingredients.map((ingredient, index) => (
  78. <div key={index}>
  79. <div className='cbk-ingredients-component-item'>
  80. <Input
  81. label=''
  82. value={ingredient.name}
  83. onChange={updateField(['ingredients', activeGroup, 'ingredients', index, 'name'])}
  84. />
  85. <Input
  86. label=''
  87. value={ingredient.quantity}
  88. onChange={updateField(['ingredients', activeGroup, 'ingredients', index, 'quantity'])}
  89. />
  90. <Select
  91. label=''
  92. value={this.normalizeUnits(ingredient.unit)}
  93. options={unitOptions}
  94. onChange={this.onChangeUnit(['ingredients', activeGroup, 'ingredients', index, 'unit'])}
  95. />
  96. <Input
  97. label=''
  98. value={''}
  99. onChange={updateField(['ingredients', activeGroup, 'ingredients', index, 'notes'])}
  100. />
  101. <Input
  102. label=''
  103. value={ingredient._original}
  104. onChange={updateField(['ingredients', activeGroup, 'ingredients', index, '_original'])}
  105. />
  106. </div>
  107. <Suggestions
  108. suggestions={ingredient.suggestions}
  109. onSelect={(idx) => console.log('selected', idx)}
  110. />
  111. </div>
  112. ))
  113. }
  114. </div>
  115. </div>
  116. )
  117. }
  118. }
  119. export default IngredientForm;