Ignore:
Timestamp:
09/11/22 18:03:58 (22 months ago)
Author:
Marko <Marko@…>
Branches:
master
Children:
775e15e
Parents:
527b93f
Message:

Prototype version

File:
1 edited

Legend:

Unmodified
Added
Removed
  • phonelux-frontend/src/components/PhoneCardGridComponent/PhoneCardGridComponent.js

    r527b93f re5b84dc  
    1 import { Grid } from '@mui/material'
     1import { Grid, Pagination } from '@mui/material'
    22import axios from 'axios'
    33import React, { Component } from 'react'
    4 import PaginationComponent from '../PaginationComponent/PaginationComponent'
    54import '../PhoneCardComponent/PhoneCardComponent'
    65import PhoneCardComponent from '../PhoneCardComponent/PhoneCardComponent'
     
    1514    this.state = {
    1615      phones: [],
    17       loading: true,
    1816      currentPage: 1,
    1917      phonesPerPage: 12,
     
    2422  }
    2523
     24  componentDidUpdate(prevProps) {
     25    if(JSON.stringify(prevProps) != JSON.stringify(this.props)){
     26      let filters = '?'
     27
     28      if(this.props.shops)
     29      {
     30        filters += 'shops='+this.props.shops+'&'
     31      }
     32      if(this.props.brands)
     33      {
     34        filters += 'brands='+this.props.brands+'&'
     35      }
     36      if(this.props.priceRange)
     37      {
     38        filters += 'priceRange='+this.props.priceRange+'&'
     39      }
     40      if(this.props.searchValue)
     41      {
     42        filters += 'searchValue='+this.props.searchValue+'&'
     43      }
     44
     45      if(this.props.sortBy)
     46      {
     47        filters += 'sortBy='+this.props.sortBy+'&'
     48      }
     49
     50      axios.get('/phones'+filters)
     51      .then(response => {
     52        this.setState({
     53          phones: response.data,
     54          numberOfPages: Math.ceil(response.data.length / this.state.phonesPerPage)
     55        },(e) => this.setNewPage(e,this.state.currentPage))
     56      }
     57      )
     58      .catch(error => console.log(error))
     59      console.log(filters)
     60    }
     61  }
     62
    2663  componentDidMount() {
    27 
    2864    axios.get('/phones')
    2965      .then(response => {
    3066        this.setState({
    3167          phones: response.data,
    32           loading: false,
    3368          numberOfPages: Math.ceil(response.data.length / this.state.phonesPerPage)
    34         },() => this.setNewPage(this.state.currentPage))
     69        },(e) => this.setNewPage(e,this.state.currentPage))
    3570      }
    3671      )
     
    3873  }
    3974
    40   setNewPage = (newPage) => {
    41     if(newPage == '')
    42     {
    43       newPage = this.state.currentPage-1
    44     }
    4575
    46     if(newPage == '')
    47     {
    48       newPage = this.state.currentPage+1
    49     }
     76  setNewPage = (event,page) => {
    5077
    51 
    52     const indexOfLastPhone = parseInt(newPage) * this.state.phonesPerPage;
     78    const indexOfLastPhone = parseInt(page) * this.state.phonesPerPage;
    5379    const indexOfFirstPhone = indexOfLastPhone - this.state.phonesPerPage;
    5480
     
    5682
    5783    this.setState({
    58       currentPage: parseInt(newPage),
     84      currentPage: parseInt(page),
    5985      currentPhones: currPhones
    6086    })
     
    6389
    6490  render() {
     91
    6592    return (
    6693      <div className='phonecardgrid-wrapper'>
     
    7198       spacing={2}>
    7299
    73         {this.state.currentPhones.map((phone) => <Grid className='phonecardgrid-item' item md={3}>
    74           <PhoneCardComponent key={phone.id} id={phone.id} brand={phone.brand}
     100        {this.state.currentPhones.map((phone,idx) => <Grid key={idx} className='phonecardgrid-item' item md={3}>
     101          <PhoneCardComponent id={phone.id} brand={phone.brand}
    75102          model={phone.model} image_url={phone.image_url == null ? phoneImage : phone.image_url} total_offers={phone.total_offers} lowestPrice={phone.lowestPrice}/></Grid>)}
    76103
    77         {/* <Grid item xs={12} md={12}>
    78         <PaginationComponent
    79           currentPage={this.state.currentPage}
    80           changePageHandler={this.setNewPage}
    81           numberOfPages={this.state.numberOfPages} />
    82           </Grid> */}
    83104      </Grid>
    84       <PaginationComponent
    85           currentPage={this.state.currentPage}
    86           changePageHandler={this.setNewPage}
    87           numberOfPages={this.state.numberOfPages} />
     105
     106      <div className='pagination-wrapper'>
     107         <Pagination className='paginationcomponent-pagination' onChange={this.setNewPage} page={this.state.currentPage}
     108          count={this.state.numberOfPages} color="primary" />
     109      </div>
     110
    88111      </div>
    89112    )
Note: See TracChangeset for help on using the changeset viewer.