Skip to content

Commit

Permalink
Add domain picker to FSE (#42951)
Browse files Browse the repository at this point in the history
* Add domain picker to FSE

* Update to work with domain picker changes in the last 2 weeks.

* Added yarn.lock file.

Co-authored-by: Yan Sern <yan.sern@automattic.com>
  • Loading branch information
alshakero and yansern authored Jun 25, 2020
1 parent 9b0e0ed commit fbd7a89
Show file tree
Hide file tree
Showing 11 changed files with 206 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ function enqueue_script_and_style() {
$asset_file = include plugin_dir_path( __FILE__ ) . 'dist/editor-domain-picker.asset.php';
$script_dependencies = isset( $asset_file['dependencies'] ) ? $asset_file['dependencies'] : array();
$script_version = isset( $asset_file['version'] ) ? $asset_file['version'] : filemtime( plugin_dir_path( __FILE__ ) . 'dist/editor-domain-picker.js' );
$styles_version = isset( $asset_file['version'] ) ? $asset_file['version'] : filemtime( plugin_dir_path( __FILE__ ) . 'dist/editor-domain-picker.css' );

wp_enqueue_script(
'a8c-fse-editor-domain-picker-script',
Expand All @@ -37,5 +38,12 @@ function enqueue_script_and_style() {
$script_version,
true
);

wp_enqueue_style(
'a8c-fse-editor-domain-picker-styles',
plugins_url( 'dist/editor-domain-picker.css', __FILE__ ),
array(),
$styles_version
);
}
add_action( 'enqueue_block_editor_assets', __NAMESPACE__ . '\enqueue_script_and_style' );
Original file line number Diff line number Diff line change
@@ -1,28 +1,21 @@
/**
* External dependencies
*/
import * as React from 'react';
import 'a8c-fse-common-data-stores';
import { select } from '@wordpress/data';

/* eslint-disable no-console */
import DomainPickerButton from './src/domain-picker-button';
import * as ReactDOM from 'react-dom';

console.log( "👋 Hi! I'm the editor-domain-picker bundle running!" );
const awaitSettingsBar = setInterval( () => {
const settingsBar = document.querySelector( '.edit-post-header__settings' );
if ( ! settingsBar ) {
return;
}
clearInterval( awaitSettingsBar );

let results;
let int: number | undefined = setInterval( () => {
results = select( 'automattic/domains/suggestions' ).getCategories();
const domainPickerContainer = document.createElement( 'div' );
settingsBar.prepend( domainPickerContainer );

if ( results.length ) {
// eslint-disable-next-line no-console
console.log( results );
clearInterval( int );
int = undefined;
}
ReactDOM.render( React.createElement( DomainPickerButton ), domainPickerContainer );
} );

setTimeout( () => {
if ( int ) {
console.log( '😢 Timed out before we were able to get Domain Categories.' );
clearInterval( int );
}
}, 5000 );
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/**
* External dependencies
*/
import * as React from 'react';
import { useSelect } from '@wordpress/data';
import 'a8c-fse-common-data-stores';
import { Site } from '@automattic/data-stores';

import DomainPickerModal from '../domain-picker-modal';
import { Button } from '@wordpress/components';
import { Icon, chevronDown } from '@wordpress/icons';
import { useDomainSuggestions, DOMAIN_SUGGESTION_VENDOR } from '../hooks/use-domain-suggestions';
const FLOW_ID = 'gutenboarding';

const SITES_STORE = Site.register( { client_id: '', client_secret: '' } );

declare global {
interface Window {
_currentSiteId: number;
}
}

export default function DomainPickerButton() {
const [ isDomainModalVisible, setDomainModalVisibility ] = React.useState( false );
const [ domainSearch, setDomainSearch ] = React.useState( '' );
const [ domainCategory, setDomainCategory ] = React.useState< string | undefined >( '' );

const domainSuggestions = useDomainSuggestions( domainSearch, domainCategory );

const domainCategories = useSelect( ( select ) =>
select( 'automattic/domains/suggestions' ).getCategories()
);

const site = useSelect( ( select ) => select( SITES_STORE ).getSite( window._currentSiteId ) );

const currentDomain = {
domain_name: ( site?.URL && new URL( site?.URL ).hostname ) || '',
is_free: site?.URL?.endsWith( 'wordpress.com' ),
};

const search = ( domainSearch.trim() || site?.name ) ?? '';

return (
<>
<Button
aria-expanded={ isDomainModalVisible }
aria-haspopup="menu"
aria-pressed={ isDomainModalVisible }
onClick={ () => setDomainModalVisibility( ( s ) => ! s ) }
>
<span className="domain-picker-button__label">{ `Domain: ${ currentDomain.domain_name }` }</span>
<Icon icon={ chevronDown } size={ 22 } />
</Button>
<DomainPickerModal
analyticsFlowId={ FLOW_ID }
domainSuggestions={ domainSuggestions }
initialDomainSearch={ search }
domainCategories={ domainCategories }
domainCategory={ domainCategory }
onSetDomainCategory={ setDomainCategory }
onSetDomainSearch={ setDomainSearch }
isOpen={ isDomainModalVisible }
showDomainConnectButton
showDomainCategories
// eslint-disable-next-line @typescript-eslint/ban-ts-ignore
// @ts-ignore
currentDomain={ currentDomain }
onDomainSelect={ () => setDomainModalVisibility( false ) }
onClose={ () => setDomainModalVisibility( false ) }
// eslint-disable-next-line @typescript-eslint/no-empty-function
recordAnalytics={ () => {} }
railcarId={ 'railcarId' }
domainSuggestionVendor={ DOMAIN_SUGGESTION_VENDOR }
/>
</>
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* External dependencies
*/
import * as React from 'react';
import { Modal } from '@wordpress/components';

/**
* Internal dependencies
*/
import DomainPicker, { Props as DomainPickerProps } from '@automattic/domain-picker';
import './styles.scss';

interface Props extends DomainPickerProps {
isOpen: boolean;
onClose: () => void;
}

const DomainPickerModal: React.FunctionComponent< Props > = ( { isOpen, onClose, ...props } ) => {
if ( ! isOpen ) {
return null;
}

return (
<Modal
className="domain-picker-modal"
overlayClassName="domain-picker-modal-overlay"
bodyOpenClassName="has-domain-picker-modal"
onRequestClose={ onClose }
title=""
>
<DomainPicker showDomainCategories { ...props } />
</Modal>
);
};

export default DomainPickerModal;
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* External dependencies
*/
import { useSelect } from '@wordpress/data';

/**
* Internal dependencies
*/
import { DOMAIN_SUGGESTIONS_STORE } from '../stores';

export const DOMAIN_SUGGESTION_VENDOR = 'variation4_front';
const PAID_DOMAINS_TO_SHOW = 10;

export function useDomainSuggestions(
searchTerm: string,
domainCategory,
locale = 'en',
quantity = PAID_DOMAINS_TO_SHOW
) {
return useSelect(
( select ) => {
if ( ! searchTerm ) {
return;
}
return select( DOMAIN_SUGGESTIONS_STORE ).getDomainSuggestions( searchTerm, {
// Avoid `only_wordpressdotcom` — it seems to fail to find results sometimes
include_wordpressdotcom: true,
include_dotblogsubdomain: false,
quantity,
locale,
vendor: DOMAIN_SUGGESTION_VENDOR,
category_slug: domainCategory,
} );
},
[ searchTerm, domainCategory, quantity ]
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/**
* External dependencies
*/
import 'a8c-fse-common-data-stores';

export const DOMAIN_SUGGESTIONS_STORE = 'automattic/domains/suggestions';
6 changes: 5 additions & 1 deletion apps/full-site-editing/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,10 @@
"dependencies": {
"@automattic/calypso-build": "*",
"@automattic/data-stores": "^1.0.0-alpha.1",
"@automattic/domain-picker": "^1.0.0-alpha.0",
"@automattic/format-currency": "^1.0.0-alpha.0",
"@wordpress/api-fetch": "*",
"@wordpress/base-styles": "1.9.0",
"@wordpress/block-editor": "*",
"@wordpress/blocks": "*",
"@wordpress/components": "*",
Expand All @@ -113,7 +115,9 @@
"classnames": "^2.2.6",
"lodash": "*",
"moment": "*",
"newspack-blocks": "github:Automattic/newspack-blocks#v1.7.0"
"newspack-blocks": "github:Automattic/newspack-blocks#v1.7.0",
"react": "^16.12.0",
"react-dom": "^16.12.0"
},
"devDependencies": {
"@wordpress/eslint-plugin": "^6.0.0"
Expand Down
23 changes: 18 additions & 5 deletions bin/pre-commit-hook.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ const path = require( 'path' );
const phpcsPath = getPathForCommand( 'phpcs' );
const phpcbfPath = getPathForCommand( 'phpcbf' );

function quotedPath( pathToQuote ) {
if ( pathToQuote.includes( ' ' ) ) {
return `"${ pathToQuote }"`;
}
return pathToQuote;
}

console.log(
'\nBy contributing to this project, you license the materials you contribute ' +
'under the GNU General Public License v2 (or later). All materials must have ' +
Expand Down Expand Up @@ -130,7 +137,9 @@ toPHPCBF.forEach( ( file ) => console.log( `PHPCBF formatting staged file: ${ fi
if ( toPHPCBF.length ) {
if ( phpcs ) {
try {
execSync( `${ phpcbfPath } --standard=apps/phpcs.xml ${ toPHPCBF.join( ' ' ) }` );
execSync(
`${ quotedPath( phpcbfPath ) } --standard=apps/phpcs.xml ${ toPHPCBF.join( ' ' ) }`
);
} catch ( error ) {
// PHPCBF returns a `0` or `1` exit code on success, and `2` on failures. ¯\_(ツ)_/¯
// https://github.com/squizlabs/PHP_CodeSniffer/blob/master/src/Runner.php#L210
Expand Down Expand Up @@ -186,10 +195,14 @@ if ( toEslint.length ) {
// and finally PHPCS
if ( toPHPCS.length ) {
if ( phpcs ) {
const lintResult = spawnSync( phpcsPath, [ '--standard=apps/phpcs.xml', ...toPHPCS ], {
shell: true,
stdio: 'inherit',
} );
const lintResult = spawnSync(
quotedPath( phpcsPath ),
[ '--standard=apps/phpcs.xml', ...toPHPCS ],
{
shell: true,
stdio: 'inherit',
}
);

if ( lintResult.status ) {
linterFailure();
Expand Down
4 changes: 2 additions & 2 deletions packages/domain-picker/src/domain-picker/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export interface Props {
domainSuggestionVendor: string;

/** The initial domain search query */
initialDomainSearch: string;
initialDomainSearch?: string;

/** Called when the domain search query is changed */
onSetDomainSearch: ( value: string ) => void;
Expand All @@ -68,7 +68,7 @@ const DomainPicker: FunctionComponent< Props > = ( {
analyticsFlowId,
analyticsUiAlgo,
domainSuggestionVendor,
initialDomainSearch,
initialDomainSearch = '',
onSetDomainSearch,
} ) => {
const { __ } = useI18n();
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4386,6 +4386,11 @@
"@wordpress/warning" "^1.1.0"
core-js "^3.6.4"

"@wordpress/base-styles@1.9.0":
version "1.9.0"
resolved "https://registry.yarnpkg.com/@wordpress/base-styles/-/base-styles-1.9.0.tgz#d6f83169749cf28555db601c2415e90f758bcf85"
integrity sha512-AuqLpbGFV0Jt+uYHEHjvv6ewBzjJOo0MytAMy0J3CK9KOc/XiZsLHbbGG1mS2cuQiWQATfjP5sjHveQzQ2zBNQ==

"@wordpress/base-styles@^1.8.0":
version "1.8.0"
resolved "https://registry.yarnpkg.com/@wordpress/base-styles/-/base-styles-1.8.0.tgz#4735e7c8039c5290225441e472554352cb050d1b"
Expand Down

0 comments on commit fbd7a89

Please sign in to comment.