Skip to content

Commit

Permalink
Add transfer funds modal
Browse files Browse the repository at this point in the history
  • Loading branch information
gustavlrsn committed Nov 7, 2024
1 parent 7dcb36f commit c06c741
Show file tree
Hide file tree
Showing 8 changed files with 533 additions and 119 deletions.
87 changes: 87 additions & 0 deletions components/FormField.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import React, { useContext } from 'react';
import type { FormikProps } from 'formik';
import { Field } from 'formik';
import { pickBy } from 'lodash';
import { useIntl } from 'react-intl';

import { isOCError } from '../lib/errors';
import { formatFormErrorMessage, RICH_ERROR_MESSAGES } from '../lib/form-utils';

import { Input } from './ui/Input';
import { Label } from './ui/Label';
import { FormikZodContext, getInputAttributesFromZodSchema } from './FormikZod';

export function FormField({
label,
showError = true,
name,
hint,
placeholder,
children,
error: customError,

Check failure on line 21 in components/FormField.tsx

View workflow job for this annotation

GitHub Actions / typescript

Property 'error' does not exist on type '{ label?: string; showError?: boolean; name: string; hint?: string; placeholder?: string; children?: (props: { form: FormikProps<any>; meta: any; field: any; }) => Element; required?: boolean; ... 4 more ...; htmlFor?: string; }'.
...props
}: {
label?: string;
showError?: boolean;
name: string;
hint?: string;
placeholder?: string;
children?: (props: { form: FormikProps<any>; meta: any; field: any }) => JSX.Element;
required?: boolean;
min?: number;
max?: number;
inputType?: string;
disabled?: boolean;
htmlFor?: string;
}) {
const intl = useIntl();
const htmlFor = props.htmlFor || `input-${name}`;
const { schema } = useContext(FormikZodContext);

return (
<Field name={name}>
{({ field, form, meta }) => {
const hasError = Boolean(meta.error && (meta.touched || form.submitCount)) || Boolean(customError);
const error = customError || meta.error;

const fieldAttributes = {
...(schema ? getInputAttributesFromZodSchema(schema, name) : null),
...pickBy(
{
...field,
name: name || htmlFor,
id: htmlFor,
type: props.inputType,
disabled: props.disabled,
min: props.min,
max: props.max,
required: props.required,
error: hasError,
placeholder,
},
value => value !== undefined,
),
};
console.log({ fieldAttributes });

Check failure on line 65 in components/FormField.tsx

View workflow job for this annotation

GitHub Actions / lint

Unexpected console statement
if (
'required' in fieldAttributes &&
!fieldAttributes.required &&
meta.error &&
meta.error === intl.formatMessage(RICH_ERROR_MESSAGES.requiredValue)
) {
fieldAttributes.required = true;
}
return (
<div className="flex w-full flex-col gap-1">
{label && <Label className="leading-normal">{label}</Label>}
{hint && <p className="text-sm text-muted-foreground">{hint}</p>}
{children ? children({ form, meta, field: fieldAttributes }) : <Input {...fieldAttributes} />}
{hasError && showError && (
<p className="text-xs text-red-600">{isOCError(error) ? formatFormErrorMessage(intl, error) : error}</p>
)}
</div>
);
}}
</Field>
);
}
9 changes: 2 additions & 7 deletions components/crowdfunding-redesign/edit/EditFundraiser.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { i18nGraphqlException } from '../../../lib/errors';
import { API_V2_CONTEXT } from '../../../lib/graphql/helpers';
import { isValidUrl } from '../../../lib/utils';

import { FormField } from '../../FormField';
import { FormikZod } from '../../FormikZod';
import StyledDropzone, { DROPZONE_ACCEPT_IMAGES } from '../../StyledDropzone';
import Tabs from '../../Tabs';
Expand All @@ -19,13 +20,7 @@ import { toast } from '../../ui/useToast';
import type { Fundraiser } from '../helpers';
import { fundraiserSchema, getDefaultFundraiserValues } from '../helpers';

import {
ColumnSection,
editCrowdfundingSettingsMutation,
FormField,
LongDescriptionForm,
MainDetailsForm,
} from './common';
import { ColumnSection, editCrowdfundingSettingsMutation, LongDescriptionForm, MainDetailsForm } from './common';

