Skip to content

Commit

Permalink
Merge pull request #94 from cabcookie/cabcookie/issue36
Browse files Browse the repository at this point in the history
feat: adding editing of existing account responsibilities
  • Loading branch information
cabcookie authored May 30, 2024
2 parents 9a7c684 + 2dd6773 commit e1ea86e
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 30 deletions.
11 changes: 9 additions & 2 deletions components/accounts/ResponsibilityRecord.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { format } from "date-fns";
import { FC } from "react";
import ResponsibilitiesDialog from "./responsibilities-dialog";

export type Responsibility = {
id: string;
Expand All @@ -12,15 +13,21 @@ type ResponsibilityRecordProps = {
};

const ResponsibilityRecord: FC<ResponsibilityRecordProps> = ({
responsibility: { startDate, endDate },
responsibility: { id, startDate, endDate },
}) => {
return (
<div>
<div className="flex flex-row gap-2">
Since{" "}
{[startDate, ...(endDate ? [endDate] : [])]
.map((date) => format(date, "PPP"))
.join(" to ")}
.
<ResponsibilitiesDialog
updateAccount={{ id, startDate, endDate }}
updateResponsibility={(r) => {
console.log(r);
}}
/>
</div>
);
};
Expand Down
93 changes: 67 additions & 26 deletions components/accounts/responsibilities-dialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
import { toLocaleDateString } from "@/helpers/functional";
import { cn } from "@/lib/utils";
import { zodResolver } from "@hookform/resolvers/zod";
import { CalendarIcon } from "lucide-react";
import { CalendarIcon, Edit } from "lucide-react";
import { FC, useState } from "react";
import { useForm } from "react-hook-form";
import { z } from "zod";
Expand All @@ -21,7 +21,6 @@ import {
Dialog,
DialogClose,
DialogContent,
DialogDescription,
DialogFooter,
DialogHeader,
DialogTitle,
Expand All @@ -44,22 +43,39 @@ const FormSchema = z
path: ["endDate"],
});

type ResponsibilitiesDialogProps = {
type CreateResponsibility = {
account: Account;
addResponsibility: (newResp: Responsibility) => void;
updateAccount?: never;
updateResponsibility?: never;
};

type UpdateResponsibility = {
account?: never;
addResponsibility?: never;
updateAccount: Responsibility;
updateResponsibility: (newResp: Responsibility) => void;
};

type ResponsibilitiesDialogProps = CreateResponsibility | UpdateResponsibility;

const ResponsibilitiesDialog: FC<ResponsibilitiesDialogProps> = ({
account,
addResponsibility,
updateAccount,
updateResponsibility,
}) => {
const [open, setOpen] = useState(false);
const { toast } = useToast();
const form = useForm<z.infer<typeof FormSchema>>({
resolver: zodResolver(FormSchema),
defaultValues: !updateAccount
? undefined
: { startDate: updateAccount.startDate, endDate: updateAccount.endDate },
});

const onSubmit = ({ startDate, endDate }: z.infer<typeof FormSchema>) => {
const onAddSubmit = ({ startDate, endDate }: z.infer<typeof FormSchema>) => {
if (!account) return;
toast({
title: "Responsibility created",
description: `Responsibility created for account ${account.name} from ${[
Expand All @@ -70,40 +86,65 @@ const ResponsibilitiesDialog: FC<ResponsibilitiesDialogProps> = ({
.join(" to ")}.`,
});
setOpen(false);
addResponsibility({
id: account.id,
startDate,
endDate,
addResponsibility({ id: account.id, startDate, endDate });
};

const onUpdateSubmit = ({
startDate,
endDate,
}: z.infer<typeof FormSchema>) => {
if (!updateAccount) return;
toast({
title: "Responsibility updated",
description: `Responsibility updated for the account from ${[
startDate,
...(endDate ? [endDate] : []),
]
.map(toLocaleDateString)
.join(" to ")}.`,
});
setOpen(false);
updateResponsibility({ id: updateAccount.id, startDate, endDate });
};

const onSubmit = (data: z.infer<typeof FormSchema>) => {
if (addResponsibility) return onAddSubmit(data);
return onUpdateSubmit(data);
};

return (
<Form {...form}>
<form>
<Dialog open={open} onOpenChange={setOpen}>
<DialogTrigger asChild>
<Button size="sm">Add Responsibility</Button>
{addResponsibility ? (
<Button size="sm">Add Responsibility</Button>
) : (
<Edit className="w-5 h-5 text-muted-foreground hover:text-primary" />
)}
</DialogTrigger>
<DialogContent>
<DialogHeader>
<DialogTitle>Responsibilities {account.name}</DialogTitle>
<DialogDescription>
Set the date range for your responsibility for the account{" "}
{account.name}.
</DialogDescription>
<DialogTitle>
{updateAccount
? "Update Responsibility"
: `Responsibilities ${account.name}`}
</DialogTitle>
</DialogHeader>
<div>
<small>
<div>
<strong>Existing responsibilities:</strong>
</div>
<div>
<ResponsibilitiesList
responsibilities={account.responsibilities}
/>
</div>
</small>
</div>
{account && (
<div>
<small>
<div>
<strong>Existing responsibilities:</strong>
</div>
<div>
<ResponsibilitiesList
responsibilities={account.responsibilities}
/>
</div>
</small>
</div>
)}
<div className="grid grid-cols-2 gap-2">
<FormField
control={form.control}
Expand Down
4 changes: 2 additions & 2 deletions docs/releases/next.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# Account Ansicht verbessert (Version :VERSION)
# Editieren von Zuständigkeiten (Version :VERSION)

Bei Accounts, die keine Töchterunternehmen haben, wurden die Details nicht korrekt angezeigt.
Die Zuständigkeiten für Accounts können nun editiert werden.

0 comments on commit e1ea86e

Please sign in to comment.