fix search, show file
This commit is contained in:
		
							parent
							
								
									4896461838
								
							
						
					
					
						commit
						60925a83af
					
				| 
						 | 
					@ -2,6 +2,8 @@ import { useEffect, useState } from 'react'
 | 
				
			||||||
import { get } from '@/rtk/helpers/apiService'
 | 
					import { get } from '@/rtk/helpers/apiService'
 | 
				
			||||||
import { deleteDocument, listDocument } from '@/api/Admin'
 | 
					import { deleteDocument, listDocument } from '@/api/Admin'
 | 
				
			||||||
import { Xdelete } from '@/rtk/helpers/CRUD'
 | 
					import { Xdelete } from '@/rtk/helpers/CRUD'
 | 
				
			||||||
 | 
					import { useSelector } from 'react-redux'
 | 
				
			||||||
 | 
					import { RootState } from '@/rtk/store'
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import { Anchor, Box, Button, Dialog, Group, Loader, Text } from '@mantine/core'
 | 
					import { Anchor, Box, Button, Dialog, Group, Loader, Text } from '@mantine/core'
 | 
				
			||||||
import { useDisclosure } from '@mantine/hooks'
 | 
					import { useDisclosure } from '@mantine/hooks'
 | 
				
			||||||
| 
						 | 
					@ -20,7 +22,6 @@ import ModalFileDocument from './ModalFileDocument'
 | 
				
			||||||
import classes from './Document.module.css'
 | 
					import classes from './Document.module.css'
 | 
				
			||||||
import ModalAddDocument from './ModalAddDocument'
 | 
					import ModalAddDocument from './ModalAddDocument'
 | 
				
			||||||
import ModalEditDocument from './ModalEditDocument'
 | 
					import ModalEditDocument from './ModalEditDocument'
 | 
				
			||||||
import { checkPermissions } from '@/utils/checkRoles'
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
interface TDocument {
 | 
					interface TDocument {
 | 
				
			||||||
  id: number
 | 
					  id: number
 | 
				
