Skip to content

Commit

Permalink
feat: add hosting for post feature image (freeCodeCamp#354)
Browse files Browse the repository at this point in the history
Co-authored-by: yoko <[email protected]>
  • Loading branch information
Sembauke and sidemt authored Nov 27, 2023
1 parent 9a11144 commit 2f7c227
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 33 deletions.
12 changes: 1 addition & 11 deletions apps/backend/config/middlewares.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,6 @@ module.exports = [
"strapi::query",
"strapi::session",
"strapi::favicon",
"strapi::body",
"strapi::public",
{
name: "strapi::body",
config: {
formLimit: "256mb",
jsonLimit: "256mb",
textLimit: "256mb",
formidable: {
maxFileSize: 250 * 1024 * 1024,
},
},
},
];
9 changes: 0 additions & 9 deletions apps/backend/config/plugins.js

This file was deleted.

1 change: 0 additions & 1 deletion apps/backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
"@strapi/plugin-i18n": "4.15.4",
"@strapi/plugin-users-permissions": "4.15.4",
"@strapi/provider-email-nodemailer": "^4.12.4",
"@strapi/provider-upload-local": "^4.15.4",
"@strapi/strapi": "4.15.4",
"nanoid": "^3.3.6",
"pg": "^8.11.3",
Expand Down
31 changes: 26 additions & 5 deletions apps/frontend/src/components/editor-drawer.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,12 @@ const EditorDrawer = ({
postUrl,
postTagId,
title,
featureImage,
handleUnsavedChanges,
handlePostTagId,
handleAuthorChange,
handlePostUrlChange,
handleFeatureImageChange,
handleSubmit,
}) => {
const { isOpen, onClose, onOpen } = useDisclosure();
Expand All @@ -62,8 +64,6 @@ const EditorDrawer = ({

const [authorName, setAuthorName] = useState("");

const [featureImage, setFeatureImage] = useState("");

useEffect(() => {
if (post) {
const { tags } = post.attributes;
Expand All @@ -80,9 +80,30 @@ const EditorDrawer = ({
}
}, [post, handlePostTagId]);

const handleFileInputChange = (event) => {
const handleFileInputChange = async (event) => {
const file = event.target.files[0];
setFeatureImage(URL.createObjectURL(file));

if (!file) return;

const apiBase = process.env.NEXT_PUBLIC_STRAPI_BACKEND_URL;

const formData = new FormData();
formData.append("files", file);

const response = await fetch(new URL("api/upload", apiBase), {
method: "post",
body: formData,
headers: {
Accept: "application/json",
Authorization: `Bearer ${user.jwt}`,
},
});

if (response.status === 200) {
const data = await response.json();

handleFeatureImageChange(new URL(data[0].url, apiBase), data[0].id);
}
};

function addTag(tagName, tagSlug) {
Expand Down Expand Up @@ -237,7 +258,7 @@ const EditorDrawer = ({
<Button
colorScheme="red"
w="100%"
onClick={() => setFeatureImage(null)}
onClick={() => handleFeatureImageChange(null, null)}
>
Delete Image
</Button>
Expand Down
34 changes: 32 additions & 2 deletions apps/frontend/src/components/post-form.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,13 @@ const PostForm = ({ tags, user, authors, post }) => {

const [unsavedChanges, setUnsavedChanges] = useState(false);

const [featureImage, setFeatureImageUrl] = useState();
const [featureImageId, setFeatureImageId] = useState();

useEffect(() => {
if (post) {
const { title, body, slug, tags } = post.attributes;
const apiBase = process.env.NEXT_PUBLIC_STRAPI_BACKEND_URL;
const { title, body, slug, tags, feature_image } = post.attributes;
const tagIds = tags.data.map((tag) => tag.id);

setTitle(title);
Expand All @@ -52,6 +56,13 @@ const PostForm = ({ tags, user, authors, post }) => {
setPostUrl(slug ?? "");
setPostId(post.id);
setPostTagId(tagIds);

if (feature_image.data) {
setFeatureImageUrl(
new URL(feature_image.data[0].attributes.url, apiBase),
);
setFeatureImageId(feature_image.data[0].id);
}
}
}, [post]);

Expand Down Expand Up @@ -79,6 +90,12 @@ const PostForm = ({ tags, user, authors, post }) => {
setUnsavedChanges(true);
};

const handleFeatureImageChange = (url, id) => {
setFeatureImageUrl(url);
setFeatureImageId(id);
setUnsavedChanges(true);
};

const handleSubmit = useCallback(
async (shouldPublish = null, scheduledDate = "", scheduledTime = "") => {
const nonce = uuidv4();
Expand All @@ -87,6 +104,7 @@ const PostForm = ({ tags, user, authors, post }) => {
const data = {
data: {
title: title,
feature_image: featureImageId !== null ? [featureImageId] : [],
slug: slugify(
postUrl != "" ? postUrl : title != "(UNTITLED)" ? title : nonce,
{
Expand Down Expand Up @@ -162,7 +180,17 @@ const PostForm = ({ tags, user, authors, post }) => {
});
}
},
[toast, title, postUrl, postTagId, content, author, postId, user],
[
toast,
title,
postUrl,
postTagId,
featureImageId,
content,
author,
postId,
user,
],
);

useEffect(() => {
Expand Down Expand Up @@ -246,10 +274,12 @@ const PostForm = ({ tags, user, authors, post }) => {
postTagId={postTagId}
title={title}
postUrl={postUrl}
featureImage={featureImage}
handleTitleChange={handleTitleChange}
handlePostUrlChange={handlePostUrlChange}
handleAuthorChange={handleAuthorChange}
handleUnsavedChanges={handleUnsavedChanges}
handleFeatureImageChange={handleFeatureImageChange}
handlePostTagId={handlePostTagId}
handleSubmit={handleSubmit}
/>
Expand Down
8 changes: 4 additions & 4 deletions apps/frontend/src/components/tiptap.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ function ToolBar({ editor, user }) {
const apiBase = process.env.NEXT_PUBLIC_STRAPI_BACKEND_URL;

const formData = new FormData();
const image = document.getElementById("feature-image").files;
const image = document.getElementById("add-image").files;

// Handle the case where the user opts not to submit an image.
if (!image) return;
Expand Down Expand Up @@ -214,7 +214,7 @@ function ToolBar({ editor, user }) {
leftIcon={<FontAwesomeIcon icon={faListOl} />}
/>
<div className="vl"></div>
<label htmlFor="feature-image" className="custom-file-upload">
<label htmlFor="add-image" className="custom-file-upload">
<Button
type="button"
variant="ghost"
Expand All @@ -223,13 +223,13 @@ function ToolBar({ editor, user }) {
title="Add an image"
aria-label="Add an image"
leftIcon={<FontAwesomeIcon icon={faImage} />}
onClick={() => document.getElementById("feature-image").click()}
onClick={() => document.getElementById("add-image").click()}
/>
</label>
<form id="choose-image-form" onChange={handleImageSubmit}>
<input
type="file"
id="feature-image"
id="add-image"
accept="image/*"
style={{ display: "none" }}
/>{" "}
Expand Down
1 change: 0 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 2f7c227

Please sign in to comment.