Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
fix: Fix error when loading, prettify all codebase
  • Loading branch information
Miguel Jimenez Esun committed Sep 18, 2019
1 parent 0bccbf1 commit 69c0e63
Show file tree
Hide file tree
Showing 24 changed files with 1,057 additions and 871 deletions.
48 changes: 21 additions & 27 deletions .eslintrc.js
@@ -1,29 +1,23 @@
module.exports = {
"env": {
"browser": true,
"es6": true
env: {
browser: true,
es6: true,
},
extends: ['prettier', 'prettier/react'],
globals: {
Atomics: 'readonly',
SharedArrayBuffer: 'readonly',
},
parser: 'babel-eslint',
parserOptions: {
ecmaFeatures: {
jsx: true,
},
"extends": [
"prettier",
"prettier/react",
],
"globals": {
"Atomics": "readonly",
"SharedArrayBuffer": "readonly"
},
"parser": "babel-eslint",
"parserOptions": {
"ecmaFeatures": {
"jsx": true
},
"ecmaVersion": 2018,
"sourceType": "module"
},
"plugins": [
"react",
"prettier"
],
"rules": {
"prettier/prettier": "error"
}
};
ecmaVersion: 2018,
sourceType: 'module',
},
plugins: ['react', 'prettier'],
rules: {
'prettier/prettier': 'error',
},
};
6 changes: 3 additions & 3 deletions .prettierrc.js
@@ -1,4 +1,4 @@
module.exports = {
singleQuote: true,
trailingComma: "es5",
};
singleQuote: true,
trailingComma: 'es5',
};
182 changes: 94 additions & 88 deletions components/account-picker.js
@@ -1,106 +1,112 @@
import React from 'react';
import PropTypes from 'prop-types';

import { AccountsQuery, Dropdown, DropdownItem, UserStorageMutation, UserStorageQuery } from 'nr1';

const USER_ACCOUNT_COLLECTION = 'user_account_collection_v0';
import {
AccountsQuery,
Dropdown,
DropdownItem,
UserStorageMutation,
UserStorageQuery,
} from 'nr1';

const USER_ACCOUNT_COLLECTION = 'user_account_collection_v1';
const USER_SELECTED_ACCOUNT_ID = 'user_account_id';

export default class AccountPicker extends React.Component {
static propTypes = {
hostname: PropTypes.string,
refreshRate: PropTypes.number
static propTypes = {
hostname: PropTypes.string,
refreshRate: PropTypes.number,
};

constructor(props) {
super(props);
this.state = {
selectedAccount: { name: '' },
accounts: [],
filter: '',
};
this.onAccountChange = this.onAccountChange.bind(this);
}

constructor(props) {
super(props);
this.state = {
selectedAccount: { name: '' },
accounts: [],
filter: ''
}
this.onAccountChange = this.onAccountChange.bind(this);
}

async componentDidMount() {
const accountsResults = await AccountsQuery.query({});

if (accountsResults.data && accountsResults.data) {
const accounts = accountsResults.data;
this.setState({ accounts });
async componentDidMount() {
const accountsResults = await AccountsQuery.query({});

let accountId = await this.getLastChoseAccountId();
if (!accountId) {
accountId = accounts[0].id;
}
if (accountsResults.data && accountsResults.data) {
const accounts = accountsResults.data;
this.setState({ accounts });

const account = accounts.find(a => a.id === accountId);
let accountId = await this.getLastChoseAccountId();
if (!accountId) {
accountId = accounts[0].id;
}

if (account) {
this._accountChanged(account);
}
}
}

async getLastChoseAccountId() {
const userStorageQuery = {
collection: USER_ACCOUNT_COLLECTION,
documentId: USER_SELECTED_ACCOUNT_ID
}
// TODO: Add error handling
const queryResults = await UserStorageQuery.query(userStorageQuery);
return queryResults.data
}
const account = accounts.find(a => a.id === accountId);

async saveOffLastChosenAccountId(accountId) {
const userMutation = {
actionType: UserStorageMutation.ACTION_TYPE.WRITE_DOCUMENT,
collection: USER_ACCOUNT_COLLECTION,
document: accountId,
documentId: USER_SELECTED_ACCOUNT_ID
}
UserStorageMutation.mutate(userMutation);
if (account) {
this._accountChanged(account);
}
}
}

async _accountChanged(account){
const accountId = account.id;
const { accountChangedCallback } = this.props;
this.saveOffLastChosenAccountId(accountId);
if (accountChangedCallback) {
await accountChangedCallback(accountId, this.state.accounts);
}
this.setState({ selectedAccount: account });
async getLastChoseAccountId() {
const userStorageQuery = {
collection: USER_ACCOUNT_COLLECTION,
documentId: USER_SELECTED_ACCOUNT_ID,
};
// TODO: Add error handling
const queryResults = await UserStorageQuery.query(userStorageQuery);
return queryResults.data;
}

async saveOffLastChosenAccountId(accountId) {
const userMutation = {
actionType: UserStorageMutation.ACTION_TYPE.WRITE_DOCUMENT,
collection: USER_ACCOUNT_COLLECTION,
document: { account: accountId },
documentId: USER_SELECTED_ACCOUNT_ID,
};
UserStorageMutation.mutate(userMutation);
}

async _accountChanged(account) {
const accountId = account.id;
const { accountChangedCallback } = this.props;
this.saveOffLastChosenAccountId(accountId);
if (accountChangedCallback) {
await accountChangedCallback(accountId, this.state.accounts);
}

async onAccountChange(account) {
await this._accountChanged(account);
this.setState({ selectedAccount: account });
}

async onAccountChange(account) {
await this._accountChanged(account);
}

render() {
const { accounts, filter, selectedAccount } = this.state;

let filteredAccounts = [...accounts];
if (filter && filter.length > 0) {
const re = new RegExp(filter, 'i');
filteredAccounts = accounts.filter(a => {
return a.name.match(re);
});
}

render() {
const { accounts, filter, selectedAccount } = this.state;

let filteredAccounts = [...accounts];
if (filter && filter.length > 0) {
const re = new RegExp(filter, 'i');
filteredAccounts = accounts.filter(a => {
return a.name.match(re);
});
}

return (
<Dropdown
title={selectedAccount.name}
search={filter}
onSearch={event => { this.setState({ filter: event.target.value })}}
>
{filteredAccounts.map(a =>
<DropdownItem key={a.id} onClick={() => this.onAccountChange(a)}>
{a.name}
</DropdownItem>
)}
</Dropdown>
);
}
return (
<Dropdown
title={selectedAccount.name}
search={filter}
onSearch={event => {
this.setState({ filter: event.target.value });
}}
>
{filteredAccounts.map(a => (
<DropdownItem key={a.id} onClick={() => this.onAccountChange(a)}>
{a.name}
</DropdownItem>
))}
</Dropdown>
);
}
}


0 comments on commit 69c0e63

Please sign in to comment.