import { useMutation, useQuery } from '@tanstack/react-query'; import { Button, Card, CardBody, CheckboxControl, Notice, TextareaControl, __experimentalText as Text, __experimentalHeading as Heading, __experimentalHStack as HStack, __experimentalVStack as VStack, ExternalLink, TextControl, } from '@wordpress/components'; import { DataForm } from '@wordpress/dataviews'; import { createInterpolateElement, useMemo } from '@wordpress/element'; import { __, sprintf } from '@wordpress/i18n'; import { useState } from 'react'; import { profileQuery, profileMutation } from '../../app/queries/me-profile'; import InlineSupportLink from '../../components/inline-support-link'; import { PageHeader } from '../../components/page-header'; import PageLayout from '../../components/page-layout'; import EditGravatar from '../edit-gravatar'; import type { UserProfile } from '../../data/types'; import type { Field } from '@wordpress/dataviews'; import './style.scss'; const fields: Field< UserProfile >[] = [ { id: 'user_login', label: __( 'Username' ), type: 'text', Edit: ( { field, data, hideLabelFromVision } ) => { const { getValue } = field; return ( {} } /> ); }, }, { id: 'display_name', label: __( 'Public display name' ), type: 'text', }, { id: 'user_email', label: __( 'Email' ), type: 'text', }, { id: 'user_URL', label: __( 'Public web address' ), type: 'text', }, { id: 'description', label: __( 'About me' ), type: 'text', Edit: ( { field, onChange, data, hideLabelFromVision } ) => { const { id, getValue } = field; return ( onChange( { [ id ]: value } ) } rows={ 4 } /> ); }, }, { id: 'is_dev_account', label: __( 'I am a developer' ), // To do: replace with boolean once implemented. type: 'integer', description: __( 'Opt me into previews of new developer-focused features.' ), Edit: ( { field, onChange, data, hideLabelFromVision } ) => { const { id, getValue, description } = field; return ( onChange( { [ id ]: ! getValue( { item: data } ) } ) } /> ); }, }, ]; const form = { type: 'regular' as const, labelPosition: 'top' as const, fields: [ { id: 'personalInfo', label: __( 'Personal Information' ), children: [ 'user_login', 'display_name', 'user_email', 'user_URL', 'description' ], }, { id: 'developerOptions', label: __( 'Developer options' ), children: [ 'is_dev_account' ], }, ], }; export default function Profile() { const { data: serverData } = useQuery( profileQuery() ); const [ localData, setLocalData ] = useState< Partial< UserProfile > | undefined >(); const [ savingData, setSavingData ] = useState< Partial< UserProfile > | undefined >(); const data = useMemo( () => ( serverData ? { ...serverData, ...savingData, ...localData } : undefined ), [ serverData, savingData, localData ] ); const mutation = useMutation( profileMutation() ); if ( ! data ) { return; } const isSaving = mutation.isPending; const isDirty = !! localData && !! serverData && Object.entries( localData ).some( ( [ key, value ] ) => { return serverData[ key as keyof UserProfile ] !== value; } ); let saveButtonLabel = __( 'Save' ); if ( isSaving ) { saveButtonLabel = __( 'Saving…' ); } else if ( mutation.isSuccess && ! isDirty ) { saveButtonLabel = __( 'Saved!' ); } const handleSubmit = ( e: React.FormEvent ) => { e.preventDefault(); if ( localData ) { // Set a local ref, because onError might called synchronously, and // setSavingData is async. const mutationData = localData; // Clear the local data, so new edits start fresh. setLocalData( undefined ); // Set saving state, so the data shown until settling is correct. setSavingData( mutationData ); mutation.mutate( mutationData, { onSettled: () => { setSavingData( undefined ); }, onError: () => { // Prepend the data to the local data on error. setLocalData( ( currentData ) => ( { ...mutationData, ...currentData } ) ); }, } ); } }; return ( <>
{ __( 'Set your name, bio, and other public-facing information.' ) }{ ' ' } } /> } >
{ __( 'This is your profile photo.' ) }
{ __( 'It appears when you comment on other blogs.' ) }
data={ data } fields={ fields } form={ form } onChange={ ( edits: Partial< UserProfile > ) => { setLocalData( ( current ) => ( { ...current, ...edits } ) ); } } /> { mutation.error && ( { mutation.error.message } ) }
{ __( 'About your profile' ) }

{ createInterpolateElement( sprintf( /* translators: %1$s: User email */ __( 'Your WordPress profile is linked to Gravatar, making your Gravatar public by default. It might appear on other sites using Gravatar when logged in with %s. Manage your Gravatar settings on your Gravatar profile.' ), data.user_email ), { strong: , // @ts-expect-error children prop is injected by createInterpolateElement external: , } ) }

); }