Skip to content

Commit

Permalink
ante: improve RevisionDateGetButton error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
ChlodAlejandro committed Sep 9, 2023
1 parent edbb37d commit e14f49b
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 12 deletions.
1 change: 1 addition & 0 deletions i18n/ante/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@

"deputy.ante.dateAuto": "Pull the date from the provided revision ID (`$1` parameter)",
"deputy.ante.dateAuto.failed": "Could not pull date from revision: $1",
"deputy.ante.dateAuto.missing": "The revision $1 could not be found. Its page may have been deleted.",

"deputy.ante.copied.label": "Copied $1",

Expand Down
35 changes: 23 additions & 12 deletions src/modules/ante/ui/components/RevisionDateGetButton.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import MwApi from '../../../../MwApi';
import getApiErrorText from '../../../../wiki/util/getApiErrorText';

export interface RevisionDateGetButtonData extends OO.ui.ButtonWidget.ConfigOptions {
revisionInputWidget: OO.ui.InputWidget;
Expand Down Expand Up @@ -29,16 +30,16 @@ function initRevisionDateGetButton() {
this.revisionInputWidget = config.revisionInputWidget;
this.dateInputWidget = config.dateInputWidget;

this.revisionInputWidget.on( 'change', this.updateDateAutoButton.bind( this ) );
this.dateInputWidget.on( 'change', this.updateDateAutoButton.bind( this ) );
this.revisionInputWidget.on( 'change', this.updateButton.bind( this ) );
this.dateInputWidget.on( 'change', this.updateButton.bind( this ) );
this.on( 'click', this.setDateFromRevision.bind( this ) );
this.updateDateAutoButton();
this.updateButton();
}

/**
* Update the disabled state of the button.
*/
updateDateAutoButton(): void {
updateButton(): void {
this.setDisabled(
isNaN( +this.revisionInputWidget.getValue() ) ||
!!this.dateInputWidget.getValue()
Expand All @@ -54,27 +55,37 @@ function initRevisionDateGetButton() {
.setIcon( 'ellipsis' )
.setDisabled( true );
this.dateInputWidget.setDisabled( true );
const revid = this.revisionInputWidget.getValue();
await MwApi.action.get( {
action: 'query',
prop: 'revisions',
revids: this.revisionInputWidget.getValue(),
revids: revid,
rvprop: 'timestamp'
} ).then( ( data ) => {
if ( ( data.query as any ).badrevids != null ) {
mw.notify(
mw.msg( 'deputy.ante.dateAuto.missing', revid ),
{ type: 'error' }
);
this.updateButton();
return;
}

this.dateInputWidget.setValue(
// ISO-format date
data.query.pages[ 0 ].revisions[ 0 ].timestamp.split( 'T' )[ 0 ]
);
this.dateInputWidget.setDisabled( false );
this.setIcon( 'download' );
this.updateDateAutoButton();
}, ( error, errorData ) => {
this.updateButton();
}, ( _error, errorData ) => {
mw.notify(
mw.msg( 'deputy.ante.dateAuto.failed', errorData.info ),
{
type: 'error'
}
mw.msg( 'deputy.ante.dateAuto.failed', getApiErrorText( errorData ) ),
{ type: 'error' }
);
this.updateDateAutoButton();
this.dateInputWidget.setDisabled( false );
this.setIcon( 'download' );
this.updateButton();
} );
}

Expand Down
19 changes: 19 additions & 0 deletions src/wiki/util/getApiErrorText.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { h } from 'tsx-dom';

/**
* Get the API error text from an API response.
*
* @param errorData
* @param n Get the `n`th error. Defaults to 0 (first error).
*/
export default function getApiErrorText( errorData: any, n: number = 0 ): string | JQuery {
// errorformat=html
return errorData.errors?.[ n ]?.html ?
<span dangerouslySetInnerHTML={errorData.errors?.[ n ]?.html}/> :
(
// errorformat=plaintext/wikitext
errorData.errors?.[ n ]?.text ??
// errorformat=bc
errorData.info
);
}

0 comments on commit e14f49b

Please sign in to comment.