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

Men11/feat/mentor upload resources #295

Merged
merged 3 commits into from
Oct 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ interface CourseContentProps {
contentShown: {
titlee: string;
duration: number;
id: string;
_id: string;
}[];
contentHidden: {
titlee: string;
duration: number;
id: string;
_id: string;
}[];
}

Expand All @@ -30,13 +30,13 @@ const CourseContents = function ({
return (
<div className="p-4 rounded-[8px] border-Neutra10 border-[1px] row-start-2 row-end-3 col-span-full">
{contentShown.map((content) => (
<CourseContent key={content.id} title={content.titlee} />
<CourseContent key={content._id} title={content.titlee} />
))}
<motion.div animate={{ height }} className="overflow-hidden origin-top">
<div ref={ref}>
{isExpanded &&
contentHidden.map((content) => (
<CourseContent key={content.id} title={content.titlee} />
<CourseContent key={content._id} title={content.titlee} />
))}
</div>
</motion.div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@ export default async function Resource({
}
const data = await res.json();
console.log(data);
const resources = data.courseContents as {
id: string;
titlee: string;
duration: number;
}[];
const resources = (
data.courseContents as {
_id: string;
titlee: string;
duration: number;
}[]
).filter((content) => !!content.titlee);
const resourcesShown = resources.slice(0, 3);
const resourcesHidden = resources.slice(3);
const totalTime = resources.reduce((prev, curr) => prev + curr.duration, 0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { AnimatePresence, motion } from "framer-motion";
import { CancelIcon, CaretIcon } from "@/public/SVGs";
import DropDown from "@/components/DropDown";
import ResourceCurriculum from "./resourceCurriculum";
import { useAuthCtx } from "@/context/AuthContext";

const COURSETYPEOPTIONS = [
"Video Course",
Expand Down Expand Up @@ -86,10 +87,10 @@ export default function UploadResourcesPage() {
localStorage.getItem("Mentor") ||
JSON.stringify({ data: { token: null } })
);
let videoBase64;
const {
data: { token },
} = user;
let videoBase64;

const toastId = toast.loading("Uploading resource, please wait.");

Expand Down Expand Up @@ -130,6 +131,7 @@ export default function UploadResourcesPage() {
price: priceRef.current?.value!,
name: anotherData?.userDetails?.fullName,
role: anotherData?.userDetails?.role,
mentorId: anotherData._id,
company: anotherData?.company,
ratings: "0.0",
reviews: "0",
Expand All @@ -150,14 +152,13 @@ export default function UploadResourcesPage() {
console.log(data);
toast.dismiss(toastId);
if (data.error) {
toast.error(data.error.message);
toast.error(data.error);
return;
}
toast("Resource uploaded successfully!");
router.push(`/mentor-resources/${data._id}`);
};
} catch (e) {
console.log(e);
toast.dismiss(toastId);
toast.error("There was an error uploading the resource.");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export default function UploadResources() {
currentPage: 0,
});
const [data, setData] = useState<
{ name: string; price: number; _id: string; currency: string }[]
{ title: string; price: number; _id: string; currency: string }[]
>([]);
const [deletingResource, setDeletingResource] = useState({
resourceName: "",
Expand All @@ -35,15 +35,33 @@ export default function UploadResources() {
);
if (!res.ok) throw new Error("Failed to fetch resources");
const data = await res.json();
if (data.length === 0) {
console.log(data);

const user = JSON.parse(
localStorage.getItem("Mentor") ||
JSON.stringify({ data: { user: { profileLink: "" } } })
);
console.log(user);
const {
data: {
user: { profileLink },
},
} = user;

const filteredData = data.filter(
(resource: { mentorId: string }) => resource.mentorId === profileLink
);
if (filteredData.length === 0) {
toast("You have no uploaded resources.", {
icon: "ℹ️",
});
}
setData(data);

console.log(filteredData);
setData(filteredData);
setPaginationData({
currentPage: 1,
totalPages: Math.ceil(data.length / 8),
totalPages: Math.ceil(filteredData.length / 8),
});
} catch (e: any) {
toast.error(e.message);
Expand All @@ -54,7 +72,7 @@ export default function UploadResources() {
}, []);

return (
<div className="bg-[#FBFBFB] p-4">
<div className="bg-[#FBFBFB] p-4 min-h-screen">
{deletingResource.resourceName ? (
<div className="fixed inset-0 bg-[rgba(0,_0,_0,_0.2)] backdrop-blur-sm z-30 grid grid-cols-[450px] place-content-center">
<DeleteModal
Expand Down Expand Up @@ -83,7 +101,12 @@ export default function UploadResources() {
<div className="py-5 px-4 bg-white border-[#EAEBF0] border-[1px] rounded-t-xl">
<p className="w-max ml-auto text-[#272D37] font-Inter">
{(paginationData.currentPage - 1) * 8 + 1} -{" "}
{paginationData.currentPage * 8}
{data.slice((paginationData.currentPage - 1) * 8 + 1).length >
8
? paginationData.currentPage * 8
: data.slice((paginationData.currentPage - 1) * 8 + 1)
.length +
((paginationData.currentPage - 1) * 8 + 1)}
</p>
</div>
</th>
Expand Down Expand Up @@ -121,7 +144,7 @@ export default function UploadResources() {
key={resource._id}
isAllSelected={isAllSelected}
id={resource._id}
title={resource.name}
title={resource.title}
price={resource.price}
setDeletingResource={setDeletingResource}
currency={resource.currency}
Expand Down Expand Up @@ -156,10 +179,7 @@ export default function UploadResources() {
<ArrowIcon />
Prev
</button>
<button
type="button"
className="flex items-center gap-4 font-Inter font-medium text-[#5F6D7E]"
>
<div className="flex items-center gap-4 font-Inter font-medium text-[#5F6D7E]">
{paginationData.totalPages < 5 ? (
<>
{Array(paginationData.totalPages)
Expand Down Expand Up @@ -194,15 +214,16 @@ export default function UploadResources() {
<>
{Array(paginationData.totalPages)
.fill(null)
.map((_page, idx, arr) => {
.reduce((acc, _page, idx, arr) => {
const key = Math.random();
if (
idx === 0 ||
idx === 1 ||
idx === arr.length - 2 ||
idx === arr.length - 1
) {
return (
return [
...acc,
<button
type="button"
onKeyUp={() => {
Expand All @@ -222,14 +243,22 @@ export default function UploadResources() {
key={key}
>
{idx + 1}
</button>
);
</button>,
];
}
return <EllipsisIcon className="cursor-pointer" />;
})}
if (idx === 2)
return [
...acc,
<EllipsisIcon
key={key}
className="cursor-pointer"
/>,
];
return acc;
}, [])}
</>
)}
</button>
</div>
<button
type="button"
onKeyUp={() => {
Expand Down Expand Up @@ -313,7 +342,7 @@ const Course = ({
<label
htmlFor={id}
title={title}
className="font-Inter font-medium text-[#272D37] max-w-[140px] overflow-hidden whitespace-nowrap"
className="font-Inter font-medium text-[#272D37] max-w-[140px] overflow-hidden whitespace-nowrap overflow-ellipsis"
>
{title}
</label>
Expand Down
25 changes: 20 additions & 5 deletions app/api/upload-resource/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,26 @@ export async function POST(request: NextRequest) {
);

if (!res.ok) throw new Error("Error fetching resource image");
const imageResponse = await res.json();

if (imageResponse.total === 0) {
console.log("Errored");
throw new Error("Please enter a short title with logical meaning");
}

const {
results: [
{
urls: { raw },
},
],
} = await res.json();
} = imageResponse;

console.log("formdata", formData);
formData.imageUrl = raw;

console.log("formData", formData);

const res2 = await fetch("https://hngmentorme.onrender.com/api/resources", {
method: "POST",
body: JSON.stringify(formData),
Expand All @@ -51,13 +60,19 @@ export async function POST(request: NextRequest) {

const data = await res2.json();
console.log(data);
if (data._id === undefined) {
return NextResponse.json({
success: false,
error: "There was an error uploading the resource.",
});
}
if (data.error) {
return NextResponse.json({ success: false, error: data.error });
return NextResponse.json({ success: false, error: data.error.message });
}
revalidatePath("/mentor-resources");
return NextResponse.json(data);
} catch (e) {
console.log(e);
return NextResponse.json(e);
} catch (e: any) {
console.log(e.message);
return NextResponse.json({ error: e.message });
}
}
2 changes: 1 addition & 1 deletion components/mentor-resources/DeleteModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ interface DeleteModalProps {
setData: Dispatch<
SetStateAction<
{
name: string;
title: string;
price: number;
_id: string;
currency: string;
Expand Down
Loading