Skip to content

Commit

Permalink
Merge pull request #964 from Keith-CY/add-skip-data-toggle
Browse files Browse the repository at this point in the history
Add skip data toggle
  • Loading branch information
Keith-CY committed Sep 29, 2019
2 parents 40c9f82 + a79b780 commit 9d71ee3
Show file tree
Hide file tree
Showing 19 changed files with 159 additions and 16 deletions.
5 changes: 4 additions & 1 deletion packages/neuron-ui/src/components/Addresses/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ const Addresses = ({
},
wallet: { addresses = [], id: walletID },
chain: { networkID },
settings: { showAddressBook = false, networks = [] },
settings: {
general: { showAddressBook = false },
networks = [],
},
history,
dispatch,
}: React.PropsWithoutRef<StateWithDispatch & RouteComponentProps>) => {
Expand Down
27 changes: 20 additions & 7 deletions packages/neuron-ui/src/components/GeneralSetting/index.tsx
Original file line number Diff line number Diff line change
@@ -1,23 +1,36 @@
import React, { useCallback } from 'react'
import React, { useMemo } from 'react'
import { Stack, Toggle } from 'office-ui-fabric-react'
import { useTranslation } from 'react-i18next'

import { StateWithDispatch } from 'states/stateProvider/reducer'
import { toggleAddressBook } from 'states/stateProvider/actionCreators'
import { toggleAddressBook, setSkipDataAndType } from 'states/stateProvider/actionCreators'

const GeneralSetting = ({ settings: { showAddressBook }, dispatch }: React.PropsWithoutRef<StateWithDispatch>) => {
const GeneralSetting = ({
settings: {
general: { showAddressBook, skipDataAndType },
},
dispatch,
}: React.PropsWithoutRef<StateWithDispatch>) => {
const [t] = useTranslation()
const onToggle = useCallback(() => {
dispatch(toggleAddressBook())
}, [dispatch])
const [onToggleAddressVisibility, onSetSkipDataAndType] = useMemo(
() => [() => dispatch(toggleAddressBook()), () => setSkipDataAndType(!skipDataAndType)(dispatch)],
[dispatch, skipDataAndType]
)
return (
<Stack tokens={{ childrenGap: 15 }}>
<Toggle
checked={showAddressBook}
label={t('settings.general.display-address-book-in-the-navbar')}
onText={t('common.toggle.on')}
offText={t('common.toggle.off')}
onChange={onToggle}
onChange={onToggleAddressVisibility}
/>
<Toggle
checked={skipDataAndType}
label={t('settings.general.skip-data-and-type')}
onText={t('common.toggle.on')}
offText={t('common.toggle.off')}
onChange={onSetSkipDataAndType}
/>
</Stack>
)
Expand Down
5 changes: 4 additions & 1 deletion packages/neuron-ui/src/containers/Navbar/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,10 @@ const Navbar = ({
}: React.PropsWithoutRef<StateWithDispatch & RouteComponentProps>) => {
const neuronWallet = useState()
const {
settings: { wallets = [], showAddressBook = false },
settings: {
wallets = [],
general: { showAddressBook = false },
},
} = neuronWallet
const [t] = useTranslation()

Expand Down
1 change: 1 addition & 0 deletions packages/neuron-ui/src/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@
},
"general": {
"display-address-book-in-the-navbar": "Show the address book",
"skip-data-and-type": "Skip the cells which contain data or type script",
"show": "Show",
"hide": "Hide"
},
Expand Down
1 change: 1 addition & 0 deletions packages/neuron-ui/src/locales/zh.json
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@
},
"general": {
"display-address-book-in-the-navbar": "显示地址簿",
"skip-data-and-type": "忽略包含 Data 或 Type Script 的 Cell",
"show": "显示",
"hide": "隐藏"
},
Expand Down
1 change: 1 addition & 0 deletions packages/neuron-ui/src/services/remote/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export * from './app'
export * from './wallets'
export * from './networks'
export * from './transactions'
export * from './skipDataAndType'

export const getLocale = () => {
if (!window.remote) {
Expand Down
11 changes: 11 additions & 0 deletions packages/neuron-ui/src/services/remote/skipDataAndType.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { controllerMethodWrapper } from './controllerMethodWrapper'

const CONTROLLER_NAME = 'skip-data-and-type'

export const setSkipDataAndType = controllerMethodWrapper(CONTROLLER_NAME)(
(controller: any) => (params: Controller.SetSkipAndTypeParam) => {
return controller.update(params)
}
)

export default { setSkipDataAndType }
5 changes: 4 additions & 1 deletion packages/neuron-ui/src/states/initStates/settings.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { addressBook, wallets, networks } from 'services/localCache'

export const settingsState: State.Settings = {
showAddressBook: addressBook.isVisible(),
general: {
skipDataAndType: false,
showAddressBook: addressBook.isVisible(),
},
networks: networks.load(),
wallets: wallets.load(),
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export const initAppState = () => (dispatch: StateDispatch, history: any) => {
syncedBlockNumber = '',
connectionStatus = false,
codeHash = '',
skipDataAndType = false,
} = res.result
dispatch({
type: NeuronWalletActions.InitAppState,
Expand All @@ -38,6 +39,7 @@ export const initAppState = () => (dispatch: StateDispatch, history: any) => {
syncedBlockNumber,
connectionStatus,
codeHash,
skipDataAndType,
},
})
if (!wallet) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,19 @@ import app from './app'
import wallets from './wallets'
import transactions from './transactions'
import settings from './settings'
import skipDataAndType from './skipDataAndType'

export * from './app'
export * from './wallets'
export * from './transactions'
export * from './settings'
export * from './skipDataAndType'
export const actionCreators = {
...app,
...wallets,
...transactions,
...settings,
...skipDataAndType,
}

export default actionCreators
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { setSkipDataAndType as setRemoteSkipDataAndType } from 'services/remote'
import { failureResToNotification } from 'utils/formatters'
import { StateDispatch, NeuronWalletActions } from '../reducer'
import { addNotification } from './app'

export const setSkipDataAndType = (skip: Controller.SetSkipAndTypeParam) => (dispatch: StateDispatch) => {
setRemoteSkipDataAndType(skip).then(res => {
if (res.status === 1) {
dispatch({
type: NeuronWalletActions.UpdateSkipDataAndType,
payload: res.result,
})
} else {
addNotification(failureResToNotification(res))(dispatch)
}
})
}

export default {
setSkipDataAndType,
}
25 changes: 23 additions & 2 deletions packages/neuron-ui/src/states/stateProvider/reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ export enum NeuronWalletActions {
// Connection
UpdateConnectionStatus = 'updateConnectionStatus',
UpdateSyncedBlockNumber = 'updateSyncedBlockNumber',
// settings
UpdateSkipDataAndType = 'updateSkipDataAndType',
}
export enum AppActions {
ToggleAddressBookVisibility = 'toggleAddressBookVisibility',
Expand Down Expand Up @@ -77,6 +79,7 @@ export const reducer = (
syncedBlockNumber,
connectionStatus,
codeHash,
skipDataAndType,
} = payload
return {
...state,
Expand All @@ -90,7 +93,10 @@ export const reducer = (
tipBlockNumber: syncedBlockNumber,
},
settings: {
...state.settings,
general: {
...state.settings.general,
skipDataAndType,
},
networks,
wallets,
},
Expand All @@ -101,7 +107,22 @@ export const reducer = (
...state,
settings: {
...settings,
showAddressBook: !settings.showAddressBook,
general: {
...settings.general,
showAddressBook: !settings.general.showAddressBook,
},
},
}
}
case NeuronWalletActions.UpdateSkipDataAndType: {
return {
...state,
settings: {
...settings,
general: {
...settings.general,
skipDataAndType: payload,
},
},
}
}
Expand Down
5 changes: 4 additions & 1 deletion packages/neuron-ui/src/types/App/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,10 @@ declare namespace State {
}
}
interface Settings {
showAddressBook: boolean
general: {
skipDataAndType: boolean
showAddressBook: boolean
}
networks: Network[]
wallets: WalletIdentity[]
}
Expand Down
1 change: 1 addition & 0 deletions packages/neuron-ui/src/types/Controller/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,5 @@ declare namespace Controller {
hash: string
description: string
}
type SetSkipAndTypeParam = boolean
}
11 changes: 8 additions & 3 deletions packages/neuron-wallet/src/controllers/app/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import WalletsService from 'services/wallets'
import WalletsController from 'controllers/wallets'
import SyncInfoController from 'controllers/sync-info'
import UpdateController from 'controllers/update'
import SkipDataAndType from 'services/settings/skip-data-and-type'

import { ResponseCode } from 'utils/const'
import WindowManager from 'models/window-manager'
Expand Down Expand Up @@ -63,7 +64,7 @@ export default class AppController {
},
() => {
resolve(false)
}
},
)
}),
new Promise(resolve => {
Expand All @@ -84,8 +85,8 @@ export default class AppController {
}
}
return undefined
})
)
}),
),
)
const addresses: Controller.Address[] = await (currentWallet
? WalletsController.getAllAddresses(currentWallet.id).then(res => res.result)
Expand All @@ -99,6 +100,9 @@ export default class AppController {
walletID: currentWallet.id,
}).then(res => res.result)
: []

