Skip to content

Commit

Permalink
implements sst bucket to change profile pic
Browse files Browse the repository at this point in the history
  • Loading branch information
pedroalonsoms committed May 28, 2024
1 parent 8278da6 commit fa1dd7c
Show file tree
Hide file tree
Showing 8 changed files with 705 additions and 5 deletions.
3 changes: 1 addition & 2 deletions components/BannerImagePanel.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"use client";

import { useState } from "react";
import NextImage from "next/image";
import Banner0 from "@/public/Banner0.svg";

Check failure on line 4 in components/BannerImagePanel.tsx

View workflow job for this annotation

GitHub Actions / build

Cannot find module '@/public/Banner0.svg' or its corresponding type declarations.
import Banner1 from "@/public/Banner1.svg";

Check failure on line 5 in components/BannerImagePanel.tsx

View workflow job for this annotation

GitHub Actions / build

Cannot find module '@/public/Banner1.svg' or its corresponding type declarations.
import Banner2 from "@/public/Banner2.svg";

Check failure on line 6 in components/BannerImagePanel.tsx

View workflow job for this annotation

GitHub Actions / build

Cannot find module '@/public/Banner2.svg' or its corresponding type declarations.
Expand Down Expand Up @@ -67,7 +66,7 @@ const BannerImagePanel = ({ closeModal }: { closeModal: () => void }) => {
} mt-4 rounded-lg px-10 py-2 font-medium text-white drop-shadow-lg`}
onClick={handleBannerSubmit}
>
Upload
Done
</button>
</div>
</div>
Expand Down
31 changes: 30 additions & 1 deletion components/ProfileImagePanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,38 @@
import { useState } from "react";
import { Image } from "@mantine/core";
import { Dropzone, IMAGE_MIME_TYPE, FileWithPath } from "@mantine/dropzone";
import { changeProfileImage, getPreSignedURL } from "@/services/images";
import { useRouter } from "next/navigation";
import { useQueryClient } from "@tanstack/react-query";

const ProfileImagePanel = () => {
const ProfileImagePanel = ({ closeModal }: { closeModal: () => void }) => {
const queryClient = useQueryClient();
const router = useRouter();
const [file, setFile] = useState<FileWithPath[]>([]);

const handleSubmitImage = async () => {
if (!file[0]) {
throw new Error("Image was not selected by the user");
}

const url = await getPreSignedURL();

const image = await fetch(url, {
body: file[0],
method: "PUT",
headers: {
"Content-Type": file[0].type,
"Content-Disposition": `attachment; filename="${file[0].name}"`,
},
});

const uploadedURL = image.url.split("?")[0];
await changeProfileImage(uploadedURL);
closeModal();
await queryClient.invalidateQueries({ queryKey: ["user"] });
router.refresh();
};

const preview = file.map((image, index) => {
const imageUrl = URL.createObjectURL(image);
return (
Expand Down Expand Up @@ -56,6 +84,7 @@ const ProfileImagePanel = () => {
Reset
</button>
<button
onClick={handleSubmitImage}
disabled={file.length === 0}
className={`${
file.length === 0
Expand Down
1 change: 0 additions & 1 deletion components/UserProfileButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ const UserProfileButton = ({
>
{photoUrl ? (
<Image
priority
src={photoUrl}
alt={"User profile photo"}
className={`rounded-full ${sizes[size]}`}
Expand Down
2 changes: 1 addition & 1 deletion components/modals/SettingsDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ const SettingsDialog = ({ showModal, onClose }: SettingsDialogProps) => {
</Tabs.List>

<Tabs.Panel value="profileImage">
<ProfileImagePanel />
<ProfileImagePanel closeModal={onClose} />
</Tabs.Panel>

<Tabs.Panel value="bannerImage">
Expand Down
4 changes: 4 additions & 0 deletions next.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ const nextConfig = {
// Optionally, you can also specify the pathname prefix if needed:
// pathname: '/your/pathname/prefix/*',
},
{
protocol: "https",
hostname: "*.amazonaws.com",
},
],
},
eslint: {
Expand Down
Loading

0 comments on commit fa1dd7c

Please sign in to comment.