From c45893cf09718d827f4770f75d81a2c15e1da701 Mon Sep 17 00:00:00 2001 From: Barry Gitarts Date: Fri, 18 Jan 2019 12:03:17 -0500 Subject: [PATCH] add LiquidModel add with-observables --- app/components/FunderProfilesTable.jsx | 16 ++++++++++------ app/model/profiles.js | 6 ++---- app/utils/models.js | 25 +++++++++++++++++++++++++ package.json | 1 + yarn.lock | 7 +++++++ 5 files changed, 45 insertions(+), 10 deletions(-) create mode 100644 app/utils/models.js diff --git a/app/components/FunderProfilesTable.jsx b/app/components/FunderProfilesTable.jsx index f046b24..edf6651 100644 --- a/app/components/FunderProfilesTable.jsx +++ b/app/components/FunderProfilesTable.jsx @@ -1,6 +1,8 @@ -import React, { Fragment, memo } from 'react' +import React, { Fragment } from 'react' import MaterialTable from 'material-table' import LiquidPledging from 'Embark/contracts/LiquidPledging' +import withObservables from '@nozbe/with-observables' +import { withDatabase } from '@nozbe/watermelondb/DatabaseProvider' import { FundingContext } from '../context' const { cancelProject } = LiquidPledging.methods @@ -8,11 +10,11 @@ const { cancelProject } = LiquidPledging.methods const convertToHours = seconds => seconds / 60 / 60 const cancelText = canceled => canceled ? 'Yes' : 'No' const formatField = field => ({ - ...field, + ...field.getFields(), commitTime: convertToHours(field.commitTime), canceled: cancelText(field.canceled) }) -const FunderProfilesTable = ({ data, cancelFundProfile }) => ( +const FunderProfilesTable = ({ data, cancelFundProfile, profiles }) => ( {({ account }) => @@ -26,13 +28,13 @@ const FunderProfilesTable = ({ data, cancelFundProfile }) => ( { title: 'Type', field: 'type' }, { title: 'Canceled', field: 'canceled' } ]} - data={data.map(formatField)} + data={profiles.map(formatField)} title="Funding Profiles" options={{ showEmptyDataSourceMessage: true }} actions={[ rowData => ({ icon: 'cancel', - disabled: rowData.addr.toLowerCase() != account.toLowerCase(), + disabled: !account || rowData.addr.toLowerCase() != account.toLowerCase(), tooltip: 'Cancel', onClick: (event, rowData) => { cancelProject(rowData.idProject || rowData.idProfile) @@ -50,4 +52,6 @@ const FunderProfilesTable = ({ data, cancelFundProfile }) => ( ) -export default memo(FunderProfilesTable) +export default withDatabase(withObservables([], ({ database }) => ({ + profiles: database.collections.get('profiles').query().fetch(), +}))(FunderProfilesTable)) diff --git a/app/model/profiles.js b/app/model/profiles.js index c97a9dc..58c1324 100644 --- a/app/model/profiles.js +++ b/app/model/profiles.js @@ -1,9 +1,8 @@ -import { Model } from '@nozbe/watermelondb' import { field } from '@nozbe/watermelondb/decorators' +import { LiquidModel } from '../utils/models' -export default class Profiles extends Model { - +export default class Profiles extends LiquidModel { static table = 'profiles' @field('addr') addr @@ -13,5 +12,4 @@ export default class Profiles extends Model { @field('name') name @field('url') url @field('id_profile') idProfile - } diff --git a/app/utils/models.js b/app/utils/models.js new file mode 100644 index 0000000..56ef35f --- /dev/null +++ b/app/utils/models.js @@ -0,0 +1,25 @@ +import { Model } from '@nozbe/watermelondb' + +export function getFields(obj) { + const validTypes = new Set(['string', 'number', 'boolean']) + const newObj = {} + const proto = Object.getPrototypeOf(obj) + const names = Object.getOwnPropertyNames(proto) + names + .filter(name => validTypes.has(typeof obj[name])) + .forEach(name => { newObj[name] = obj[name] }) + return newObj +} + +export class LiquidModel extends Model { + getFields() { + const validTypes = new Set(['string', 'number', 'boolean']) + const newObj = {} + const proto = Object.getPrototypeOf(this) + const names = Object.getOwnPropertyNames(proto) + names + .filter(name => validTypes.has(typeof this[name])) + .forEach(name => { newObj[name] = this[name] }) + return newObj + } +} diff --git a/package.json b/package.json index 7d9181e..4c2e1c8 100644 --- a/package.json +++ b/package.json @@ -58,6 +58,7 @@ "@material-ui/core": "^3.6.0", "@material-ui/icons": "^3.0.1", "@nozbe/watermelondb": "^0.9.0", + "@nozbe/with-observables": "^1.0.2", "async": "^2.4.0", "chai": "^4.1.0", "chart.js": "^2.7.3", diff --git a/yarn.lock b/yarn.lock index 0cadff2..0d7c253 100644 --- a/yarn.lock +++ b/yarn.lock @@ -331,6 +331,13 @@ rxjs-compat "^6.3.2" sql-escape-string "^1.1.0" +"@nozbe/with-observables@^1.0.2": + version "1.0.2" + resolved "https://registry.yarnpkg.com/@nozbe/with-observables/-/with-observables-1.0.2.tgz#9749b3b5d33a058f8aed92d75138a5778bead89b" + integrity sha512-p9WNGTUm0eKb28ylcMBayUgBbzGoZ2bVBxzqtS5SxRL3Hopwf1eSNF+fGbC8fX1YHxhQjma4nN4pkKAt+H6NLA== + dependencies: + rxjs "^6.2.2" + "@types/jss@^9.5.6": version "9.5.7" resolved "https://registry.yarnpkg.com/@types/jss/-/jss-9.5.7.tgz#fa57a6d0b38a3abef8a425e3eb6a53495cb9d5a0"