|
|
import { translate } from 'i18n-calypso'; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export const prepareTimestamp = ( |
|
|
frequency: string, |
|
|
day: string, |
|
|
hour: string, |
|
|
period: string |
|
|
) => { |
|
|
const event = new Date(); |
|
|
const now = new Date(); |
|
|
|
|
|
let hours = parseInt( hour ) % 12; |
|
|
hours += period === 'pm' ? 12 : 0; |
|
|
hours %= 24; |
|
|
event.setHours( hours, 0, 0, 0 ); |
|
|
|
|
|
|
|
|
if ( frequency === 'daily' && event < now ) { |
|
|
event.setDate( event.getDate() + 1 ); |
|
|
} |
|
|
|
|
|
if ( frequency === 'weekly' ) { |
|
|
|
|
|
let dayDifference = parseInt( day ) - now.getDay(); |
|
|
|
|
|
|
|
|
if ( dayDifference === 0 && event < now ) { |
|
|
dayDifference += 7; |
|
|
} |
|
|
|
|
|
|
|
|
event.setDate( event.getDate() + dayDifference + ( dayDifference < 0 ? 7 : 0 ) ); |
|
|
} |
|
|
|
|
|
|
|
|
return event.getTime() / 1000; |
|
|
}; |
|
|
|
|
|
export const convertHourTo24 = ( hour: string, period: string ): string => { |
|
|
if ( period === 'am' ) { |
|
|
return hour === '12' ? '0' : hour; |
|
|
} else if ( period === 'pm' ) { |
|
|
return hour === '12' ? '12' : ( parseInt( hour, 10 ) + 12 ).toString(); |
|
|
} |
|
|
|
|
|
return hour; |
|
|
}; |
|
|
|
|
|
export const convertHourTo12 = ( hour: string ): string => { |
|
|
const _hour = parseInt( hour, 10 ); |
|
|
|
|
|
if ( _hour === 0 ) { |
|
|
return '12'; |
|
|
} |
|
|
|
|
|
return _hour > 12 ? ( _hour - 12 ).toString() : _hour.toString(); |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export const prepareRelativePath = ( url: string ): string => { |
|
|
const value = url.trim(); |
|
|
|
|
|
|
|
|
const urlRegex = /^(?!https?:\/\/)[\w.]+(?:\.[\w]+)+[\w\-._~:/?#[\]@!$&'()*+,;=%]*$/i; |
|
|
const withoutProtocol = urlRegex.test( value ); |
|
|
|
|
|
try { |
|
|
const _url = new URL( withoutProtocol ? `http://${ value }` : value ); |
|
|
return `${ _url.pathname }${ _url.search }${ _url.hash }`; |
|
|
} catch ( e ) { |
|
|
return value.startsWith( '/' ) ? value : `/${ value }`; |
|
|
} |
|
|
}; |
|
|
|
|
|
type TimeSlot = { |
|
|
frequency: string; |
|
|
timestamp: number; |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export const validateTimeSlot = ( newSchedule: TimeSlot, existingSchedules: TimeSlot[] = [] ) => { |
|
|
let error = ''; |
|
|
const newDate = new Date( newSchedule.timestamp * 1000 ); |
|
|
|
|
|
|
|
|
if ( newDate < new Date() ) { |
|
|
error = 'Please choose a time in the future for this schedule.'; |
|
|
} |
|
|
|
|
|
existingSchedules.forEach( ( schedule ) => { |
|
|
if ( error ) { |
|
|
return; |
|
|
} |
|
|
|
|
|
const existingDate = new Date( schedule.timestamp * 1000 ); |
|
|
|
|
|
if ( |
|
|
( newSchedule.frequency === 'daily' || schedule.frequency === 'daily' ) && |
|
|
existingDate.getHours() === newDate.getHours() |
|
|
) { |
|
|
error = translate( 'Please choose another time, as this slot is already scheduled.' ); |
|
|
} else if ( |
|
|
newSchedule.frequency === 'weekly' && |
|
|
schedule.frequency === 'weekly' && |
|
|
newDate.getDay() === existingDate.getDay() && |
|
|
newDate.getHours() === existingDate.getHours() |
|
|
) { |
|
|
error = translate( 'Please choose another time, as this slot is already scheduled.' ); |
|
|
} |
|
|
} ); |
|
|
|
|
|
return error; |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export const validatePlugins = ( plugins: string[], existingPlugins: Array< string[] > = [] ) => { |
|
|
let error = ''; |
|
|
|
|
|
if ( plugins.length === 0 ) { |
|
|
error = translate( 'Please select at least one plugin to update.' ); |
|
|
} else if ( existingPlugins.length ) { |
|
|
const _plugins = [ ...plugins ].sort(); |
|
|
|
|
|
existingPlugins.forEach( ( existing ) => { |
|
|
if ( JSON.stringify( _plugins ) === JSON.stringify( [ ...existing ].sort() ) ) { |
|
|
error = translate( |
|
|
'Please select a different set of plugins, as this one has already been chosen.' |
|
|
); |
|
|
} |
|
|
} ); |
|
|
} |
|
|
|
|
|
return error; |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export const validateSites = ( sites: number[] ): string => { |
|
|
return sites.length === 0 ? translate( 'Please select at least one site.' ) : ''; |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export const validatePath = ( path: string, paths: string[] ): string => { |
|
|
const URL_PATH_REGEX = /^\/[a-zA-Z0-9-._~:/?#[\]@!$&'()*+,;=]*$/; |
|
|
let error = ''; |
|
|
|
|
|
|
|
|
if ( path.length <= 1 ) { |
|
|
error = translate( 'Please enter a path.' ); |
|
|
} else if ( ! URL_PATH_REGEX.test( path ) ) { |
|
|
error = translate( 'Please enter a valid path.' ); |
|
|
} else if ( paths.includes( path ) ) { |
|
|
error = translate( 'This path is already added.' ); |
|
|
} |
|
|
|
|
|
return error; |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export const validatePaths = ( hasUnsubmittedPath: boolean ): string => { |
|
|
let error = ''; |
|
|
if ( hasUnsubmittedPath ) { |
|
|
error = translate( 'Please submit the path before saving the schedule.' ); |
|
|
} |
|
|
return error; |
|
|
}; |
|
|
|