word-count-modal.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. import React, { useEffect, useState } from 'react'
  2. import PropTypes from 'prop-types'
  3. import WordCountModalContent from './word-count-modal-content'
  4. import { fetchWordCount } from '../utils/api'
  5. function WordCountModal({ clsiServerId, handleHide, projectId, show }) {
  6. const [loading, setLoading] = useState(true)
  7. const [error, setError] = useState(false)
  8. const [data, setData] = useState()
  9. useEffect(() => {
  10. if (!show) {
  11. return
  12. }
  13. setData(undefined)
  14. setError(false)
  15. setLoading(true)
  16. fetchWordCount(projectId, clsiServerId)
  17. .then(data => {
  18. setData(data.texcount)
  19. })
  20. .catch(error => {
  21. if (error.cause?.name !== 'AbortError') {
  22. setError(true)
  23. }
  24. })
  25. .finally(() => {
  26. setLoading(false)
  27. })
  28. }, [show, projectId, clsiServerId])
  29. return (
  30. <WordCountModalContent
  31. data={data}
  32. error={error}
  33. show={show}
  34. handleHide={handleHide}
  35. loading={loading}
  36. />
  37. )
  38. }
  39. WordCountModal.propTypes = {
  40. clsiServerId: PropTypes.string,
  41. handleHide: PropTypes.func.isRequired,
  42. projectId: PropTypes.string.isRequired,
  43. show: PropTypes.bool.isRequired
  44. }
  45. export default WordCountModal