11import React , { useState , useEffect } from 'react' ;
22import Editor from '@monaco-editor/react' ;
33import { saveAs } from 'file-saver' ;
4+ import ReactModal from 'react-modal' ;
45import Modal from './ParentModal' ;
56import { toast } from 'react-toastify' ;
67import './file-edit.css' ;
@@ -11,6 +12,8 @@ const FileEditModal = ({ superState, dispatcher }) => {
1112 const [ fileName , setFileName ] = useState ( '' ) ;
1213 const [ dirButton , setDirButton ] = useState ( false ) ;
1314 const [ language , setLanguage ] = useState ( 'plaintext' ) ;
15+ const [ newFileName , setNewFileName ] = useState ( '' ) ;
16+ const [ showFilenameModal , setShowFilenameModal ] = useState ( false ) ;
1417
1518 useEffect ( ( ) => {
1619 if ( navigator . userAgent . indexOf ( 'Edg' ) !== - 1 || navigator . userAgent . indexOf ( 'Chrome' ) !== - 1 ) {
@@ -52,11 +55,19 @@ const FileEditModal = ({ superState, dispatcher }) => {
5255 dispatcher ( { type : T . SET_FILE_STATE , payload : fS } ) ;
5356 }
5457
55- async function saveSubmit ( ) {
56- const newFileName = prompt ( 'Filename:' ) ;
58+ function handleSaveAsClick ( ) {
59+ setShowFilenameModal ( true ) ;
60+ }
61+
62+ function confirmSaveAs ( ) {
63+ if ( newFileName . trim ( ) === '' ) {
64+ toast . error ( 'Filename cannot be empty!' ) ;
65+ return ;
66+ }
5767 const bytes = new TextEncoder ( ) . encode ( codeStuff ) ;
5868 const blob = new Blob ( [ bytes ] , { type : 'application/json;charset=utf-8' } ) ;
5969 saveAs ( blob , newFileName ) ;
70+ setShowFilenameModal ( false ) ;
6071 }
6172
6273 useEffect ( ( ) => {
@@ -121,10 +132,37 @@ const FileEditModal = ({ superState, dispatcher }) => {
121132 < button type = "submit" className = "btn btn-primary" onClick = { saveAsSubmit } > Save As</ button >
122133 ) }
123134 { ! dirButton && (
124- < button type = "submit" className = "btn btn-primary" onClick = { saveSubmit } > Save As</ button >
135+ < button type = "submit" className = "btn btn-primary" onClick = { handleSaveAsClick } > Save As</ button >
125136 ) }
126137 </ div >
127138 </ div >
139+ { /* Filename Input Modal */ }
140+ < ReactModal
141+ isOpen = { showFilenameModal }
142+ onRequestClose = { ( ) => setShowFilenameModal ( false ) }
143+ className = "modal-content"
144+ overlayClassName = "modal-overlay"
145+ ariaHideApp = { false }
146+ >
147+ < h2 > Enter Filename</ h2 >
148+ < input
149+ type = "text"
150+ value = { newFileName }
151+ onChange = { ( e ) => setNewFileName ( e . target . value ) }
152+ className = "modal-input"
153+ placeholder = "Filename"
154+ />
155+ < div className = "modal-actions" >
156+ < button type = "button" onClick = { confirmSaveAs } className = "btn btn-primary" > Save</ button >
157+ < button
158+ type = "button"
159+ onClick = { ( ) => setShowFilenameModal ( false ) }
160+ className = "btn btn-secondary"
161+ >
162+ Cancel
163+ </ button >
164+ </ div >
165+ </ ReactModal >
128166 </ Modal >
129167 ) ;
130168} ;
0 commit comments