Skip to content

Commit

Permalink
Merge pull request #751 from Keith-CY/optimize-updaing-description
Browse files Browse the repository at this point in the history
feat(neuron-ui): optimize updating descriptions
  • Loading branch information
ashchan authored Jul 26, 2019
2 parents f3e756e + 6a72502 commit c82f54a
Show file tree
Hide file tree
Showing 11 changed files with 150 additions and 111 deletions.
46 changes: 26 additions & 20 deletions packages/neuron-ui/src/components/Addresses/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { localNumberFormatter, shannonToCKBFormatter } from 'utils/formatters'

const Addresses = ({
app: {
loadings: { addressList: isLoading },
loadings: { addressList: isLoading, updateDescription: isUpdatingDescription },
},
wallet: { addresses = [], id: walletID },
settings: { showAddressBook = false },
Expand All @@ -34,19 +34,13 @@ const Addresses = ({
}
}, [showAddressBook, history])

const { localDescription, onDescriptionPress, onDescriptionFieldBlur, onDescriptionChange } = useLocalDescription(
'address',
walletID,
useMemo(
() =>
addresses.map(({ address: key = '', description = '' }) => ({
key,
description,
})),
[addresses]
),
dispatch
)
const {
localDescription,
onDescriptionPress,
onDescriptionFieldBlur,
onDescriptionChange,
onDescriptionFocus,
} = useLocalDescription('address', walletID, dispatch)

const theme = getTheme()
const { semanticColors } = theme
Expand Down Expand Up @@ -104,12 +98,15 @@ const Addresses = ({
<TextField
borderless
title={item.description}
value={
(localDescription.find(local => local.key === item.address) || { description: '' }).description || ''
}
onKeyPress={onDescriptionPress(item.address)}
onBlur={onDescriptionFieldBlur(item.address)}
value={localDescription.key === item.address ? localDescription.description : item.description || ''}
onBlur={onDescriptionFieldBlur(item.address, item.description)}
onFocus={onDescriptionFocus}
onKeyPress={onDescriptionPress(item.address, item.description)}
onChange={onDescriptionChange(item.address)}
disabled={localDescription.key === item.address && isUpdatingDescription}
iconProps={{
iconName: localDescription.key === item.address && isUpdatingDescription ? 'Updating' : '',
}}
styles={(props: ITextFieldStyleProps) => {
return {
root: {
Expand Down Expand Up @@ -160,7 +157,16 @@ const Addresses = ({
},
},
],
[onDescriptionChange, localDescription, onDescriptionFieldBlur, onDescriptionPress, t, semanticColors]
[
onDescriptionChange,
localDescription,
onDescriptionFieldBlur,
onDescriptionPress,
onDescriptionFocus,
isUpdatingDescription,
t,
semanticColors,
]
)

return (
Expand Down
10 changes: 8 additions & 2 deletions packages/neuron-ui/src/components/History/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { useSearch } from './hooks'

const History = ({
app: {
loadings: { transactionList: isLoading },
loadings: { transactionList: isLoading, updateDescription: isUpdatingDescription },
},
wallet: { id },
chain: {
Expand Down Expand Up @@ -45,7 +45,13 @@ const History = ({
iconProps={{ iconName: 'Search', styles: { root: { height: '18px' } } }}
/>
</Stack>
<TransactionList isLoading={isLoading} walletID={id} items={items} dispatch={dispatch} />
<TransactionList
isLoading={isLoading}
isUpdatingDescription={isUpdatingDescription}
walletID={id}
items={items}
dispatch={dispatch}
/>
<Pagination
selectedPageIndex={pageNo - 1}
pageCount={Math.ceil(totalCount / pageSize)}
Expand Down
47 changes: 27 additions & 20 deletions packages/neuron-ui/src/components/TransactionList/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,30 +46,26 @@ const onRenderHeader = ({ group }: any) => {

const TransactionList = ({
isLoading = false,
isUpdatingDescription = false,
items = [],
walletID,
dispatch,
}: {
isLoading: boolean
isLoading?: boolean
isUpdatingDescription?: boolean
walletID: string
items: State.Transaction[]
dispatch: StateDispatch
}) => {
const [t] = useTranslation()

const { localDescription, onDescriptionPress, onDescriptionFieldBlur, onDescriptionChange } = useLocalDescription(
'transaction',
walletID,
useMemo(
() =>
items.map(({ hash: key, description = '' }) => ({
key,
description,
})),
[items]
),
dispatch
)
const {
localDescription,
onDescriptionPress,
onDescriptionFieldBlur,
onDescriptionChange,
onDescriptionFocus,
} = useLocalDescription('transaction', walletID, dispatch)

const transactionColumns: IColumn[] = useMemo(
(): IColumn[] =>
Expand Down Expand Up @@ -113,12 +109,15 @@ const TransactionList = ({
return item ? (
<TextField
title={item.description}
value={
(localDescription.find(local => local.key === item.hash) || { description: '' }).description || ''
}
onKeyPress={onDescriptionPress(item.hash)}
onBlur={onDescriptionFieldBlur(item.hash)}
value={localDescription.key === item.hash ? localDescription.description : item.description || ''}
onFocus={onDescriptionFocus}
onBlur={onDescriptionFieldBlur(item.hash, item.description)}
onKeyPress={onDescriptionPress(item.hash, item.description)}
onChange={onDescriptionChange(item.hash)}
disabled={localDescription.key === item.hash && isUpdatingDescription}
iconProps={{
iconName: localDescription.key === item.hash && isUpdatingDescription ? 'Updating' : '',
}}
borderless
styles={(props: ITextFieldStyleProps) => {
return {
Expand Down Expand Up @@ -152,7 +151,15 @@ const TransactionList = ({
].map(
(col): IColumn => ({ fieldName: col.key, ariaLabel: col.name, isResizable: true, isCollapsable: false, ...col })
),
[localDescription, onDescriptionChange, onDescriptionFieldBlur, onDescriptionPress, t]
[
localDescription,
onDescriptionChange,
onDescriptionFieldBlur,
onDescriptionPress,
onDescriptionFocus,
isUpdatingDescription,
t,
]
)

const { groups, txs } = useMemo(() => {
Expand Down
2 changes: 2 additions & 0 deletions packages/neuron-ui/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
Scan as ScanIcon,
Search as SearchIcon,
SubtractCircle as RemoveIcon,
Update as UpdateIcon,
} from 'grommet-icons'

import 'styles/index.scss'
Expand Down Expand Up @@ -76,6 +77,7 @@ registerIcons({
Leave: <LeaveIcon />,
Connected: <ConnectedIcon size="small" color="green" />,
Disconnected: <AlertIcon size="small" color="red" />,
Updating: <UpdateIcon size="16px" style={{ animation: 'rotate360 3s linear infinite' }} />,
},
})

Expand Down
1 change: 1 addition & 0 deletions packages/neuron-ui/src/states/initStates/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ const appState: State.App = {
sending: false,
addressList: false,
transactionList: false,
updateDescription: false,
},
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,31 @@ export const updateTransaction = (params: { walletID: string; hash: string }) =>
})
}
export const updateTransactionList = (params: GetTransactionListParams) => (dispatch: StateDispatch) => {
getTransactionList(params).then(res => {
if (res.status) {
dispatch({
type: NeuronWalletActions.UpdateTransactionList,
payload: res.result,
})
} else {
addNotification({ type: 'alert', content: res.message.title })(dispatch)
}
})
}

export const updateTransactionDescription = (params: Controller.UpdateTransactionDescriptionParams) => (
dispatch: StateDispatch
) => {
dispatch({
type: AppActions.UpdateLoadings,
payload: {
transactionList: true,
updateDescription: true,
},
})
getTransactionList(params)
updateRemoteTransactionDescription(params)
.then(res => {
if (res.status) {
dispatch({
type: NeuronWalletActions.UpdateTransactionList,
payload: res.result,
})
dispatch({ type: AppActions.Ignore, payload: null })
} else {
addNotification({ type: 'alert', content: res.message.title })(dispatch)
}
Expand All @@ -41,24 +53,12 @@ export const updateTransactionList = (params: GetTransactionListParams) => (disp
dispatch({
type: AppActions.UpdateLoadings,
payload: {
transactionList: false,
updateDescription: false,
},
})
})
}

export const updateTransactionDescription = (params: Controller.UpdateTransactionDescriptionParams) => (
dispatch: StateDispatch
) => {
updateRemoteTransactionDescription(params).then(res => {
if (res.status) {
dispatch({ type: AppActions.Ignore, payload: null })
} else {
addNotification({ type: 'alert', content: res.message.title })(dispatch)
}
})
}

export default {
updateTransaction,
updateTransactionList,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,16 +156,31 @@ export const updateAddressList = (params: Controller.GetAddressesByWalletIDParam
export const updateAddressDescription = (params: Controller.UpdateAddressDescriptionParams) => (
dispatch: StateDispatch
) => {
updateRemoteAddressDescription(params).then(res => {
if (res.status) {
dispatch({
type: AppActions.UpdateLoadings,
payload: {
updateDescription: true,
},
})
updateRemoteAddressDescription(params)
.then(res => {
if (res.status) {
dispatch({
type: AppActions.Ignore,
payload: null,
})
} else {
addNotification({ type: 'alert', content: res.message.title })(dispatch)
}
})
.finally(() => {
dispatch({
type: AppActions.Ignore,
payload: null,
type: AppActions.UpdateLoadings,
payload: {
updateDescription: false,
},
})
} else {
addNotification({ type: 'alert', content: res.message.title })(dispatch)
}
})
})
}

export const deleteWallet = (params: Controller.DeleteWalletParams) => (dispatch: StateDispatch) => {
Expand Down
7 changes: 5 additions & 2 deletions packages/neuron-ui/src/stories/TransactionList.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,21 @@ import transactions from './data/transactions'

const stories = storiesOf('TransactionList', module)
Object.entries(transactions).forEach(([title, list]) => {
stories.add(title, () => <TransactionList isLoading={false} walletID="1" items={list} dispatch={() => {}} />)
stories.add(title, () => (
<TransactionList isLoading={false} isUpdatingDescription={false} walletID="1" items={list} dispatch={() => {}} />
))
})

stories.add('Wtih empty pending list', () => (
<TransactionList
isLoading={false}
isUpdatingDescription={false}
walletID="1"
items={transactions['Content List'].filter(item => item.status !== 'pending')}
dispatch={() => {}}
/>
))

stories.add('Shimmered List', () => {
return <TransactionList isLoading={false} walletID="1" items={[]} dispatch={() => {}} />
return <TransactionList isLoading={false} isUpdatingDescription={false} walletID="1" items={[]} dispatch={() => {}} />
})
10 changes: 10 additions & 0 deletions packages/neuron-ui/src/styles/index.scss
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,13 @@ navbar {
text-overflow: ellipsis
}
}

@keyframes rotate360 {
from {
transform: rotate(0)
}

to {
transform: rotate(360deg)
}
}
1 change: 1 addition & 0 deletions packages/neuron-ui/src/types/App/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ declare namespace State {
sending: boolean
addressList: boolean
transactionList: boolean
updateDescription: boolean
}
}

Expand Down
Loading

0 comments on commit c82f54a

Please sign in to comment.