Skip to content

Commit

Permalink
feat(nc): implement unregister nano contract saga effect
Browse files Browse the repository at this point in the history
  • Loading branch information
alexruzenhack committed May 13, 2024
1 parent 553f87b commit a369c8c
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 0 deletions.
40 changes: 40 additions & 0 deletions src/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = () => ({
Expand Down Expand Up @@ -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,
});
33 changes: 33 additions & 0 deletions src/reducers/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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 {{
Expand Down
28 changes: 28 additions & 0 deletions src/sagas/nanoContract.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
nanoContractHistorySuccess,
nanoContractRegisterFailure,
nanoContractRegisterSuccess,
nanoContractUnregisterSuccess,
onExceptionCaptured,
types,
} from '../actions';
Expand Down Expand Up @@ -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),
]);
}

0 comments on commit a369c8c

Please sign in to comment.