Merge pull request #585 from IsaacWise06/issue-559
fix(links): Show bulk actions on search page
This commit is contained in:
commit
7fb50337d3
137
pages/search.tsx
137
pages/search.tsx
|
@ -11,11 +11,15 @@ import CardView from "@/components/LinkViews/Layouts/CardView";
|
||||||
// import GridView from "@/components/LinkViews/Layouts/GridView";
|
// import GridView from "@/components/LinkViews/Layouts/GridView";
|
||||||
import ListView from "@/components/LinkViews/Layouts/ListView";
|
import ListView from "@/components/LinkViews/Layouts/ListView";
|
||||||
import PageHeader from "@/components/PageHeader";
|
import PageHeader from "@/components/PageHeader";
|
||||||
import { GridLoader, PropagateLoader } from "react-spinners";
|
import { GridLoader } from "react-spinners";
|
||||||
|
import useCollectivePermissions from "@/hooks/useCollectivePermissions";
|
||||||
|
import toast from "react-hot-toast";
|
||||||
|
import BulkDeleteLinksModal from "@/components/ModalContent/BulkDeleteLinksModal";
|
||||||
|
import BulkEditLinksModal from "@/components/ModalContent/BulkEditLinksModal";
|
||||||
import MasonryView from "@/components/LinkViews/Layouts/MasonryView";
|
import MasonryView from "@/components/LinkViews/Layouts/MasonryView";
|
||||||
|
|
||||||
export default function Search() {
|
export default function Search() {
|
||||||
const { links } = useLinkStore();
|
const { links, selectedLinks, setSelectedLinks, deleteLinksById } = useLinkStore();
|
||||||
|
|
||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
|
|
||||||
|
@ -30,8 +34,48 @@ export default function Search() {
|
||||||
const [viewMode, setViewMode] = useState<string>(
|
const [viewMode, setViewMode] = useState<string>(
|
||||||
localStorage.getItem("viewMode") || ViewMode.Card
|
localStorage.getItem("viewMode") || ViewMode.Card
|
||||||
);
|
);
|
||||||
|
|
||||||
const [sortBy, setSortBy] = useState<Sort>(Sort.DateNewestFirst);
|
const [sortBy, setSortBy] = useState<Sort>(Sort.DateNewestFirst);
|
||||||
|
|
||||||
|
const [bulkDeleteLinksModal, setBulkDeleteLinksModal] = useState(false);
|
||||||
|
const [bulkEditLinksModal, setBulkEditLinksModal] = useState(false);
|
||||||
|
const [editMode, setEditMode] = useState(false);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (editMode) return setEditMode(false);
|
||||||
|
}, [router]);
|
||||||
|
|
||||||
|
const collectivePermissions = useCollectivePermissions(
|
||||||
|
selectedLinks.map((link) => link.collectionId as number)
|
||||||
|
);
|
||||||
|
|
||||||
|
const handleSelectAll = () => {
|
||||||
|
if (selectedLinks.length === links.length) {
|
||||||
|
setSelectedLinks([]);
|
||||||
|
} else {
|
||||||
|
setSelectedLinks(links.map((link) => link));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const bulkDeleteLinks = async () => {
|
||||||
|
const load = toast.loading(
|
||||||
|
`Deleting ${selectedLinks.length} Link${selectedLinks.length > 1 ? "s" : ""
|
||||||
|
}...`
|
||||||
|
);
|
||||||
|
|
||||||
|
const response = await deleteLinksById(
|
||||||
|
selectedLinks.map((link) => link.id as number)
|
||||||
|
);
|
||||||
|
|
||||||
|
toast.dismiss(load);
|
||||||
|
|
||||||
|
response.ok &&
|
||||||
|
toast.success(
|
||||||
|
`Deleted ${selectedLinks.length} Link${selectedLinks.length > 1 ? "s" : ""
|
||||||
|
}!`
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
const { isLoading } = useLinks({
|
const { isLoading } = useLinks({
|
||||||
sort: sortBy,
|
sort: sortBy,
|
||||||
searchQueryString: decodeURIComponent(router.query.q as string),
|
searchQueryString: decodeURIComponent(router.query.q as string),
|
||||||
|
@ -64,6 +108,21 @@ export default function Search() {
|
||||||
|
|
||||||
<div className="flex gap-3 items-center justify-end">
|
<div className="flex gap-3 items-center justify-end">
|
||||||
<div className="flex gap-2 items-center mt-2">
|
<div className="flex gap-2 items-center mt-2">
|
||||||
|
{links.length > 0 && (
|
||||||
|
<div
|
||||||
|
role="button"
|
||||||
|
onClick={() => {
|
||||||
|
setEditMode(!editMode);
|
||||||
|
setSelectedLinks([]);
|
||||||
|
}}
|
||||||
|
className={`btn btn-square btn-sm btn-ghost ${editMode
|
||||||
|
? "bg-primary/20 hover:bg-primary/20"
|
||||||
|
: "hover:bg-neutral/20"
|
||||||
|
}`}
|
||||||
|
>
|
||||||
|
<i className="bi-pencil-fill text-neutral text-xl"></i>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
<FilterSearchDropdown
|
<FilterSearchDropdown
|
||||||
searchFilter={searchFilter}
|
searchFilter={searchFilter}
|
||||||
setSearchFilter={setSearchFilter}
|
setSearchFilter={setSearchFilter}
|
||||||
|
@ -74,6 +133,64 @@ export default function Search() {
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
{editMode && links.length > 0 && (
|
||||||
|
<div className="w-full flex justify-between items-center min-h-[32px]">
|
||||||
|
{links.length > 0 && (
|
||||||
|
<div className="flex gap-3 ml-3">
|
||||||
|
<input
|
||||||
|
type="checkbox"
|
||||||
|
className="checkbox checkbox-primary"
|
||||||
|
onChange={() => handleSelectAll()}
|
||||||
|
checked={
|
||||||
|
selectedLinks.length === links.length && links.length > 0
|
||||||
|
}
|
||||||
|
/>
|
||||||
|
{selectedLinks.length > 0 ? (
|
||||||
|
<span>
|
||||||
|
{selectedLinks.length}{" "}
|
||||||
|
{selectedLinks.length === 1 ? "link" : "links"} selected
|
||||||
|
</span>
|
||||||
|
) : (
|
||||||
|
<span>Nothing selected</span>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
<div className="flex gap-3">
|
||||||
|
<button
|
||||||
|
onClick={() => setBulkEditLinksModal(true)}
|
||||||
|
className="btn btn-sm btn-accent text-white w-fit ml-auto"
|
||||||
|
disabled={
|
||||||
|
selectedLinks.length === 0 ||
|
||||||
|
!(
|
||||||
|
collectivePermissions === true ||
|
||||||
|
collectivePermissions?.canUpdate
|
||||||
|
)
|
||||||
|
}
|
||||||
|
>
|
||||||
|
Edit
|
||||||
|
</button>
|
||||||
|
<button
|
||||||
|
onClick={(e) => {
|
||||||
|
(document?.activeElement as HTMLElement)?.blur();
|
||||||
|
e.shiftKey
|
||||||
|
? bulkDeleteLinks()
|
||||||
|
: setBulkDeleteLinksModal(true);
|
||||||
|
}}
|
||||||
|
className="btn btn-sm bg-red-400 border-red-400 hover:border-red-500 hover:bg-red-500 text-white w-fit ml-auto"
|
||||||
|
disabled={
|
||||||
|
selectedLinks.length === 0 ||
|
||||||
|
!(
|
||||||
|
collectivePermissions === true ||
|
||||||
|
collectivePermissions?.canDelete
|
||||||
|
)
|
||||||
|
}
|
||||||
|
>
|
||||||
|
Delete
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
|
||||||
{!isLoading && !links[0] ? (
|
{!isLoading && !links[0] ? (
|
||||||
<p>
|
<p>
|
||||||
Nothing found.{" "}
|
Nothing found.{" "}
|
||||||
|
@ -82,7 +199,7 @@ export default function Search() {
|
||||||
</span>
|
</span>
|
||||||
</p>
|
</p>
|
||||||
) : links[0] ? (
|
) : links[0] ? (
|
||||||
<LinkComponent links={links} isLoading={isLoading} />
|
<LinkComponent editMode={editMode} links={links} isLoading={isLoading} />
|
||||||
) : (
|
) : (
|
||||||
isLoading && (
|
isLoading && (
|
||||||
<GridLoader
|
<GridLoader
|
||||||
|
@ -94,6 +211,20 @@ export default function Search() {
|
||||||
)
|
)
|
||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
|
{bulkDeleteLinksModal && (
|
||||||
|
<BulkDeleteLinksModal
|
||||||
|
onClose={() => {
|
||||||
|
setBulkDeleteLinksModal(false);
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
)}
|
||||||
|
{bulkEditLinksModal && (
|
||||||
|
<BulkEditLinksModal
|
||||||
|
onClose={() => {
|
||||||
|
setBulkEditLinksModal(false);
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
)}
|
||||||
</MainLayout>
|
</MainLayout>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
Ŝarĝante…
Reference in New Issue