Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: ability to add alt text to images #421

Open
wants to merge 21 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 0 additions & 39 deletions apps/frontend/src/components/add-image-modal.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,41 +37,6 @@ export default function ImageModal({
setSubmitableImage(event.target.files);
};

const submitFileInfo = async (fileId, apiBase) => {
const form = new FormData();

const fileInfo = {
alternativeText: currentAlt,
caption: currentCaption,
};

form.append("fileInfo", JSON.stringify(fileInfo));

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

if (updatedFileInfo.status === 200) {
return await updatedFileInfo.json();
} else {
toast({
title: "Error",
description: "There was an error uploading your image",
status: "error",
duration: 5000,
isClosable: true,
});
}
};

const handleImageSubmit = async (event) => {
event.preventDefault();

Expand All @@ -95,14 +60,10 @@ export default function ImageModal({
if (res.status === 200) {
const data = await res.json();

submitFileInfo(data[0].id, apiBase);

editor.commands.setImage({
src: new URL(data[0].url, apiBase),
alt: currentAlt,
title: currentCaption,
id: data[0].id,
jwt: user.jwt,
save: handleSubmit,
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import {
Button,
Input,
useDisclosure,
useToast,
Spacer,
Img,
} from "@chakra-ui/react";
import { useRef, useState } from "react";

Expand All @@ -24,56 +24,19 @@ function UpdateModalAttributes({
incAlt,
incCap,
updateAndRerender,
id,
jwt,
save,
}) {
const [currentAlt, setCurrentAlt] = useState(incAlt);
const [currentCaption, setCurrentCaption] = useState(incCap);

const toast = useToast();

const apiBase = process.env.NEXT_PUBLIC_STRAPI_BACKEND_URL;

const submitFileInfo = async () => {
const form = new FormData();

const fileInfo = {
alternativeText: currentAlt,
caption: currentCaption,
};

form.append("fileInfo", JSON.stringify(fileInfo));
updateAndRerender({
alt: currentAlt,
title: currentCaption,
});

const updatedFileInfo = await fetch(
new URL(`api/upload?id=${id}`, apiBase),
{
method: "post",
headers: {
Accept: "application/json",
Authorization: `Bearer ${jwt}`,
},
body: form,
},
);

if (updatedFileInfo.status === 200) {
console.log("updated file info");
updateAndRerender({
alt: currentAlt,
title: currentCaption,
});
save();
onClose();
} else {
toast({
title: "Error",
description: "There was an error uploading your image",
status: "error",
duration: 5000,
isClosable: true,
});
}
save();
onClose();
};

return (
Expand Down Expand Up @@ -139,7 +102,7 @@ function ImageNode(props) {
return (
<NodeViewWrapper className={className}>
<figure onClick={onOpen}>
<img src={src} alt={alt} title={title} />
<Img src={src} alt={alt} title={title} />
Sembauke marked this conversation as resolved.
Show resolved Hide resolved
<figcaption>{title}</figcaption>
</figure>
<UpdateModalAttributes
Expand Down Expand Up @@ -172,12 +135,6 @@ export default Image.extend({
title: {
default: null,
},
id: {
default: null,
},
jwt: {
default: null,
},
save: {
default: null,
},
Expand Down