1 | import { Grid, Pagination } from '@mui/material'
|
---|
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'
|
---|
8 | import SortByComponent from '../FiltersComponents/SortByComponent'
|
---|
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 |
|
---|
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 |
|
---|
49 | componentDidUpdate(prevProps) {
|
---|
50 | if(JSON.stringify(prevProps) != JSON.stringify(this.props)){
|
---|
51 | let filters = '?'
|
---|
52 |
|
---|
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+'&'
|
---|
73 | }
|
---|
74 |
|
---|
75 | if(this.props.ram)
|
---|
76 | {
|
---|
77 | filters += 'ram='+this.props.ram+'&'
|
---|
78 | }
|
---|
79 |
|
---|
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 | }
|
---|
119 |
|
---|
120 | axios.get('/phones'+filters)
|
---|
121 | .then(response => {
|
---|
122 | this.setState({
|
---|
123 | phones: response.data,
|
---|
124 | numberOfPages: Math.ceil(response.data.length / this.state.phonesPerPage)
|
---|
125 | },(e) => this.setNewPage(e,this.state.currentPage))
|
---|
126 | }
|
---|
127 | )
|
---|
128 | .catch(error => console.log(error))
|
---|
129 | console.log(filters)
|
---|
130 | }
|
---|
131 | }
|
---|
132 |
|
---|
133 | componentDidMount() {
|
---|
134 | axios.get('/phones'+this.getQueryString())
|
---|
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 | }
|
---|
144 |
|
---|
145 |
|
---|
146 | setNewPage = (event,page) => {
|
---|
147 |
|
---|
148 | const indexOfLastPhone = parseInt(page) * this.state.phonesPerPage;
|
---|
149 | const indexOfFirstPhone = indexOfLastPhone - this.state.phonesPerPage;
|
---|
150 |
|
---|
151 | const currPhones = this.state.phones.slice(indexOfFirstPhone, indexOfLastPhone)
|
---|
152 |
|
---|
153 | this.setState({
|
---|
154 | currentPage: parseInt(page),
|
---|
155 | currentPhones: currPhones
|
---|
156 | })
|
---|
157 |
|
---|
158 | }
|
---|
159 |
|
---|
160 | render() {
|
---|
161 |
|
---|
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 |
|
---|
170 | {this.state.currentPhones.map((phone,idx) => <Grid key={idx} className='phonecardgrid-item' item md={3}>
|
---|
171 | <PhoneCardComponent id={phone.id} brand={phone.brand}
|
---|
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>
|
---|
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 |
|
---|
181 | </div>
|
---|
182 | )
|
---|
183 | }
|
---|
184 | }
|
---|
185 |
|
---|
186 | export default PhoneCardGridComponent
|
---|