-
Notifications
You must be signed in to change notification settings - Fork 909
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
Create frontend providers and widget factory #22008
Open
igorschoester
wants to merge
9
commits into
trunk
Choose a base branch
from
420-create-frontend-data-provider-and-wp-implementation
base: trunk
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
029a283
Fix route naming conflict
igorschoester 509e6bf
Create services: data provider and widget factory
igorschoester f9b77f8
Add top pages widget
igorschoester 1f7380e
Add remote data provider
igorschoester 64a0499
Refactor Scores to use the data providers
igorschoester 2cf80f3
Refactor Score support link
igorschoester 272906b
Add site kit configuration
igorschoester 936756d
Add widget factory tests
igorschoester aa075c5
Add top pages widget test
igorschoester File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 0 additions & 46 deletions
46
packages/js/src/dashboard/components/most-popular-table.js
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
import { createInterpolateElement, useEffect, useState } from "@wordpress/element"; | ||
import { createInterpolateElement, useCallback, useEffect, useState } from "@wordpress/element"; | ||
import { __, sprintf } from "@wordpress/i18n"; | ||
import { Alert, Link, Paper, Title } from "@yoast/ui-library"; | ||
import { useFetch } from "../../fetch/use-fetch"; | ||
import { Alert, Link } from "@yoast/ui-library"; | ||
import { useRemoteData } from "../../services/use-remote-data"; | ||
import { SCORE_DESCRIPTIONS } from "../score-meta"; | ||
import { ContentTypeFilter } from "./content-type-filter"; | ||
import { ScoreContent } from "./score-content"; | ||
|
@@ -15,105 +15,99 @@ import { TermFilter } from "./term-filter"; | |
*/ | ||
|
||
/** | ||
* @param {string|URL} endpoint The endpoint. | ||
* @param {ContentType} contentType The content type. | ||
* @param {Term?} [term] The term. | ||
* @returns {URL} The URL to get scores. | ||
* @param {string} message The message with placeholders. | ||
* @param {JSX.Element} link The link. | ||
* @returns {JSX.Element|string} The message. | ||
*/ | ||
const createScoresUrl = ( endpoint, contentType, term ) => { | ||
const url = new URL( endpoint ); | ||
|
||
url.searchParams.set( "contentType", contentType.name ); | ||
|
||
if ( contentType.taxonomy?.name && term?.name ) { | ||
url.searchParams.set( "taxonomy", contentType.taxonomy.name ); | ||
url.searchParams.set( "term", term.name ); | ||
const createLinkMessage = ( message, link ) => { | ||
try { | ||
return createInterpolateElement( sprintf( message, "<link>", "</link>" ), { link } ); | ||
} catch ( e ) { | ||
return sprintf( message, "", "" ); | ||
} | ||
|
||
return url; | ||
}; | ||
|
||
// Added dummy space as content to prevent children prop warnings in the console. | ||
const supportLink = <Link variant="error" href="admin.php?page=wpseo_page_support"> </Link>; | ||
|
||
const TimeoutErrorMessage = createInterpolateElement( | ||
sprintf( | ||
/* translators: %1$s and %2$s expand to an opening/closing tag for a link to the support page. */ | ||
__( "A timeout occurred, possibly due to a large number of posts or terms. In case you need further help, please take a look at our %1$sSupport page%2$s.", "wordpress-seo" ), | ||
"<supportLink>", | ||
"</supportLink>" | ||
), | ||
{ | ||
supportLink, | ||
} | ||
); | ||
const OtherErrorMessage = createInterpolateElement( | ||
sprintf( | ||
/* translators: %1$s and %2$s expand to an opening/closing tag for a link to the support page. */ | ||
__( "Something went wrong. In case you need further help, please take a look at our %1$sSupport page%2$s.", "wordpress-seo" ), | ||
"<supportLink>", | ||
"</supportLink>" | ||
), | ||
{ | ||
supportLink, | ||
} | ||
); | ||
|
||
/** | ||
* @param {Error?} [error] The error. | ||
* @param {string} supportLink The support link. | ||
* @returns {JSX.Element} The element. | ||
*/ | ||
const ErrorAlert = ( { error } ) => { | ||
const ErrorAlert = ( { error, supportLink } ) => { | ||
if ( ! error ) { | ||
return null; | ||
} | ||
|
||
// Added dummy space as content to prevent children prop warnings in the console. | ||
const link = <Link variant="error" href={ supportLink }> </Link>; | ||
|
||
return ( | ||
<Alert variant="error"> | ||
{ error?.name === "TimeoutError" | ||
? TimeoutErrorMessage | ||
: OtherErrorMessage | ||
? createLinkMessage( | ||
/* translators: %1$s and %2$s expand to an opening/closing tag for a link to the support page. */ | ||
__( "A timeout occurred, possibly due to a large number of posts or terms. In case you need further help, please take a look at our %1$sSupport page%2$s.", "wordpress-seo" ), | ||
link | ||
) | ||
: createLinkMessage( | ||
/* translators: %1$s and %2$s expand to an opening/closing tag for a link to the support page. */ | ||
__( "Something went wrong. In case you need further help, please take a look at our %1$sSupport page%2$s.", "wordpress-seo" ), | ||
link | ||
) | ||
} | ||
</Alert> | ||
); | ||
}; | ||
|
||
/** | ||
* @param {ContentType?} [contentType] The selected content type. | ||
* @param {Term?} [term] The selected term. | ||
* @returns {{contentType: string, taxonomy?: string, term?: string}} The score query parameters. | ||
*/ | ||
const getScoreQueryParams = ( contentType, term ) => { // eslint-disable-line complexity | ||
const params = { | ||
contentType: contentType?.name, | ||
}; | ||
if ( contentType?.taxonomy?.name && term?.name ) { | ||
params.taxonomy = contentType.taxonomy.name; | ||
params.term = term.name; | ||
} | ||
|
||
return params; | ||
}; | ||
|
||
/** | ||
* @param {?{scores: Score[]}} [data] The data. | ||
* @returns {?Score[]} scores The scores. | ||
*/ | ||
const prepareScoreData = ( data ) => data?.scores; | ||
Comment on lines
+61
to
+82
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. These might be moved to the dataProvider or props at a later stage, to actually make it reusable |
||
|
||
/** | ||
* @param {AnalysisType} analysisType The analysis type. Either "seo" or "readability". | ||
* @param {ContentType[]} contentTypes The content types. May not be empty. | ||
* @param {string} endpoint The endpoint or base URL. | ||
* @param {Object<string,string>} headers The headers to send with the request. | ||
* @param {import("../services/data-provider")} dataProvider The data provider. | ||
* @param {import("../services/remote-data-provider")} remoteDataProvider The remote data provider. | ||
* @returns {JSX.Element} The element. | ||
*/ | ||
export const Scores = ( { analysisType, contentTypes, endpoint, headers } ) => { | ||
export const Scores = ( { analysisType, contentTypes, dataProvider, remoteDataProvider } ) => { // eslint-disable-line complexity | ||
const [ selectedContentType, setSelectedContentType ] = useState( contentTypes[ 0 ] ); | ||
/** @type {[Term?, function(Term?)]} */ | ||
const [ selectedTerm, setSelectedTerm ] = useState(); | ||
|
||
const { data: scores, error, isPending } = useFetch( { | ||
dependencies: [ selectedContentType.name, selectedContentType?.taxonomy, selectedTerm?.name ], | ||
url: createScoresUrl( endpoint, selectedContentType, selectedTerm ), | ||
options: { | ||
headers: { | ||
"Content-Type": "application/json", | ||
...headers, | ||
}, | ||
}, | ||
fetchDelay: 0, | ||
prepareData: ( data ) => data?.scores, | ||
} ); | ||
const getScores = useCallback( ( options ) => remoteDataProvider.fetchJson( | ||
dataProvider.getEndpoint( analysisType + "Scores" ), | ||
getScoreQueryParams( selectedContentType, selectedTerm ), | ||
options | ||
), [ dataProvider, analysisType, selectedContentType, selectedTerm ] ); | ||
|
||
const { data: scores, error, isPending } = useRemoteData( getScores, prepareScoreData ); | ||
|
||
useEffect( () => { | ||
// Reset the selected term when the selected content type changes. | ||
setSelectedTerm( undefined ); // eslint-disable-line no-undefined | ||
}, [ selectedContentType.name ] ); | ||
}, [ selectedContentType?.name ] ); | ||
|
||
return ( | ||
<Paper className="yst-@container yst-grow yst-max-w-screen-sm yst-p-8 yst-shadow-md"> | ||
<Title as="h2"> | ||
{ analysisType === "readability" | ||
? __( "Readability scores", "wordpress-seo" ) | ||
: __( "SEO scores", "wordpress-seo" ) | ||
} | ||
</Title> | ||
<> | ||
<div className="yst-grid yst-grid-cols-1 @md:yst-grid-cols-2 yst-gap-6 yst-mt-4"> | ||
<ContentTypeFilter | ||
idSuffix={ analysisType } | ||
|
@@ -131,7 +125,7 @@ export const Scores = ( { analysisType, contentTypes, endpoint, headers } ) => { | |
} | ||
</div> | ||
<div className="yst-mt-6"> | ||
<ErrorAlert error={ error } /> | ||
<ErrorAlert error={ error } supportLink={ dataProvider.getLink( "errorSupport" ) } /> | ||
{ ! error && ( | ||
<ScoreContent | ||
scores={ scores } | ||
|
@@ -141,6 +135,6 @@ export const Scores = ( { analysisType, contentTypes, endpoint, headers } ) => { | |
/> | ||
) } | ||
</div> | ||
</Paper> | ||
</> | ||
); | ||
}; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Should this be a widget too?