import React, { ReactNode, useRef } from 'react' import KeyValue from 'src/components/KeyValue/KeyValue' import InputText from '../InputText/InputText' import Editor from 'rich-markdown-editor' import ImageUploader from 'src/components/ImageUploader' import { User } from 'types/graphql' export const fieldComponents = { name: NameField, userName: UserNameField, image: ImageField, bio: BioField, createdAt: MemberSinceField, } export interface UserProfileType { user: User isEditing: boolean loading: boolean error: boolean onSave: Function projects: {}[] } export interface FieldType { name: string currentValue: any newValue: any isEditing: boolean hasPermissionToEdit: boolean } export interface FieldComponentPropsType { field: FieldType dispatch?: React.Dispatch user?: User save?: Function hasPermissionToEdit?: boolean } interface ProfileKeyValueType extends FieldComponentPropsType { children: ReactNode bottom: boolean } const ProfileKeyValue = ({ field, dispatch, user, save, hasPermissionToEdit, children, bottom = false, }: ProfileKeyValueType) => { return ( user[field.name] && hasPermissionToEdit && ( { if (field.isEditing && field.currentValue !== field.newValue) { save(user.userName, { [field.name]: field.newValue }) } dispatch({ type: 'SET_CURRENT_VALUE', payload: { field: field.name, value: field.newValue }, }) dispatch({ type: 'TOGGLE_EDITING', payload: field.name }) }, }} bottom={bottom} className="mb-4" > {children} ) ) } function BioField(props) { const ref = useRef(null) const { field, dispatch } = props return (
e?.target?.id === 'bio-wrap' && ref?.current?.focusAtEnd() } > dispatch({ type: 'SET_NEW_VALUE', payload: { field: 'bio', value: bio() }, }) } />
) } function MemberSinceField(props: FieldComponentPropsType) { return (

{new Date(props.field.currentValue).toLocaleDateString()}

) } function ImageField(props: FieldComponentPropsType) { const { field, user, save, hasPermissionToEdit } = props return ( { save(user.userName, { image, }) }} aspectRatio={1} isEditable={hasPermissionToEdit} imageUrl={user.image} width={300} /> ) } function NameField(props: FieldComponentPropsType) { const { user, dispatch, field } = props return ( {!field.isEditing ? (

{user?.name}

) : ( dispatch({ type: 'SET_NEW_VALUE', payload: { field: 'name', value }, }) } isEditable={field.isEditing} /> )}
) } function UserNameField(props: FieldComponentPropsType) { const { dispatch, field } = props return ( {!field.isEditing ? (

@{field?.currentValue?.replace(/([^a-zA-Z\d_:])/g, '-')}

) : ( dispatch({ type: 'SET_NEW_VALUE', payload: { field: 'userName', value }, }) } isEditable={field.isEditing} /> )}
) } export function fieldReducer(state, action) { switch (action.type) { case 'TOGGLE_EDITING': return { ...state, [action.payload]: { ...state[action.payload], isEditing: state[action.payload].hasPermissionToEdit && !state[action.payload].isEditing ? true : false, }, } case 'SET_NEW_VALUE': const newState = { ...state, [action.payload.field]: { ...state[action.payload.field], newValue: action.payload.value, }, } return newState default: return state } }