add LiquidModel

add with-observables
This commit is contained in:
Barry Gitarts 2019-01-18 12:03:17 -05:00
parent 4987d8057d
commit c45893cf09
5 changed files with 45 additions and 10 deletions

View File

@ -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 }) => (
<FundingContext.Consumer>
{({ account }) =>
<Fragment>
@ -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 }) => (
</FundingContext.Consumer>
)
export default memo(FunderProfilesTable)
export default withDatabase(withObservables([], ({ database }) => ({
profiles: database.collections.get('profiles').query().fetch(),
}))(FunderProfilesTable))

View File

@ -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
}

25
app/utils/models.js Normal file
View File

@ -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
}
}

View File

@ -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",

View File

@ -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"