-
Notifications
You must be signed in to change notification settings - Fork 2k
Dashboard v2: Implement PHP version settings #103668
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,120 @@ | ||
| import { DataForm } from '@automattic/dataviews'; | ||
| import { useQuery, useMutation } from '@tanstack/react-query'; | ||
| import { | ||
| Card, | ||
| CardBody, | ||
| __experimentalHStack as HStack, | ||
| __experimentalVStack as VStack, | ||
| Button, | ||
| } from '@wordpress/components'; | ||
| import { useDispatch } from '@wordpress/data'; | ||
| import { __ } from '@wordpress/i18n'; | ||
| import { store as noticesStore } from '@wordpress/notices'; | ||
| import { useState } from 'react'; | ||
| import { getPHPVersions } from 'calypso/data/php-versions'; | ||
| import { siteQuery, sitePHPVersionQuery, sitePHPVersionMutation } from '../../app/queries'; | ||
| import PageLayout from '../../components/page-layout'; | ||
| import SettingsPageHeader from '../settings-page-header'; | ||
| import { canUpdatePHPVersion } from './utils'; | ||
| import type { Field } from '@automattic/dataviews'; | ||
|
|
||
| export default function PHPVersionSettings( { siteSlug }: { siteSlug: string } ) { | ||
| const { data: site } = useQuery( siteQuery( siteSlug ) ); | ||
| const canUpdate = site && canUpdatePHPVersion( site ); | ||
|
|
||
| const { data: currentVersion } = useQuery( { | ||
| ...sitePHPVersionQuery( siteSlug ), | ||
| enabled: canUpdate, | ||
| } ); | ||
| const mutation = useMutation( sitePHPVersionMutation( siteSlug ) ); | ||
| const { createSuccessNotice, createErrorNotice } = useDispatch( noticesStore ); | ||
|
|
||
| const [ formData, setFormData ] = useState< { version: string } >( { | ||
| version: currentVersion ?? '', | ||
| } ); | ||
|
|
||
| if ( ! site ) { | ||
| return null; | ||
| } | ||
|
|
||
| const { phpVersions } = getPHPVersions(); | ||
|
|
||
| const fields: Field< { version: string } >[] = [ | ||
| { | ||
| id: 'version', | ||
| label: __( 'PHP version' ), | ||
| Edit: 'select', | ||
| elements: phpVersions.filter( ( option ) => { | ||
| // Show disabled PHP version only if the site is still using it. | ||
| if ( option.disabled && option.value !== currentVersion ) { | ||
| return false; | ||
| } | ||
| return true; | ||
| } ), | ||
| }, | ||
| ]; | ||
|
|
||
| const form = { | ||
| type: 'regular' as const, | ||
| fields: [ 'version' ], | ||
| }; | ||
|
|
||
| const isDirty = formData.version !== currentVersion; | ||
| const { isPending } = mutation; | ||
|
|
||
| const handleSubmit = ( e: React.FormEvent ) => { | ||
| e.preventDefault(); | ||
| mutation.mutate( formData.version, { | ||
| onSuccess: () => { | ||
| createSuccessNotice( __( 'Settings saved.' ), { type: 'snackbar' } ); | ||
| }, | ||
| onError: () => { | ||
| createErrorNotice( __( 'Failed to save settings.' ), { | ||
| type: 'snackbar', | ||
| } ); | ||
| }, | ||
| } ); | ||
| }; | ||
|
|
||
| const renderForm = () => { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is this an inline function? I've always found these weird. Why not render it in
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it's just a matter of taste; I just find it's easier to read / follow instead of a giant ternary :) Maybe @taipeicoder can update it when you implement the upsell 🙈
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's fine, I just don't often see it I guess :)
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Addressing it in #103702 |
||
| return ( | ||
| <Card> | ||
| <CardBody> | ||
| <form onSubmit={ handleSubmit }> | ||
| <VStack spacing={ 4 }> | ||
| <DataForm< { version: string } > | ||
| data={ formData } | ||
| fields={ fields } | ||
| form={ form } | ||
| onChange={ ( edits: { version?: string } ) => { | ||
| setFormData( ( data ) => ( { ...data, ...edits } ) ); | ||
| } } | ||
| /> | ||
| <HStack justify="flex-start"> | ||
| <Button | ||
| variant="primary" | ||
| type="submit" | ||
| isBusy={ isPending } | ||
| disabled={ isPending || ! isDirty } | ||
| > | ||
| { __( 'Save' ) } | ||
| </Button> | ||
| </HStack> | ||
| </VStack> | ||
| </form> | ||
| </CardBody> | ||
| </Card> | ||
| ); | ||
| }; | ||
|
|
||
| const renderCallout = () => { | ||
| return <p>TODO: callout</p>; | ||
| }; | ||
|
|
||
| return ( | ||
| <PageLayout size="small"> | ||
| <SettingsPageHeader title="PHP" /> | ||
| { canUpdate ? renderForm() : renderCallout() } | ||
| </PageLayout> | ||
| ); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| import { useQuery } from '@tanstack/react-query'; | ||
| import { Icon } from '@wordpress/components'; | ||
| import { __ } from '@wordpress/i18n'; | ||
| import { code } from '@wordpress/icons'; | ||
| import { getPHPVersions } from 'calypso/data/php-versions'; | ||
| import { sitePHPVersionQuery } from '../../app/queries'; | ||
| import RouterLinkSummaryButton from '../../components/router-link-summary-button'; | ||
| import { canUpdatePHPVersion } from './utils'; | ||
| import type { Site } from '../../data/types'; | ||
|
|
||
| export default function PHPSettingsSummary( { site }: { site: Site } ) { | ||
| const { data: version } = useQuery( { | ||
| ...sitePHPVersionQuery( site.slug ), | ||
| enabled: canUpdatePHPVersion( site ), | ||
| } ); | ||
|
|
||
| const { recommendedValue } = getPHPVersions(); | ||
|
|
||
| const badge = { | ||
| text: version ?? __( 'Managed' ), | ||
| intent: | ||
| version && version !== recommendedValue ? ( 'warning' as const ) : ( 'success' as const ), | ||
| }; | ||
|
|
||
| return ( | ||
| <RouterLinkSummaryButton | ||
| to={ `/sites/${ site.slug }/settings/php` } | ||
| title="PHP" | ||
| density="medium" | ||
| decoration={ <Icon icon={ code } /> } | ||
| badges={ [ badge ] } | ||
| /> | ||
| ); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| import { Site } from '../../data/types'; | ||
|
|
||
| export function canUpdatePHPVersion( site: Site ) { | ||
| return site.is_wpcom_atomic; | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: no need to return anything
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Addressing it in #103702