			||||||
| 
						 | 
					@ -38,6 +39,8 @@ type RequestPagination = {
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const Document = () => {
 | 
					const Document = () => {
 | 
				
			||||||
 | 
					  const user = useSelector((state: RootState) => state.authentication)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  const [loader, setLoader] = useState<boolean>(false)
 | 
					  const [loader, setLoader] = useState<boolean>(false)
 | 
				
			||||||
  const [rows, setRows] = useState<RequestPagination>({
 | 
					  const [rows, setRows] = useState<RequestPagination>({
 | 
				
			||||||
    data: [],
 | 
					    data: [],
 | 
				
			||||||
| 
						 | 
					@ -91,42 +94,68 @@ const Document = () => {
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  const getFileTypeIcon = (uri: string) => {
 | 
					  const getFileTypeIcon = (row: TDocument) => {
 | 
				
			||||||
 | 
					    const uri = row?.uri
 | 
				
			||||||
    if (!uri) return null
 | 
					    if (!uri) return null
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    const extension = uri.split('.').pop()?.toLowerCase()
 | 
					    const extension = uri.split('.').pop()?.toLowerCase()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if (['doc', 'docx'].includes(extension!)) {
 | 
					    if (['doc'].includes(extension!)) {
 | 
				
			||||||
      return <IconFileTypeDoc style={{ color: '#1e62c1' }} />
 | 
					      return (
 | 
				
			||||||
 | 
					        <a
 | 
				
			||||||
 | 
					          href={`${import.meta.env.VITE_BACKEND_URL}${uri}`}
 | 
				
			||||||
 | 
					          download="Document Download"
 | 
				
			||||||
 | 
					          target="_self"
 | 
				
			||||||
 | 
					        >
 | 
				
			||||||
 | 
					          <IconFileTypeDoc style={{ color: '#1e62c1' }} />
 | 
				
			||||||
 | 
					        </a>
 | 
				
			||||||
 | 
					      )
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if (['xls', 'xlsx'].includes(extension!)) {
 | 
					    if (['xls', 'xlsx'].includes(extension!)) {
 | 
				
			||||||
      return <IconFileTypeXls style={{ color: '#0e864b' }} />
 | 
					      return (
 | 
				
			||||||
 | 
					        <a
 | 
				
			||||||
 | 
					          href={`${import.meta.env.VITE_BACKEND_URL}${uri}`}
 | 
				
			||||||
 | 
					          download="Document Download"
 | 
				
			||||||
 | 
					          target="_self"
 | 
				
			||||||
 | 
					        >
 | 
				
			||||||
 | 
					          <IconFileTypeXls style={{ color: '#0e864b' }} />
 | 
				
			||||||
 | 
					        </a>
 | 
				
			||||||
 | 
					      )
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    return <IconFileTypePdf style={{ color: '#ff1b0e' }} />
 | 
					    if (['docx'].includes(extension!)) {
 | 
				
			||||||
 | 
					      return (
 | 
				
			||||||
 | 
					        <IconFileTypeDoc
 | 
				
			||||||
 | 
					          onClick={() => {
 | 
				
			||||||
 | 
					            openModalFile()
 | 
				
			||||||
 | 
					            setSelectDataRow(row)
 | 
				
			||||||
 | 
					          }}
 | 
				
			||||||
 | 
					          style={{ color: '#1e62c1' }}
 | 
				
			||||||
 | 
					        />
 | 
				
			||||||
 | 
					      )
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    return (
 | 
				
			||||||
 | 
					      <IconFileTypePdf
 | 
				
			||||||
 | 
					        onClick={() => {
 | 
				
			||||||
 | 
					          openModalFile()
 | 
				
			||||||
 | 
					          setSelectDataRow(row)
 | 
				
			||||||
 | 
					        }}
 | 
				
			||||||
 | 
					        style={{ color: '#ff1b0e' }}
 | 
				
			||||||
 | 
					      />
 | 
				
			||||||
 | 
					    )
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  const columns = [
 | 
					  const columns = [
 | 
				
			||||||
    {
 | 
					 | 
				
			||||||
      name: 'id',
 | 
					 | 
				
			||||||
      size: '5%',
 | 
					 | 
				
			||||||
      header: 'ID',
 | 
					 | 
				
			||||||
      render: (row: TDocument) => {
 | 
					 | 
				
			||||||
        return <Box>{row?.id ? row.id : ''}</Box>
 | 
					 | 
				
			||||||
      },
 | 
					 | 
				
			||||||
    },
 | 
					 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
      name: 'title',
 | 
					      name: 'title',
 | 
				
			||||||
      size: '50%',
 | 
					      size: '50%',
 | 
				
			||||||
      header: 'Title',
 | 
					      header: 'Title',
 | 
				
			||||||
      render: (row: TDocument) => {
 | 
					 | 
				
			||||||
        return <Text ta="start">{row?.title}</Text>
 | 
					 | 
				
			||||||
      },
 | 
					 | 
				
			||||||
    },
 | 
					    },
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
      name: 'uri',
 | 
					      name: 'uri',
 | 
				
			||||||
      size: '40%',
 | 
					      size: '45%',
 | 
				
			||||||
      header: 'URI',
 | 
					      header: 'URI',
 | 
				
			||||||
      render: (row: TDocument) => {
 | 
					      render: (row: TDocument) => {
 | 
				
			||||||
        return (
 | 
					        return (
 | 
				
			||||||
| 
						 | 
					@ -135,16 +164,12 @@ const Document = () => {
 | 
				
			||||||
              <Box
 | 
					              <Box
 | 
				
			||||||
                w="fit-content"
 | 
					                w="fit-content"
 | 
				
			||||||
                style={{ cursor: 'pointer' }}
 | 
					                style={{ cursor: 'pointer' }}
 | 
				
			||||||
                onClick={() => {
 | 
					                title={`File .${row?.uri
 | 
				
			||||||
                  setSelectDataRow(row)
 | 
					 | 
				
			||||||
                  openModalFile()
 | 
					 | 
				
			||||||
                }}
 | 
					 | 
				
			||||||
                title={`File ${row?.uri
 | 
					 | 
				
			||||||
                  .split('.')
 | 
					                  .split('.')
 | 
				
			||||||
                  .pop()
 | 
					                  .pop()
 | 
				
			||||||
                  ?.toLowerCase()} detail`}
 | 
					                  ?.toLowerCase()} detail`}
 | 
				
			||||||
              >
 | 
					              >
 | 
				
			||||||
                {getFileTypeIcon(row?.uri)}
 | 
					                {getFileTypeIcon(row)}
 | 
				
			||||||
              </Box>
 | 
					              </Box>
 | 
				
			||||||
            ) : (
 | 
					            ) : (
 | 
				
			||||||
              <Anchor
 | 
					              <Anchor
 | 
				
			||||||
| 
						 | 
					@ -165,7 +190,10 @@ const Document = () => {
 | 
				
			||||||
      size: '5%',
 | 
					      size: '5%',
 | 
				
			||||||
      header: 'Action',
 | 
					      header: 'Action',
 | 
				
			||||||
      render: (row: TDocument) => {
 | 
					      render: (row: TDocument) => {
 | 
				
			||||||
        if (checkPermissions('admin')) {
 | 
					        if (!user.user.user.permission.includes('admin')) {
 | 
				
			||||||
 | 
					          return ''
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        return (
 | 
					        return (
 | 
				
			||||||
          <Box className={classes.optionIcon}>
 | 
					          <Box className={classes.optionIcon}>
 | 
				
			||||||
            <IconEdit
 | 
					            <IconEdit
 | 
				
			||||||
| 
						 | 
					@ -188,9 +216,6 @@ const Document = () => {
 | 
				
			||||||
            />
 | 
					            />
 | 
				
			||||||
          </Box>
 | 
					          </Box>
 | 
				
			||||||
        )
 | 
					        )
 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        return <Box></Box>
 | 
					 | 
				
			||||||
      },
 | 
					      },
 | 
				
			||||||
    },
 | 
					    },
 | 
				
			||||||
  ]
 | 
					  ]
 | 
				
			||||||
| 
						 | 
					@ -203,7 +228,7 @@ const Document = () => {
 | 
				
			||||||
          Documents
 | 
					          Documents
 | 
				
			||||||
        </h3>
 | 
					        </h3>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        {checkPermissions('admin') ? (
 | 
					        {user.user.user.permission.includes('admin') ? (
 | 
				
			||||||
          <Button onClick={() => openModalAdd()} disabled={loader}>
 | 
					          <Button onClick={() => openModalAdd()} disabled={loader}>
 | 
				
			||||||
            + Add
 | 
					            + Add
 | 
				
			||||||
          </Button>
 | 
					          </Button>
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -9,6 +9,7 @@ import {
 | 
				
			||||||
  FileInput,
 | 
					  FileInput,
 | 
				
			||||||
  Badge,
 | 
					  Badge,
 | 
				
			||||||
  TextInput,
 | 
					  TextInput,
 | 
				
			||||||
 | 
					  Group,
 | 
				
			||||||
} from '@mantine/core'
 | 
					} from '@mantine/core'
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import { update } from '@/rtk/helpers/CRUD'
 | 
					import { update } from '@/rtk/helpers/CRUD'
 | 
				
			||||||
| 
						 | 
					@ -113,15 +114,51 @@ const ModalEditDocument = ({
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    const extension = uri.split('.').pop()?.toLowerCase()
 | 
					    const extension = uri.split('.').pop()?.toLowerCase()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if (['doc', 'docx'].includes(extension!)) {
 | 
					    if (['doc'].includes(extension!)) {
 | 
				
			||||||
      return <IconFileTypeDoc style={{ color: '#1e62c1' }} />
 | 
					      return (
 | 
				
			||||||
 | 
					        <a
 | 
				
			||||||
 | 
					          href={`${import.meta.env.VITE_BACKEND_URL}${uri}`}
 | 
				
			||||||
 | 
					          download="Document Download"
 | 
				
			||||||
 | 
					          target="_self"
 | 
				
			||||||
 | 
					        >
 | 
				
			||||||
 | 
					          <IconFileTypeDoc style={{ color: '#1e62c1' }} />
 | 
				
			||||||
 | 
					        </a>
 | 
				
			||||||
 | 
					      )
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if (['xls', 'xlsx'].includes(extension!)) {
 | 
					    if (['xls', 'xlsx'].includes(extension!)) {
 | 
				
			||||||
      return <IconFileTypeXls style={{ color: '#0e864b' }} />
 | 
					      return (
 | 
				
			||||||
 | 
					        <a
 | 
				
			||||||
 | 
					          href={`${import.meta.env.VITE_BACKEND_URL}${uri}`}
 | 
				
			||||||
 | 
					          download="Document Download"
 | 
				
			||||||
 | 
					          target="_self"
 | 
				
			||||||
 | 
					        >
 | 
				
			||||||
 | 
					          <IconFileTypeXls style={{ color: '#0e864b' }} />
 | 
				
			||||||
 | 
					        </a>
 | 
				
			||||||
 | 
					      )
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    return <IconFileTypePdf style={{ color: '#ff1b0e' }} />
 | 
					    if (['docx'].includes(extension!)) {
 | 
				
			||||||
 | 
					      return (
 | 
				
			||||||
 | 
					        <IconFileTypeDoc
 | 
				
			||||||
 | 
					          onClick={() => {
 | 
				
			||||||
 | 
					            openModalFile()
 | 
				
			||||||
 | 
					            setSelectDataFileRow(selectDataRow)
 | 
				
			||||||
 | 
					          }}
 | 
				
			||||||
 | 
					          style={{ color: '#1e62c1' }}
 | 
				
			||||||
 | 
					        />
 | 
				
			||||||
 | 
					      )
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    return (
 | 
				
			||||||
 | 
					      <IconFileTypePdf
 | 
				
			||||||
 | 
					        onClick={() => {
 | 
				
			||||||
 | 
					          openModalFile()
 | 
				
			||||||
 | 
					          setSelectDataFileRow(selectDataRow)
 | 
				
			||||||
 | 
					        }}
 | 
				
			||||||
 | 
					        style={{ color: '#ff1b0e' }}
 | 
				
			||||||
 | 
					      />
 | 
				
			||||||
 | 
					    )
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  const resetForm = () => {
 | 
					  const resetForm = () => {
 | 
				
			||||||
| 
						 | 
					@ -179,7 +216,7 @@ const ModalEditDocument = ({
 | 
				
			||||||
            {form.values.file ? (
 | 
					            {form.values.file ? (
 | 
				
			||||||
              ''
 | 
					              ''
 | 
				
			||||||
            ) : (
 | 
					            ) : (
 | 
				
			||||||
              <Box
 | 
					              <Group
 | 
				
			||||||
                w="fit-content"
 | 
					                w="fit-content"
 | 
				
			||||||
                style={{ cursor: 'pointer' }}
 | 
					                style={{ cursor: 'pointer' }}
 | 
				
			||||||
                mb={'md'}
 | 
					                mb={'md'}
 | 
				
			||||||
| 
						 | 
					@ -187,13 +224,11 @@ const ModalEditDocument = ({
 | 
				
			||||||
                  .split('.')
 | 
					                  .split('.')
 | 
				
			||||||
                  .pop()
 | 
					                  .pop()
 | 
				
			||||||
                  ?.toLowerCase()} detail`}
 | 
					                  ?.toLowerCase()} detail`}
 | 
				
			||||||
                onClick={() => {
 | 
					 | 
				
			||||||
                  openModalFile()
 | 
					 | 
				
			||||||
                  setSelectDataFileRow(selectDataRow)
 | 
					 | 
				
			||||||
                }}
 | 
					 | 
				
			||||||
              >
 | 
					              >
 | 
				
			||||||
                {getFileTypeIcon(form.values.uri)}
 | 
					                {getFileTypeIcon(form.values.uri)}
 | 
				
			||||||
              </Box>
 | 
					
 | 
				
			||||||
 | 
					                <Text>.{form.values.uri.split('.').pop()?.toLowerCase()}</Text>
 | 
				
			||||||
 | 
					              </Group>
 | 
				
			||||||
            )}
 | 
					            )}
 | 
				
			||||||
          </Box>
 | 
					          </Box>
 | 
				
			||||||
        ) : (
 | 
					        ) : (
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue