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 | // dodaj filtri za specifikacija i sredi go sortBy
|
---|
47 |
|
---|
48 | return filters
|
---|
49 | }
|
---|
50 |
|
---|
51 | componentDidUpdate(prevProps) {
|
---|
52 | if(JSON.stringify(prevProps) != JSON.stringify(this.props)){
|
---|
53 | let filters = '?'
|
---|
54 |
|
---|
55 | if(this.props.shops)
|
---|
56 | {
|
---|
57 | filters += 'shops='+this.props.shops+'&'
|
---|
58 | }
|
---|
59 | if(this.props.brands)
|
---|
60 | {
|
---|
61 | filters += 'brands='+this.props.brands+'&'
|
---|
62 | }
|
---|
63 | if(this.props.priceRange)
|
---|
64 | {
|
---|
65 | filters += 'priceRange='+this.props.priceRange+'&'
|
---|
66 | }
|
---|
67 | if(this.props.searchValue)
|
---|
68 | {
|
---|
69 | filters += 'searchValue='+this.props.searchValue+'&'
|
---|
70 | }
|
---|
71 |
|
---|
72 | if(this.props.sortBy)
|
---|
73 | {
|
---|
74 | filters += 'sortBy='+this.props.sortBy+'&'
|
---|
75 | }
|
---|
76 |
|
---|
77 | if(this.props.ram)
|
---|
78 | {
|
---|
79 | filters += 'ram='+this.props.ram+'&'
|
---|
80 | }
|
---|
81 |
|
---|
82 | if(this.props.rom)
|
---|
83 | {
|
---|
84 | filters += 'rom='+this.props.rom+'&'
|
---|
85 | }
|
---|
86 |
|
---|
87 | if(this.props.frontcamera)
|
---|
88 | {
|
---|
89 | filters += 'frontcamera='+this.props.frontcamera+'&'
|
---|
90 | }
|
---|
91 |
|
---|
92 | if(this.props.backcamera)
|
---|
93 | {
|
---|
94 | filters += 'backcamera='+this.props.backcamera+'&'
|
---|
95 | }
|
---|
96 |
|
---|
97 | if(this.props.chipset)
|
---|
98 | {
|
---|
99 | filters += 'chipset='+this.props.chipset+'&'
|
---|
100 | }
|
---|
101 |
|
---|
102 | if(this.props.cpu)
|
---|
103 | {
|
---|
104 | filters += 'cpu='+this.props.cpu+'&'
|
---|
105 | }
|
---|
106 |
|
---|
107 | if(this.props.operatingsystem)
|
---|
108 | {
|
---|
109 | filters += 'operatingsystem='+this.props.operatingsystem+'&'
|
---|
110 | }
|
---|
111 |
|
---|
112 | if(this.props.color)
|
---|
113 | {
|
---|
114 | filters += 'color='+this.props.color+'&'
|
---|
115 | }
|
---|
116 |
|
---|
117 | if(this.props.battery)
|
---|
118 | {
|
---|
119 | filters += 'battery='+this.props.battery+'&'
|
---|
120 | }
|
---|
121 |
|
---|
122 | axios.get('/phones'+filters)
|
---|
123 | .then(response => {
|
---|
124 | this.setState({
|
---|
125 | phones: response.data,
|
---|
126 | numberOfPages: Math.ceil(response.data.length / this.state.phonesPerPage)
|
---|
127 | },(e) => this.setNewPage(e,this.state.currentPage))
|
---|
128 | }
|
---|
129 | )
|
---|
130 | .catch(error => console.log(error))
|
---|
131 | console.log(filters)
|
---|
132 | }
|
---|
133 | }
|
---|
134 |
|
---|
135 | componentDidMount() {
|
---|
136 | axios.get('/phones'+this.getQueryString())
|
---|
137 | .then(response => {
|
---|
138 | this.setState({
|
---|
139 | phones: response.data,
|
---|
140 | numberOfPages: Math.ceil(response.data.length / this.state.phonesPerPage)
|
---|
141 | },(e) => this.setNewPage(e,this.state.currentPage))
|
---|
142 | }
|
---|
143 | )
|
---|
144 | .catch(error => console.log(error))
|
---|
145 | }
|
---|
146 |
|
---|
147 |
|
---|
148 | setNewPage = (event,page) => {
|
---|
149 |
|
---|
150 | const indexOfLastPhone = parseInt(page) * this.state.phonesPerPage;
|
---|
151 | const indexOfFirstPhone = indexOfLastPhone - this.state.phonesPerPage;
|
---|
152 |
|
---|
153 | const currPhones = this.state.phones.slice(indexOfFirstPhone, indexOfLastPhone)
|
---|
154 |
|
---|
155 | this.setState({
|
---|
156 | currentPage: parseInt(page),
|
---|
157 | currentPhones: currPhones
|
---|
158 | })
|
---|
159 |
|
---|
160 | }
|
---|
161 |
|
---|
162 | render() {
|
---|
163 |
|
---|
164 | return (
|
---|
165 | <div className='phonecardgrid-wrapper'>
|
---|
166 | <Grid className='phonecardgrid-grid-container'
|
---|
167 | container
|
---|
168 | alignItems="center"
|
---|
169 | justifyItems="center"
|
---|
170 | spacing={2}>
|
---|
171 |
|
---|
172 | {this.state.currentPhones.map((phone,idx) => <Grid key={idx} className='phonecardgrid-item' item md={3}>
|
---|
173 | <PhoneCardComponent id={phone.id} brand={phone.brand}
|
---|
174 | model={phone.model} image_url={phone.image_url == null ? phoneImage : phone.image_url} total_offers={phone.total_offers} lowestPrice={phone.lowestPrice}/></Grid>)}
|
---|
175 |
|
---|
176 | </Grid>
|
---|
177 |
|
---|
178 | <div className='pagination-wrapper'>
|
---|
179 | <Pagination className='paginationcomponent-pagination' onChange={this.setNewPage} page={this.state.currentPage}
|
---|
180 | count={this.state.numberOfPages} color="primary" />
|
---|
181 | </div>
|
---|
182 |
|
---|
183 | </div>
|
---|
184 | )
|
---|
185 | }
|
---|
186 | }
|
---|
187 |
|
---|
188 | export default PhoneCardGridComponent
|
---|