Example #1
0
export const getCreateDefaultZoneActionListSteps = state => {
	const siteId = getSelectedSiteId( state );
	const serverZones = getAPIShippingZones( state );
	if ( isDefaultShippingZoneCreated( state ) ) {
		// The default zone was already created at some point in the past. Don't do anything.
		return [];
	}

	// Only create the zone if there are no zones already configured
	const steps = [];
	if ( 1 === serverZones.length && isEmpty( serverZones[ 0 ].methodIds ) ) {
		const zoneId = { index: 0 };
		const storeCountry = getStoreLocation( state ).country;
		const locations = { continent: [], country: [ storeCountry ], state: [], postcode: [] };
		const zone = {
			name: getCountryName( state, storeCountry ),
			order: getZoneLocationsPriority( locations ),
		};
		const method = { id: { index: 0 }, methodType: 'free_shipping' };

		steps.push.apply( steps, getCreateShippingZoneSteps( siteId, zoneId, zone ) );
		steps.push.apply( steps, getUpdateShippingZoneLocationsSteps( siteId, zoneId, locations ) );
		steps.push.apply( steps, getZoneMethodCreateSteps( siteId, zoneId, method ) );
	}

	steps.push( {
		description: translate( 'Finishing initial setup' ),
		onStep: ( dispatch, actionList ) => {
			setCreatedDefaultShippingZone( siteId, true )( dispatch )
				.then( () => dispatch( actionListStepSuccess( actionList ) ) )
				.catch( err => dispatch( actionListStepFailure( actionList, err ) ) );
		},
	} );
	return steps;
};
Example #2
0
	( state, zoneId, siteId ) => {
		const loaded = areShippingZonesLoaded( state, siteId );
		return [
			loaded,
			loaded && getCurrentlyEditingShippingZone( state, siteId ),
			loaded && getAPIShippingZones( state, siteId ),
		];
	},
Example #3
0
	( state, zoneId, siteId ) => {
		if ( ! areShippingZonesLoaded( state, siteId ) ) {
			return null;
		}
		if ( null === zoneId ) {
			return getCurrentlyEditingShippingZone( state, siteId );
		}
		return find( getAPIShippingZones( state, siteId ), { id: zoneId } );
	},
Example #4
0
const getLastZoneMethodOrder = ( state, siteId ) => {
	const serverZones = getAPIShippingZones( state );
	const zoneId = getShippingZonesEdits( state, siteId ).currentlyEditingId;
	const serverZone = find( serverZones, { id: zoneId } );
	const serverMethodIds = serverZone ? serverZone.methodIds : [];
	const serverMethods = serverMethodIds.map( id => getShippingZoneMethod( state, id, siteId ) );
	const lastMethodOrder = Math.max( ...map( serverMethods, 'order' ) );
	return Math.max( lastMethodOrder, 0 );
};
Example #5
0
export const getSaveZoneActionListSteps = state => {
	const siteId = getSelectedSiteId( state );
	const serverZones = getAPIShippingZones( state );
	const zoneEdits = getShippingZonesEdits( state, siteId );
	const zoneId = zoneEdits.currentlyEditingId;
	const zoneProperties = omit( zoneEdits.currentlyEditingChanges, 'methods', 'locations' );

	const getZoneStepsFunc =
		'number' === typeof zoneId ? getUpdateShippingZoneSteps : getCreateShippingZoneSteps;

	const locations = getShippingZoneLocationsWithEdits( state, siteId );
	const allServerLocations = getRawShippingZoneLocations( state, siteId );
	const serverLocations = allServerLocations[ zoneId ];

	const order = getZoneLocationsPriority( locations );
	const serverZone = find( serverZones, { id: zoneId } );
	if ( ! serverZone || serverZone.order !== order ) {
		zoneProperties.order = order;
	}

	if ( 0 !== zoneId ) {
		if (
			shouldSaveGeneratedName(
				serverZone,
				zoneProperties,
				generateZoneName( state, zoneId, siteId )
			)
		) {
			zoneProperties.name = generateCurrentlyEditingZoneName( state, siteId );
		}
		if ( serverZone && serverZone.name === zoneProperties.name ) {
			// No need to update the zone name, since it's the same one saved in the server
			delete zoneProperties.name;
		}
	}

	const methodEdits = zoneEdits.currentlyEditingChanges.methods;
	const lastMethodOrder = getLastZoneMethodOrder( state, siteId );

	return [
		...getZoneStepsFunc( siteId, zoneId, zoneProperties ),
		...getAutoOrderZonesSteps( siteId, zoneId, serverZones, allServerLocations ),
		...getUpdateShippingZoneLocationsSteps( siteId, zoneId, locations, serverLocations ),
		...flatten(
			methodEdits.deletes.map( method => getZoneMethodDeleteSteps( siteId, zoneId, method, state ) )
		),
		...flatten(
			methodEdits.updates.map( method => getZoneMethodUpdateSteps( siteId, zoneId, method, state ) )
		),
		...flatten(
			methodEdits.creates.map( method =>
				getZoneMethodCreateSteps( siteId, zoneId, method, lastMethodOrder, state )
			)
		),
	];
};
Example #6
0
export const getOrderOperationsToSaveCurrentZone = (
	state,
	siteId = getSelectedSiteId( state )
) => {
	const moves = {};
	const allLocations = getRawShippingZoneLocations( state, siteId );
	const allZones = orderBy( getAPIShippingZones( state, siteId ), 'order' );

	const currentZone = getCurrentlyEditingShippingZone( state, siteId );
	const currentZoneOrder = 'number' === typeof currentZone.id ? currentZone.order : 0;
	const currentZoneLocations = getShippingZoneLocationsWithEdits( state, siteId );
	const currentZonePriority = getZoneLocationsPriority( currentZoneLocations );
	if ( currentZonePriority && currentZoneOrder !== currentZonePriority ) {
		moves[ currentZone.id ] = currentZonePriority;
	}

	// Normally this won't be needed because all the already saved zones will have the correct order according
	// to their priority, but for example that's not the case after the initial setup (the "home country" zone
	// has order=0)
	for ( const { id, order } of allZones ) {
		// Skip over the "Locations not covered by your other zones" zone, the current zone and zones with no locations
		if ( currentZone.id === id || 0 === id ) {
			continue;
		}
		const priority = getZoneLocationsPriority( allLocations[ id ] );
		if ( ! priority ) {
			continue;
		}

		if ( order !== priority ) {
			moves[ id ] = priority;
		}
	}

	return moves;
};