-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented Image Upload Modal in Article Editor
- Loading branch information
1 parent
f611887
commit 1750542
Showing
9 changed files
with
1,128 additions
and
452 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import React, {useEffect} from "react"; | ||
import Modal from "./Modal"; | ||
|
||
const ImageEditor = function({open = false, onClose, callback}: {open: boolean, onClose: () => void, callback: (location: string) => void}){ | ||
async function onSubmit(event: React.FormEvent) { | ||
event.preventDefault(); | ||
|
||
const formData = new FormData(event.target as HTMLFormElement); | ||
let response = await fetch("/images/create", { | ||
method: "PUT", | ||
body: formData | ||
}) | ||
if (!response.ok) { | ||
throw new Error(response.statusText); | ||
} | ||
|
||
(event.target as HTMLFormElement)?.reset() | ||
|
||
const loc = response.headers.get("Location") as string; | ||
callback(loc); | ||
} | ||
|
||
return ( | ||
<Modal open={open} onClose={onClose}> | ||
<form onSubmit={onSubmit} className="flex gap-2 p-2"> | ||
<input id="file" name="file" type="file" alt="Image" | ||
className="file-input file-input-bordered w-full max-w-xs" autoFocus={true}/> | ||
|
||
<button type="submit" className="btn btn-primary w-full sm:btn-wide">Upload</button> | ||
</form> | ||
</Modal> | ||
) | ||
} | ||
|
||
export default ImageEditor; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import React, {useEffect, useRef} from "react"; | ||
|
||
interface ModalProperties { | ||
open: boolean, | ||
onClose: () => void, | ||
className?: string, | ||
children: React.ReactNode, | ||
} | ||
|
||
const Modal = function({open, onClose, children}: ModalProperties) { | ||
const ref = useRef<HTMLDialogElement>(null); | ||
|
||
useEffect(() => { | ||
if (open && ref.current) ref.current.showModal(); | ||
else if (ref.current) ref.current.close() | ||
}, [open]) | ||
|
||
return ( | ||
<dialog ref={ref} onCancel={onClose} className="p-4 rounded-lg bg-base-200 border border-base-300 shadow z-[100] backdrop:bg-base-100 backdrop:bg-opacity-50"> | ||
{children} | ||
|
||
<button type="button" className="btn btn-primary w-full sm:btn-wide" onClick={onClose}> | ||
Cancel | ||
</button> | ||
</dialog> | ||
) | ||
} | ||
|
||
export default Modal; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.