-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from devfreddy/wip-rollup-multi-config
Alpha with rollupjs
- Loading branch information
Showing
17 changed files
with
1,355 additions
and
51 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
# AccountDropdown | ||
|
||
## Description | ||
|
||
Applications (Nerdpacks) developed in New Relic One are limited (for security purposes) to data in the account they are installed (subscribed) to and hierarchically to any child accounts. | ||
|
||
Often the goal is to limit the view by a given account to ensure accurate context. | ||
|
||
This component provides a common interface for choosing an account with a callback (`onSelect`) that allows for integration into the rest of your application. | ||
|
||
## Installation | ||
|
||
1. Install nr1-community | ||
|
||
```bash | ||
npm i nr1-community | ||
``` | ||
|
||
2. Install peer dependencies | ||
|
||
```bash | ||
npm i moment react-moment | ||
``` | ||
|
||
3. Import styles | ||
|
||
Add: | ||
|
||
```scss | ||
@import '~nr1-community/dist/components/AccountDropdown.css'; | ||
``` | ||
|
||
to your `styles.scss` | ||
|
||
## Usage | ||
|
||
```jsx | ||
import { AccountDropdown } from 'nr1-community'; | ||
|
||
render () { | ||
<AccountDropdown></AccountDropdown> | ||
} | ||
``` | ||
|
||
## Props | ||
|
||
```jsx | ||
onSelect: PropTypes.func, | ||
urlState: PropTypes.object, | ||
className: PropTypes.string, | ||
style: PropTypes.object, | ||
title: PropTypes.string | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,176 @@ | ||
import React from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import { | ||
navigation, | ||
UserStorageQuery, | ||
UserStorageMutation, | ||
AccountsQuery, | ||
Dropdown, | ||
DropdownItem, | ||
Spinner | ||
} from 'nr1'; | ||
|
||
import styles from './styles.scss'; | ||
|
||
const collection = 'nr1-community:AccountDropdown'; | ||
const documentId = 'default-account'; | ||
|
||
export class AccountDropdown extends React.Component { | ||
static propTypes = { | ||
onSelect: PropTypes.func, | ||
urlState: PropTypes.object, | ||
className: PropTypes.string, | ||
style: PropTypes.object, | ||
title: PropTypes.string | ||
}; | ||
|
||
static defaultProps = { | ||
title: 'Select account...' | ||
}; | ||
|
||
constructor(props) { | ||
super(props); | ||
|
||
this.state = { | ||
accounts: null, | ||
defaultAccount: undefined, | ||
selected: null | ||
}; | ||
|
||
this.select = this.select.bind(this); | ||
} | ||
|
||
async componentDidMount() { | ||
await Promise.all([this.loadAccounts(), this.loadDefaultAccount()]); | ||
|
||
this.setState(state => { | ||
if (this.props.urlState && this.props.urlState.account) { | ||
const account = this.state.accounts.find( | ||
account => account.id === this.props.urlState.account | ||
); | ||
if (account) { | ||
return { | ||
selectedFromUrlState: true, | ||
selected: account | ||
}; | ||
} | ||
} | ||
|
||
if (state.selected === null && state.defaultAccount && state.accounts) { | ||
const account = this.state.accounts.find( | ||
account => account.id === this.state.defaultAccount | ||
); | ||
if (account) { | ||
return { | ||
selected: account | ||
}; | ||
} | ||
} | ||
|
||
return null; | ||
}); | ||
} | ||
|
||
async componentDidUpdate(prevProps, prevState) { | ||
const prevAccount = prevState.selected; | ||
const account = this.state.selected; | ||
|
||
if (account && (!prevAccount || account.id !== prevAccount.id)) { | ||
this.props.onSelect(account); | ||
|
||
if (!this.state.selectedFromUrlState) { | ||
if (this.state.selected.id !== this.state.defaultAccount) { | ||
this.updateDefaultAccount(this.state.selected); | ||
} | ||
|
||
if ( | ||
this.props.urlState && | ||
this.state.selected.id !== this.props.urlState.account | ||
) { | ||
navigation.setUrlState({ | ||
account: this.state.selected.id | ||
}); | ||
} | ||
} | ||
} | ||
} | ||
|
||
async loadDefaultAccount() { | ||
const result = await UserStorageQuery.query({ collection, documentId }); | ||
const id = | ||
((((result.data || {}).actor || {}).nerdStorage || {}).document || {}) | ||
.id || null; | ||
this.setState(() => ({ | ||
defaultAccount: id | ||
})); | ||
} | ||
|
||
async loadAccounts() { | ||
// eslint-disable-next-line no-unused-vars | ||
const { loading, data, errors } = await AccountsQuery.query(); | ||
|
||
if (!data) { | ||
// TO DO | ||
} | ||
|
||
if (errors) { | ||
// TO DO | ||
} | ||
|
||
this.setState({ | ||
accounts: data | ||
}); | ||
} | ||
|
||
async updateDefaultAccount(account) { | ||
await UserStorageMutation.mutate({ | ||
actionType: UserStorageMutation.ACTION_TYPE.WRITE_DOCUMENT, | ||
collection, | ||
documentId, | ||
document: { id: account.id } | ||
}); | ||
|
||
this.setState({ | ||
defaultAccount: account.id | ||
}); | ||
} | ||
|
||
select(account) { | ||
this.setState(state => { | ||
if (!state.selected || state.selected.id !== account.id) { | ||
return { | ||
selectedFromUrlState: false, | ||
selected: account | ||
}; | ||
} | ||
|
||
return {}; | ||
}); | ||
} | ||
|
||
render() { | ||
// eslint-disable-next-line no-unused-vars | ||
const { className, style, title } = this.props; | ||
const { accounts, defaultAccount, selected } = this.state; | ||
|
||
if (!accounts || defaultAccount === undefined) { | ||
return <Spinner />; | ||
} | ||
|
||
const items = accounts.map(account => ( | ||
<DropdownItem key={account.id} onClick={() => this.select(account)}> | ||
{account.name} | ||
</DropdownItem> | ||
)); | ||
|
||
return ( | ||
<Dropdown | ||
title={(selected || {}).name || title} | ||
className={styles.big} | ||
style={style} | ||
> | ||
{items} | ||
</Dropdown> | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
.big { | ||
min-height: 150px; | ||
} |
Oops, something went wrong.