File size: 5,178 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 |
import { translate } from 'i18n-calypso';
/**
* Prepare unix timestamp in seconds
* based on selected frequency, day, hour and period
*/
export const prepareTimestamp = (
frequency: string,
day: string,
hour: string,
period: string
) => {
const event = new Date();
const now = new Date();
let hours = parseInt( hour ) % 12; // Get the hour value (0-11).
hours += period === 'pm' ? 12 : 0; // Add 12 if period is 'pm'.
hours %= 24; // Ensure the hour is in the range of 0-23.
event.setHours( hours, 0, 0, 0 );
// If the selected hour is in the past, advance to the next day.
if ( frequency === 'daily' && event < now ) {
event.setDate( event.getDate() + 1 );
}
if ( frequency === 'weekly' ) {
// Calculate the difference between the target day and today.
let dayDifference = parseInt( day ) - now.getDay();
// If the target day is today and the selected hour is in the past, advance to the next week.
if ( dayDifference === 0 && event < now ) {
dayDifference += 7;
}
// Set the date to the next selected day.
event.setDate( event.getDate() + dayDifference + ( dayDifference < 0 ? 7 : 0 ) );
}
// return timestamp in seconds
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();
};
/**
* Prepare relative path
* based on URL or relative path
*/
export const prepareRelativePath = ( url: string ): string => {
const value = url.trim();
// Check if the value is a URL without a protocol
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;
};
/**
* Validate time slot
* based on existing schedules in context of frequency
*/
export const validateTimeSlot = ( newSchedule: TimeSlot, existingSchedules: TimeSlot[] = [] ) => {
let error = '';
const newDate = new Date( newSchedule.timestamp * 1000 );
// prepareTimestamp should always return a future date, but we need to catch it if it doesn't.
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;
};
/**
* Validate plugins
* compared with existing scheduled plugins
*/
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;
};
/**
* Validate sites
* check if at least one site is selected
*/
export const validateSites = ( sites: number[] ): string => {
return sites.length === 0 ? translate( 'Please select at least one site.' ) : '';
};
/**
* Validate path
* check if path passes URL_PATH_REGEX
*/
export const validatePath = ( path: string, paths: string[] ): string => {
const URL_PATH_REGEX = /^\/[a-zA-Z0-9-._~:/?#[\]@!$&'()*+,;=]*$/;
let error = '';
// leading slash is required
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;
};
/* Validate paths
* check if path is submitted before saving the schedule
*/
export const validatePaths = ( hasUnsubmittedPath: boolean ): string => {
let error = '';
if ( hasUnsubmittedPath ) {
error = translate( 'Please submit the path before saving the schedule.' );
}
return error;
};
|