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

Image uploader module #361

Merged
merged 10 commits into from
Jun 11, 2024
8 changes: 4 additions & 4 deletions apps/new/widget/page/projects/Editor.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ useEffect(() => {
const {
name,
description,
profileImage,
image,
backgroundImage,
linktree,
plTeam,
Expand All @@ -138,7 +138,7 @@ useEffect(() => {
: `https://${website}`
: null,
);
setAvatar(profileImage);
setAvatar(image);
setCoverImage(backgroundImage);
setProjectAccount(poltlockProjectId);
setTags(
Expand Down Expand Up @@ -610,7 +610,7 @@ const SecondScreen = () => {
<div className="form-group mb-3">
<label className="pb-2">Avatar</label>
<Widget
src="${alias_old}/widget/components.ImageUploader"
src="${alias_old}/widget/components.UploadField"
props={{
image: avatar,
onChange: (image) => setAvatar({ image }),
Expand All @@ -620,7 +620,7 @@ const SecondScreen = () => {
<div className="form-group mb-3">
<label className="pb-2">Cover Image</label>
<Widget
src="${alias_old}/widget/components.ImageUploader"
src="${alias_old}/widget/components.UploadField"
props={{
image: coverImage,
onChange: (image) => setCoverImage({ image }),
Expand Down
4 changes: 0 additions & 4 deletions apps/old/widget/components.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@ const { Step } = VM.require("${config_account}/widget/components.Step");
const { InputField } = VM.require(
"${config_account}/widget/components.InputField",
);
const { UploadField } = VM.require(
"${config_account}/widget/components.UploadField",
);
Comment on lines -10 to -12
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why are we removing it from here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi , As hooks are unavailable for modules and I have used this
<Widget
src="${alias_old}/widget/components.UploadField"
props={{
image: avatar,
onChange: (image) => setAvatar({ image }),
}}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi , As hooks are unavailable for modules and I have used this
<Widget
src="${alias_old}/widget/components.UploadField"
props={{
image: avatar,
onChange: (image) => setAvatar({ image }),
}}

const { TextBox } = VM.require("${config_account}/widget/components.TextBox");
const { TextEditor } = VM.require(
"${config_account}/widget/components.TextEditor",
Expand Down Expand Up @@ -71,7 +68,6 @@ return {
Step,
Hashtag,
InputField,
UploadField,
TextBox,
TextEditor,
Checkbox,
Expand Down
3 changes: 1 addition & 2 deletions apps/old/widget/components/Library.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -375,8 +375,7 @@ const components = [
},
preview: (
<div className="d-flex flex-column gap-3 mb-3">
<UploadField />
<UploadField background />
<Widget src="${alias_old}/widget/components.UploadField" />
</div>
),
embedCode: `
Expand Down
111 changes: 89 additions & 22 deletions apps/old/widget/components/UploadField.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@
const { Button } = VM.require("${config_account}/widget/components.Button") || {
Button: () => <></>,
const [img, setImg] = useState("");
const [msg, setMsg] = useState("Upload");
const uploadFile = (files) => {
setMsg("Uploading...");
asyncFetch("https://ipfs.near.social/add", {
method: "POST",
headers: { Accept: "application/json" },
body: files[0],
})
.catch((e) => {
console.error(e);
setMsg("Failed to upload");
})
.then((res) => {
setImg(res.body.cid);
props.setImage({
ipfs_cid: res.body.cid,
});
});
};

const UploadContainer = styled.div`
Expand Down Expand Up @@ -45,25 +62,75 @@ const UploadContainer = styled.div`
font-size: 2rem;
}
`;
const Button = styled.div`
.btn {
all: unset;
display: inline-flex;
padding: 10px 20px;
justify-content: center;
align-items: center;
gap: 4px;
border-radius: 8px;
font-size: 14px;
font-weight: 600;
line-height: normal;
font-family: "Poppins", sans-serif;
transition: all 300ms;
background: var(--button-outline-bg, transparent);
color: var(--button-outline-color, #fff);
color: ${props.theme === "light" ? "black" : ""};
border: 1px solid var(--stroke-color, rgba(255, 255, 255, 0.2));
&:hover:not(:disabled) {
background: var(--button-outline-hover-bg, rgba(255, 255, 255, 0.2));
cursor: pointer;
}
}
`;
const ButtonContainer = styled.div`
display: flex;
align-items: center;
gap: 16px;
`;

function UploadField({ background }) {
return (
<UploadContainer background={background}>
<i class="bi bi-cloud-upload"></i>
<div className="d-flex flex-column gap-2">
<p>Choose a file or drag & drop it here.</p>
<p className="secondary">
JPEG, PNG, PDF, and MP4 formats, up to 50 MB.
</p>
</div>
<Button
variant="outline"
style={{ background: background && "var(--bg-2,#23242B)" }}
>
Browse Files
</Button>
</UploadContainer>
);
}
const UploadedImage = styled.img`
height: 40px;
width: 40px;
border-radius: 16px;
`;

return { UploadField };
return (
<UploadContainer background={background}>
<i class="bi bi-cloud-upload"></i>
<div className="d-flex flex-column gap-2">
<p>Choose a file or drag & drop it here.</p>
<p className="secondary">JPEG, PNG, PDF, and MP4 formats, up to 50 MB.</p>
</div>
<ButtonContainer>
<Button>
<Files
multiple={false}
accepts={["image/*"]}
clickable
className="btn btn-outline-primary"
onChange={(f) => uploadFile(f)}
>
{img ? "Replace" : "Upload File"}
</Files>
</Button>
{img ? (
<UploadedImage
src={`https://ipfs.near.social/ipfs/${img}`}
alt="Image Preview"
/>
) : props.image ? (
<Widget
src="${alias_mob}/widget/Image"
loading=""
props={{ image: props.image, style: { height: 40, width: 40 } }}
/>
) : (
""
)}
</ButtonContainer>
</UploadContainer>
);
Loading