You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

EditCategory.js 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. import React from "react";
  2. import PropTypes from "prop-types";
  3. import BackdropComponent from "../../MUI/BackdropComponent";
  4. import {
  5. ButtonsContainer,
  6. CategoryTitleField,
  7. EditCategoryContainer,
  8. EditCategoryImagePicker,
  9. EditCategoryTitle,
  10. FieldLabel,
  11. InputContainer,
  12. SaveButton,
  13. SupportedFormats,
  14. XIcon,
  15. } from "./EditCategory.styled";
  16. import { useState } from "react";
  17. import { Trans, useTranslation } from "react-i18next";
  18. import { useFormik } from "formik";
  19. import { useMemo } from "react";
  20. import { useDispatch } from "react-redux";
  21. import { fetchAdminMethod } from "../../../store/actions/admin/adminActions";
  22. import { useRef } from "react";
  23. const EditCategory = (props) => {
  24. const { t } = useTranslation();
  25. const dispatch = useDispatch();
  26. const [clickedOnNext, setClickedOnNext] = useState(false);
  27. const setImage = useState("")[1];
  28. const inputRef = useRef(null);
  29. const title = useMemo(() => {
  30. return t(`admin.${props.type}.${props.method}.title`);
  31. }, [props.type, props.method]);
  32. const placeholder = useMemo(() => {
  33. return t(`admin.${props.type}.${props.method}.placeholder`);
  34. }, [props.type, props.method]);
  35. const fieldLabel = useMemo(() => {
  36. return t(`admin.${props.type}.${props.method}.fieldTitle`);
  37. }, [props.type, props.method]);
  38. const firstButtonText = useMemo(() => {
  39. return t(`admin.${props.type}.${props.method}.next`);
  40. }, [props.type, props.method]);
  41. const secondButtonText = useMemo(() => {
  42. return t(`admin.${props.type}.${props.method}.save`);
  43. }, [props.type, props.method]);
  44. const setImageHandler = (image) => {
  45. setImage(image);
  46. formik.setFieldValue("image", image);
  47. };
  48. const handleApiResponseSuccess = () => {
  49. if (clickedOnNext) {
  50. formik.resetForm();
  51. inputRef.current.focus();
  52. } else props.setOpenedEditModal(false);
  53. };
  54. const handleSubmit = (values) => {
  55. dispatch(
  56. fetchAdminMethod({
  57. type: props.type,
  58. method: props.method,
  59. values,
  60. name: props?.category?.name,
  61. id: props?.category?._id,
  62. categoryId: props.categoryId,
  63. handleApiResponseSuccess,
  64. })
  65. );
  66. };
  67. const formik = useFormik({
  68. initialValues: {
  69. image: "",
  70. title: props?.category?.name || props?.category?.city || "",
  71. },
  72. onSubmit: handleSubmit,
  73. });
  74. const handleClick = (next) => {
  75. if (next !== clickedOnNext) setClickedOnNext(next);
  76. formik.handleSubmit();
  77. };
  78. return (
  79. <>
  80. <BackdropComponent
  81. isLoading
  82. handleClose={() => props.setOpenedEditModal(false)}
  83. position="fixed"
  84. />
  85. <EditCategoryContainer hideImagePicker={props.hideImagePicker}>
  86. <XIcon onClick={() => props.setOpenedEditModal(false)} />
  87. <EditCategoryTitle>{title}</EditCategoryTitle>
  88. {!props.hideImagePicker && (
  89. <>
  90. <EditCategoryImagePicker setImage={setImageHandler} singleImage />
  91. <SupportedFormats>
  92. <Trans i18nKey="offer.supportedImagesFormats" />
  93. </SupportedFormats>
  94. </>
  95. )}
  96. <InputContainer hideImagePicker={props.hideImagePicker}>
  97. <FieldLabel leftText={fieldLabel} />
  98. <CategoryTitleField
  99. name="title"
  100. ref={inputRef}
  101. placeholder={placeholder}
  102. italicPlaceholder
  103. margin="normal"
  104. value={formik.values.title}
  105. onChange={formik.handleChange}
  106. error={formik.touched.title && formik.errors.title}
  107. helperText={formik.touched.title && formik.errors.title}
  108. autoFocus
  109. fullWidth
  110. />
  111. </InputContainer>
  112. <ButtonsContainer>
  113. <SaveButton
  114. showSecondButton={props.showSecondButton}
  115. variant={props.firstOutlined ? "outlined" : "contained"}
  116. height="48px"
  117. onClick={() => handleClick(true)}
  118. >
  119. {firstButtonText}
  120. </SaveButton>
  121. {props.showSecondButton && (
  122. <SaveButton
  123. variant={props.secondOutlined ? "outlined" : "contained"}
  124. showSecondButton={props.showSecondButton}
  125. onClick={() => handleClick(false)}
  126. >
  127. {secondButtonText}
  128. </SaveButton>
  129. )}
  130. </ButtonsContainer>
  131. </EditCategoryContainer>
  132. </>
  133. );
  134. };
  135. EditCategory.propTypes = {
  136. category: PropTypes.any,
  137. setOpenedEditModal: PropTypes.func,
  138. hideImagePicker: PropTypes.bool,
  139. type: PropTypes.string,
  140. showSecondButton: PropTypes.bool,
  141. method: PropTypes.string,
  142. firstOutlined: PropTypes.bool,
  143. secondOutlined: PropTypes.bool,
  144. categoryId: PropTypes.string,
  145. };
  146. EditCategory.defaultProps = {
  147. firstOutlined: true,
  148. };
  149. export default EditCategory;