Skip to content

Commit

Permalink
Merge pull request #93 from cabcookie/staging
Browse files Browse the repository at this point in the history
Staging
  • Loading branch information
cabcookie authored May 30, 2024
2 parents d9b26ef + e1ea86e commit 7ee8696
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 122 deletions.
89 changes: 42 additions & 47 deletions components/accounts/AccountDetails.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,59 +51,54 @@ const AccountDetails: FC<AccountDetailsProps> = ({
<AddControllerDialog account={account} />
<div className="mt-8" />

{accounts &&
accounts.filter(({ controller }) => controller?.id === account.id)
.length > 0 && (
<Accordion type="single" collapsible className="w-full">
<LeanAccordianItem
title="Responsibilities"
isVisible={showResponsibilities}
>
<ResponsibilitiesList
responsibilities={account.responsibilities}
onlyCurrent
/>
<div className="mt-4" />
<ResponsibilitiesDialog
account={account}
<Accordion type="single" collapsible className="w-full">
<LeanAccordianItem
title="Responsibilities"
isVisible={showResponsibilities}
>
<ResponsibilitiesList
responsibilities={account.responsibilities}
onlyCurrent
/>
<div className="mt-4" />
<ResponsibilitiesDialog
account={account}
addResponsibility={addResponsibility}
/>
</LeanAccordianItem>

{accounts && (
<LeanAccordianItem title="Subsidiaries" isVisible={showSubsidaries}>
<Accordion type="single" collapsible className="w-full">
<AccountsList
accounts={accounts}
controllerId={account.id}
addResponsibility={addResponsibility}
/>
</LeanAccordianItem>

<LeanAccordianItem title="Subsidiaries" isVisible={showSubsidaries}>
<Accordion type="single" collapsible className="w-full">
<AccountsList
accounts={accounts}
controllerId={account.id}
addResponsibility={addResponsibility}
/>
</Accordion>
</LeanAccordianItem>
</Accordion>
</LeanAccordianItem>
)}

<LeanAccordianItem
title="Introduction"
isVisible={showIntroduction}
>
<NotesWriter
notes={account.introduction}
placeholder="Describe the account..."
saveNotes={handleUpdateIntroduction}
/>
</LeanAccordianItem>
<LeanAccordianItem title="Introduction" isVisible={showIntroduction}>
<NotesWriter
notes={account.introduction}
placeholder="Describe the account..."
saveNotes={handleUpdateIntroduction}
/>
</LeanAccordianItem>

<LeanAccordianItem title="Projects" isVisible={showProjects}>
<ProjectList accountId={account.id} />
</LeanAccordianItem>
<LeanAccordianItem title="Projects" isVisible={showProjects}>
<ProjectList accountId={account.id} />
</LeanAccordianItem>

<LeanAccordianItem title="Contacts" isVisible={showContacts}>
Test
</LeanAccordianItem>
<LeanAccordianItem title="Contacts" isVisible={showContacts}>
WORK IN PROGRESS
</LeanAccordianItem>

<LeanAccordianItem title="Notes" isVisible={showNotes}>
<AccountNotes accountId={account.id} />
</LeanAccordianItem>
</Accordion>
)}
<LeanAccordianItem title="Notes" isVisible={showNotes}>
<AccountNotes accountId={account.id} />
</LeanAccordianItem>
</Accordion>
</>
);
};
Expand Down
46 changes: 12 additions & 34 deletions components/accounts/AccountsList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,13 @@ const getSortedAccounts = ({
}: GetSortedAccountsProps) =>
accounts
.filter(({ controller, responsibilities }) => {
if (controllerId && controller?.id === controllerId) return true;
if (controllerId && controller && controller.id === controllerId)
return true;
if (controller) return false;
const currentResponsibility =
responsibilities.filter(
({ startDate, endDate }) =>
startDate <= new Date() && (!endDate || endDate >= new Date())
).length > 0;
const currentResponsibility = responsibilities.some(
({ startDate, endDate }) =>
startDate <= new Date() && (!endDate || endDate >= new Date())
);
return (
(showCurrentOnly && currentResponsibility) ||
(showInvalidOnly && !currentResponsibility)
Expand Down Expand Up @@ -112,29 +112,11 @@ const AccountsList: FC<AccountsListProps> = ({
[accounts, controllerId, showCurrentOnly, showInvalidOnly]
);

const updateOrderNumbers =
(newIndex: number) =>
(list: Account[]): Account[] => {
const current = list[newIndex];
if (newIndex === 0) return [{ ...current, order: list[1].order + 1000 }];
if (newIndex === list.length - 1)
return [{ ...current, order: list[newIndex - 1].order - 1000 }];

const orderPrev = list[newIndex - 1].order;
const orderNext = list[newIndex + 1].order;
const orderBetween =
orderPrev > orderNext + 1
? Math.round((orderPrev + orderNext) / 2)
: undefined;
if (orderBetween) return [{ ...current, order: orderBetween }];

return list
.filter((_, index) => index <= newIndex)
.map((account, idx) => ({
...account,
order: orderNext + 1000 * (newIndex - idx + 1),
}));
};
const updateOrderNumbers = (list: Account[]): Account[] =>
list.map((account, idx) => ({
...account,
order: (list.length - idx) * 10,
}));

const moveItem = (items: Account[], oldIndex: number) => (newIndex: number) =>
arrayMove(items, oldIndex, newIndex);
Expand All @@ -145,11 +127,7 @@ const AccountsList: FC<AccountsListProps> = ({
const oldIndex = items.findIndex((item) => item.id === active.id);
const newIndex = items.findIndex((item) => item.id === over.id);

flow(
moveItem(items, oldIndex),
updateOrderNumbers(newIndex),
updateOrder
)(newIndex);
flow(moveItem(items, oldIndex), updateOrderNumbers, updateOrder)(newIndex);
};

return items.length === 0 ? (
Expand Down
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
15 changes: 2 additions & 13 deletions docs/releases/next.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,3 @@
# Oberfläche für Accounts implementiert (Version :VERSION)
# Editieren von Zuständigkeiten (Version :VERSION)

In der Account Liste sehe ich nun alle Accounts, für die ich im Moment zuständig bin. Ich kann zusätzlich eine Liste aller Accounts aufklappen, für die ich im Moment nicht zuständig bin.

Ich kann jedem Account ein Mutterunternehmen zuordnen.

Ich kann die Reihenfolge der Accounts nun nach Wichtigkeit ordnen und Zeiträume für Zuständigkeiten definieren.

In der Account-Detailansicht kann ich mir Zuständigkeiten, Tochterunternehmen, eine Einleitung zum Account, aktuelle Projekte und Notizen anschauen.

## Geschlossene Issues

Fixes [#36](https://github.com/cabcookie/personal-crm/issues/36)
Fixes [#33](https://github.com/cabcookie/personal-crm/issues/33)
Die Zuständigkeiten für Accounts können nun editiert werden.

0 comments on commit 7ee8696

Please sign in to comment.