improvements + better error handling

This commit is contained in:
Daniel 2023-07-24 09:39:51 -04:00
parent 6708be2dfd
commit cbf53f6e30
4 changed files with 51 additions and 28 deletions

View File

@ -2,7 +2,11 @@ import {
CollectionIncludingMembersAndLinkCount, CollectionIncludingMembersAndLinkCount,
LinkIncludingShortenedCollectionAndTags, LinkIncludingShortenedCollectionAndTags,
} from "@/types/global"; } from "@/types/global";
import { faFolder, faEllipsis } from "@fortawesome/free-solid-svg-icons"; import {
faFolder,
faEllipsis,
faLink,
} from "@fortawesome/free-solid-svg-icons";
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"; import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import { useEffect, useState } from "react"; import { useEffect, useState } from "react";
import Image from "next/image"; import Image from "next/image";
@ -33,6 +37,14 @@ export default function LinkCard({ link, count, className }: Props) {
const { account } = useAccountStore(); const { account } = useAccountStore();
let shortendURL;
try {
shortendURL = new URL(link.url).host.toLowerCase();
} catch (error) {
console.log(error);
}
const [collection, setCollection] = const [collection, setCollection] =
useState<CollectionIncludingMembersAndLinkCount>( useState<CollectionIncludingMembersAndLinkCount>(
collections.find( collections.find(
@ -144,7 +156,7 @@ export default function LinkCard({ link, count, className }: Props) {
<div className="flex items-baseline gap-1"> <div className="flex items-baseline gap-1">
<p className="text-sm text-sky-500 font-bold">{count + 1}.</p> <p className="text-sm text-sky-500 font-bold">{count + 1}.</p>
<p className="text-lg text-sky-700 font-bold truncate capitalize w-full pr-8"> <p className="text-lg text-sky-700 font-bold truncate capitalize w-full pr-8">
{link.name} {link.name || link.description}
</p> </p>
</div> </div>
<div className="flex gap-3 items-center my-3"> <div className="flex gap-3 items-center my-3">
@ -159,6 +171,10 @@ export default function LinkCard({ link, count, className }: Props) {
</p> </p>
</div> </div>
</div> </div>
<div className="flex items-center gap-1 w-full pr-20 text-gray-500">
<FontAwesomeIcon icon={faLink} className="mt-1 w-4 h-4" />
<p className="truncate w-full">{shortendURL}</p>
</div>
<div className="flex items-center gap-1 text-gray-500"> <div className="flex items-center gap-1 text-gray-500">
<FontAwesomeIcon icon={faCalendarDays} className="w-4 h-4" /> <FontAwesomeIcon icon={faCalendarDays} className="w-4 h-4" />
<p>{formattedDate}</p> <p>{formattedDate}</p>

View File

@ -11,6 +11,7 @@ import useCollectionStore from "@/store/collections";
import { useRouter } from "next/router"; import { useRouter } from "next/router";
import SubmitButton from "../../SubmitButton"; import SubmitButton from "../../SubmitButton";
import { toast } from "react-hot-toast"; import { toast } from "react-hot-toast";
import Link from "next/link";
type Props = type Props =
| { | {
@ -70,8 +71,6 @@ export default function AddOrEditLink({
} }
}, []); }, []);
// const shortendURL = method === "UPDATE" ? new URL(link.url).host.toLowerCase() : undefined;
const setTags = (e: any) => { const setTags = (e: any) => {
const tagNames = e.map((e: any) => { const tagNames = e.map((e: any) => {
return { name: e.label }; return { name: e.label };
@ -119,27 +118,15 @@ export default function AddOrEditLink({
className="text-gray-500 my-2 text-center truncate w-full" className="text-gray-500 my-2 text-center truncate w-full"
title={link.url} title={link.url}
> >
Edit <span className="underline">{link.url}</span> <Link href={link.url} target="_blank" className=" font-bold">
{link.url}
</Link>
</p> </p>
) : null} ) : null}
<div className="grid sm:grid-cols-2 gap-3"> <div className="grid sm:grid-cols-2 gap-3">
<div>
<p className="text-sm text-sky-700 mb-2">
Name
<RequiredBadge />
</p>
<input
value={link.name}
onChange={(e) => setLink({ ...link, name: e.target.value })}
type="text"
placeholder="e.g. Example Link"
className="w-full rounded-md p-2 border-sky-100 border-solid border outline-none focus:border-sky-700 duration-100"
/>
</div>
{method === "CREATE" ? ( {method === "CREATE" ? (
<div> <div className="sm:col-span-2">
<p className="text-sm text-sky-700 mb-2"> <p className="text-sm text-sky-700 mb-2">
URL URL
<RequiredBadge /> <RequiredBadge />
@ -173,7 +160,7 @@ export default function AddOrEditLink({
/> />
</div> </div>
<div className={method === "UPDATE" ? "sm:col-span-2" : ""}> <div>
<p className="text-sm text-sky-700 mb-2">Tags</p> <p className="text-sm text-sky-700 mb-2">Tags</p>
<TagSelection <TagSelection
onChange={setTags} onChange={setTags}
@ -182,6 +169,18 @@ export default function AddOrEditLink({
})} })}
/> />
</div> </div>
<div className="sm:col-span-2">
<p className="text-sm text-sky-700 mb-2">Name</p>
<input
value={link.name}
onChange={(e) => setLink({ ...link, name: e.target.value })}
type="text"
placeholder="e.g. Example Link"
className="w-full rounded-md p-2 border-sky-100 border-solid border outline-none focus:border-sky-700 duration-100"
/>
</div>
<div className="sm:col-span-2"> <div className="sm:col-span-2">
<p className="text-sm text-sky-700 mb-2">Description</p> <p className="text-sm text-sky-700 mb-2">Description</p>
<textarea <textarea

View File

@ -10,11 +10,19 @@ export default async function postLink(
link: LinkIncludingShortenedCollectionAndTags, link: LinkIncludingShortenedCollectionAndTags,
userId: number userId: number
) { ) {
try {
new URL(link.url);
} catch (error) {
return {
response:
"Please enter a valid Address for the Link. (It should start with http/https)",
status: 400,
};
}
link.collection.name = link.collection.name.trim(); link.collection.name = link.collection.name.trim();
if (!link.name) { if (!link.collection.name) {
return { response: "Please enter a valid name for the link.", status: 400 };
} else if (!link.collection.name) {
link.collection.name = "Unnamed Collection"; link.collection.name = "Unnamed Collection";
} }
@ -45,8 +53,8 @@ export default async function postLink(
const newLink: Link = await prisma.link.create({ const newLink: Link = await prisma.link.create({
data: { data: {
name: link.name,
url: link.url, url: link.url,
name: link.name,
description, description,
collection: { collection: {
connectOrCreate: { connectOrCreate: {
@ -66,12 +74,12 @@ export default async function postLink(
connectOrCreate: link.tags.map((tag) => ({ connectOrCreate: link.tags.map((tag) => ({
where: { where: {
name_ownerId: { name_ownerId: {
name: tag.name, name: tag.name.trim(),
ownerId: link.collection.ownerId, ownerId: link.collection.ownerId,
}, },
}, },
create: { create: {
name: tag.name, name: tag.name.trim(),
owner: { owner: {
connect: { connect: {
id: link.collection.ownerId, id: link.collection.ownerId,

View File

@ -5,7 +5,7 @@ export default async function getTitle(url: string) {
// regular expression to find the <title> tag // regular expression to find the <title> tag
let match = text.match(/<title.*>([^<]*)<\/title>/); let match = text.match(/<title.*>([^<]*)<\/title>/);
if (match) return match[1] + " [AUTO GENERATED]"; if (match) return match[1];
else return ""; else return "";
} catch (err) { } catch (err) {
console.log(err); console.log(err);