Esempio n. 1
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 )
			)
		),
	];
};
	state => {
		const locations = getShippingZoneLocationsWithEdits( state );

		return {
			canFilter: canLocationsBeFiltered( state ),
			canFilterByState: canLocationsBeFilteredByState( state ),
			filteredByPostcode: areLocationsFilteredByPostcode( state ),
			filteredByState: areLocationsFilteredByState( state ),
			unfiltered: areLocationsUnfiltered( state ),
			states: getCurrentlyEditingShippingZoneStates( state ),
			postcode: locations && locations.postcode[ 0 ],
			countryOwner: getCurrentSelectedCountryZoneOwner( state ),
		};
	},