remove certification on Revoke
This commit is contained in:
		
							parent
							
								
									fd88421e88
								
							
						
					
					
						commit
						6d20592f76
					
				| @ -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 | ||||
| }); | ||||
|  | ||||
| @ -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) => { | ||||
|  | ||||
| @ -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; | ||||
| }; | ||||
|  | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user