1- import { useRef , type Dispatch , type SetStateAction , type RefObject , memo } from "react" ;
2- import FileUploader , { type FileUploaderRef } from "devextreme-react/file-uploader" ;
3- import { useEvent } from "./utils" ;
4- import { backendURL } from "./constants" ;
5- import type { FileUploaderTypes } from "devextreme-react/file-uploader" ;
6- import type { DataGridTypes } from "devextreme-react/data-grid" ;
7- import type { Employee } from "./data" ;
1+ import React , {
2+ useRef , type Dispatch , type SetStateAction , type RefObject , memo ,
3+ } from 'react' ;
4+ import FileUploader , { type FileUploaderRef , type FileUploaderTypes } from 'devextreme-react/file-uploader' ;
5+ import type { DataGridTypes } from 'devextreme-react/data-grid' ;
6+ import { useEvent } from './utils' ;
7+ import { backendURL } from './constants' ;
8+ import type { Employee } from './data' ;
89
910interface FileUploaderPreviewProps {
1011 cellInfo : DataGridTypes . ColumnEditCellTemplateData < Employee , number > ;
1112 setRetryButtonVisible : Dispatch < SetStateAction < boolean > > ;
1213 fileUploaderRef : RefObject < FileUploaderRef > ;
1314}
1415
15- export const FileUploaderWithPreview = memo < FileUploaderPreviewProps > ( ( {
16- setRetryButtonVisible,
17- cellInfo,
18- fileUploaderRef
16+ export const FileUploaderWithPreview = memo < FileUploaderPreviewProps > ( ( {
17+ setRetryButtonVisible,
18+ cellInfo,
19+ fileUploaderRef,
1920} ) => {
2021 const imgRef = useRef < HTMLImageElement > ( null ) ;
21-
22+
2223 const onValueChanged = useEvent ( ( e : FileUploaderTypes . ValueChangedEvent ) : void => {
2324 const files = e . value ;
2425 if ( files && files . length > 0 ) {
2526 const reader = new FileReader ( ) ;
26- reader . onload = function ( args ) {
27+ reader . onload = ( args ) : void => {
2728 if ( typeof args . target ?. result === 'string' && imgRef . current ) {
2829 imgRef . current . setAttribute ( 'src' , args . target . result ) ;
2930 }
3031 } ;
31- reader . readAsDataURL ( files [ 0 ] ) ; // convert to base64 string
32+ reader . readAsDataURL ( files [ 0 ] ) ; // convert to base64 string
3233 }
3334 } ) ;
3435
3536 const onUploaded = useEvent ( ( e : FileUploaderTypes . UploadedEvent ) : void => {
3637 if ( e . request ?. responseText ) {
37- cellInfo . setValue ( " images/employees/" + e . request . responseText ) ;
38+ cellInfo . setValue ( ` images/employees/${ e . request . responseText } ` ) ;
3839 setRetryButtonVisible ( false ) ;
3940 }
4041 } ) ;
4142
4243 const onUploadError = useEvent ( ( e : FileUploaderTypes . UploadErrorEvent ) : void => {
4344 const xhttp = e . request ;
4445 if ( xhttp && xhttp . status === 400 ) {
45- e . message = e . error ?. responseText || " Upload error" ;
46+ e . message = e . error ?. responseText || ' Upload error' ;
4647 }
4748 if ( xhttp && xhttp . readyState === 4 && xhttp . status === 0 ) {
48- e . message = " Connection refused" ;
49+ e . message = ' Connection refused' ;
4950 }
5051 setRetryButtonVisible ( true ) ;
5152 } ) ;
5253
5354 return (
54- < >
55- < img
56- ref = { imgRef }
57- className = "uploadedImage"
58- src = { `${ backendURL } ${ cellInfo . value } ` }
59- alt = "employee pic"
55+ < React . Fragment >
56+ < img
57+ ref = { imgRef }
58+ className = "uploaded-image"
59+ src = { `${ backendURL } ${ cellInfo . value } ` }
60+ alt = "employee pic"
6061 style = { { maxWidth : '100%' , height : 'auto' , marginBottom : '10px' } }
6162 />
62- < FileUploader
63- ref = { fileUploaderRef }
64- multiple = { false }
65- accept = "image/*"
63+ < FileUploader
64+ ref = { fileUploaderRef }
65+ multiple = { false }
66+ accept = "image/*"
6667 uploadMode = "instantly"
67- uploadUrl = { backendURL + " FileUpload/post" }
68+ uploadUrl = { ` ${ backendURL } FileUpload/post` }
6869 onValueChanged = { onValueChanged }
69- onUploaded = { onUploaded }
70- onUploadError = { onUploadError }
70+ onUploaded = { onUploaded }
71+ onUploadError = { onUploadError }
7172 />
72- </ >
73+ </ React . Fragment >
7374 ) ;
7475} ) ;
7576
76- FileUploaderWithPreview . displayName = 'FileUploaderWithPreview' ;
77+ FileUploaderWithPreview . displayName = 'FileUploaderWithPreview' ;
0 commit comments