ScoreReanalyzing.js
3 years ago
notOptimized.js
3 years ago
optimized.js
3 years ago
optimizing.js
3 years ago
score.js
3 years ago
set_scoreData.js
3 years ago
ScoreReanalyzing.js
117 lines
| 1 | import {useState} from "react"; |
| 2 | const { __ } = wp.i18n; |
| 3 | |
| 4 | export const ScoreReanalyzing = ({post_id, score_data}) => { |
| 5 | let reanalyze_button_status = false, reanalyze_button_status_current = false, reanalyze_button_status_previous = false; |
| 6 | if( score_data ) { |
| 7 | if (score_data['current_score'] && score_data['current_score']['status'] |
| 8 | && score_data['current_score']['status'] == 'inprogress' ) { |
| 9 | reanalyze_button_status = true; |
| 10 | reanalyze_button_status_current = true; |
| 11 | } |
| 12 | if (score_data['previous_score'] && score_data['previous_score']['status'] |
| 13 | && score_data['previous_score']['status'] == 'inprogress' ) { |
| 14 | reanalyze_button_status = true; |
| 15 | reanalyze_button_status_previous = true; |
| 16 | } |
| 17 | } |
| 18 | |
| 19 | let no_old_scores = '', no_new_scores = '', reanalyze_score_for = 'new'; |
| 20 | let score_check_link_before = '', score_check_link_after ='', score_check_button =''; |
| 21 | if ( !score_data || !score_data['previous_score'] || reanalyze_button_status_previous ) { |
| 22 | no_old_scores = 'two-no-scores'; |
| 23 | reanalyze_score_for = 'old'; |
| 24 | } |
| 25 | if ( !score_data || !score_data['current_score'] || reanalyze_button_status_current ) { |
| 26 | no_new_scores = 'two-no-scores'; |
| 27 | } |
| 28 | if ( no_old_scores != '' && no_new_scores != '' ) { |
| 29 | reanalyze_score_for = 'both'; |
| 30 | } |
| 31 | |
| 32 | |
| 33 | const [isReanalyzing, setIsReanalyzing] = useState(reanalyze_button_status); |
| 34 | const [isReanalyzeButton, setIsReanalyzeButton] = useState(false); |
| 35 | const [new_no_new_scores, setno_new_scores] = useState(no_new_scores); |
| 36 | |
| 37 | const reanalyze = (post_id,ReanalyzeButton) => { |
| 38 | if ( !isReanalyzing ) { |
| 39 | setIsReanalyzing(true); |
| 40 | if (ReanalyzeButton == 'button') { |
| 41 | setIsReanalyzeButton(true); |
| 42 | setno_new_scores('two-no-scores'); |
| 43 | } |
| 44 | |
| 45 | const url = two_speed.ajax_url; |
| 46 | var params = new FormData(); |
| 47 | |
| 48 | params.append('action', 'two_recount_score'); |
| 49 | params.append('post_id', post_id); |
| 50 | params.append('nonce', two_speed.nonce); |
| 51 | params.append('initiator', 'gutenberg'); |
| 52 | params.append('reanalyze_score_for', reanalyze_score_for); |
| 53 | |
| 54 | fetch(url, { |
| 55 | method: "POST", |
| 56 | body: params, |
| 57 | } |
| 58 | ) |
| 59 | .then(response => response.json()) |
| 60 | .then(data => { |
| 61 | console.log(data) |
| 62 | }) |
| 63 | .catch((err) => console.log(err)); |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | if ( isReanalyzing ) { |
| 68 | score_check_button = <div className="two-gutenberg-reanalyze_container two_reanalyze_loading"> |
| 69 | <a href="#" className="two_reanalyze_button">{__("Reanalyzing...", "tenweb-speed-optimizer")}</a> |
| 70 | <p className="two-description">{__("Reload in 2 minutes to see the new score","tenweb-speed-optimizer")}</p> |
| 71 | </div>; |
| 72 | if ( isReanalyzeButton ) { |
| 73 | score_check_link_after = <span className="two-page-speed two-optimizing"></span>; |
| 74 | if ( reanalyze_score_for == 'both') { |
| 75 | score_check_link_before = <span className="two-page-speed two-optimizing"></span>; |
| 76 | } |
| 77 | } |
| 78 | else { |
| 79 | if ( reanalyze_score_for == 'both') { |
| 80 | score_check_link_before = <span className="two-page-speed two-optimizing"></span>; |
| 81 | score_check_link_after = <span className="two-page-speed two-optimizing"></span>; |
| 82 | } else if (reanalyze_score_for == 'old'){ |
| 83 | score_check_link_before = <span className="two-page-speed two-optimizing"></span>; |
| 84 | } else if (reanalyze_score_for == 'new') { |
| 85 | score_check_link_after = <span className="two-page-speed two-optimizing"></span>; |
| 86 | } |
| 87 | } |
| 88 | } else { |
| 89 | score_check_button = <div className="two-gutenberg-reanalyze_container"> |
| 90 | <a onClick={() => reanalyze(post_id,'button')} |
| 91 | className="two_reanalyze_button">{__("Reanalyze", "tenweb-speed-optimizer")} |
| 92 | </a> |
| 93 | </div>; |
| 94 | let link = <a onClick={() => reanalyze(post_id,'')} data-post_id={post_id} |
| 95 | target="_blank" className="two_reanalyze_link"></a>; |
| 96 | if ( no_old_scores != '' && no_new_scores != '') { |
| 97 | score_check_link_before = link; |
| 98 | score_check_link_after = link; |
| 99 | } else if (no_old_scores != ''){ |
| 100 | score_check_link_before = link; |
| 101 | score_check_link_after = ''; |
| 102 | |
| 103 | } else if (no_new_scores != '') { |
| 104 | score_check_link_before = ''; |
| 105 | score_check_link_after = link; |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | return { |
| 110 | 'score_check_button': score_check_button, |
| 111 | 'score_check_link_before': score_check_link_before, |
| 112 | 'score_check_link_after': score_check_link_after, |
| 113 | 'no_new_scores': new_no_new_scores, |
| 114 | 'no_old_scores': no_old_scores, |
| 115 | 'reanalyzingStatus': isReanalyzing, |
| 116 | } |
| 117 | } |