Skip to content

Commit

Permalink
Merge pull request #96 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 b12f56d + de2f0ac commit 1037284
Show file tree
Hide file tree
Showing 9 changed files with 122 additions and 59 deletions.
84 changes: 68 additions & 16 deletions api/ContextAccounts.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,16 @@ interface AccountsContextType {
) => Promise<Schema["Account"]["type"] | undefined>;
getAccountById: (accountId: string) => Account | undefined;
updateAccount: (props: UpdateAccountProps) => Promise<string | undefined>;
addResponsibility: (newResp: Responsibility) => Promise<string | undefined>;
addResponsibility: (
accountId: string,
startDate: Date,
endDate?: Date
) => Promise<string | undefined>;
updateResponsibility: (
responsibilityId: string,
startDate: Date,
endDate?: Date
) => Promise<string | undefined>;
assignController: (
accountId: string,
controllerId: string | null
Expand Down Expand Up @@ -65,7 +74,7 @@ const selectionSet = [
type AccountData = SelectionSet<Schema["Account"]["type"], typeof selectionSet>;

export const mapAccount: (account: AccountData) => Account = ({
id,
id: accountId,
name,
controller,
introduction,
Expand All @@ -75,7 +84,7 @@ export const mapAccount: (account: AccountData) => Account = ({
responsibilities,
createdAt,
}) => ({
id,
id: accountId,
name,
introduction: transformNotesVersion({
version: formatVersion,
Expand All @@ -86,11 +95,15 @@ export const mapAccount: (account: AccountData) => Account = ({
order: order || 0,
createdAt: new Date(createdAt),
responsibilities: responsibilities
.map(({ id, startDate, endDate }) => ({
id,
startDate: new Date(startDate),
endDate: !endDate ? undefined : new Date(endDate),
}))
.map(
({ id: respId, startDate, endDate }): Responsibility => ({
id: respId,
accountId: accountId,
accountName: name,
startDate: new Date(startDate),
endDate: !endDate ? undefined : new Date(endDate),
})
)
.sort((a, b) => b.startDate.getTime() - a.startDate.getTime()),
});

Expand Down Expand Up @@ -210,24 +223,33 @@ export const AccountsContextProvider: FC<AccountsContextProviderProps> = ({
return data.id;
};

const addResponsibility = async ({
id,
startDate,
endDate,
}: Responsibility) => {
const addResponsibility = async (
accountId: string,
startDate: Date,
endDate?: Date
) => {
const updated = accounts?.map((account) =>
account.id !== id
account.id !== accountId
? account
: {
...account,
responsibilities: [{ id: crypto.randomUUID(), startDate, endDate }],
responsibilities: [
...account.responsibilities,
{
id: crypto.randomUUID(),
accountId: account.id,
accountName: account.name,
startDate,
endDate,
},
],
}
);
if (accounts) mutate(updated, false);

const { data, errors } = await client.models.AccountResponsibilities.create(
{
accountId: id,
accountId: accountId,
startDate: toISODateString(startDate),
endDate: !endDate ? undefined : toISODateString(endDate),
}
Expand All @@ -238,6 +260,35 @@ export const AccountsContextProvider: FC<AccountsContextProviderProps> = ({
return data.id;
};

const updateResponsibility = async (
responsibilityId: string,
startDate: Date,
endDate?: Date
) => {
const updated = accounts?.map((account) =>
!account.responsibilities.some((r) => r.id === responsibilityId)
? account
: {
...account,
responsibilities: account.responsibilities.map((r) =>
r.id !== responsibilityId ? r : { ...r, startDate, endDate }
),
}
);
if (accounts) mutate(updated, false);

const { data, errors } = await client.models.AccountResponsibilities.update(
{
id: responsibilityId,
startDate: toISODateString(startDate),
endDate: !endDate ? null : toISODateString(endDate),
}
);
if (errors) handleApiErrors(errors, "Error updating responsibility");
if (accounts) mutate(updated);
return data?.id;
};

const updateAccountOrderNo = async (
id: string,
order: number
Expand Down Expand Up @@ -271,6 +322,7 @@ export const AccountsContextProvider: FC<AccountsContextProviderProps> = ({
getAccountById,
updateAccount,
addResponsibility,
updateResponsibility,
assignController,
updateOrder,
}}
Expand Down
14 changes: 8 additions & 6 deletions api/globals.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { flow, map, join } from "lodash/fp";
import { toast } from "@/components/ui/use-toast";
import { flow, join, map } from "lodash/fp";

/**
* Represents a location in a Source.
Expand Down Expand Up @@ -56,15 +57,16 @@ export const handleApiErrors = (
message?: string
) => {
console.error(message, errors);
let errorText = flow(
const errorText = flow(
map(
({ errorType, message }: GraphQLFormattedError) =>
`${errorType}: ${message}`
),
join("; ")
)(errors);
if (message) {
errorText = `${message}: ${errorText}`;
}
alert(errorText);
toast({
title: message || "Error",
description: errorText,
variant: "destructive",
});
};
12 changes: 6 additions & 6 deletions components/accounts/AccountDetails.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,15 @@ import AddControllerDialog from "./AddControllerDialog";
import LeanAccordianItem from "./LeanAccordionItem";
import ProjectList from "./ProjectList";
import ResponsibilitiesList from "./ResponsibilitiesList";
import { Responsibility } from "./ResponsibilityRecord";
import ResponsibilitiesDialog from "./responsibilities-dialog";

type AccountDetailsProps = {
account: Account;
addResponsibility: (resp: Responsibility) => void;
addResponsibility: (
accountId: string,
startDate: Date,
endDate?: Date
) => void;
showResponsibilities?: boolean;
showSubsidaries?: boolean;
showIntroduction?: boolean;
Expand Down Expand Up @@ -56,10 +59,7 @@ const AccountDetails: FC<AccountDetailsProps> = ({
title="Responsibilities"
isVisible={showResponsibilities}
>
<ResponsibilitiesList
responsibilities={account.responsibilities}
onlyCurrent
/>
<ResponsibilitiesList responsibilities={account.responsibilities} />
<div className="mt-4" />
<ResponsibilitiesDialog
account={account}
Expand Down
7 changes: 5 additions & 2 deletions components/accounts/AccountsList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import {
} from "@dnd-kit/sortable";
import { flow } from "lodash/fp";
import { FC, useEffect, useState } from "react";
import { Responsibility } from "./ResponsibilityRecord";
import AccountRecord from "./account-record";

type ShowInvalidOnly = {
Expand All @@ -41,7 +40,11 @@ type AccountsListProps = (
| ShowSubsidaries
) & {
accounts: Account[];
addResponsibility: (resp: Responsibility) => void;
addResponsibility: (
accountId: string,
startDate: Date,
endDate?: Date
) => void;
};

type GetSortedAccountsProps = {
Expand Down
10 changes: 0 additions & 10 deletions components/accounts/ResponsibilitiesList.tsx
Original file line number Diff line number Diff line change
@@ -1,26 +1,16 @@
import { addDaysToDate } from "@/helpers/functional";
import { FC } from "react";
import ResponsibilityRecord, { Responsibility } from "./ResponsibilityRecord";

type ResponsibilitiesListProps = {
responsibilities: Responsibility[];
onlyCurrent?: boolean;
};

const ResponsibilitiesList: FC<ResponsibilitiesListProps> = ({
responsibilities,
onlyCurrent,
}) => {
return (
<div>
{responsibilities
.filter(
({ startDate, endDate }) =>
!onlyCurrent ||
startDate >= new Date() ||
!endDate ||
endDate >= addDaysToDate(-60)(new Date())
)
.sort((a, b) => a.startDate.getTime() - b.startDate.getTime())
.map((resp) => (
<ResponsibilityRecord key={resp.id} responsibility={resp} />
Expand Down
18 changes: 12 additions & 6 deletions components/accounts/ResponsibilityRecord.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import { useAccountsContext } from "@/api/ContextAccounts";
import { format } from "date-fns";
import { FC } from "react";
import ResponsibilitiesDialog from "./responsibilities-dialog";

export type Responsibility = {
id: string;
accountId: string;
accountName: string;
startDate: Date;
endDate?: Date;
};
Expand All @@ -13,20 +16,23 @@ type ResponsibilityRecordProps = {
};

const ResponsibilityRecord: FC<ResponsibilityRecordProps> = ({
responsibility: { id, startDate, endDate },
responsibility,
}) => {
const { updateResponsibility } = useAccountsContext();

return (
<div className="flex flex-row gap-2">
Since{" "}
{[startDate, ...(endDate ? [endDate] : [])]
{[
responsibility.startDate,
...(responsibility.endDate ? [responsibility.endDate] : []),
]
.map((date) => format(date, "PPP"))
.join(" to ")}
.
<ResponsibilitiesDialog
updateAccount={{ id, startDate, endDate }}
updateResponsibility={(r) => {
console.log(r);
}}
updateAccount={responsibility}
updateResponsibility={updateResponsibility}
/>
</div>
);
Expand Down
7 changes: 5 additions & 2 deletions components/accounts/account-record.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,15 @@ import {
AccordionTrigger,
} from "../ui/accordion";
import AccountDetails from "./AccountDetails";
import { Responsibility } from "./ResponsibilityRecord";

type AccountRecordProps = {
account: Account;
className?: string;
addResponsibility: (resp: Responsibility) => void;
addResponsibility: (
accountId: string,
startDate: Date,
endDate?: Date
) => void;
};

const AccountRecord: FC<AccountRecordProps> = ({
Expand Down
25 changes: 16 additions & 9 deletions components/accounts/responsibilities-dialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,11 @@ const FormSchema = z

type CreateResponsibility = {
account: Account;
addResponsibility: (newResp: Responsibility) => void;
addResponsibility: (
accountId: string,
startDate: Date,
endDate?: Date
) => void;
updateAccount?: never;
updateResponsibility?: never;
};
Expand All @@ -54,7 +58,11 @@ type UpdateResponsibility = {
account?: never;
addResponsibility?: never;
updateAccount: Responsibility;
updateResponsibility: (newResp: Responsibility) => void;
updateResponsibility: (
responsibilityId: string,
startDate: Date,
endDate?: Date
) => void;
};

type ResponsibilitiesDialogProps = CreateResponsibility | UpdateResponsibility;
Expand Down Expand Up @@ -86,7 +94,7 @@ const ResponsibilitiesDialog: FC<ResponsibilitiesDialogProps> = ({
.join(" to ")}.`,
});
setOpen(false);
addResponsibility({ id: account.id, startDate, endDate });
addResponsibility(account.id, startDate, endDate);
};

const onUpdateSubmit = ({
Expand All @@ -96,15 +104,14 @@ const ResponsibilitiesDialog: FC<ResponsibilitiesDialogProps> = ({
if (!updateAccount) return;
toast({
title: "Responsibility updated",
description: `Responsibility updated for the account from ${[
startDate,
...(endDate ? [endDate] : []),
]
description: `Responsibility updated for account ${
updateAccount.accountName
} from ${[startDate, ...(endDate ? [endDate] : [])]
.map(toLocaleDateString)
.join(" to ")}.`,
});
setOpen(false);
updateResponsibility({ id: updateAccount.id, startDate, endDate });
updateResponsibility(updateAccount.id, startDate, endDate);
};

const onSubmit = (data: z.infer<typeof FormSchema>) => {
Expand All @@ -127,7 +134,7 @@ const ResponsibilitiesDialog: FC<ResponsibilitiesDialogProps> = ({
<DialogHeader>
<DialogTitle>
{updateAccount
? "Update Responsibility"
? `Update Responsibility ${updateAccount.accountName}`
: `Responsibilities ${account.name}`}
</DialogTitle>
</DialogHeader>
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 @@
# Editieren von Zuständigkeiten (Version :VERSION)
# Editieren von Zuständigkeiten funktioniert (Version :VERSION)

Die Zuständigkeiten für Accounts können nun editiert werden.
Die Zuständigkeiten für Accounts können nun editiert werden. Sie wurde zuvor zwar in der Oberfläche angezeigt und angeboten, aber die Änderungen wurden nicht in der Datenbank gespeichert.

0 comments on commit 1037284

Please sign in to comment.