J
J
Justique2018-08-28 09:28:55
React
Justique, 2018-08-28 09:28:55

Apollo Service gql query by state value?

Good afternoon, I’m just starting to learn react and I ran into such a problem that I don’t quite understand how to transfer a value from state to gql?

class GalleriesPage extends Component {

  modal = {
    openPortal : () => null
  };

  state = {
    modalTitle           : '',
    currentLightboxIndex : 0
  };

  showPortal = (index, title) => () => {
    this.setState({
      modalTitle : title,
      currentLightboxIndex : index
    }, () => {
      setTimeout(() => {
        this.props.galleryData.updateQuery({ variables: { id: this.state.currentLightboxIndex }});
        this.modal.openPortal();
      }, 500)
    })
  };

  closeLightbox = (index) => () => {
    this.modal.closePortal();

  };

  render() {
    const { galleriesData : { albums } } = this.props;
    console.log(this.props);
    const activeAlbumId = this.state.currentLightboxIndex;

    return (
      <div className="galleries-page__galleries">
      {!!albums && (
      <div className="max-width">
      <div className="galleries-header">
        <h2 className="galleries-title"><FormattedMessage id="galleries.title"/></h2>
        <SocialsInfo/>
      </div>
      <div className="galleries-list" id="galleries-list">
       
        {albums.edges.map((item, i) => {
         
          return (
              <div className="galleries-list__item" 
                onClick={this.showPortal(item.node.id, item.node.title)}
                style={{ backgroundImage : `url(${item.node.photos[0].url})` }} 
                key={i}
              >
                    <div className="galleries-list__item_header">
                        <span>{item.node.count} <FormattedMessage id="galleries.photo_count"/></span>
                    </div>
                    <div className="galleries-list__item_footer">
                        <div className="galleries-list__item_title">
                            {item.node.title}
                        </div>
                    </div>
          </div>
            
          )
        })}
      </div>
        <div id="gallery-list">
      <GalleryPortal  portalRef={ref => this.modal = ref}>
            
        <div>
          
          <div className="gallery-header">
              <div className="max-width">
                  <h2 className="gallery-title">
                      {this.state.modalTitle}
                  </h2>
                  <div className="gallery-share">
                      <div className="button-go-to" onClick={this.closeLightbox()}>
                          <Icon name="icon-chevron-circle-left"/>
                          <span className="button-go-to__text">
                            <FormattedMessage id="gallery_page.back_to"/>
                          </span>
                      </div>
                      <SocialsInfo/>
                  </div>
              </div>
              <ProgressBar />
          </div>

          <div className="gallery-backlink" onClick={this.closeLightbox()}>
              <div className="button-go-to">
                  <Icon name="icon-chevron-circle-left"/>
                  <span className="button-go-to__text">
                    <FormattedMessage id="gallery_page.back"/>
                  </span>
              </div>
          </div>
          <div>
      {/*<GalleryPage {...this.props} albumId={activeAlbumId}/>*/}
          </div>
        </div>
          
      </GalleryPortal>

      </div>
      </div>)}
      </div>
  
    )
  }
}

GalleriesPage = compose(
  graphqlLocale(Query.albums.query, {
    name    : 'galleriesData',
    options : props => {
      return {
        variables : {
          first  : 10,
          after  : null,
          before : null,
          last   : null
        }
      }
    }
  }),
  graphqlLocale(Query.album.query, {
    name    : 'galleryData',
    options : props => {
      return {
        variables : {
          id  : null,
        }
      }
    },
  })
)(GalleriesPage); 



export default GalleriesPage;

That is, on click, I'm trying to pass "gallery_id" to variables => id in the state

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question