Skip to content

Commit

Permalink
Do not reload page when adding new redirects
Browse files Browse the repository at this point in the history
  • Loading branch information
rilwis committed Dec 22, 2023
1 parent 54b5b8c commit 1455a10
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 14 deletions.
22 changes: 15 additions & 7 deletions js/redirection/redirects/Update.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import { Button, Modal } from '@wordpress/components';
import { useEffect, useReducer, useState } from '@wordpress/element';
import { __ } from '@wordpress/i18n';
import { Tooltip, fetcher } from '../helper/misc';
import { Tooltip, fetcher, useApi } from '../helper/misc';

const Update = ( { redirectToEdit = {}, children, linkClassName, callback } ) => {
const [ redirect, setRedirect ] = useState( {} );
const [ isProcessing, setIsProcessing ] = useState( false );
const [ warningMessage, setWarningMessage ] = useState( '' );
const [ showAdvancedOptions, toggleAdvancedOptions ] = useReducer( onOrOff => !onOrOff, false );
const [ showModal, setShowModal ] = useState( false );
const { result: redirects, mutate } = useApi( 'redirects', {}, { returnMutate: true } );

const title = redirect.id ? __( 'Update Redirect', 'slim-seo' ) : __( 'Add Redirect', 'slim-seo' );

const openModal = e => {
Expand All @@ -20,15 +22,21 @@ const Update = ( { redirectToEdit = {}, children, linkClassName, callback } ) =>
const updateRedirect = () => {
setWarningMessage( '' );

fetcher( 'update_redirect', { redirect }, 'POST' ).then( result => {
if ( ! redirect.id ) {
window.location.reload();
fetcher( 'update_redirect', { redirect }, 'POST' ).then( id => {
setShowModal( false );
setIsProcessing( false );

// Update a redirect.
if ( redirect.id ) {
callback( redirect );
return;
}

setShowModal( false );
setIsProcessing( false );
callback( redirect );
// Add new redirect.
redirect.id = id;
let newRedirects = [ ...redirects ];
newRedirects.push( redirect );
mutate( newRedirects, { revalidate: false } );
} );
};

Expand Down
6 changes: 2 additions & 4 deletions src/Redirection/Api/Redirects.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,10 @@ public function exists( WP_REST_Request $request ): bool {
return $this->db_redirects->exists( $from );
}

public function update_redirect( WP_REST_Request $request ): bool {
public function update_redirect( WP_REST_Request $request ): string {
$redirect = $request->get_param( 'redirect' );

$this->db_redirects->update( $redirect );

return true;
return $this->db_redirects->update( $redirect );
}

public function delete_redirects( WP_REST_Request $request ): bool {
Expand Down
8 changes: 5 additions & 3 deletions src/Redirection/Database/Redirects.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,18 @@ public function exists( string $url ): bool {
} ) ) > 0;
}

public function update( array $redirect ) {
public function update( array $redirect ): string {
$redirect['from'] = Helper::normalize_url( $redirect['from'], false );
$redirect['to'] = Helper::normalize_url( $redirect['to'], true, true, false );
$redirect['note'] = sanitize_text_field( $redirect['note'] );
$redirect_id = empty( $redirect['id'] ) ? uniqid( wp_rand() ) : $redirect['id'];
$id = (string) ( empty( $redirect['id'] ) ? uniqid() : $redirect['id'] );

unset( $redirect['id'] );
$this->redirects[ $redirect_id ] = $redirect;
$this->redirects[ $id ] = $redirect;

update_option( SLIM_SEO_REDIRECTS, $this->redirects );

return $id;
}

public function delete( array $ids ) {
Expand Down

0 comments on commit 1455a10

Please sign in to comment.