From 6d20592f761ac7b948dfa917e719d9805e472a3c Mon Sep 17 00:00:00 2001 From: Jannis R Date: Fri, 9 Dec 2016 14:06:02 +0100 Subject: [PATCH] remove certification on Revoke --- .../redux/providers/certifications/actions.js | 4 ++-- .../providers/certifications/middleware.js | 11 ++++++--- .../redux/providers/certifications/reducer.js | 24 ++++++++++++------- 3 files changed, 26 insertions(+), 13 deletions(-) diff --git a/js/src/redux/providers/certifications/actions.js b/js/src/redux/providers/certifications/actions.js index d0c2d56f4..3638896b4 100644 --- a/js/src/redux/providers/certifications/actions.js +++ b/js/src/redux/providers/certifications/actions.js @@ -26,6 +26,6 @@ export const addCertification = (address, id, name, title, icon) => ({ type: 'addCertification', address, id, name, title, icon }); -export const removeCertification = (address, id, name, title, icon) => ({ - type: 'removeCertification', address, id, name, title, icon +export const removeCertification = (address, id) => ({ + type: 'removeCertification', address, id }); diff --git a/js/src/redux/providers/certifications/middleware.js b/js/src/redux/providers/certifications/middleware.js index d90045270..fe3d6b8df 100644 --- a/js/src/redux/providers/certifications/middleware.js +++ b/js/src/redux/providers/certifications/middleware.js @@ -19,7 +19,7 @@ import { uniq } from 'lodash'; import ABI from '~/contracts/abi/certifier.json'; import Contract from '~/api/contract'; import Contracts from '~/contracts'; -import { addCertification } from './actions'; +import { addCertification, removeCertification } from './actions'; export default class CertificationsMiddleware { toMiddleware () { @@ -27,6 +27,7 @@ export default class CertificationsMiddleware { const badgeReg = Contracts.get().badgeReg; const contract = new Contract(api, ABI); const Confirmed = contract.events.find((e) => e.name === 'Confirmed'); + const Revoked = contract.events.find((e) => e.name === 'Revoked'); let certifiers = []; let accounts = []; // these are addresses @@ -37,7 +38,7 @@ export default class CertificationsMiddleware { fromBlock: 0, toBlock: 'latest', address: certifiers.map((c) => c.address), - topics: [ Confirmed.signature, accounts ] + topics: [ [ Confirmed.signature, Revoked.signature ], accounts ] }) .then((logs) => contract.parseEventLogs(logs)) .then((logs) => { @@ -45,7 +46,11 @@ export default class CertificationsMiddleware { const certifier = certifiers.find((c) => c.address === log.address); if (!certifier) throw new Error(`Could not find certifier at ${log.address}.`); const { id, name, title, icon } = certifier; - dispatch(addCertification(log.params.who.value, id, name, title, icon)); + if (log.event === 'Revoked') { + dispatch(removeCertification(log.params.who.value, id)); + } else { + dispatch(addCertification(log.params.who.value, id, name, title, icon)); + } }); }) .catch((err) => { diff --git a/js/src/redux/providers/certifications/reducer.js b/js/src/redux/providers/certifications/reducer.js index df726be82..4014ca241 100644 --- a/js/src/redux/providers/certifications/reducer.js +++ b/js/src/redux/providers/certifications/reducer.js @@ -17,17 +17,25 @@ const initialState = {}; export default (state = initialState, action) => { - if (action.type !== 'addCertification') { - return state; + if (action.type === 'addCertification') { + const { address, id, name, icon, title } = action; + const certifications = state[address] || []; + + if (certifications.some((c) => c.id === id)) { + return state; + } + const newCertifications = certifications.concat({ id, name, icon, title }); + + return { ...state, [address]: newCertifications }; } - const { address, id, name, icon, title } = action; - const certifications = state[address] || []; + if (action.type === 'removeCertification') { + const { address, id } = action; + const certifications = state[address] || []; - if (certifications.some((c) => c.id === id)) { - return state; + const newCertifications = certifications.filter((c) => c.id !== id); + return { ...state, [address]: newCertifications }; } - const newCertifications = certifications.concat({ id, name, icon, title }); - return { ...state, [address]: newCertifications }; + return state; };