Fix sorting links when editing and handle not providing any data

This commit is contained in:
Isaac Wise
2024-02-11 01:01:52 -06:00
parent e753f1dded
commit 63597a041f
7 changed files with 45 additions and 32 deletions
@@ -7,15 +7,16 @@ import CreatableSelect from "react-select/creatable";
type Props = { type Props = {
onChange: any; onChange: any;
showDefaultValue?: boolean;
defaultValue?: defaultValue?:
| { | {
label: string; label: string;
value?: number; value?: number;
} }
| undefined; | undefined;
}; };
export default function CollectionSelection({ onChange, defaultValue }: Props) { export default function CollectionSelection({ onChange, defaultValue, showDefaultValue = true }: Props) {
const { collections } = useCollectionStore(); const { collections } = useCollectionStore();
const router = useRouter(); const router = useRouter();
@@ -50,8 +51,8 @@ export default function CollectionSelection({ onChange, defaultValue }: Props) {
onChange={onChange} onChange={onChange}
options={options} options={options}
styles={styles} styles={styles}
defaultValue={defaultValue} defaultValue={showDefaultValue ? defaultValue : null}
// menuPosition="fixed" // menuPosition="fixed"
/> />
); );
} }
+8 -6
View File
@@ -35,11 +35,13 @@ export default function LinkCardCompact({
const { account } = useAccountStore(); const { account } = useAccountStore();
const { links, setSelectedLinks, selectedLinks } = useLinkStore(); const { links, setSelectedLinks, selectedLinks } = useLinkStore();
const handleCheckboxClick = ( const handleCheckboxClick = (link: LinkIncludingShortenedCollectionAndTags) => {
link: LinkIncludingShortenedCollectionAndTags const linkIndex = selectedLinks.findIndex(selectedLink => selectedLink.id === link.id);
) => {
if (selectedLinks.includes(link)) { if (linkIndex !== -1) {
setSelectedLinks(selectedLinks.filter((e) => e !== link)); const updatedLinks = [...selectedLinks];
updatedLinks.splice(linkIndex, 1);
setSelectedLinks(updatedLinks);
} else { } else {
setSelectedLinks([...selectedLinks, link]); setSelectedLinks([...selectedLinks, link]);
} }
@@ -85,7 +87,7 @@ export default function LinkCardCompact({
<input <input
type="checkbox" type="checkbox"
className="checkbox checkbox-primary my-auto mr-2" className="checkbox checkbox-primary my-auto mr-2"
checked={selectedLinks.includes(link)} checked={selectedLinks.some(selectedLink => selectedLink.id === link.id)}
onChange={() => handleCheckboxClick(link)} onChange={() => handleCheckboxClick(link)}
/> />
)} )}
@@ -57,7 +57,7 @@ export default function BulkEditLinksModal({ onClose }: Props) {
<div className="grid sm:grid-cols-2 gap-3"> <div className="grid sm:grid-cols-2 gap-3">
<div> <div>
<p className="mb-2">Collection</p> <p className="mb-2">Collection</p>
<CollectionSelection onChange={setCollection} /> <CollectionSelection showDefaultValue={false} onChange={setCollection} />
</div> </div>
<div> <div>
+5 -1
View File
@@ -18,7 +18,7 @@ export default function useLinks(
searchByTextContent, searchByTextContent,
}: LinkRequestQuery = { sort: 0 } }: LinkRequestQuery = { sort: 0 }
) { ) {
const { links, setLinks, resetLinks } = useLinkStore(); const { links, setLinks, resetLinks, selectedLinks, setSelectedLinks } = useLinkStore();
const router = useRouter(); const router = useRouter();
const { reachedBottom, setReachedBottom } = useDetectPageBottom(); const { reachedBottom, setReachedBottom } = useDetectPageBottom();
@@ -68,8 +68,12 @@ export default function useLinks(
}; };
useEffect(() => { useEffect(() => {
// Save the selected links before resetting the links
// and then restore the selected links after resetting the links
const previouslySelected = selectedLinks
resetLinks(); resetLinks();
setSelectedLinks(previouslySelected);
getLinks(true); getLinks(true);
}, [ }, [
router, router,
+10 -12
View File
@@ -170,7 +170,7 @@ export default function Index() {
<i className="bi-three-dots text-xl" title="More"></i> <i className="bi-three-dots text-xl" title="More"></i>
</div> </div>
<ul className="dropdown-content z-[30] menu shadow bg-base-200 border border-neutral-content rounded-box w-52 mt-1"> <ul className="dropdown-content z-[30] menu shadow bg-base-200 border border-neutral-content rounded-box w-52 mt-1">
{permissions === true ? ( {permissions === true && (
<li> <li>
<div <div
role="button" role="button"
@@ -183,7 +183,7 @@ export default function Index() {
Edit Collection Info Edit Collection Info
</div> </div>
</li> </li>
) : undefined} )}
<li> <li>
<div <div
role="button" role="button"
@@ -198,7 +198,7 @@ export default function Index() {
: "View Team"} : "View Team"}
</div> </div>
</li> </li>
{permissions === true ? ( {permissions === true && (
<li> <li>
<div <div
role="button" role="button"
@@ -211,7 +211,7 @@ export default function Index() {
Create Sub-Collection Create Sub-Collection
</div> </div>
</li> </li>
) : undefined} )}
<li> <li>
<div <div
role="button" role="button"
@@ -231,7 +231,7 @@ export default function Index() {
</div> </div>
)} )}
{activeCollection ? ( {activeCollection && (
<div className={`min-w-[15rem]`}> <div className={`min-w-[15rem]`}>
<div className="flex gap-1 justify-center sm:justify-end items-center w-fit"> <div className="flex gap-1 justify-center sm:justify-end items-center w-fit">
<div <div
@@ -267,18 +267,16 @@ export default function Index() {
</div> </div>
<p className="text-neutral text-sm font-semibold"> <p className="text-neutral text-sm font-semibold">
By {collectionOwner.name} By {collectionOwner.name}
{activeCollection.members.length > 0 {activeCollection.members.length > 0 &&
? ` and ${activeCollection.members.length} others` ` and ${activeCollection.members.length} others`}.
: undefined}
.
</p> </p>
</div> </div>
</div> </div>
) : undefined} )}
{activeCollection?.description ? ( {activeCollection?.description && (
<p>{activeCollection?.description}</p> <p>{activeCollection?.description}</p>
) : undefined} )}
{/* {collections.some((e) => e.parentId === activeCollection.id) ? ( {/* {collections.some((e) => e.parentId === activeCollection.id) ? (
<fieldset className="border rounded-md p-2 border-neutral-content"> <fieldset className="border rounded-md p-2 border-neutral-content">
+11 -3
View File
@@ -258,11 +258,13 @@ export default function Index() {
selectedLinks.length === links.length && links.length > 0 selectedLinks.length === links.length && links.length > 0
} }
/> />
{selectedLinks.length > 0 && ( {selectedLinks.length > 0 ? (
<span> <span>
{selectedLinks.length}{" "} {selectedLinks.length}{" "}
{selectedLinks.length === 1 ? "link" : "links"} selected {selectedLinks.length === 1 ? "link" : "links"} selected
</span> </span>
) : (
<span>Nothing selected</span>
)} )}
</div> </div>
)} )}
@@ -302,11 +304,17 @@ export default function Index() {
</div> </div>
{bulkDeleteLinksModal && ( {bulkDeleteLinksModal && (
<BulkDeleteLinksModal <BulkDeleteLinksModal
onClose={() => setBulkDeleteLinksModal(false)} onClose={() => {
setBulkDeleteLinksModal(false);
setEditMode(false);
}}
/> />
)} )}
{bulkEditLinksModal && ( {bulkEditLinksModal && (
<BulkEditLinksModal onClose={() => setBulkEditLinksModal(false)} /> <BulkEditLinksModal onClose={() => {
setBulkEditLinksModal(false);
setEditMode(false);
}} />
)} )}
</MainLayout> </MainLayout>
); );
+1 -1
View File
@@ -148,7 +148,7 @@ const useLinkStore = create<LinkStore>()((set) => ({
if (response.ok) { if (response.ok) {
set((state) => ({ set((state) => ({
links: state.links.map((e) => links: state.links.map((e) =>
links.some((link) => link.id === e.id) ? { ...e, ...newData } : e links.some((link) => link.id === e.id) ? { ...e, tags: [...e.tags, ...(newData.tags ?? [])] } : e
), ),
})); }));
useTagStore.getState().setTags(); useTagStore.getState().setTags();