const CoverImageForm = ({ schema, initialValues, onSubmit }) => {
const tabs = React.useMemo(
Expand Down
9 changes: 2 additions & 7 deletions components/crowdfunding-redesign/edit/EditProfile.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { i18nGraphqlException } from '../../../lib/errors';
import { API_V2_CONTEXT } from '../../../lib/graphql/helpers';
import { isValidUrl } from '../../../lib/utils';

import { FormField } from '../../FormField';
import { FormikZod } from '../../FormikZod';
import StyledDropzone, { DROPZONE_ACCEPT_IMAGES } from '../../StyledDropzone';
import { Button } from '../../ui/Button';
Expand All @@ -17,13 +18,7 @@ import { toast } from '../../ui/useToast';
import type { Fundraiser } from '../helpers';
import { getDefaultProfileValues, profileSchema } from '../helpers';

import {
ColumnSection,
editCrowdfundingSettingsMutation,
FormField,
LongDescriptionForm,
MainDetailsForm,
} from './common';
import { ColumnSection, editCrowdfundingSettingsMutation, LongDescriptionForm, MainDetailsForm } from './common';

const CoverImageForm = ({ schema, initialValues, onSubmit }) => {
return (
Expand Down
89 changes: 6 additions & 83 deletions components/crowdfunding-redesign/edit/common.tsx
Original file line number Diff line number Diff line change
@@ -1,95 +1,18 @@
import React, { useContext } from 'react';
import React from 'react';
import { gql } from '@apollo/client';
import clsx from 'clsx';
import type { FormikProps } from 'formik';
import { Field, Form } from 'formik';
import { pickBy } from 'lodash';
import { FormattedMessage, useIntl } from 'react-intl';
import { Form } from 'formik';
import { FormattedMessage } from 'react-intl';
import type { z } from 'zod';

import { isOCError } from '../../../lib/errors';
import { formatFormErrorMessage, RICH_ERROR_MESSAGES } from '../../../lib/form-utils';

import { FormikZod, FormikZodContext, getInputAttributesFromZodSchema } from '../../FormikZod';
import { FormField } from '../../FormField';
import { FormikZod } from '../../FormikZod';
import RichTextEditor from '../../RichTextEditor';
import { Button } from '../../ui/Button';
import { Input, InputGroup } from '../../ui/Input';
import { Label } from '../../ui/Label';
import { InputGroup } from '../../ui/Input';
import { Popover, PopoverContent, PopoverTrigger } from '../../ui/Popover';

export function FormField({
label,
showError = true,
name,
hint,
placeholder,
children,
...props
}: {
label?: string;
showError?: boolean;
name: string;
hint?: string;
placeholder?: string;
children?: (props: { form: FormikProps<any>; meta: any; field: any }) => JSX.Element;
required?: boolean;
min?: number;
max?: number;
inputType?: string;
disabled?: boolean;
htmlFor?: string;
}) {
const intl = useIntl();
const htmlFor = props.htmlFor || `input-${name}`;
const { schema } = useContext(FormikZodContext);

return (
<Field name={name}>
{({ field, form, meta }) => {
const hasError = Boolean(meta.error && (meta.touched || form.submitCount));
const fieldAttributes = {
...(schema ? getInputAttributesFromZodSchema(schema, name) : null),
...pickBy(
{
...field,
name: name || htmlFor,
id: htmlFor,
type: props.inputType,
disabled: props.disabled,
min: props.min,
max: props.max,
required: props.required,
error: hasError,
placeholder,
},
value => value !== undefined,
),
};
if (
'required' in fieldAttributes &&
!fieldAttributes.required &&
meta.error &&
meta.error === intl.formatMessage(RICH_ERROR_MESSAGES.requiredValue)
) {
fieldAttributes.required = true;
}
return (
<div className="flex w-full flex-col gap-1">
{label && <Label className="text-base leading-normal">{label}</Label>}
{hint && <p className="text-sm text-muted-foreground">{hint}</p>}
{children ? children({ form, meta, field: fieldAttributes }) : <Input {...fieldAttributes} />}
{hasError && showError && (
<p className="text-xs text-red-500">
{isOCError(meta.error) ? formatFormErrorMessage(intl, meta.error) : meta.error}
</p>
)}
</div>
);
}}
</Field>
);
}

export function ColumnSection({ title, description, children }) {
return (
<div className="grid grid-cols-12 gap-4">
Expand Down
Loading

0 comments on commit c06c741

Please sign in to comment.