-
-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: able to add department links (#1786)
- Loading branch information
Showing
18 changed files
with
293 additions
and
8 deletions.
There are no files selected for viewing
12 changes: 12 additions & 0 deletions
12
apps/api/prisma/migrations/20230908142856_department_links/migration.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
-- CreateTable | ||
CREATE TABLE "DepartmentValueLink" ( | ||
"id" TEXT NOT NULL, | ||
"title" TEXT NOT NULL, | ||
"url" TEXT NOT NULL, | ||
"departmentId" TEXT NOT NULL, | ||
|
||
CONSTRAINT "DepartmentValueLink_pkey" PRIMARY KEY ("id") | ||
); | ||
|
||
-- AddForeignKey | ||
ALTER TABLE "DepartmentValueLink" ADD CONSTRAINT "DepartmentValueLink_departmentId_fkey" FOREIGN KEY ("departmentId") REFERENCES "DepartmentValue"("id") ON DELETE RESTRICT ON UPDATE CASCADE; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
123 changes: 123 additions & 0 deletions
123
apps/client/src/components/admin/values/manage-modal/department-links-section.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
import { Button, TextField } from "@snailycad/ui"; | ||
import * as Popover from "@radix-ui/react-popover"; | ||
import * as React from "react"; | ||
import { useTranslations } from "use-intl"; | ||
import { useFormikContext } from "formik"; | ||
import { DepartmentValueLink } from "@snailycad/types"; | ||
import { Table, useTableState } from "components/shared/Table"; | ||
import { v4 } from "uuid"; | ||
|
||
export function DepartmentLinksSection() { | ||
const [openPopover, setOpenPopover] = React.useState<"new" | null>(null); | ||
const tableState = useTableState(); | ||
const { values, setFieldValue } = useFormikContext<{ departmentLinks: DepartmentValueLink[] }>(); | ||
const common = useTranslations("Common"); | ||
|
||
console.log({ | ||
links: values.departmentLinks, | ||
}); | ||
|
||
return ( | ||
<section className="mt-6"> | ||
<header className="flex items-center justify-between"> | ||
<h2 className="text-xl font-semibold">Links</h2> | ||
|
||
<LinkPopover | ||
isPopoverOpen={openPopover === "new"} | ||
setIsPopoverOpen={(v) => setOpenPopover(v ? "new" : null)} | ||
trigger={ | ||
<Button onPress={() => setOpenPopover("new")} size="xs"> | ||
Add Link | ||
</Button> | ||
} | ||
/> | ||
</header> | ||
|
||
{values.departmentLinks.length <= 0 ? ( | ||
<p> | ||
No links added yet. Click the <b>Add Link</b> button to add a link. | ||
</p> | ||
) : ( | ||
<Table | ||
features={{ isWithinCardOrModal: true }} | ||
tableState={tableState} | ||
data={values.departmentLinks.map((url) => { | ||
return { | ||
id: `${url.url}-${url.id}`, | ||
name: url.title, | ||
url: url.url, | ||
actions: ( | ||
<Button | ||
onPress={() => { | ||
setFieldValue( | ||
"departmentLinks", | ||
values.departmentLinks.filter((v) => v.id !== url.id), | ||
); | ||
}} | ||
size="xs" | ||
variant="danger" | ||
> | ||
{common("delete")} | ||
</Button> | ||
), | ||
}; | ||
})} | ||
columns={[ | ||
{ header: common("name"), accessorKey: "name" }, | ||
{ header: common("url"), accessorKey: "url" }, | ||
{ header: common("actions"), accessorKey: "actions" }, | ||
]} | ||
/> | ||
)} | ||
</section> | ||
); | ||
} | ||
|
||
interface LinkPopoverProps { | ||
trigger: React.ReactNode; | ||
isPopoverOpen: boolean; | ||
setIsPopoverOpen: React.Dispatch<React.SetStateAction<boolean>>; | ||
|
||
url?: { name: string; url: string }; | ||
} | ||
|
||
function LinkPopover(props: LinkPopoverProps) { | ||
const common = useTranslations("Common"); | ||
const { values, setFieldValue } = useFormikContext<{ departmentLinks: DepartmentValueLink[] }>(); | ||
|
||
const [name, setName] = React.useState(props.url?.name ?? ""); | ||
const [url, setUrl] = React.useState(props.url?.url ?? ""); | ||
|
||
function handleSubmit() { | ||
setFieldValue("departmentLinks", [...values.departmentLinks, { id: v4(), title: name, url }]); | ||
props.setIsPopoverOpen(false); | ||
} | ||
|
||
return ( | ||
<Popover.Root open={props.isPopoverOpen} onOpenChange={props.setIsPopoverOpen}> | ||
<Popover.Trigger asChild> | ||
<span>{props.trigger}</span> | ||
</Popover.Trigger> | ||
|
||
<Popover.Content className="z-[999] p-4 bg-gray-200 rounded-md shadow-md dropdown-fade w-96 dark:bg-primary dark:border dark:border-secondary text-base font-normal"> | ||
<h3 className="text-xl font-semibold mb-3">Add Link</h3> | ||
|
||
<div> | ||
<TextField label={common("name")} value={name} onChange={(value) => setName(value)} /> | ||
<TextField | ||
type="url" | ||
label={common("url")} | ||
value={url} | ||
onChange={(value) => setUrl(value)} | ||
/> | ||
|
||
<Button type="button" onPress={handleSubmit} size="xs"> | ||
{common("save")} | ||
</Button> | ||
</div> | ||
|
||
<Popover.Arrow className="fill-primary" /> | ||
</Popover.Content> | ||
</Popover.Root> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
apps/client/src/components/leo/modals/department-info-modal.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import { Button } from "@snailycad/ui"; | ||
import { Modal } from "components/modal/Modal"; | ||
import { useModal } from "state/modalState"; | ||
import { ModalIds } from "types/modal-ids"; | ||
import { useTranslations } from "use-intl"; | ||
import { useLeoState } from "state/leo-state"; | ||
|
||
import { useEmsFdState } from "state/ems-fd-state"; | ||
import { useRouter } from "next/router"; | ||
import { Table, useTableState } from "components/shared/Table"; | ||
|
||
export function DepartmentInformationModal() { | ||
const activeOfficer = useLeoState((state) => state.activeOfficer); | ||
const activeEmsFdDeputy = useEmsFdState((state) => state.activeDeputy); | ||
|
||
const modalState = useModal(); | ||
const common = useTranslations("Common"); | ||
const t = useTranslations("Leo"); | ||
const router = useRouter(); | ||
const isLeo = router.pathname.startsWith("/officer"); | ||
const activeUnit = isLeo ? activeOfficer : activeEmsFdDeputy; | ||
const links = activeUnit?.department?.links ?? []; | ||
const tableState = useTableState(); | ||
|
||
console.log({ | ||
activeUnit, | ||
links, | ||
}); | ||
|
||
return ( | ||
<Modal | ||
title={t("departmentInformation")} | ||
onClose={() => modalState.closeModal(ModalIds.DepartmentInfo)} | ||
isOpen={modalState.isOpen(ModalIds.DepartmentInfo)} | ||
className="w-[600px]" | ||
> | ||
<p className="text-[17px] max-w-lg dark:text-gray-400 text-neutral-800"> | ||
{t("departmentInformationDesc")} | ||
</p> | ||
|
||
{links.length <= 0 ? ( | ||
<p className="mt-3 dark:text-gray-400 text-neutral-800 max-w-sm"> | ||
{t("noDepartmentLinks")} | ||
</p> | ||
) : ( | ||
<Table | ||
features={{ isWithinCardOrModal: true }} | ||
data={links.map((url) => { | ||
return { | ||
id: `${url.url}-${url.id}`, | ||
name: url.title, | ||
url: ( | ||
<a | ||
className="underline text-blue-400" | ||
rel="noreferrer" | ||
target="_blank" | ||
href={url.url} | ||
> | ||
{url.url} | ||
</a> | ||
), | ||
}; | ||
})} | ||
columns={[ | ||
{ header: common("name"), accessorKey: "name" }, | ||
{ header: common("url"), accessorKey: "url" }, | ||
]} | ||
tableState={tableState} | ||
/> | ||
)} | ||
|
||
<footer className="flex justify-end mt-5"> | ||
<Button | ||
type="reset" | ||
onPress={() => modalState.closeModal(ModalIds.DepartmentInfo)} | ||
variant="cancel" | ||
> | ||
{common("cancel")} | ||
</Button> | ||
</footer> | ||
</Modal> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.