Skip to content

Commit

Permalink
Merge pull request #30 from cabcookie/staging
Browse files Browse the repository at this point in the history
Staging to main
  • Loading branch information
cabcookie authored Apr 27, 2024
2 parents 10dfe80 + d2296cd commit 8b6cdfb
Show file tree
Hide file tree
Showing 39 changed files with 2,786 additions and 3,148 deletions.
1 change: 1 addition & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ jobs:

- name: Commit changes
run: |
VERSION=$(node -p "require('./package.json').version")
git config --local user.email "[email protected]"
git config --local user.name "GitHub Action"
git add docs/releases/v*.md package.json
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,5 @@ next-env.d.ts
amplifyconfiguration*
components/import-data/*
!components/import-data/imports-aws-sdk

scripts/temp
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
[![Release Workflow](https://github.com/cabcookie/personal-crm/actions/workflows/release.yml/badge.svg?branch=main)](https://github.com/cabcookie/personal-crm/actions/workflows/release.yml)

Make sure you add a `.env.local` file in the root directory with the content:

```
NEXT_PUBLIC_ALLOW_FAKE_DATA_CREATION=true
```
1 change: 1 addition & 0 deletions amplify.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ frontend:
- npm ci
build:
commands:
- echo "NEXT_PUBLIC_ALLOW_FAKE_DATA_CREATION=$NEXT_PUBLIC_ALLOW_FAKE_DATA_CREATION" >> .env
- npm run build
artifacts:
baseDirectory: .next
Expand Down
13 changes: 13 additions & 0 deletions amplify/data/resource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,18 +136,31 @@ const schema = a.schema({
projects: a.belongsTo("Projects", "projectsId"),
})
.authorization((allow) => [allow.owner()]),
AccountResponsibilities: a
.model({
owner: a
.string()
.authorization((allow) => [allow.owner().to(["read", "delete"])]),
accountId: a.id().required(),
account: a.belongsTo("Account", "accountId"),
startDate: a.date().required(),
endDate: a.date(),
})
.authorization((allow) => [allow.owner()]),
Account: a
.model({
owner: a
.string()
.authorization((allow) => [allow.owner().to(["read", "delete"])]),
notionId: a.integer(),
name: a.string().required(),
order: a.integer(),
introduction: a.string(),
subsidiaries: a.hasMany("Account", "accountSubsidiariesId"),
projects: a.hasMany("AccountProjects", "accountId"),
accountSubsidiariesId: a.id(),
controller: a.belongsTo("Account", "accountSubsidiariesId"),
responsibilities: a.hasMany("AccountResponsibilities", "accountId"),
})
.authorization((allow) => [allow.owner()]),
SixWeekCycle: a
Expand Down
156 changes: 156 additions & 0 deletions api/ContextAccounts.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
import { FC, ReactNode, createContext, useContext } from "react";
import { type Schema } from "@/amplify/data/resource";
import { SelectionSet, generateClient } from "aws-amplify/data";
import useSWR from "swr";
import { handleApiErrors } from "./globals";
const client = generateClient<Schema>();

interface AccountsContextType {
accounts: Account[] | undefined;
errorAccounts: any;
loadingAccounts: boolean;
createAccount: (
accountName: string
) => Promise<Schema["Account"] | undefined>;
getAccountById: (accountId: string) => Account | undefined;
saveAccountName: (
accountId: string,
accountName: string
) => Promise<string | undefined>;
}

export type Account = {
id: string;
name: string;
introduction: string;
controllerId?: string;
order: number;
responsibilities: { startDate: Date; endDate?: Date }[];
};

const selectionSet = [
"id",
"name",
"accountSubsidiariesId",
"introduction",
"order",
"responsibilities.startDate",
"responsibilities.endDate",
] as const;

type AccountData = SelectionSet<Schema["Account"], typeof selectionSet>;

export const mapAccount: (account: AccountData) => Account = ({
id,
name,
accountSubsidiariesId,
introduction,
order,
responsibilities,
}) => ({
id,
name,
introduction: introduction || "",
controllerId: accountSubsidiariesId || undefined,
order: order || 0,
responsibilities: responsibilities
.map(({ startDate, endDate }) => ({
startDate: new Date(startDate),
endDate: !endDate ? undefined : new Date(endDate),
}))
.sort((a, b) => b.startDate.getTime() - a.startDate.getTime()),
});

const fetchAccounts = async () => {
const { data, errors } = await client.models.Account.list({
limit: 500,
selectionSet,
});
if (errors) throw errors;
return data.map(mapAccount).sort((a, b) => a.order - b.order);
};

interface AccountsContextProviderProps {
children: ReactNode;
}

export const AccountsContextProvider: FC<AccountsContextProviderProps> = ({
children,
}) => {
const {
data: accounts,
error: errorAccounts,
isLoading: loadingAccounts,
mutate,
} = useSWR(`/api/accounts/`, fetchAccounts);

const createAccount = async (
accountName: string
): Promise<Schema["Account"] | undefined> => {
if (accountName.length < 3) return;

const newAccount: Account = {
id: crypto.randomUUID(),
name: accountName,
introduction: "",
order: 0,
responsibilities: [],
};

const updatedAccounts = [...(accounts || []), newAccount];
mutate(updatedAccounts, false);

const { data, errors } = await client.models.Account.create({
name: accountName,
});
if (errors) handleApiErrors(errors, "Error creating account");
mutate(updatedAccounts);
return data;
};

const getAccountById = (accountId: string) =>
accounts?.find((account) => account.id === accountId);

const saveAccountName = async (accountId: string, accountName: string) => {
const updated: Account[] =
accounts?.map((a) =>
a.id !== accountId ? a : { ...a, name: accountName }
) || [];
mutate(updated, false);
const { data, errors } = await client.models.Account.update({
id: accountId,
name: accountName,
});
if (errors) handleApiErrors(errors, "Error updating account");
mutate(updated);
return data?.id;
};

return (
<AccountsContext.Provider
value={{
accounts,
errorAccounts,
loadingAccounts,
createAccount,
getAccountById,
saveAccountName,
}}
>
{children}
</AccountsContext.Provider>
);
};

const AccountsContext = createContext<AccountsContextType | undefined>(
undefined
);

export const useAccountsContext = () => {
const accounts = useContext(AccountsContext);
if (!accounts)
throw new Error(
"useAccountsContext must be used within AccountsContextProvider"
);
return accounts;
};
Loading

0 comments on commit 8b6cdfb

Please sign in to comment.