File size: 2,522 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 |
import { appStates } from 'calypso/state/imports/constants';
// Left( UI ) - Right( API )
const importerStateMap = [
[ appStates.CANCEL_PENDING, 'cancel' ],
[ appStates.DEFUNCT, 'importStopped' ],
[ appStates.DISABLED, 'disabled' ],
[ appStates.EXPIRED, 'importExpired' ],
[ appStates.EXPIRE_PENDING, 'expire' ],
[ appStates.IMPORT_FAILURE, 'importFailure' ],
[ appStates.IMPORT_SUCCESS, 'importSuccess' ],
[ appStates.IMPORTING, 'importing' ],
[ appStates.INACTIVE, 'importer-inactive' ],
[ appStates.MAP_AUTHORS, 'importer-map-authors' ],
[ appStates.READY_FOR_UPLOAD, 'importer-ready-for-upload' ],
[ appStates.UPLOAD_PROCESSING, 'uploadProcessing' ],
[ appStates.UPLOAD_SUCCESS, 'uploadSuccess' ],
[ appStates.UPLOAD_FAILURE, 'importer-upload-failure' ],
[ appStates.UPLOADING, 'importer-uploading' ],
[ appStates.IMPORT_CLEAR, 'importer-clear' ],
];
function apiToAppState( state ) {
return importerStateMap.find( ( [ , api ] ) => api === state )[ 0 ];
}
function appStateToApi( state ) {
return importerStateMap.find( ( [ appState ] ) => appState === state )[ 1 ];
}
function generateSourceAuthorIds( customData ) {
if ( ! customData.sourceAuthors ) {
return customData;
}
return Object.assign( {}, customData, {
sourceAuthors: customData.sourceAuthors.map( ( author ) =>
author.id ? author : Object.assign( {}, author, { id: author.login } )
),
} );
}
function replaceUserInfoWithIds( customData ) {
if ( ! customData.sourceAuthors ) {
return customData;
}
return {
...customData,
sourceAuthors: customData.sourceAuthors.map( ( author ) =>
author.mappedTo ? { ...author, mappedTo: author.mappedTo.ID } : author
),
};
}
export function fromApi( state ) {
const {
importId: importerId,
importStatus,
importerFileType,
type,
progress,
customData,
errorData,
siteId,
} = state;
return {
importerId,
importerState: apiToAppState( importStatus ),
importerFileType,
type: `importer-type-${ type }`,
progress,
...( customData && { customData: generateSourceAuthorIds( customData ) } ),
site: { ID: siteId },
errorData,
};
}
export function toApi( state ) {
const { importerId, site, type, importerState, customData, progress = undefined } = state;
return {
importerId,
progress,
importStatus: appStateToApi( importerState ),
...( site && site.ID && { siteId: site.ID } ),
...( type && { type: type.replace( 'importer-type-', '' ) } ),
...( customData && { customData: replaceUserInfoWithIds( customData ) } ),
};
}
|