diff --git a/src/actions.js b/src/actions.js index 2347b759f..6a5496930 100644 --- a/src/actions.js +++ b/src/actions.js @@ -149,6 +149,12 @@ export const types = { NANOCONTRACT_HISTORY_SUCCESS: 'NANOCONTRACT_HISTORY_SUCCESS', /* It indicates a Nano Contract history failed to load. */ NANOCONTRACT_HISTORY_FAILURE: 'NANOCONTRACT_HISTORY_FAILURE', + /* It initiates an unregistration process of a Nano Contract. */ + NANOCONTRACT_UNREGISTER_REQUEST: 'NANOCONTRACT_UNREGISTER_REQUEST', + /* It initiates an unregistration process is complete. */ + NANOCONTRACT_UNREGISTER_SUCCESS: 'NANOCONTRACT_UNREGISTER_SUCCESS', + /* It initiates a process to change the address on registered Nano Contract. */ + NANOCONTRACT_ADDRESS_CHANGE_REQUEST: 'NANOCONTRACT_ADDRESS_CHANGE_REQUEST', }; export const featureToggleInitialized = () => ({ @@ -1055,3 +1061,37 @@ export const nanoContractHistoryFailure = (payload) => ({ type: types.NANOCONTRACT_HISTORY_FAILURE, payload, }); + +/** + * Request unregistration of a Nano Contract by its key. + * @param {{ + * ncId: string, + * }} Nano Contract ID to unregister. + */ +export const nanoContractUnregisterRequest = (unregisterRequest) => ({ + type: types.NANOCONTRACT_UNREGISTER_REQUEST, + payload: unregisterRequest, +}); + +/** + * Unregistration of a Nano Contract has finished with success. + * @param {{ + * ncId: string, + * }} Nano Contract ID unregistered. + */ +export const nanoContractUnregisterSuccess = (unregistered) => ({ + type: types.NANOCONTRACT_UNREGISTER_SUCCESS, + payload: unregistered, +}); + +/** + * Request a change on the Nano Contract registered. + * @param {{ + * newAddress: string; + * oldNc: Object; + * }} changeAddressRequest + */ +export const nanoContractAddressChangeRequest = (changeAddressRequest) => ({ + type: types.NANOCONTRACT_ADDRESS_CHANGE_REQUEST, + payload: changeAddressRequest, +}); diff --git a/src/reducers/reducer.js b/src/reducers/reducer.js index f3537535d..df47ec9ec 100644 --- a/src/reducers/reducer.js +++ b/src/reducers/reducer.js @@ -477,6 +477,8 @@ export const reducer = (state = initialState, action) => { return onNanoContractHistoryFailure(state, action); case types.NANOCONTRACT_HISTORY_SUCCESS: return onNanoContractHistorySuccess(state, action); + case types.NANOCONTRACT_UNREGISTER_SUCCESS: + return onNanoContractUnregisterSuccess(state, action); default: return state; } @@ -1329,6 +1331,37 @@ export const onNanoContractRegisterSuccess = (state, { payload }) => ({ }, }); +/** + * @param {Object} state + * @param {{ + * payload: { + * ncId: string, + * } + * }} action + */ +export const onNanoContractUnregisterSuccess = (state, { payload }) => { + const { ncId } = payload; + + const newRegisteredContracts = { ...state.nanoContract.registered, }; + delete newRegisteredContracts[ncId]; + + const newContractsHistory = { ...state.nanoContract.history }; + delete newContractsHistory[ncId]; + + const newContractsHistoryMeta = { ...state.nanoContract.historyMeta }; + delete newContractsHistoryMeta[ncId]; + + return ({ + ...state, + nanoContract: { + ...state.nanoContract, + registered: newRegisteredContracts, + history: newContractsHistory, + historyMeta: newContractsHistoryMeta, + }, + }); +}; + /** * @param {Object} state * @param {{ diff --git a/src/sagas/nanoContract.js b/src/sagas/nanoContract.js index 6e9fd3e27..82acee63d 100644 --- a/src/sagas/nanoContract.js +++ b/src/sagas/nanoContract.js @@ -24,6 +24,7 @@ import { nanoContractHistorySuccess, nanoContractRegisterFailure, nanoContractRegisterSuccess, + nanoContractUnregisterSuccess, onExceptionCaptured, types, } from '../actions'; @@ -254,9 +255,36 @@ export function* requestHistoryNanoContract({ payload }) { } } +/** + * Process Nano Contract unregister request. + * @param {{ + * payload: { + * ncId: string; + * } + * }} action with request payload. + */ +export function* unregisterNanoContract({ payload }) { + const { ncId } = payload; + + const wallet = yield select((state) => state.wallet); + if (!wallet.isReady()) { + log.debug('Fail unregistering Nano Contract because wallet is not ready yet.'); + // This will show user an error modal with the option to send the error to sentry. + yield put(onExceptionCaptured(new Error(failureMessage.walletNotReadyError), true)); + return; + } + + yield call(wallet.storage.unregisterNanoContract.bind(wallet.storage), ncId); + + log.debug(`Success unregistering Nano Contract. ncId = ${ncId}`); + // emit action NANOCONTRACT_REGISTER_SUCCESS + yield put(nanoContractUnregisterSuccess({ ncId })); +} + export function* saga() { yield all([ takeEvery(types.NANOCONTRACT_REGISTER_REQUEST, registerNanoContract), takeEvery(types.NANOCONTRACT_HISTORY_REQUEST, requestHistoryNanoContract), + takeEvery(types.NANOCONTRACT_UNREGISTER_REQUEST, unregisterNanoContract), ]); }