Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Request] atomFamily with param and optional value #393

Closed
devnode opened this issue Jun 24, 2020 · 1 comment
Closed

[Request] atomFamily with param and optional value #393

devnode opened this issue Jun 24, 2020 · 1 comment
Labels
duplicate This issue or pull request already exists

Comments

@devnode
Copy link

devnode commented Jun 24, 2020

#330 touched on this subject but the solution there doesn't really work if you have multiple data sources.

For example, what I'm working on right now, has state for Quotes and Orders. Both have Products and all the logic associated with Products is identical between them.

It would be convenient if atomFamily (or a new util) accepted an optional, non-memoized, value to be used for initialization:

export const products = atomFamily({
  key: 'products',
  default: (key, value) => {
    let out = []
    value.forEach((v, i) => {
      const rkey = `${key}/product/${i}`
      
      // create individual atoms for the mutable fields
      productQuantity(rkey, v.quantity)
      productPrice(rkey, v.price)

      // add rkey to each product for later reference
      out.push({ ...v, rkey })
    })
    return out
  }
})

Here's a simplified overview of what I'm doing now:

export const products = memoize(
  (rkey: string, value?: Array<Product>) => {
    return atom({
      key: `${rkey}/products`,
      default: value || [],
    })
  },
  { length: 1 }
)

export const quoteData = atomFamily<Quote, number>({
  key: 'quoteData',
  default: selectorFamily<Quote, number>({
    key: 'quoteData/Default',
    get: (id) => async () => {
      const resp = await fetch(`/api/quotes/${id}`)
      if (!resp.ok) {
        throw `${resp.status} - ${resp.statusText}`
      }
      let data: Quote = await resp.json()

      const rkey = `quote/${id}`
      data.rkey = rkey
      products(rkey, data.products)

      return data
    },
  }),
})

export const orderData = atomFamily<Order, number>({
  key: 'orderData',
  default: selectorFamily<Order, number>({
    key: 'orderData/Default',
    get: (id) => async () => {
      const resp = await fetch(`/api/orders/${id}`)
      if (!resp.ok) {
        throw `${resp.status} - ${resp.statusText}`
      }
      let data: Order = await resp.json()

      const rkey = `order/${id}`
      data.rkey = rkey
      products(rkey, data.products)

      return data
    },
  }),
})

function ListProducts({ rkey }) {
  const results = useRecoilValue(products(rkey))
  ...
}
@drarmstr drarmstr added the duplicate This issue or pull request already exists label Jun 24, 2020
@drarmstr
Copy link
Contributor

@devnode - Please see the discussion in #231

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
duplicate This issue or pull request already exists
Projects
None yet
Development

No branches or pull requests

2 participants