Advertisement
Guest User

Untitled

a guest
Jun 17th, 2019
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.52 KB | None | 0 0
  1. import React, { Fragment , useState} from 'react'
  2. import { addAppReviewApi } from '~/api'
  3. import * as SC from "./styled";
  4. import { Formik, Form, ErrorMessage, FastField } from 'formik'
  5. import StarRatingComponent from 'react-star-rating-component';
  6. import * as yup from 'yup'
  7. import ErrorList from './ErrorList'
  8.  
  9.  
  10. const requestSchema = yup.object().shape({
  11. review: yup.string()
  12. .trim()
  13. .required('Please share your feedback'),
  14. rating: yup.number()
  15. .required('Please click a rate')
  16. .min(1, 'Please click a rate'),
  17. })
  18.  
  19.  
  20. const FormRatings = (props) => {
  21. const { addReviews, userProfile, appId, resetAddStatus } = props
  22. const { _id:userId, email } = userProfile
  23. const [serverError, setServerError] = useState('');
  24. const [postSuccess, setPostSuccess] = useState('');
  25. const [submitting, setSubmitting] = useState(false);
  26.  
  27. const loader = submitting
  28. ? <SC.Loader className="fas fa-circle-notch fa-spin"/>
  29. : null
  30.  
  31.  
  32.  
  33. const username = (email) => {
  34. const name = email.split('@')
  35. return name[0]
  36. }
  37.  
  38. const name = username(email)
  39.  
  40. return (
  41. <SC.FormRatings className="row">
  42. <div className="col">
  43. <Formik
  44. initialValues={{
  45. review: '',
  46. rating: 0
  47. }}
  48. enableReinitialize={true}
  49. validationSchema={requestSchema}
  50. validate={ () => {
  51. if (postSuccess || serverError) {
  52.  
  53. setServerError('')
  54. }
  55.  
  56. }}
  57. onSubmit={async (values, actions) => {
  58. setSubmitting(true)
  59. }}
  60. >
  61. {(props) => {
  62. const {
  63. values,
  64. setFieldValue,
  65. errors,
  66. setFieldTouched
  67. } = props
  68.  
  69. if(submitting) {
  70. setFieldTouched("review", false )
  71. setFieldTouched("rating", false )
  72. setFieldValue('review', '')
  73. setFieldValue('rating', 0)
  74. }
  75. return (
  76. <Fragment>
  77. { postSuccess && <div className="success-message">{postSuccess}</div> }
  78. { serverError && <div className="error-message">{serverError}</div> }
  79. <ErrorList {...props}/>
  80. <Form>
  81. <h3 className="author">{name}</h3>
  82. <FastField
  83. name="review"
  84. component="textarea"
  85. className="desc"
  86. value={values.review || ''}
  87. placeholder="Please share your feedback on this app with others. If you recommend this app, why?"/>
  88.  
  89. <div className="star-rating">
  90. <StarRatingComponent
  91. starColor="#749cc6"
  92. emptyStarColor="#345888"
  93. name="rating"
  94. starCount={5}
  95. editing={true}
  96. value={values.rating || 0}
  97. onStarClick={(nextValue) => setFieldValue('rating', nextValue) }
  98. />
  99. </div>
  100. <div className="col d-flex justify-content-end btn-wrap">
  101. <button className="submit-btn" type="submit" disabled={submitting}>Submit {loader}</button>
  102. </div>
  103. </Form>
  104. </Fragment>
  105. )
  106. }}
  107. </Formik>
  108. </div>
  109. </SC.FormRatings>
  110. )
  111. }
  112.  
  113. export default FormRatings
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement