2018-05-21 23:10:51 +00:00
|
|
|
import { TypeKeys, AddressBookAction, AddressLabelEntry } from 'actions/addressBook';
|
|
|
|
|
|
|
|
export interface State {
|
|
|
|
addresses: {
|
|
|
|
[address: string]: string;
|
|
|
|
};
|
|
|
|
labels: {
|
|
|
|
[labels: string]: string;
|
|
|
|
};
|
|
|
|
entries: {
|
|
|
|
[id: string]: AddressLabelEntry;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export const INITIAL_STATE: State = {
|
|
|
|
addresses: {},
|
|
|
|
labels: {},
|
|
|
|
entries: {}
|
|
|
|
};
|
|
|
|
|
|
|
|
export function addressBook(state: State = INITIAL_STATE, action: AddressBookAction): State {
|
|
|
|
switch (action.type) {
|
|
|
|
case TypeKeys.SET_ADDRESS_LABEL: {
|
|
|
|
const { addresses, labels } = state;
|
2018-06-15 23:28:42 +00:00
|
|
|
const { label } = action.payload;
|
|
|
|
const address = action.payload.address.toLowerCase();
|
|
|
|
|
2018-05-21 23:10:51 +00:00
|
|
|
const updatedAddresses = {
|
|
|
|
...addresses,
|
2018-06-15 23:28:42 +00:00
|
|
|
[address]: label
|
2018-05-21 23:10:51 +00:00
|
|
|
};
|
|
|
|
const updatedLabels = {
|
|
|
|
...labels,
|
2018-06-15 23:28:42 +00:00
|
|
|
[label]: address
|
2018-05-21 23:10:51 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
addresses: updatedAddresses,
|
|
|
|
labels: updatedLabels
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
case TypeKeys.CLEAR_ADDRESS_LABEL: {
|
|
|
|
const { addresses, labels } = state;
|
2018-06-15 23:28:42 +00:00
|
|
|
const address = action.payload.toLowerCase();
|
2018-05-21 23:10:51 +00:00
|
|
|
const label = addresses[address];
|
|
|
|
const updatedAddresses = { ...addresses };
|
|
|
|
const updatedLabels = { ...labels };
|
|
|
|
|
2018-06-15 23:28:42 +00:00
|
|
|
delete updatedAddresses[address];
|
2018-05-21 23:10:51 +00:00
|
|
|
delete updatedLabels[label];
|
|
|
|
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
addresses: updatedAddresses,
|
|
|
|
labels: updatedLabels
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
case TypeKeys.SET_ADDRESS_LABEL_ENTRY: {
|
2018-06-15 23:28:42 +00:00
|
|
|
const { id } = action.payload;
|
|
|
|
const address = action.payload.address.toLowerCase();
|
2018-05-21 23:10:51 +00:00
|
|
|
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
entries: {
|
|
|
|
...state.entries,
|
|
|
|
[id]: {
|
|
|
|
...action.payload,
|
2018-06-15 23:28:42 +00:00
|
|
|
address
|
2018-05-21 23:10:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
case TypeKeys.CLEAR_ADDRESS_LABEL_ENTRY: {
|
|
|
|
const id = action.payload;
|
|
|
|
const entries = { ...state.entries };
|
|
|
|
|
|
|
|
delete entries[id];
|
|
|
|
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
entries
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
}
|