{"size": 921, "ext": "js", "max_stars_repo_path": "src/hooks/use-dappy-templates.hook.js", "max_stars_repo_name": "JOebin/crypto-dappy", "max_stars_repo_stars_event_min_datetime": "2021-07-23T22:44:21.000Z", "max_stars_repo_stars_event_max_datetime": "2022-03-29T12:39:51.000Z", "max_issues_repo_path": "src/hooks/use-dappy-templates.hook.js", "max_issues_repo_name": "JOebin/crypto-dappy", "max_issues_repo_issues_event_min_datetime": "2021-07-26T23:18:36.000Z", "max_issues_repo_issues_event_max_datetime": "2022-02-02T15:10:55.000Z", "max_forks_repo_path": "src/hooks/use-dappy-templates.hook.js", "max_forks_repo_name": "JOebin/crypto-dappy", "max_forks_repo_forks_event_min_datetime": "2021-07-30T16:18:23.000Z", "max_forks_repo_forks_event_max_datetime": "2022-03-30T05:48:10.000Z"}
{"max_stars_count": 72, "max_issues_count": 9, "max_forks_count": 50, "avg_line_length": 31.7586206897, "max_line_length": 98, "alphanum_fraction": 0.6319218241}
import { useEffect, useReducer } from 'react'
import { query } from '@onflow/fcl'

import { LIST_DAPPY_TEMPLATES } from '../flow/list-dappy-templates.script'
import { defaultReducer } from '../reducer/defaultReducer'
import DappyClass from '../utils/DappyClass'

export default function useDappyTemplates() {
  const [state, dispatch] = useReducer(defaultReducer, { loading: false, error: false, data: [] })

  useEffect(() => {
    const fetchDappyTemplates = async () => {
      dispatch({ type: 'PROCESSING' })
      try {
        let res = await query({ cadence: LIST_DAPPY_TEMPLATES })
        let mappedDappies = Object.values(res).map(d => {
          return new DappyClass(d?.templateID, d?.dna, d?.name, d?.price)
        })
        dispatch({ type: 'SUCCESS', payload: mappedDappies })
      } catch (err) {
        dispatch({ type: 'ERROR' })
      }
    }
    fetchDappyTemplates()
  }, [])

  return state
}
