Create.tsx 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. import React from 'react';
  2. import { assocPath } from 'ramda';
  3. import Navbar from 'components/Navbar';
  4. import { Grid, Row, Cell } from '@material/react-layout-grid';
  5. import Input from 'components/Input';
  6. import TagInput from 'components/TagInput';
  7. import './styles.scss';
  8. import sample_img from "../../sample.png";
  9. import IRecipe, { ISubRecipe, IAuthor, IDetails, _recipe, IIngredient } from 'types/recipes';
  10. type CreateRecipeProps = {
  11. };
  12. type CreateRecipeState = {
  13. form: IRecipe
  14. };
  15. // author?: {
  16. // name: string,
  17. // },
  18. // details?: {
  19. // preparationTime: string,
  20. // cookingTime: string,
  21. // servings: number
  22. // },
  23. // ingredients?: ISubRecipe[],
  24. // instructions?: string[],
  25. // tags?: string[],
  26. type FormKeys = keyof IRecipe | keyof ISubRecipe | keyof IIngredient | keyof IAuthor | keyof IDetails | number;
  27. class CreateRecipe extends React.Component<CreateRecipeProps, CreateRecipeState> {
  28. constructor(props: any) {
  29. super(props);
  30. this.state = {
  31. form: { ..._recipe }
  32. }
  33. }
  34. updateField(key: FormKeys[]) {
  35. return (e: any) => {
  36. const newValue = e.currentTarget.value;
  37. this.setState({
  38. form: assocPath(key, newValue, this.state.form)
  39. } as Pick<CreateRecipeState, keyof CreateRecipeState>)
  40. }
  41. }
  42. componentDidUpdate() {
  43. }
  44. render() {
  45. const { form } = this.state;
  46. return (
  47. <div>
  48. <Navbar
  49. title="Create a recipe"
  50. />
  51. <div className="cbk-create-recipe">
  52. <Grid>
  53. <Row>
  54. <Cell columns={2}>
  55. <img src={sample_img} style={{ width: '100%' }}/>
  56. </Cell>
  57. <Cell columns={10}>
  58. <Input
  59. label='name'
  60. value={form.name}
  61. onChange={this.updateField(['name'])}
  62. style='display'
  63. />
  64. <Input
  65. label='summary'
  66. helperText='Recipe description'
  67. value={form.summary}
  68. onChange={this.updateField(['summary'])}
  69. textarea
  70. />
  71. </Cell>
  72. </Row>
  73. <h5>Author Information</h5>
  74. <Row>
  75. <Cell columns={3}>
  76. <Input
  77. label='name'
  78. value={form.author.name}
  79. onChange={this.updateField(['author', 'name'])}
  80. />
  81. </Cell>
  82. <Cell columns={3}>
  83. <Input
  84. label='website'
  85. value={form.author.website}
  86. onChange={this.updateField(['author', 'website'])}
  87. />
  88. </Cell>
  89. </Row>
  90. <h5>Recipe Information</h5>
  91. <Row>
  92. <Cell columns={3}>
  93. <Input
  94. label='Preparation time'
  95. value={form.details.preparationTime}
  96. onChange={this.updateField(['details', 'preparationTime'])}
  97. />
  98. </Cell>
  99. <Cell columns={3}>
  100. <Input
  101. label='Cooking time'
  102. value={form.details.cookingTime}
  103. onChange={this.updateField(['details', 'cookingTime'])}
  104. />
  105. </Cell>
  106. <Cell columns={3}>
  107. <Input
  108. label='servings'
  109. value={form.details.servings}
  110. onChange={this.updateField(['details', 'servings'])}
  111. type='number'
  112. />
  113. </Cell>
  114. <Cell columns={3}>
  115. <Input
  116. label='recipe url'
  117. value={form.details.url || ''}
  118. onChange={this.updateField(['details', 'url'])}
  119. />
  120. </Cell>
  121. <Cell columns={12}>
  122. <TagInput
  123. onNewTag={(tag) => { console.log(tag) }}
  124. />
  125. </Cell>
  126. </Row>
  127. <h5>Ingredients</h5>
  128. {
  129. form.ingredients.map((group, i) => (
  130. <div key={i}>
  131. <Row>
  132. <Cell columns={3}>
  133. <Input
  134. label='group name'
  135. value={group.name}
  136. onChange={this.updateField(['ingredients', i, 'name'])}
  137. />
  138. </Cell>
  139. </Row>
  140. {
  141. group.ingredients.map((ingredient, j) => (
  142. <Row key={j}>
  143. <Cell columns={3}>
  144. <Input
  145. label="ingredient"
  146. value={ingredient.name}
  147. onChange={this.updateField(['ingredients', i, 'ingredients', j, 'name'])}
  148. />
  149. </Cell>
  150. <Cell columns={3}>
  151. <Input
  152. label="quantity"
  153. value={ingredient.quantity}
  154. onChange={this.updateField(['ingredients', i, 'ingredients', j, 'quantity'])}
  155. />
  156. </Cell>
  157. <Cell columns={3}>
  158. <Input
  159. label="unit"
  160. value={ingredient.unit}
  161. onChange={this.updateField(['ingredients', i, 'ingredients', j, 'unit'])}
  162. />
  163. </Cell>
  164. <Cell columns={3}>
  165. <Input
  166. label="original/notes"
  167. value={ingredient._original}
  168. onChange={this.updateField(['ingredients', i, 'ingredients', j, '_original'])}
  169. />
  170. </Cell>
  171. </Row>
  172. ))
  173. }
  174. </div>
  175. ))
  176. }
  177. </Grid>
  178. </div>
  179. </div>
  180. )
  181. }
  182. }
  183. export default CreateRecipe;