-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add: image preview before upload, upload thumbnail in chirp bubble add dependencies: React-redux, @reduxjs/toolkit edit: chirp component into bottom component --------- Co-authored-by: pbzweihander <pbzweihander@protonmail.com>
- Loading branch information
1 parent
78753f9
commit af36c0e
Showing
10 changed files
with
249 additions
and
49 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,42 @@ | ||
import { useState } from "react"; | ||
import z from "zod"; | ||
|
||
import { File } from "../dto"; | ||
|
||
interface propsType { | ||
files: z.infer<typeof File>[]; | ||
} | ||
|
||
const Images = ({ files }: propsType) => { | ||
const [clicked, setClicked] = useState(false); | ||
const [url, setUrl] = useState<string>(""); | ||
|
||
const handleImageClick = (url: string) => { | ||
setClicked(!clicked); | ||
setUrl(url); | ||
}; | ||
|
||
return ( | ||
<div> | ||
{files && | ||
files.map((file) => ( | ||
<img | ||
key={file.url} | ||
src={file.url} | ||
className="max-w-[24rem] cursor-pointer" | ||
onClick={() => handleImageClick(file.url)} | ||
/> | ||
))} | ||
{clicked && ( | ||
<div | ||
className="fixed left-0 top-0 z-10 flex items-center justify-center bg-slate-500/40" | ||
onClick={() => setClicked(!clicked)} | ||
> | ||
<img className="h-screen w-screen object-scale-down" src={url} /> | ||
</div> | ||
)} | ||
</div> | ||
); | ||
}; | ||
|
||
export default Images; |
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,100 @@ | ||
import { TrashIcon } from "@heroicons/react/24/outline"; | ||
import { useAtomValue, useSetAtom } from "jotai"; | ||
import { useEffect } from "react"; | ||
import { SubmitHandler, useForm } from "react-hook-form"; | ||
import z from "zod"; | ||
|
||
import { CreatePost } from "../../dto"; | ||
import { usePostNoteMutation } from "../../queries/note"; | ||
import { pictureUrl } from "../../states/states"; | ||
import BottomUpload from "./BottomUpload"; | ||
|
||
export default function BottomNewChirp() { | ||
const { register, handleSubmit, setValue, reset } = | ||
useForm<z.infer<typeof CreatePost>>(); | ||
const { | ||
mutate: postNote, | ||
isLoading, | ||
error, | ||
} = usePostNoteMutation(() => { | ||
reset(); | ||
}); | ||
const pictureUrlArr = useAtomValue(pictureUrl); | ||
const deleteUrl = useSetAtom(pictureUrl); | ||
|
||
const onSubmit: SubmitHandler<z.infer<typeof CreatePost>> = (data) => { | ||
postNote(data); | ||
deletePicture(); | ||
}; | ||
|
||
const deletePicture = () => { | ||
deleteUrl(() => []); | ||
}; | ||
|
||
useEffect(() => { | ||
const ulid: string[] = pictureUrlArr.map((el: string) => | ||
el.substring(el.length - 26, el.length), | ||
); | ||
setValue("files", ulid); | ||
}, [pictureUrlArr, setValue]); | ||
|
||
return ( | ||
<> | ||
<form | ||
className="chat chat-end absolute bottom-4 right-4" | ||
onSubmit={handleSubmit(onSubmit)} | ||
> | ||
<input type="hidden" value="public" {...register("visibility")} /> | ||
<div className="chat-bubble chat-bubble-primary"> | ||
<div className="flex items-center"> | ||
<BottomUpload /> | ||
<div> | ||
{pictureUrlArr.length > 0 && ( | ||
<div> | ||
{pictureUrlArr.map((value, i) => ( | ||
<div key={i}> | ||
<input | ||
type="hidden" | ||
value={pictureUrlArr} | ||
{...register("files")} | ||
/> | ||
<img | ||
src={value} | ||
alt="pictureurl" | ||
className="w-48 rounded-lg" | ||
/> | ||
<button | ||
type="button" | ||
className="btn btn-circle btn-error btn-sm absolute right-4 top-4" | ||
onClick={() => deletePicture()} | ||
> | ||
<TrashIcon className="h-5 w-5" /> | ||
</button> | ||
</div> | ||
))} | ||
</div> | ||
)} | ||
|
||
<input | ||
type="text" | ||
className="input w-full bg-transparent" | ||
placeholder="Write something..." | ||
required | ||
{...register("text")} | ||
/> | ||
</div> | ||
</div> | ||
</div> | ||
<div className="chat-footer"> | ||
<input | ||
type="submit" | ||
className="btn btn-primary btn-sm mt-2" | ||
value="Chirp!" | ||
disabled={isLoading} | ||
/> | ||
</div> | ||
{error && <div className="mt-5 text-error">{error.message}</div>} | ||
</form> | ||
</> | ||
); | ||
} |
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,59 @@ | ||
import { ArrowUpTrayIcon } from "@heroicons/react/24/outline"; | ||
import { useSetAtom } from "jotai"; | ||
import { Fragment, useRef } from "react"; | ||
|
||
import { useLocalFiles } from "../../queries/file"; | ||
import { pictureUrl } from "../../states/states"; | ||
|
||
export default function BottomUpload() { | ||
const modalRef = useRef<HTMLDialogElement>(null); | ||
const setUrl = useSetAtom(pictureUrl); | ||
|
||
const { data } = useLocalFiles(); | ||
|
||
const handlePictureClick = (url: string) => { | ||
setUrl((el) => [...el, url]); | ||
|
||
modalRef?.current?.close(); | ||
}; | ||
|
||
return ( | ||
<> | ||
<div | ||
onClick={() => modalRef?.current?.showModal()} | ||
className="mr-4 rounded-lg p-1 hover:bg-slate-50 hover:bg-opacity-10 active:bg-slate-700" | ||
> | ||
<ArrowUpTrayIcon width={24} height={24} /> | ||
</div> | ||
<dialog ref={modalRef} className="modal"> | ||
<div className="modal-box max-w-screen-xl"> | ||
{(data?.pages ?? []).map((page, i) => ( | ||
<div key={i} className="flex"> | ||
<Fragment> | ||
{page.length !== 0 ? ( | ||
<div className="grid grid-cols-3 gap-4"> | ||
{page.map((file) => ( | ||
<div key={file.id} className="h-48 w-48"> | ||
<img | ||
src={file.url} | ||
alt={file.alt ?? undefined} | ||
className="cursor-pointer rounded-lg border border-solid hover:shadow-lg" | ||
onClick={() => handlePictureClick(file.url)} | ||
/> | ||
</div> | ||
))} | ||
</div> | ||
) : ( | ||
<span>no items</span> | ||
)} | ||
</Fragment> | ||
</div> | ||
))} | ||
</div> | ||
<form method="dialog" className="modal-backdrop"> | ||
<button>close</button> | ||
</form> | ||
</dialog> | ||
</> | ||
); | ||
} |
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 |
---|---|---|
@@ -1,11 +1,14 @@ | ||
import { Provider as JotaiProvider } from "jotai"; | ||
import React from "react"; | ||
import ReactDOM from "react-dom/client"; | ||
|
||
import App from "./App"; | ||
import "./index.css"; | ||
|
||
ReactDOM.createRoot(document.getElementById("root")!).render( | ||
<React.StrictMode> | ||
<App /> | ||
</React.StrictMode>, | ||
<JotaiProvider> | ||
<React.StrictMode> | ||
<App /> | ||
</React.StrictMode> | ||
</JotaiProvider>, | ||
); |
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,3 @@ | ||
import { atom } from "jotai"; | ||
|
||
export const pictureUrl = atom<string[]>([]); |
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