forked from aws-samples/amplify-next-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #30 from cabcookie/staging
Staging to main
- Loading branch information
Showing
39 changed files
with
2,786 additions
and
3,148 deletions.
There are no files selected for viewing
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 |
---|---|---|
|
@@ -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 | ||
|
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
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 | ||
``` |
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
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; | ||
}; |
Oops, something went wrong.