File size: 5,609 Bytes
f5071ca |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 |
import React, { useState, useEffect } from "react";
import {
Box,
HStack,
Image,
Input,
Spinner,
Text,
useColorModeValue,
} from "@chakra-ui/react";
import { PrimaryBtn, SecondaryBtn } from "../../utils/Buttons";
import AddCvImg from "../AddCvImg";
import AddLangTag from "../TagSuggestion/AddLangTag";
import MDE from "../MDE";
import logo from "../../assets/images/logo.png";
import LeavePageAlert from "../LeavePageAlert";
import { useNavigate } from "react-router-dom";
import PostPreview from "./PostPreview";
import NoTitleMessage from "../../utils/NoTitleMessage";
const CreatePostForm = ({
publishPostHandler,
draftPostHandler,
eidtPostHandler,
setPostTitle,
postTitle,
postData,
pageTitle,
publishing,
savingDraft,
uploadingImg,
setUploadingImg,
toEdit,
}) => {
const naviagte = useNavigate();
const [touch, setTouch] = useState(false);
const [mdeTab, setMdeTab] = useState("write");
const mdeTabChangeHandler = (tabName) => {
setMdeTab(tabName);
};
const isToEdit = toEdit && !postData.draft;
const onSubmit = (handler) => {
setTouch(true);
if (postTitle) {
handler();
}
};
//set placeholder
useEffect(() => {
if (mdeTab === "write") {
document.querySelector(".mde-text").placeholder =
"Write your post content here...";
}
}, [mdeTab]);
return (
<Box
mt="-3.5rem"
sx={{ "input, textarea": { background: "transparent !important" } }}
>
<Box maxW="768px" m="auto">
{/* navbar */}
<Box display="flex" mx={{ base: ".5rem", lg: "0rem" }}>
<Box
display={{ base: "none", md: "flex" }}
alignItems="center"
mr="auto"
>
<Image
src={logo}
cursor="pointer"
alt="logo"
w="50px"
h="40px"
onClick={() => naviagte("/")}
/>
<Text fontSize="xl" ms="1">
{pageTitle} Post
</Text>
</Box>
<Box ms="auto">
<SecondaryBtn onClick={() => mdeTabChangeHandler("write")}>
Edit
</SecondaryBtn>
<SecondaryBtn
m="0 1rem 0 .5rem"
onClick={() => mdeTabChangeHandler("preview")}
disabled={uploadingImg}
>
Preview
</SecondaryBtn>
<LeavePageAlert />
</Box>
</Box>
{/* body */}
<Box height="calc(100vh - 110px)" overflow="auto">
<Box
align="start"
bg={useColorModeValue("light.cardBg", "dark.cardBg")}
className="shadow"
borderRadius="5px"
mt={{ base: ".5rem", md: "1rem" }}
p={{ base: ".5rem", md: "1rem" }}
>
{mdeTab === "write" && (
<Box w="100%">
<AddCvImg
cvImgFromLocalStorage={postData?.cvImg}
setUploadingImg={setUploadingImg}
/>
<Input
variant="unstyled"
ps=".5rem"
placeholder="New post title here..."
bg="transparent !important"
fontSize={{ base: "2rem", md: "2.5rem" }}
fontWeight="700"
value={postTitle}
height="60px"
_focus={{
border: "none !important",
boxShadow: "none !important",
}}
m="0"
required
onChange={({ target }) => setPostTitle(target.value)}
_placeholder={{ color: "#525252" }}
/>
{touch && !postTitle && <NoTitleMessage />}
<AddLangTag filteredTagsFromLocalStorage={postData?.tags} />
<Box w="100%" mt=".5rem !important">
<MDE
MDEValue={postData?.MDEValue}
isSubmitting={publishing || savingDraft}
setUploadingImg={setUploadingImg}
/>
</Box>
</Box>
)}
{mdeTab === "preview" && <PostPreview />}
</Box>
</Box>
{/* buttons container */}
<HStack
justify="flex-end"
w="100%"
py=".5rem"
px=".5rem"
pos="sticky"
bottom="0"
zIndex="1"
bg={useColorModeValue("light.bg", "dark.bg")}
>
{!isToEdit && (
<SecondaryBtn
onClick={() => onSubmit(draftPostHandler)}
disabled={savingDraft || publishing || uploadingImg}
>
{savingDraft ? (
<>
<Spinner size="sm" mr="1" /> Saving draft
</>
) : (
"Save draft"
)}
</SecondaryBtn>
)}
<PrimaryBtn
bg="light.primary"
onClick={() =>
onSubmit(isToEdit ? eidtPostHandler : publishPostHandler)
}
disabled={publishing || savingDraft || uploadingImg}
>
{publishing ? (
<>
<Spinner size="sm" mr="1" />{" "}
{isToEdit ? "Saving changes" : "Publishing"}
</>
) : (
<>{isToEdit ? "Save changes" : "Publish"}</>
)}
</PrimaryBtn>
</HStack>
</Box>
</Box>
);
};
export default CreatePostForm;
|