InstrumentList.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. import gql from 'graphql-tag'
  2. import { Query } from 'react-apollo'
  3. import Link from 'next/link'
  4. import { InstrumentForm } from './Instrument'
  5. const INSTRUMENTS = gql`
  6. query INSTRUMENTS {
  7. instruments {
  8. id
  9. name
  10. description
  11. }
  12. }
  13. `
  14. const InstrumentList = props => (
  15. <Query query={INSTRUMENTS}>
  16. {({ data, loading, error }) => {
  17. const content = [
  18. <h1>Instrument List</h1>
  19. ]
  20. if (loading) content.push(<p>Loading instruments...</p>)
  21. else if (error) content.push(<p>Error loading instruments: {error.message}</p>)
  22. else if (data.instruments.length > 0) content.push(
  23. <ul>
  24. {data.instruments.map(instrument => (
  25. <li key={instrument.id} title={instrument.description}>
  26. <Link href={{ pathname: 'instruments', query: { id: instrument.id } }}>
  27. <a>{instrument.name}</a>
  28. </Link>
  29. </li>
  30. ))}
  31. </ul>
  32. )
  33. else content.push(<p>No instruments found.</p>)
  34. content.push(<InstrumentForm />)
  35. return content
  36. }}
  37. </Query>
  38. )
  39. export default InstrumentList