rename-modal.tsx 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. 'use client'
  2. import type { FC } from 'react'
  3. import React, { useState } from 'react'
  4. import { useTranslation } from 'react-i18next'
  5. import { useBoolean } from 'ahooks'
  6. import Toast from '../../base/toast'
  7. import Modal from '@/app/components/base/modal'
  8. import Button from '@/app/components/base/button'
  9. import { renameDocumentName } from '@/service/datasets'
  10. type Props = {
  11. datasetId: string
  12. documentId: string
  13. name: string
  14. onClose: () => void
  15. onSaved: () => void
  16. }
  17. const RenameModal: FC<Props> = ({
  18. documentId,
  19. datasetId,
  20. name,
  21. onClose,
  22. onSaved,
  23. }) => {
  24. const { t } = useTranslation()
  25. const [newName, setNewName] = useState(name)
  26. const [saveLoading, {
  27. setTrue: setSaveLoadingTrue,
  28. setFalse: setSaveLoadingFalse,
  29. }] = useBoolean(false)
  30. const handleSave = async () => {
  31. setSaveLoadingTrue()
  32. try {
  33. await renameDocumentName({
  34. datasetId,
  35. documentId,
  36. name: newName,
  37. })
  38. Toast.notify({ type: 'success', message: t('common.actionMsg.modifiedSuccessfully') })
  39. onSaved()
  40. onClose()
  41. }
  42. catch (error) {
  43. if (error)
  44. Toast.notify({ type: 'error', message: error.toString() })
  45. }
  46. finally {
  47. setSaveLoadingFalse()
  48. }
  49. }
  50. return (
  51. <Modal
  52. title={t('datasetDocuments.list.table.rename')}
  53. isShow
  54. onClose={onClose}
  55. >
  56. <div className={'mt-6 font-medium text-sm leading-[21px] text-gray-900'}>{t('datasetDocuments.list.table.name')}</div>
  57. <input className={'mt-2 w-full rounded-lg h-10 box-border px-3 text-sm leading-10 bg-gray-100'}
  58. value={newName}
  59. onChange={e => setNewName(e.target.value)}
  60. />
  61. <div className='mt-10 flex justify-end'>
  62. <Button className='mr-2 flex-shrink-0' onClick={onClose}>{t('common.operation.cancel')}</Button>
  63. <Button variant='primary' className='flex-shrink-0' onClick={handleSave} loading={saveLoading}>{t('common.operation.save')}</Button>
  64. </div>
  65. </Modal>
  66. )
  67. }
  68. export default React.memo(RenameModal)