source: phonelux-frontend/src/components/FiltersComponents/FilterSelectComponent.js

Last change on this file was 48f3030, checked in by Marko <Marko@…>, 22 months ago

Implemented all use cases

  • Property mode set to 100644
File size: 7.2 KB
Line 
1
2import * as React from 'react';
3import OutlinedInput from '@mui/material/OutlinedInput';
4import InputLabel from '@mui/material/InputLabel';
5import MenuItem from '@mui/material/MenuItem';
6import FormControl from '@mui/material/FormControl';
7import ListItemText from '@mui/material/ListItemText';
8import Select from '@mui/material/Select';
9import Checkbox from '@mui/material/Checkbox';
10import "./FilterSelectComponent.css"
11import axios from 'axios';
12
13export class FilterSelectComponent extends React.Component {
14
15 constructor(props) {
16 super(props)
17 const {type} = this.props
18 this.state = {
19 pickedItems: localStorage.getItem(type) ? localStorage.getItem(type).split(',') : [],
20 items: [],
21 type: '',
22 ITEM_HEIGHT: 48,
23 ITEM_PADDING_TOP: 8,
24 MenuProps: {}
25 }
26
27 }
28
29 componentDidMount(){
30 this.state.MenuProps = {
31 PaperProps: {
32 style: {
33 maxHeight: this.state.ITEM_HEIGHT * 5.5 + this.state.ITEM_PADDING_TOP,
34 width: 250,
35 },
36 },
37 }
38
39 let endpoint
40 switch (this.props.type) {
41
42 case 'brands':
43 endpoint = '/brands'
44 this.setState({
45 type: 'Брендови'
46 })
47 break;
48 case 'shops':
49 endpoint = '/shops'
50 this.setState({
51 type: 'Продавници'
52 })
53 break;
54
55 case 'ram':
56 endpoint = '/specifications/ram'
57 this.setState({
58 type: 'РАМ меморија'
59 })
60 break;
61
62 case 'rom':
63 endpoint = '/specifications/rom'
64 this.setState({
65 type: 'РОМ меморија'
66 })
67 break;
68
69 case 'frontcamera':
70 endpoint = '/specifications/frontcamera'
71 this.setState({
72 type: 'Предна камера'
73 })
74 break;
75
76 case 'backcamera':
77 endpoint = '/specifications/backcamera'
78 this.setState({
79 type: 'Задна камера'
80 })
81 break;
82
83 case 'chipset':
84 endpoint = '/specifications/chipset'
85 this.setState({
86 type: 'Чипсет'
87 })
88 break;
89
90 case 'cpu':
91 endpoint = '/specifications/cpu'
92 this.setState({
93 type: 'Процесор'
94 })
95 break;
96
97
98 case 'operatingsystem':
99 endpoint = '/specifications/operatingsystem'
100 this.setState({
101 type: 'Оперативен систем'
102 })
103 break;
104
105
106 case 'color':
107 endpoint = '/specifications/color'
108 this.setState({
109 type: 'Боја'
110 })
111 break;
112
113 case 'battery':
114 endpoint = '/specifications/battery'
115 this.setState({
116 type: 'Батерија'
117 })
118 break;
119
120 default:
121 break;
122 }
123
124 axios.get(endpoint)
125 .then(response => this.setState({items: response.data}))
126 .catch(error => console.log(error))
127 }
128
129 handleChange = (event) => {
130 let value = event.target.value
131 this.setState({
132 pickedItems: typeof value === 'string' ? value.split(',') : value
133 }, ()=>{
134
135 switch (this.props.type) {
136 case 'brands':
137 this.props.changeHandler({brands: this.state.pickedItems.join(',')})
138 localStorage.setItem('brands', this.state.pickedItems.join(','))
139 break;
140
141 case 'shops':
142 this.props.changeHandler({shops: this.state.pickedItems.join(',')})
143 localStorage.setItem('shops', this.state.pickedItems.join(','))
144 break;
145
146 case 'ram':
147 this.props.changeHandler({ram: this.state.pickedItems.join(',')})
148 localStorage.setItem('ram', this.state.pickedItems.join(','))
149 break;
150 case 'rom':
151 this.props.changeHandler({rom: this.state.pickedItems.join(',')})
152 localStorage.setItem('rom', this.state.pickedItems.join(','))
153 break;
154 case 'frontcamera':
155 this.props.changeHandler({frontcamera: this.state.pickedItems.join(',')})
156 localStorage.setItem('frontcamera', this.state.pickedItems.join(','))
157 break;
158
159 case 'backcamera':
160 this.props.changeHandler({backcamera: this.state.pickedItems.join(',')})
161 localStorage.setItem('backcamera', this.state.pickedItems.join(','))
162 break;
163
164 case 'chipset':
165 this.props.changeHandler({chipset: this.state.pickedItems.join(',')})
166 localStorage.setItem('chipset', this.state.pickedItems.join(','))
167 break;
168
169 case 'cpu':
170 this.props.changeHandler({cpu: this.state.pickedItems.join(',')})
171 localStorage.setItem('cpu', this.state.pickedItems.join(','))
172 break;
173
174 case 'operatingsystem':
175 this.props.changeHandler({operatingsystem: this.state.pickedItems.join(',')})
176 localStorage.setItem('operatingsystem', this.state.pickedItems.join(','))
177 break;
178
179 case 'color':
180 this.props.changeHandler({color: this.state.pickedItems.join(',')})
181 localStorage.setItem('color', this.state.pickedItems.join(','))
182 break;
183
184 case 'battery':
185 this.props.changeHandler({battery: this.state.pickedItems.join(',')})
186 localStorage.setItem('battery', this.state.pickedItems.join(','))
187 break;
188
189 default:
190 break;
191 }
192 // if(this.props.type == 'brands')
193 // {
194 // this.props.changeHandler({brands: this.state.pickedItems.join(',')})
195 // }
196
197 // if(this.props.type == 'shops')
198 // {
199 // this.props.changeHandler({shops: this.state.pickedItems.join(',')})
200 // }
201
202 })
203
204
205 };
206 // sx={{ m: 1, width: 250 }} kaj formcontrol
207
208 render() {
209 return (
210 <div>
211 <FormControl className="form-select-component" sx={{ m: 1, width: this.props.width}}>
212 <InputLabel className="input-select-label">{this.state.type}</InputLabel>
213 <Select
214 size={"small"}
215 labelId="input-label-id"
216 className="input-select-option"
217 multiple
218 value={this.state.pickedItems}
219 onChange={this.handleChange}
220 input={<OutlinedInput className='inner-input-selectfilter' label={this.state.type} />}
221 renderValue={(selected) => selected.join(', ')}
222 MenuProps={this.state.MenuProps}
223 >
224 {this.state.items.map((item) => (
225 <MenuItem key={item} value={item} >
226 <Checkbox checked={this.state.pickedItems.indexOf(item) > -1} />
227 <ListItemText className='list-item-text-filters' primary={item} />
228 </MenuItem>
229 ))}
230 </Select>
231 </FormControl>
232 </div>
233 )
234 }
235}
236
237export default FilterSelectComponent
Note: See TracBrowser for help on using the repository browser.