Fix sorting links when editing and handle not providing any data
This commit is contained in:
@@ -7,15 +7,16 @@ import CreatableSelect from "react-select/creatable";
|
||||
|
||||
type Props = {
|
||||
onChange: any;
|
||||
showDefaultValue?: boolean;
|
||||
defaultValue?:
|
||||
| {
|
||||
label: string;
|
||||
value?: number;
|
||||
}
|
||||
| undefined;
|
||||
| {
|
||||
label: string;
|
||||
value?: number;
|
||||
}
|
||||
| undefined;
|
||||
};
|
||||
|
||||
export default function CollectionSelection({ onChange, defaultValue }: Props) {
|
||||
export default function CollectionSelection({ onChange, defaultValue, showDefaultValue = true }: Props) {
|
||||
const { collections } = useCollectionStore();
|
||||
const router = useRouter();
|
||||
|
||||
@@ -50,8 +51,8 @@ export default function CollectionSelection({ onChange, defaultValue }: Props) {
|
||||
onChange={onChange}
|
||||
options={options}
|
||||
styles={styles}
|
||||
defaultValue={defaultValue}
|
||||
// menuPosition="fixed"
|
||||
defaultValue={showDefaultValue ? defaultValue : null}
|
||||
// menuPosition="fixed"
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
@@ -35,11 +35,13 @@ export default function LinkCardCompact({
|
||||
const { account } = useAccountStore();
|
||||
const { links, setSelectedLinks, selectedLinks } = useLinkStore();
|
||||
|
||||
const handleCheckboxClick = (
|
||||
link: LinkIncludingShortenedCollectionAndTags
|
||||
) => {
|
||||
if (selectedLinks.includes(link)) {
|
||||
setSelectedLinks(selectedLinks.filter((e) => e !== link));
|
||||
const handleCheckboxClick = (link: LinkIncludingShortenedCollectionAndTags) => {
|
||||
const linkIndex = selectedLinks.findIndex(selectedLink => selectedLink.id === link.id);
|
||||
|
||||
if (linkIndex !== -1) {
|
||||
const updatedLinks = [...selectedLinks];
|
||||
updatedLinks.splice(linkIndex, 1);
|
||||
setSelectedLinks(updatedLinks);
|
||||
} else {
|
||||
setSelectedLinks([...selectedLinks, link]);
|
||||
}
|
||||
@@ -85,7 +87,7 @@ export default function LinkCardCompact({
|
||||
<input
|
||||
type="checkbox"
|
||||
className="checkbox checkbox-primary my-auto mr-2"
|
||||
checked={selectedLinks.includes(link)}
|
||||
checked={selectedLinks.some(selectedLink => selectedLink.id === link.id)}
|
||||
onChange={() => handleCheckboxClick(link)}
|
||||
/>
|
||||
)}
|
||||
|
||||
@@ -57,7 +57,7 @@ export default function BulkEditLinksModal({ onClose }: Props) {
|
||||
<div className="grid sm:grid-cols-2 gap-3">
|
||||
<div>
|
||||
<p className="mb-2">Collection</p>
|
||||
<CollectionSelection onChange={setCollection} />
|
||||
<CollectionSelection showDefaultValue={false} onChange={setCollection} />
|
||||
</div>
|
||||
|
||||
<div>
|
||||
|
||||
+5
-1
@@ -18,7 +18,7 @@ export default function useLinks(
|
||||
searchByTextContent,
|
||||
}: LinkRequestQuery = { sort: 0 }
|
||||
) {
|
||||
const { links, setLinks, resetLinks } = useLinkStore();
|
||||
const { links, setLinks, resetLinks, selectedLinks, setSelectedLinks } = useLinkStore();
|
||||
const router = useRouter();
|
||||
|
||||
const { reachedBottom, setReachedBottom } = useDetectPageBottom();
|
||||
@@ -68,8 +68,12 @@ export default function useLinks(
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
// Save the selected links before resetting the links
|
||||
// and then restore the selected links after resetting the links
|
||||
const previouslySelected = selectedLinks
|
||||
resetLinks();
|
||||
|
||||
setSelectedLinks(previouslySelected);
|
||||
getLinks(true);
|
||||
}, [
|
||||
router,
|
||||
|
||||
+10
-12
@@ -170,7 +170,7 @@ export default function Index() {
|
||||
<i className="bi-three-dots text-xl" title="More"></i>
|
||||
</div>
|
||||
<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>
|
||||
<div
|
||||
role="button"
|
||||
@@ -183,7 +183,7 @@ export default function Index() {
|
||||
Edit Collection Info
|
||||
</div>
|
||||
</li>
|
||||
) : undefined}
|
||||
)}
|
||||
<li>
|
||||
<div
|
||||
role="button"
|
||||
@@ -198,7 +198,7 @@ export default function Index() {
|
||||
: "View Team"}
|
||||
</div>
|
||||
</li>
|
||||
{permissions === true ? (
|
||||
{permissions === true && (
|
||||
<li>
|
||||
<div
|
||||
role="button"
|
||||
@@ -211,7 +211,7 @@ export default function Index() {
|
||||
Create Sub-Collection
|
||||
</div>
|
||||
</li>
|
||||
) : undefined}
|
||||
)}
|
||||
<li>
|
||||
<div
|
||||
role="button"
|
||||
@@ -231,7 +231,7 @@ export default function Index() {
|
||||
</div>
|
||||
)}
|
||||
|
||||
{activeCollection ? (
|
||||
{activeCollection && (
|
||||
<div className={`min-w-[15rem]`}>
|
||||
<div className="flex gap-1 justify-center sm:justify-end items-center w-fit">
|
||||
<div
|
||||
@@ -267,18 +267,16 @@ export default function Index() {
|
||||
</div>
|
||||
<p className="text-neutral text-sm font-semibold">
|
||||
By {collectionOwner.name}
|
||||
{activeCollection.members.length > 0
|
||||
? ` and ${activeCollection.members.length} others`
|
||||
: undefined}
|
||||
.
|
||||
{activeCollection.members.length > 0 &&
|
||||
` and ${activeCollection.members.length} others`}.
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
) : undefined}
|
||||
)}
|
||||
|
||||
{activeCollection?.description ? (
|
||||
{activeCollection?.description && (
|
||||
<p>{activeCollection?.description}</p>
|
||||
) : undefined}
|
||||
)}
|
||||
|
||||
{/* {collections.some((e) => e.parentId === activeCollection.id) ? (
|
||||
<fieldset className="border rounded-md p-2 border-neutral-content">
|
||||
|
||||
+11
-3
@@ -258,11 +258,13 @@ export default function Index() {
|
||||
selectedLinks.length === links.length && links.length > 0
|
||||
}
|
||||
/>
|
||||
{selectedLinks.length > 0 && (
|
||||
{selectedLinks.length > 0 ? (
|
||||
<span>
|
||||
{selectedLinks.length}{" "}
|
||||
{selectedLinks.length === 1 ? "link" : "links"} selected
|
||||
</span>
|
||||
) : (
|
||||
<span>Nothing selected</span>
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
@@ -302,11 +304,17 @@ export default function Index() {
|
||||
</div>
|
||||
{bulkDeleteLinksModal && (
|
||||
<BulkDeleteLinksModal
|
||||
onClose={() => setBulkDeleteLinksModal(false)}
|
||||
onClose={() => {
|
||||
setBulkDeleteLinksModal(false);
|
||||
setEditMode(false);
|
||||
}}
|
||||
/>
|
||||
)}
|
||||
{bulkEditLinksModal && (
|
||||
<BulkEditLinksModal onClose={() => setBulkEditLinksModal(false)} />
|
||||
<BulkEditLinksModal onClose={() => {
|
||||
setBulkEditLinksModal(false);
|
||||
setEditMode(false);
|
||||
}} />
|
||||
)}
|
||||
</MainLayout>
|
||||
);
|
||||
|
||||
+1
-1
@@ -148,7 +148,7 @@ const useLinkStore = create<LinkStore>()((set) => ({
|
||||
if (response.ok) {
|
||||
set((state) => ({
|
||||
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();
|
||||
|
||||
Reference in New Issue
Block a user