[e5b84dc] | 1 | import { Grid, Pagination } from '@mui/material'
|
---|
[dbd4834] | 2 | import axios from 'axios'
|
---|
| 3 | import React, { Component } from 'react'
|
---|
| 4 | import '../PhoneCardComponent/PhoneCardComponent'
|
---|
| 5 | import PhoneCardComponent from '../PhoneCardComponent/PhoneCardComponent'
|
---|
| 6 | import './PhoneCardGridComponent.css'
|
---|
| 7 | import phoneImage from '../../images/phone.png'
|
---|
[d66b8eb] | 8 | import SortByComponent from '../FiltersComponents/SortByComponent'
|
---|
[dbd4834] | 9 |
|
---|
| 10 | export class PhoneCardGridComponent extends Component {
|
---|
| 11 |
|
---|
| 12 | constructor(props) {
|
---|
| 13 | super(props)
|
---|
| 14 |
|
---|
| 15 | this.state = {
|
---|
| 16 | phones: [],
|
---|
| 17 | currentPage: 1,
|
---|
| 18 | phonesPerPage: 12,
|
---|
| 19 | numberOfPages: 0,
|
---|
| 20 | currentPhones: []
|
---|
| 21 | }
|
---|
| 22 |
|
---|
| 23 | }
|
---|
| 24 |
|
---|
[d66b8eb] | 25 |
|
---|
| 26 | getQueryString = () => {
|
---|
| 27 | let filters = '?'
|
---|
| 28 | if(localStorage.getItem('shops'))
|
---|
| 29 | {
|
---|
| 30 | filters += 'shops='+localStorage.getItem('shops')+'&'
|
---|
| 31 | }
|
---|
| 32 | if(localStorage.getItem('brands'))
|
---|
| 33 | {
|
---|
| 34 | filters += 'brands='+localStorage.getItem('brands')+'&'
|
---|
| 35 | }
|
---|
| 36 | if(localStorage.getItem('priceRange'))
|
---|
| 37 | {
|
---|
| 38 | filters += 'priceRange='+localStorage.getItem('priceRange')+'&'
|
---|
| 39 | }
|
---|
| 40 |
|
---|
| 41 | if(localStorage.getItem('sortBy'))
|
---|
| 42 | {
|
---|
| 43 | filters += 'sortBy='+localStorage.getItem('sortBy')+'&'
|
---|
| 44 | }
|
---|
| 45 |
|
---|
| 46 | return filters
|
---|
| 47 | }
|
---|
| 48 |
|
---|
[e5b84dc] | 49 | componentDidUpdate(prevProps) {
|
---|
| 50 | if(JSON.stringify(prevProps) != JSON.stringify(this.props)){
|
---|
| 51 | let filters = '?'
|
---|
[dbd4834] | 52 |
|
---|
[e5b84dc] | 53 | if(this.props.shops)
|
---|
| 54 | {
|
---|
| 55 | filters += 'shops='+this.props.shops+'&'
|
---|
| 56 | }
|
---|
| 57 | if(this.props.brands)
|
---|
| 58 | {
|
---|
| 59 | filters += 'brands='+this.props.brands+'&'
|
---|
| 60 | }
|
---|
| 61 | if(this.props.priceRange)
|
---|
| 62 | {
|
---|
| 63 | filters += 'priceRange='+this.props.priceRange+'&'
|
---|
| 64 | }
|
---|
| 65 | if(this.props.searchValue)
|
---|
| 66 | {
|
---|
| 67 | filters += 'searchValue='+this.props.searchValue+'&'
|
---|
| 68 | }
|
---|
| 69 |
|
---|
| 70 | if(this.props.sortBy)
|
---|
| 71 | {
|
---|
| 72 | filters += 'sortBy='+this.props.sortBy+'&'
|
---|
[d66b8eb] | 73 | }
|
---|
| 74 |
|
---|
[436e0da] | 75 | if(this.props.ram)
|
---|
| 76 | {
|
---|
| 77 | filters += 'ram='+this.props.ram+'&'
|
---|
| 78 | }
|
---|
[d66b8eb] | 79 |
|
---|
[436e0da] | 80 | if(this.props.rom)
|
---|
| 81 | {
|
---|
| 82 | filters += 'rom='+this.props.rom+'&'
|
---|
| 83 | }
|
---|
| 84 |
|
---|
| 85 | if(this.props.frontcamera)
|
---|
| 86 | {
|
---|
| 87 | filters += 'frontcamera='+this.props.frontcamera+'&'
|
---|
| 88 | }
|
---|
| 89 |
|
---|
| 90 | if(this.props.backcamera)
|
---|
| 91 | {
|
---|
| 92 | filters += 'backcamera='+this.props.backcamera+'&'
|
---|
| 93 | }
|
---|
| 94 |
|
---|
| 95 | if(this.props.chipset)
|
---|
| 96 | {
|
---|
| 97 | filters += 'chipset='+this.props.chipset+'&'
|
---|
| 98 | }
|
---|
| 99 |
|
---|
| 100 | if(this.props.cpu)
|
---|
| 101 | {
|
---|
| 102 | filters += 'cpu='+this.props.cpu+'&'
|
---|
| 103 | }
|
---|
| 104 |
|
---|
| 105 | if(this.props.operatingsystem)
|
---|
| 106 | {
|
---|
| 107 | filters += 'operatingsystem='+this.props.operatingsystem+'&'
|
---|
| 108 | }
|
---|
| 109 |
|
---|
| 110 | if(this.props.color)
|
---|
| 111 | {
|
---|
| 112 | filters += 'color='+this.props.color+'&'
|
---|
| 113 | }
|
---|
| 114 |
|
---|
| 115 | if(this.props.battery)
|
---|
| 116 | {
|
---|
| 117 | filters += 'battery='+this.props.battery+'&'
|
---|
| 118 | }
|
---|
[e5b84dc] | 119 |
|
---|
| 120 | axios.get('/phones'+filters)
|
---|
[dbd4834] | 121 | .then(response => {
|
---|
| 122 | this.setState({
|
---|
| 123 | phones: response.data,
|
---|
| 124 | numberOfPages: Math.ceil(response.data.length / this.state.phonesPerPage)
|
---|
[e5b84dc] | 125 | },(e) => this.setNewPage(e,this.state.currentPage))
|
---|
[dbd4834] | 126 | }
|
---|
| 127 | )
|
---|
| 128 | .catch(error => console.log(error))
|
---|
[e5b84dc] | 129 | console.log(filters)
|
---|
| 130 | }
|
---|
[dbd4834] | 131 | }
|
---|
| 132 |
|
---|
[e5b84dc] | 133 | componentDidMount() {
|
---|
[d66b8eb] | 134 | axios.get('/phones'+this.getQueryString())
|
---|
[e5b84dc] | 135 | .then(response => {
|
---|
| 136 | this.setState({
|
---|
| 137 | phones: response.data,
|
---|
| 138 | numberOfPages: Math.ceil(response.data.length / this.state.phonesPerPage)
|
---|
| 139 | },(e) => this.setNewPage(e,this.state.currentPage))
|
---|
| 140 | }
|
---|
| 141 | )
|
---|
| 142 | .catch(error => console.log(error))
|
---|
| 143 | }
|
---|
[dbd4834] | 144 |
|
---|
| 145 |
|
---|
[e5b84dc] | 146 | setNewPage = (event,page) => {
|
---|
[dbd4834] | 147 |
|
---|
[e5b84dc] | 148 | const indexOfLastPhone = parseInt(page) * this.state.phonesPerPage;
|
---|
[dbd4834] | 149 | const indexOfFirstPhone = indexOfLastPhone - this.state.phonesPerPage;
|
---|
| 150 |
|
---|
| 151 | const currPhones = this.state.phones.slice(indexOfFirstPhone, indexOfLastPhone)
|
---|
| 152 |
|
---|
| 153 | this.setState({
|
---|
[e5b84dc] | 154 | currentPage: parseInt(page),
|
---|
[dbd4834] | 155 | currentPhones: currPhones
|
---|
| 156 | })
|
---|
| 157 |
|
---|
| 158 | }
|
---|
| 159 |
|
---|
| 160 | render() {
|
---|
[e5b84dc] | 161 |
|
---|
[dbd4834] | 162 | return (
|
---|
| 163 | <div className='phonecardgrid-wrapper'>
|
---|
| 164 | <Grid className='phonecardgrid-grid-container'
|
---|
| 165 | container
|
---|
| 166 | alignItems="center"
|
---|
| 167 | justifyItems="center"
|
---|
| 168 | spacing={2}>
|
---|
| 169 |
|
---|
[e5b84dc] | 170 | {this.state.currentPhones.map((phone,idx) => <Grid key={idx} className='phonecardgrid-item' item md={3}>
|
---|
| 171 | <PhoneCardComponent id={phone.id} brand={phone.brand}
|
---|
[dbd4834] | 172 | model={phone.model} image_url={phone.image_url == null ? phoneImage : phone.image_url} total_offers={phone.total_offers} lowestPrice={phone.lowestPrice}/></Grid>)}
|
---|
| 173 |
|
---|
| 174 | </Grid>
|
---|
[e5b84dc] | 175 |
|
---|
| 176 | <div className='pagination-wrapper'>
|
---|
| 177 | <Pagination className='paginationcomponent-pagination' onChange={this.setNewPage} page={this.state.currentPage}
|
---|
| 178 | count={this.state.numberOfPages} color="primary" />
|
---|
| 179 | </div>
|
---|
| 180 |
|
---|
[dbd4834] | 181 | </div>
|
---|
| 182 | )
|
---|
| 183 | }
|
---|
| 184 | }
|
---|
| 185 |
|
---|
| 186 | export default PhoneCardGridComponent
|
---|