Skip to content

Commit 5813e7e

Browse files
committed
Improve filter detection and background sync
1 parent 93e8ba3 commit 5813e7e

File tree

4 files changed

+58
-17
lines changed

4 files changed

+58
-17
lines changed

assets/js/wpcable.js

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,10 +140,15 @@ jQuery(document).ready(function () {
140140
if ( elProgress.is(':visible') ) {
141141
elProgress.hide();
142142

143-
// Reload the window, if user consents.
144-
if ( confirm('Sync completed. Do you want to reload this page now?') ) {
143+
if ( jQuery( '.wrap.wpcable_wrap.tasks' ).length ) {
144+
// On the tasks-page silently refresh the task list without reload.
145+
jQuery( document ).trigger( 'codeable-reload-tasks' );
146+
} else if ( jQuery( '.wrap.cable_stats_wrap' ).length ) {
147+
// On stats-page we can reload the whole window without a problem.
145148
window.location.reload();
146149
}
150+
151+
// On any other page, the refresh has no effect and no action is needed.
147152
}
148153
}
149154

@@ -208,6 +213,26 @@ jQuery(document).ready(function () {
208213
var currFilters = {};
209214
var currFlags = {};
210215

216+
function reloadData() {
217+
jQuery.post(
218+
window.ajaxurl,
219+
{
220+
action: 'wpcable_reload_tasks'
221+
},
222+
function ( res ) {
223+
if (! res || ! Array.isArray( res ) ) {
224+
return;
225+
}
226+
window.wpcable.tasks = res;
227+
228+
initFilters();
229+
updateFilters();
230+
refreshList();
231+
},
232+
'json'
233+
);
234+
}
235+
211236
function refreshList() {
212237
list.empty();
213238

@@ -511,6 +536,8 @@ jQuery(document).ready(function () {
511536
});
512537

513538
jQuery( window ).on( 'hashchange', updateFilters );
539+
jQuery( document ).on( 'codeable-reload-tasks', reloadData );
540+
514541
filterCb.on( 'click', function() { updateFilters(); initFilters(); } );
515542
flagCb.on( 'click', function() { updateFilters(); initFilters(); } );
516543

classes/api_data.php

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -406,9 +406,15 @@ private function store_tasks( $filter, $page ) {
406406
$new_task['last_activity_by'] = $task['last_event']['user']['full_name'];
407407
}
408408

409-
if ( ! empty( $new_task['last_activity'] ) ) {
410-
// Auto-change flags only for tasks that I have access to, i.e.
411-
// when another expert was paid, the task should be "lost" for us.
409+
// Some simple rules to automatically detect the correct flag for tasks.
410+
if ( 'canceled' === $task['state'] ) {
411+
// Tasks that were canceled by the client obviously are lost.
412+
$new_task['flag'] = 'lost';
413+
} elseif ( $new_task['hidden'] ) {
414+
// Tasks that I hide from my Codeable list are "lost for us".
415+
$new_task['flag'] = 'lost';
416+
} elseif ( ! empty( $new_task['last_activity'] ) ) {
417+
// This means that the workroom is public or private for me.
412418
if ( 'completed' === $task['state'] ) {
413419
$new_task['flag'] = 'completed';
414420
}
@@ -418,16 +424,19 @@ private function store_tasks( $filter, $page ) {
418424
if ( 'hired' === $task['state'] ) {
419425
$new_task['flag'] = 'estimated';
420426
}
421-
} elseif ( 'canceled' === $task['state'] ) {
422-
$new_task['flag'] = 'lost';
423-
} else {
424-
$new_task['flag'] = 'lost';
427+
} elseif ( empty( $new_task['last_activity'] ) ) {
428+
// This workroom is private for another expert = possibly lost.
429+
if ( in_array( $task['state'], [ 'hired', 'completed', 'refunded' ], true ) ) {
430+
$new_task['flag'] = 'lost';
431+
}
425432
}
426433

427434
// Flag open tasks as "canceled" after a given number of stale days.
428435
if ( in_array( $task['state'], [ 'published', 'estimated', 'hired' ], true ) ) {
429436
if ( ! empty( $new_task['last_activity'] ) ) {
430-
$stale_hours = ( time() - $new_task['last_activity'] ) / HOUR_IN_SECONDS;
437+
$stale_hours = floor(
438+
( time() - $new_task['last_activity'] ) / HOUR_IN_SECONDS
439+
);
431440

432441
if ( $stale_hours > $cancel_after_hours ) {
433442
$new_task['flag'] = 'lost';

functions/admin-tasks.php

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ function wpcable_tasks_menu() {
2626
}
2727
add_action( 'admin_menu', 'wpcable_tasks_menu', 50 );
2828

29+
/**
30+
* Ajax hndler that updates a single task in the DB.
31+
*/
2932
function wpcable_ajax_update_task() {
3033
if ( empty( $_POST['_wpnonce'] ) ) {
3134
return;
@@ -50,14 +53,16 @@ function wpcable_ajax_update_task() {
5053
add_action( 'wp_ajax_wpcable_update_task', 'wpcable_ajax_update_task' );
5154

5255
/**
53-
* Called when the settings page is loaded - process actions such as logout.
54-
*
55-
* @return void
56+
* Ajax handler that returns a full task list in JSON format.
5657
*/
57-
function codeable_load_tasks_page() {
58-
$nonce = false;
58+
function wpcable_ajax_reload_tasks() {
59+
$wpcable_tasks = new wpcable_tasks();
60+
61+
$task_list = $wpcable_tasks->get_tasks();
62+
echo wp_json_encode( $task_list );
63+
exit;
5964
}
60-
add_action( 'load-codeable-stats_page_codeable_tasks', 'codeable_load_tasks_page' );
65+
add_action( 'wp_ajax_wpcable_reload_tasks', 'wpcable_ajax_reload_tasks' );
6166

6267
/**
6368
* Render the settings page.

functions/helpers.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ function codeable_last_fetch_info() {
318318
<?php _e( 'Last refresh: ', 'wpcable' ); ?>
319319
<?php echo wpcable_date( $last_fetch ); ?>
320320
<span class="tooltip" tabindex="0">
321-
<span class="tooltip-text"><?php _e( 'API details are fetched once per hour, or when you click the "Refresh" button on the right.', 'wpcable' ); ?></span>
321+
<span class="tooltip-text"><?php _e( 'The plugin loads data from Codeable once per hour, or when you click the "Refresh" button on the right.', 'wpcable' ); ?></span>
322322
<i class="dashicons dashicons-info"></i>
323323
</span>
324324
|

0 commit comments

Comments
 (0)