2020-02-07 07:04:18 +04:00
|
|
|
import { Dispatch } from 'redux';
|
|
|
|
|
|
|
|
import { compileContract, getErrorMessage } from '../../services/api';
|
|
|
|
import { AppState } from '../app';
|
|
|
|
import { DoneLoadingAction, UpdateLoadingAction } from '../loading';
|
|
|
|
import { ChangeOutputAction } from '../result';
|
|
|
|
import { CancellableAction } from './cancellable';
|
|
|
|
|
|
|
|
export class CompileAction extends CancellableAction {
|
|
|
|
getAction() {
|
|
|
|
return async (dispatch: Dispatch, getState: () => AppState) => {
|
|
|
|
dispatch({ ...new UpdateLoadingAction('Compiling contract...') });
|
|
|
|
|
|
|
|
try {
|
|
|
|
const { editor, compile: compileState } = getState();
|
|
|
|
const michelsonCode = await compileContract(
|
|
|
|
editor.language,
|
|
|
|
editor.code,
|
2020-02-14 05:18:23 +04:00
|
|
|
compileState.entrypoint,
|
|
|
|
compileState.michelsonFormat
|
2020-02-07 07:04:18 +04:00
|
|
|
);
|
|
|
|
|
|
|
|
if (this.isCancelled()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
dispatch({ ...new ChangeOutputAction(michelsonCode.result) });
|
|
|
|
} catch (ex) {
|
|
|
|
if (this.isCancelled()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
dispatch({
|
|
|
|
...new ChangeOutputAction(`Error: ${getErrorMessage(ex)}`)
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
dispatch({ ...new DoneLoadingAction() });
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|