import type { BasicProfile, Dimensions, ImageSources } from '@self.id/framework' import { Anchor, Avatar, Box, Button, Heading, Image, Text, TextArea, TextInput } from 'grommet' import type { TextInputProps } from 'grommet' import Link from 'next/link' import { useCallback, useState } from 'react' import type { FormEvent, ReactNode } from 'react' import { useEditProfile, useImageUpload, useViewerProfile } from '../../hooks' import { getImageURL } from '../../utils' import ConnectedContainer from '../ConnectedContainer' export type FormValue = { name?: string image?: ImageSources description?: string emoji?: string background?: ImageSources birthDate?: string url?: string gender?: string homeLocation?: string residenceCountry?: string nationality?: string // Array in IDX } type ImageKey = 'image' | 'background' function profileToForm({ nationalities, ...profile }: BasicProfile): FormValue { return { ...profile, nationality: nationalities?.[0] } } function changeProfile( profile: BasicProfile, { nationality, residenceCountry, ...value }: FormValue ): BasicProfile { const changed = { ...profile, ...value } // Turn single-value nationality into array, with uppercase value const nationalities = profile.nationalities if (nationality && Array.isArray(nationalities)) { const formattedNationality = nationality.toUpperCase() if (!nationalities.includes(formattedNationality)) { nationalities.unshift(formattedNationality) } } if (nationalities?.length) { changed.nationalities = nationalities } // Residence country code must be uppercase if (residenceCountry != null && residenceCountry !== '') { changed.residenceCountry = residenceCountry.toUpperCase() } return changed } interface CommonFieldProps { inputWidth?: string label: string } interface FieldProps extends CommonFieldProps { children: ReactNode id: string } function Field({ children, id, inputWidth, label }: FieldProps) { return ( {/* @ts-ignore htmlFor from label */} {label} {children} ) } interface TextFieldProps extends CommonFieldProps, Omit, Omit { disabled?: boolean name: Exclude setValue: (value: FormValue) => void value: FormValue } function TextField({ label, name, setValue, value, ...props }: TextFieldProps) { const id = `field-${name}` return ( { setValue({ ...value, [name]: event.target.value }) }} value={value[name] ?? ''} /> ) } interface ImageFieldProps extends CommonFieldProps { disabled?: boolean maxSize?: number name: ImageKey renderImage(props: { sources: ImageSources; onClick?: () => void }): ReactNode resizeDimensions?: Array setValue: (value: FormValue) => void value: FormValue } function ImageField({ disabled, label, maxSize, name, renderImage, resizeDimensions, setValue, value, }: ImageFieldProps) { const { input, state, trigger } = useImageUpload( (sources) => setValue({ ...value, [name]: sources }), { maxSize, dimensions: resizeDimensions } ) const sources = value[name] let content = null if (state === 'uploading') { content =