Skip to content

Commit

Permalink
Block drawer implementations ...
Browse files Browse the repository at this point in the history
  • Loading branch information
JeremyG11 committed Jan 14, 2024
1 parent 48ad1f7 commit 68dea71
Show file tree
Hide file tree
Showing 9 changed files with 208 additions and 154 deletions.
21 changes: 3 additions & 18 deletions components/Profile/AddBlock.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,17 @@
import React from "react";
import { FaPlus } from "react-icons/fa6";

import { useBlock } from "@/hooks/toggle";
import TooltipButton from "../Reusable/Tooltip";
import { BlockType, useBlock } from "../../hooks/toggle";

export const AddBlock = () => {
const { setDrawerOpen, addBoardBlock } = useBlock();
const { setDrawerOpen, onOpenBlock } = useBlock();
const handleClick = (e: React.MouseEvent<HTMLButtonElement>) => {
e.preventDefault();
setDrawerOpen(true);
};
const handleAddImageBlock = () => {
const imageBlock = {
id: 0, // You can set this to an appropriate value
type: "image" as const, // Type assertion to BlockType
items: [], // Add any initial data as needed
};
addBoardBlock(imageBlock);
onOpenBlock("index");
};

const handleAddTextBlock = () => {
const textBlock = {
id: 0,
type: "text" as const,
items: [], // Add any initial data as needed
};
addBoardBlock(textBlock);
};
return (
<div className=" flex items-center justify-center py-10 w-full">
<span className="h-[2px] flex-1 bg-gray-200"></span>
Expand Down
5 changes: 4 additions & 1 deletion components/Profile/BlockButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,13 @@ const BlockButton = ({
entry.hasOwnProperty(type)
) as IconEntry;

const handleOnClick = () => {
onOpenBlock(type);
};
return (
<div className="flex">
<div className="space-y-4 w-64 pt-2 ">
<button onClick={() => onOpenBlock(type)} className={`${className}`}>
<button onClick={handleOnClick} className={`${className}`}>
<p className="flex items-center w-full">
{iconEntry[type]}
<span className="mx-2.5 font-normal capitalize">{label}</span>
Expand Down
75 changes: 69 additions & 6 deletions components/Profile/BlockDisplayCard.tsx
Original file line number Diff line number Diff line change
@@ -1,22 +1,85 @@
import React from "react";
"use client";
import React, { ChangeEvent, useState } from "react";
import { BlockType, useBlock } from "@/hooks/toggle";
import Image from "next/image";

export default function BlockDisplayCard() {
interface UploaderProps {
blockType: BlockType;
}

const Uploader = ({ blockType }: UploaderProps) => {
const { updateFiles } = useBlock();
const [selectedFile, setSelectedFile] = useState<File | null>(null);
function handleFileChange(event: ChangeEvent<HTMLInputElement>) {
const file = event.target.files?.[0];
if (file) {
const fileUrl = URL.createObjectURL(file);
updateFiles(blockType, [fileUrl]);
}
}
return (
<div className="relative w-full h-96 mx-auto max-w-5xl border-2 rounded-lg p-4 border-pink-500">
<div className="relative w-full h-96 mx-auto max-w-5xl border-2 rounded-lg p-4 border-pink-500">
<label
htmlFor="dropzone-file"
htmlFor={`${blockType}-file`}
className="relative p-3 bg-gray-200 flex flex-col justify-center w-full max-w-5xl mx-auto h-full rounded-lg border-gray-300 cursor-pointer"
>
<div className="z-30 flex flex-col items-center justify-center pt-5 pb-6 space-y-8">
<div className="flex flex-col text-base items-center justify-center space-y-3">
<p className="mb-2">
Drag and drop media, or
Drag and drop {blockType}, or
<span className="border-b border-black mx-1">Browse</span>
</p>
</div>
</div>
</label>
<input id="dropzone-file" type="file" className="hidden" />
<input
id={`${blockType}-file`}
type="file"
className="hidden"
onChange={handleFileChange}
/>
</div>
);
};

export default function BlockDisplayCard() {
const { boardData, onOpenBlock, setDrawerOpen } = useBlock();

const handleFileClick = (blockKey: BlockType) => {
onOpenBlock(blockKey);
setDrawerOpen(true);
};

return (
<div className="w-full space-y-6">
{Object.keys(boardData).map((blockKey) => {
const block = boardData[blockKey as BlockType];
if (typeof block === "object" && block !== null && "files" in block) {
return block.files.length > 0 ? (
block.files.map((file, index) => (
<div
onClick={() => handleFileClick(blockKey as BlockType)}
className="relative w-full h-screen mx-auto max-w-5xl border-2 rounded-lg p-4 border-pink-500"
>
<Image
key={`${blockKey}-${index}`}
src={file}
alt={blockKey}
fill
className="w-96 h-96"
/>
</div>
))
) : (
<div
onClick={() => handleFileClick(blockKey as BlockType)}
className="relative w-full h-screen mx-auto max-w-5xl border-2 rounded-lg p-4 border-pink-500"
>
<Uploader blockType={blockKey as BlockType} />
</div>
);
}
})}
</div>
);
}
17 changes: 14 additions & 3 deletions components/Profile/Blocks/ImageBlock.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
"use client";
import { useBlock } from "@/hooks/toggle";
import AdjustLayout from "../../Reusable/AdjustLayout";
import AltText from "../../Reusable/AltText";
import MediaUpload from "../../Reusable/MediaUpload";
import React from "react";
import useBlock from "../../../hooks/toggle";

export const ImageBlock = () => {
const { addBoardBlock } = useBlock();
const { boardData } = useBlock();
const handleClick = (event: React.MouseEvent<HTMLDivElement>) => {
event.preventDefault();
};
let mediaSrcUrl = "";
if (Array.isArray(boardData.image)) {
mediaSrcUrl = boardData.image[0];
} else if (boardData.image && "files" in boardData.image) {
mediaSrcUrl = boardData.image.files[0];
}

return (
<div>
<div className="w-full rounded-lg flex items-center cursor-pointer">
Expand All @@ -35,7 +42,11 @@ export const ImageBlock = () => {
</p>
</div>
<div>
<MediaUpload />
{false ? (
<img src={"URL.createObjectURL()"} />
) : (
<MediaUpload mediaType="image" mediaSrcUrl={mediaSrcUrl} />
)}
<AltText
label="Alt Text"
placeholder="Enter Text..."
Expand Down
77 changes: 35 additions & 42 deletions components/Profile/FileUploader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,34 @@
import Image from "next/image";
import React, { ChangeEvent, useEffect, useRef, useState } from "react";
import imageIcon from "@/public/images/shotUploadIcon.png";
import useBlock from "@/hooks/toggle";
import { useBlock } from "@/hooks/toggle";

interface FileUploaderProps {
onFilesChange: (files: File[]) => void;
}

const FileUploader = ({ onFilesChange }: FileUploaderProps) => {
const [files, setFiles] = useState<File[]>([]);
const fileContainerRef = useRef<HTMLDivElement | null>(null);
const { blocks, onOpenBlock, setDrawerOpen } = useBlock();

useEffect(() => {
if (fileContainerRef.current) {
// Handle file container logic here
}
}, [files]);
const { onOpenBlock, updateFiles, setDrawerOpen } = useBlock();

const handleFileChange = (e: ChangeEvent<HTMLInputElement>) => {
if (e.target.files) {
const selectedFiles = Array.from(e.target.files);
setFiles(selectedFiles);
onOpenBlock("image");
onFilesChange(selectedFiles);

// Create object URLs from the selected files
const fileUrls = selectedFiles.map((file) => URL.createObjectURL(file));

const fileType = selectedFiles[0].type.split("/")[0];
if (fileType === "image") {
onOpenBlock("image");
updateFiles("image", fileUrls);
} else if (fileType === "video") {
onOpenBlock("video");
updateFiles("video", fileUrls);
}

setDrawerOpen(true);
}
};
Expand All @@ -34,39 +40,26 @@ const FileUploader = ({ onFilesChange }: FileUploaderProps) => {
htmlFor="dropzone-file"
className="relative p-3 flex flex-col justify-center w-full max-w-5xl mx-auto h-full border-2 rounded-lg border-gray-300 cursor-pointer"
>
{files.length === 0 ? (
<div className="z-30 flex flex-col items-center justify-center pt-5 pb-6 space-y-8">
<div className="flex flex-col items-center justify-center space-y-3">
<Image
src={imageIcon}
alt=""
width={80}
height={100}
className=""
/>
<p className="mb-2 text-gray-500">
Drag and drop files, or
<span className="border-b-2 pb-0.5 mx-1 border-pink-500">
Browse
</span>
</p>
<p className="text-gray-500">
Images (png, jpg, gif) or Videos (mp4)
</p>
</div>
</div>
) : (
<div ref={fileContainerRef} className="relative w-full h-full z-30">
{blocks.map((block, index) => (
<div
key={index}
className="relative w-full h-full rounded-lg overflow-hidden"
>
{block.id}
</div>
))}
<div className="z-30 flex flex-col items-center justify-center pt-5 pb-6 space-y-8">
<div className="flex flex-col items-center justify-center space-y-3">
<Image
src={imageIcon}
alt=""
width={80}
height={100}
className=""
/>
<p className="mb-2 text-gray-500">
Drag and drop files, or
<span className="border-b-2 pb-0.5 mx-1 border-pink-500">
Browse
</span>
</p>
<p className="text-gray-500">
Images (png, jpg, gif) or Videos (mp4)
</p>
</div>
)}
</div>

<input
id="dropzone-file"
Expand Down
33 changes: 20 additions & 13 deletions components/Profile/ShotUploadForm.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,28 @@
"use client";
import React, { RefObject, useRef, useState } from "react";
import React, { RefObject, useState } from "react";

import { AddBlock } from "./AddBlock";
import SideDrawer from "./SideDrawer";
import { Button } from "../Reusable/Button";
import TooltipButton from "../Reusable/Tooltip";
import AltText from "../Reusable/AltText";
import FileUploader from "./FileUploader";
import useBlock from "@/hooks/toggle";
import { useBlock } from "@/hooks/toggle";
import BlockDisplayCard from "./BlockDisplayCard";

export default function ShotUploadForm() {
const { blocks, isDrawerOpen } = useBlock();
const { isDrawerOpen, setDrawerOpen, boardData } = useBlock();

const handleDropZone = (files: File[]) => {
const selectedFile = files[0];

const reader = new FileReader();
reader.onload = () => {
// setFiles([selectedFile]);
};

reader.readAsDataURL(new Blob([selectedFile]));
};

return (
<div className="flex w-full min-w-full max-h-screen">
<div className="relative flex-grow overflow-y-auto hide-scroll-bar">
Expand Down Expand Up @@ -60,19 +71,15 @@ export default function ShotUploadForm() {
false ? "mt-3" : "mt-10"
}`}
>
{/* <FileUploader onFilesChange={handleDropZone} /> */}
{blocks.map((block) => (
{Object.keys(boardData).length === 0 ? (
<FileUploader onFilesChange={handleDropZone} />
) : (
<BlockDisplayCard />
))}
)}
</div>

<>
<div className="w-8/12 mx-auto mt-8">
<AltText
placeholder="Write what went in too this design or add any details you like to mention"
className="max-w-2xl block w-full placeholder-gray-400/70 dark:placeholder-gray-500 rounded-lg border-2 border-white hover:border-gray-200 bg-white px-5 py-4 text-gray-700 focus:border-pink-300 focus:outline-none focus:ring focus:ring-pink-200 focus:ring-opacity-40"
/>{" "}
</div>
<div className="w-8/12 mx-auto mt-8"></div>
<AddBlock />
</>
</form>
Expand Down
39 changes: 28 additions & 11 deletions components/Reusable/MediaUpload.tsx
Original file line number Diff line number Diff line change
@@ -1,23 +1,40 @@
import React from "react";

export default function MediaUpload() {
interface MediaUploadProps {
mediaType: "image" | "video";
mediaSrcUrl: string;
}

export default function MediaUpload({
mediaType,
mediaSrcUrl,
}: MediaUploadProps) {
return (
<div>
<label htmlFor="file" className="block font-medium my-3 py-2 pt-4">
Media
</label>
<div className="flex flex-col items-center w-full bg-gray-100 max-w-lg p-5 mx-auto text-center border cursor-pointer rounded-lg">
{mediaType === "image" ? (
<img src={mediaSrcUrl} />
) : (
<video src={mediaSrcUrl} />
)}
</div>

<label
htmlFor="dropzone-file"
className="flex flex-col items-center w-full bg-gray-100 max-w-lg p-5 mx-auto text-center border cursor-pointer rounded-lg"
>
<div className="py-2 px-5 me-2 w-11/12 my-2 font-medium text-sm focus:outline-none bg-white rounded-full border border-gray-200">
Select media
</div>
{!mediaSrcUrl && (
<label
htmlFor="dropzone-file"
className="flex flex-col items-center w-full bg-gray-100 max-w-lg p-5 mx-auto text-center border cursor-pointer rounded-lg"
>
<div className="py-2 px-5 me-2 w-11/12 my-2 font-medium text-sm focus:outline-none bg-white rounded-full border border-gray-200">
Select media
</div>

<p className="text-sm text-gray-500">or drop media to upload</p>
<input id="dropzone-file" type="file" className="hidden" />
</label>
<p className="text-sm text-gray-500">or drop media to upload</p>
<input id="dropzone-file" type="file" className="hidden" />
</label>
)}
</div>
);
}
Loading

0 comments on commit 68dea71

Please sign in to comment.