const skipDataAndType = SkipDataAndType.getInstance().get()

const initState = {
currentWallet,
wallets: [...wallets.map(({ name, id }, idx: number) => ({ id, name, minerAddress: minerAddresses[idx] }))],
Expand All @@ -109,6 +113,7 @@ export default class AppController {
syncedBlockNumber,
connectionStatus,
codeHash,
skipDataAndType,
}

return { status: ResponseCode.Success, result: initState }
Expand Down
2 changes: 2 additions & 0 deletions packages/neuron-wallet/src/controllers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ import NetworksController from './networks'
import WalletsController from './wallets'
import TransactionsController from './transactions'
import SyncInfoController from './sync-info'
import SkipDataAndTypeController from './skip-data-and-type'

export default {
AppController,
NetworksController,
WalletsController,
TransactionsController,
SyncInfoController,
SkipDataAndTypeController,
}
7 changes: 7 additions & 0 deletions packages/neuron-wallet/tests-e2e/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,10 @@ describe('Test address book', () => {
afterAll(() => app.stop())
tests.AddressBook(app)
})

describe('Test general settings', () => {
let app = new Application()
beforeAll(() => app.start())
afterAll(() => app.stop())
tests.GeneralSettings(app)
})
40 changes: 40 additions & 0 deletions packages/neuron-wallet/tests-e2e/tests/generalSettings.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import Application from '../application'

/**
* 1. navigate to the general settings handleViewError
* 2. the toggle of address book and skip data and type script should be off
* 3. click on the toggles and their statuses should be updated
* 4. refresh the view and the statuses should be preserved
*/
export default (app: Application) => {
beforeAll(async () => {
app.clickMenu(['Electron', 'Preferences...'])
await app.waitUntilLoaded()
})

describe('Test general settings', () => {
app.test('Check the initialized statuses', async () => {
const { client } = app.spectron
const toggles = await client.$$('button[role=switch]')
expect(toggles).toHaveLength(2)
expect((await client.elementIdAttribute(toggles[0].value.ELEMENT, 'aria-checked')).value).toBe('false')
expect((await client.elementIdAttribute(toggles[1].value.ELEMENT, 'aria-checked')).value).toBe('true')
})

app.test('toggle AddressBook to true and SkipDataAndType to false', async () => {
const { client } = app.spectron
await app.waitUntilLoaded()
const toggles = await client.$$('button[role=switch]')

toggles.forEach((_, idx) => {
client.elementIdClick(toggles[idx].value.ELEMENT)
})

await app.waitUntilLoaded()
expect((await client.elementIdAttribute(toggles[0].value.ELEMENT, 'aria-checked')).value).toBe('true')
expect((await client.elementIdAttribute(toggles[1].value.ELEMENT, 'aria-checked')).value).toBe('false')
})

test.skip('Toggle statuses should be preserved', async () => {})
})
}
2 changes: 2 additions & 0 deletions packages/neuron-wallet/tests-e2e/tests/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import Transaction from './transaction'
import SendTransaction from './sendTransaction'
import Notification from './notification'
import AddressBook from './addressBook'
import GeneralSettings from './generalSettings'

export default {
Wallet,
Expand All @@ -12,4 +13,5 @@ export default {
SendTransaction,
Notification,
AddressBook,
GeneralSettings,
}

0 comments on commit 9d71ee3

Please sign in to comment.