Skip to content

Instantly share code, notes, and snippets.

@mattmccray
Last active October 16, 2019 18:19
Show Gist options
  • Save mattmccray/9f88cd9003721af6cb9e3c2adcf3c222 to your computer and use it in GitHub Desktop.
Save mattmccray/9f88cd9003721af6cb9e3c2adcf3c222 to your computer and use it in GitHub Desktop.
Example "Smart Component"
import React from 'react'
import { UIController, useController } from '../core'
import { stylesheet } from '../theme'
import { DomainObjectList } from 'fp-api'
type EmailAddressListProps = {
customerId: number
}
class EmailAddressListController extends UIController<EmailAddressListProps> {
emails: DomainObjectList<CustomerEmailAddress>
async initialize() {
const { customerId } = this.params
const { rest } = this.services
this.emails = await rest.fetchList<CustomerEmailAddress>("CustomerEmailAddress", { customerId })
}
}
export const EmailAddressList = (props: EmailAddressListProps) => {
const ctrl = useController(EmailAddressListController, props)
return ctrl.render(() => (
<div className={CSS.EmailAddressList}>
{ctrl.emails.map(email => (<EmailListItem email={email} key={email.emailAddressId} />))}
</div>
))
}
export default EmailAddressList
const CSS = stylesheet({
EmailAddressList: {},
})
/**
We could have different form factor views in the same Smart Component, if/when it makes sense.
*/
import React from 'react'
import { UIController, useController } from '../core'
import { stylesheet } from '../theme'
import { DomainObjectList } from 'fp-api'
type EmailAddressListProps = {
customerId: number
}
class EmailAddressListController extends UIController<EmailAddressListProps> {
emails: DomainObjectList<CustomerEmailAddress>
async initialize() {
const { customerId } = this.params
const { rest } = this.services
this.emails = await rest.fetchList<CustomerEmailAddress>("CustomerEmailAddress", { customerId })
}
}
export const EmailAddressList = (props: EmailAddressListProps) => {
const ctrl = useController(EmailAddressListController, props)
return ctrl.render(() => (
<div className={CSS.EmailAddressList}>
{ctrl.emails.map(email => (<EmailListItem email={email} key={email.emailAddressId} />))}
</div>
))
}
export const EmailAddressListMobile = (props: EmailAddressListProps) => {
const ctrl = useController(EmailAddressListController, props)
return ctrl.render(() => (
<ion-list className={CSS.EmailAddressListMobile}>
{ctrl.emails.map(email => (<EmailListItemMobile email={email} key={email.emailAddressId} />))}
</ion-list>
))
}
export default EmailAddressList
const CSS = stylesheet({
EmailAddressList: {},
EmailAddressListMobile: {},
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment