feed-back.tsx 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. 'use client'
  2. import { useTranslation } from 'react-i18next'
  3. import { useCallback, useState } from 'react'
  4. import { useRouter } from 'next/navigation'
  5. import { useDeleteAccountFeedback } from '../state'
  6. import { useAppContext } from '@/context/app-context'
  7. import Button from '@/app/components/base/button'
  8. import CustomDialog from '@/app/components/base/dialog'
  9. import Textarea from '@/app/components/base/textarea'
  10. import Toast from '@/app/components/base/toast'
  11. import { logout } from '@/service/common'
  12. type DeleteAccountProps = {
  13. onCancel: () => void
  14. onConfirm: () => void
  15. }
  16. export default function FeedBack(props: DeleteAccountProps) {
  17. const { t } = useTranslation()
  18. const { userProfile } = useAppContext()
  19. const router = useRouter()
  20. const [userFeedback, setUserFeedback] = useState('')
  21. const { isPending, mutateAsync: sendFeedback } = useDeleteAccountFeedback()
  22. const handleSuccess = useCallback(async () => {
  23. try {
  24. await logout({
  25. url: '/logout',
  26. params: {},
  27. })
  28. localStorage.removeItem('refresh_token')
  29. localStorage.removeItem('console_token')
  30. router.push('/signin')
  31. Toast.notify({ type: 'info', message: t('common.account.deleteSuccessTip') })
  32. }
  33. catch (error) { console.error(error) }
  34. }, [router, t])
  35. const handleSubmit = useCallback(async () => {
  36. try {
  37. await sendFeedback({ feedback: userFeedback, email: userProfile.email })
  38. props.onConfirm()
  39. await handleSuccess()
  40. }
  41. catch (error) { console.error(error) }
  42. }, [handleSuccess, userFeedback, sendFeedback, userProfile, props])
  43. const handleSkip = useCallback(() => {
  44. props.onCancel()
  45. handleSuccess()
  46. }, [handleSuccess, props])
  47. return <CustomDialog
  48. show={true}
  49. onClose={props.onCancel}
  50. title={t('common.account.feedbackTitle')}
  51. className="max-w-[480px]"
  52. footer={false}
  53. >
  54. <label className='system-sm-semibold mb-1 mt-3 flex items-center text-text-secondary'>{t('common.account.feedbackLabel')}</label>
  55. <Textarea rows={6} value={userFeedback} placeholder={t('common.account.feedbackPlaceholder') as string} onChange={(e) => {
  56. setUserFeedback(e.target.value)
  57. }} />
  58. <div className='mt-3 flex w-full flex-col gap-2'>
  59. <Button className='w-full' loading={isPending} variant='primary' onClick={handleSubmit}>{t('common.operation.submit')}</Button>
  60. <Button className='w-full' onClick={handleSkip}>{t('common.operation.skip')}</Button>
  61. </div>
  62. </CustomDialog>
  63. }