From dbe8a55febc50168e8566a3c6011f7cbff99b058 Mon Sep 17 00:00:00 2001 From: Gustav Larsson Date: Thu, 19 Dec 2024 13:51:51 +0100 Subject: [PATCH] cleanup --- components/expenses/EditExpenseDialog.tsx | 20 ++- components/expenses/ExpenseSummary.js | 2 +- components/expenses/ExpenseTypeTag.tsx | 1 - .../form/TypeOfExpenseSection.tsx | 169 ------------------ components/submit-expense/useExpenseForm.ts | 21 +-- 5 files changed, 24 insertions(+), 189 deletions(-) diff --git a/components/expenses/EditExpenseDialog.tsx b/components/expenses/EditExpenseDialog.tsx index 8b3264dca53..38dc79ec051 100644 --- a/components/expenses/EditExpenseDialog.tsx +++ b/components/expenses/EditExpenseDialog.tsx @@ -9,8 +9,9 @@ import { z } from 'zod'; import { i18nGraphqlException } from '../../lib/errors'; import { API_V2_CONTEXT } from '../../lib/graphql/helpers'; -import type { Currency, CurrencyExchangeRateInput, Expense, ExpenseType } from '../../lib/graphql/types/v2/graphql'; +import type { Currency, CurrencyExchangeRateInput, Expense } from '../../lib/graphql/types/v2/graphql'; import useLoggedInUser from '../../lib/hooks/useLoggedInUser'; +import { cn } from '../../lib/utils'; import { FormField } from '../FormField'; import { FormikZod } from '../FormikZod'; @@ -117,7 +118,7 @@ const EditPayee = ({ expense, onSubmit }) => { }, startOptions: startOptions.current, onSubmit: transformedOnSubmit, - pick: { expenseItems: true, hasTax: true, tax: true, payoutMethodId: true, payeeSlug: true }, + pickSchemaFields: { expenseItems: true, hasTax: true, tax: true, payoutMethodId: true, payeeSlug: true }, }); const hasChangedPayee = @@ -208,7 +209,14 @@ const EditPayoutMethod = ({ expense, onSubmit }) => { }, startOptions: startOptions.current, onSubmit: transformedOnSubmit, - pick: { expenseItems: true, hasTax: true, tax: true, payoutMethodId: true, payee: true, payeeLocation: true }, + pickSchemaFields: { + expenseItems: true, + hasTax: true, + tax: true, + payoutMethodId: true, + payee: true, + payeeLocation: true, + }, }); return ( @@ -259,7 +267,7 @@ const EditAttachments = ({ expense, onSubmit }) => { }, startOptions: startOptions.current, onSubmit: transformedOnSubmit, - pick: { expenseAttachedFiles: true }, + pickSchemaFields: { expenseAttachedFiles: true }, }); return ( @@ -322,7 +330,7 @@ const EditExpenseItems = ({ expense, onSubmit }) => { }, startOptions: startOptions.current, onSubmit: transformedOnSubmit, - pick: { expenseItems: true, hasTax: true, tax: true }, + pickSchemaFields: { expenseItems: true, hasTax: true, tax: true }, }); return ( @@ -442,7 +450,7 @@ export default function EditExpenseDialog({ - diff --git a/components/expenses/ExpenseSummary.js b/components/expenses/ExpenseSummary.js index 2498279f0fe..cbb73632f3d 100644 --- a/components/expenses/ExpenseSummary.js +++ b/components/expenses/ExpenseSummary.js @@ -214,7 +214,7 @@ const ExpenseSummary = ({ {expense?.type && ( -
+
{useInlineExpenseEdit && ( diff --git a/components/expenses/ExpenseTypeTag.tsx b/components/expenses/ExpenseTypeTag.tsx index b85d4daef9c..9bc65c0f2cf 100644 --- a/components/expenses/ExpenseTypeTag.tsx +++ b/components/expenses/ExpenseTypeTag.tsx @@ -22,7 +22,6 @@ const ExpenseTypeTag = ({ type, legacyId = undefined, isLoading = false, ...prop ); } - -export function TypeOfExpenseForm(props: TypeOfExpenseSectionProps) { - const intl = useIntl(); - const expenseTypeOption = props.form.values.expenseTypeOption; - const { toast } = useToast(); - - return ( - - props.form.setFieldValue('expenseTypeOption', newValue as ExpenseType)} - className="flex" - > - -
-
- -
-
- -
-
-
- - -
-
- -
-
- -
-
-
-
- - {!props.form.initialLoading && expenseTypeOption === ExpenseType.INVOICE && ( -
- {props.form.options.host?.slug !== props.form.options.account?.slug && - props.form.options.host?.policies?.EXPENSE_POLICIES?.invoicePolicy && ( -
- } - policy={props.form.options.host?.policies?.EXPENSE_POLICIES?.invoicePolicy} - checked={props.form.values.acknowledgedHostInvoiceExpensePolicy} - onAcknowledgedChanged={v => props.form.setFieldValue('acknowledgedHostInvoiceExpensePolicy', v)} - /> -
- )} - - {props.form.options.account?.policies?.EXPENSE_POLICIES?.invoicePolicy && ( -
- } - policy={props.form.options.account?.policies?.EXPENSE_POLICIES?.invoicePolicy} - checked={props.form.values.acknowledgedCollectiveInvoiceExpensePolicy} - onAcknowledgedChanged={v => props.form.setFieldValue('acknowledgedCollectiveInvoiceExpensePolicy', v)} - /> -
- )} -
- )} - - {!props.form.initialLoading && expenseTypeOption === ExpenseType.RECEIPT && ( -
- {props.form.options.host?.slug !== props.form.options.account?.slug && - props.form.options.host?.policies?.EXPENSE_POLICIES?.receiptPolicy && ( -
- } - policy={props.form.options.host?.policies?.EXPENSE_POLICIES?.receiptPolicy} - checked={props.form.values.acknowledgedHostReceiptExpensePolicy} - onAcknowledgedChanged={v => props.form.setFieldValue('acknowledgedHostReceiptExpensePolicy', v)} - /> -
- )} - - {props.form.options.account?.policies?.EXPENSE_POLICIES?.receiptPolicy && ( -
- } - policy={props.form.options.host?.policies?.EXPENSE_POLICIES?.receiptPolicy} - checked={props.form.values.acknowledgedCollectiveReceiptExpensePolicy} - onAcknowledgedChanged={v => props.form.setFieldValue('acknowledgedCollectiveReceiptExpensePolicy', v)} - /> -
- )} -
- )} - - {!props.form.initialLoading && expenseTypeOption === ExpenseType.INVOICE && ( -
- - props.form.setFieldValue('hasInvoiceOption', newValue as YesNoOption)} - > - - - - - - - - - -
-
-
- - {() => ( - { - props.form.setFieldValue('invoiceFile', uploadResults[0].file); - }} - onSuccess={files => { - props.form.setFieldValue('invoiceFile', files ? files[0] : null); - }} - onReject={msg => { - toast({ variant: 'error', message: msg }); - }} - /> - )} - -
-
-
- -
-
-
-
-
- )} -
- ); -} diff --git a/components/submit-expense/useExpenseForm.ts b/components/submit-expense/useExpenseForm.ts index 089d5513221..891e65eb447 100644 --- a/components/submit-expense/useExpenseForm.ts +++ b/components/submit-expense/useExpenseForm.ts @@ -567,11 +567,11 @@ type RecursivePartial = { : T[P]; }; -export function buildFormSchema( +function buildFormSchema( values: ExpenseFormValues, options: Omit, intl: IntlShape, - pick?: any, + pickSchemaFields?: Record, ): z.ZodType, z.ZodObjectDef, RecursivePartial> { const schema = z.object({ accountSlug: z @@ -1143,7 +1143,7 @@ export function buildFormSchema( }), }); - return pick ? schema.pick(pick) : schema; + return pickSchemaFields ? schema.pick(pickSchemaFields as { [K in keyof z.infer]?: true }) : schema; } function getPayeeSlug(values: ExpenseFormValues): string { @@ -1167,7 +1167,7 @@ async function buildFormOptions( values: ExpenseFormValues, startOptions: ExpenseFormStartOptions, refresh?: boolean, - pick?: any, + pickSchemaFields?: Record, ): Promise { const options: ExpenseFormOptions = { schema: z.object({}) }; @@ -1307,11 +1307,8 @@ async function buildFormOptions( options.totalInvoicedInExpenseCurrency = totalInvoiced; } - options.schema = buildFormSchema(values, options, intl, pick); + options.schema = buildFormSchema(values, options, intl, pickSchemaFields); - // if (pick) { - // options.schema = options.schema.pick(pick); - // } return options; } catch (err) { // eslint-disable-next-line no-console @@ -1357,7 +1354,7 @@ export function useExpenseForm(opts: { options: ExpenseFormOptions, startOptions: ExpenseFormStartOptions, ) => void | Promise; - pick?: any; + pickSchemaFields?: Record; }): ExpenseForm { const intl = useIntl(); const apolloClient = useApolloClient(); @@ -1629,7 +1626,7 @@ export function useExpenseForm(opts: { expenseForm.values, startOptions.current, false, - opts.pick, + opts.pickSchemaFields, ), ); if (!startOptions.current.expenseId) { @@ -1640,7 +1637,7 @@ export function useExpenseForm(opts: { } refreshFormOptions(); - }, [apolloClient, LoggedInUser, expenseForm.values, intl, startOptions]); + }, [apolloClient, LoggedInUser, expenseForm.values, intl, startOptions, opts.pickSchemaFields]); // revalidate form const validateForm = expenseForm.validateForm; @@ -1680,7 +1677,7 @@ export function useExpenseForm(opts: { expenseForm.values, startOptions.current, true, - opts.pick, + opts.pickSchemaFields, ), ), });