Skip to content

Commit

Permalink
Fix cancel buttons
Browse files Browse the repository at this point in the history
  • Loading branch information
koen12344 committed Feb 10, 2024
1 parent dfb1280 commit 6fdefb6
Show file tree
Hide file tree
Showing 10 changed files with 72 additions and 6 deletions.
Binary file modified README.md
Binary file not shown.
Binary file modified assets/screenshot-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified assets/screenshot-2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified assets/screenshot-3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
23 changes: 20 additions & 3 deletions src/js/Components/Wizard.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,16 @@ export default function Wizard(){
.finally(() => setDispatching(false));
}

const cancelImport = function(){
setCancelling(true);
apiFetch({path: '/sifg/v1/import/cancel'}).then((data) => {}).catch((error) => {
createErrorNotice( __('Could not cancel import', 'site-import-for-gbp'), {
type: 'snackbar',
} );
})
.finally();
}

useEffect(() => {
const checkImportStatus = () => {
apiFetch({path: '/sifg/v1/import/status'}).then((data) => {
Expand Down Expand Up @@ -182,7 +192,7 @@ export default function Wizard(){
{/* checked={selectedOptions.reviews}*/}
{/*/>*/}
<div className='form-buttons'>
<Button onClick={()=>setWizardStarted(false)} isSecondary variant='secondary'>{__('Cancel', 'site-import-for-gbp')}</Button>
<Button onClick={()=>setWizardStep('connect')} isSecondary variant='secondary'>{__('Cancel', 'site-import-for-gbp')}</Button>
<Button onClick={dispatchImport} disabled={ isDispatching} isPrimary variant='primary'> {
isDispatching ? (
<>
Expand All @@ -201,8 +211,15 @@ export default function Wizard(){
<Fragment>
<p><Spinner/>{__('Import is currently in progress, and running in the background. You may stay here and wait for it to complete or leave this page. Up to you!', 'site-import-for-gbp')}</p>
<div className='form-buttons'>
<Button onClick='' isSecondary
variant='secondary'>{__('Cancel import', 'site-import-for-gbp')}</Button>
<Button onClick={ cancelImport } isSecondary variant='secondary' disabled={cancelling}>{
cancelling ? (
<>
<Spinner />
{__('Cancelling...','site-import-for-gbp')}
</>
) :
__('Cancel import', 'site-import-for-gbp')
}</Button>
</div>
</Fragment>
);
Expand Down
8 changes: 7 additions & 1 deletion src/php/BackgroundProcessing/BackgroundProcess.php
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,13 @@ private function import_posts($item){
'_sifg_name' => $post->name,
'_sifg_gbp_post_data' => $post,
]
]);
], true, false);

if(is_wp_error($post_id)){
//translators: %1$s is Google Location identifier, %2$s is the error message
$this->logger->add(sprintf(__('Failed to import post into %1$s WordPress: %2$s', 'site-import-for-gbp'), $post->name, $post_id->get_error_message()));
continue;
}



Expand Down
2 changes: 2 additions & 0 deletions src/php/Configuration/RestApiConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Koen12344\SiteImportForGbp\DependencyInjection\Container;
use Koen12344\SiteImportForGbp\DependencyInjection\ContainerConfigurationInterface;
use Koen12344\SiteImportForGbp\RestAPI\CancelImportEndpoint;
use Koen12344\SiteImportForGbp\RestAPI\ConfirmImportEndpoint;
use Koen12344\SiteImportForGbp\RestAPI\DisconnectGoogleEndpoint;
use Koen12344\SiteImportForGbp\RestAPI\DispatchImportEndpoint;
Expand All @@ -24,6 +25,7 @@ public function modify( Container $container ) {
'import_status_endpoint' => new ImportStatusEndpoint($container['service.import_process'], $container['service.import_logger']),
'import_log_endpoint' => new GetImportLogEndpoint($container['service.import_logger']),
'confirm_import_endpoint' => new ConfirmImportEndpoint($container['service.import_logger']),
'cancel_import_endpoin' => new CancelImportEndpoint($container['service.import_process']),
];
});
}
Expand Down
2 changes: 1 addition & 1 deletion src/php/Logger/ImportLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class ImportLogger {
public function add($message){
$log = $this->read();
$timestamp = "[".current_time('mysql')."] ";
$message = $timestamp.esc_html($message)."\n";
$message = $timestamp.$message."\n";
$log = $message.$log;

$this->save($log);
Expand Down
41 changes: 41 additions & 0 deletions src/php/RestAPI/CancelImportEndpoint.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace Koen12344\SiteImportForGbp\RestAPI;

use Koen12344\SiteImportForGbp\BackgroundProcessing\BackgroundProcess;

use WP_REST_Request;

class CancelImportEndpoint implements EndpointInterface {
/**
* @var BackgroundProcess
*/
private $process;

public function __construct(BackgroundProcess $process){

$this->process = $process;
}
public function get_arguments(): array {
return [];
}

public function respond( WP_REST_Request $request ) {
if($this->process->is_processing()){
$this->process->cancel();
}
return new \WP_REST_Response(true);
}

public function validate( WP_REST_Request $request ): bool {
return current_user_can('manage_options');
}

public function get_methods(): array {
return [\WP_REST_Server::READABLE];
}

public function get_path(): string {
return '/import/confirm/';
}
}
2 changes: 1 addition & 1 deletion src/php/RestAPI/GetImportLogEndpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function get_arguments(): array {
}

public function respond( WP_REST_Request $request ) {
return new \WP_REST_Response(['log' => $this->logger->read()]);
return new \WP_REST_Response(['log' => esc_html($this->logger->read())]);
}

public function validate( WP_REST_Request $request ): bool {
Expand Down

0 comments on commit 6fdefb6

Please sign in to comment.