How to use SearchState method in redwood

Best JavaScript code snippet using redwood

searchReducer.js

Source:searchReducer.js Github

copy

Full Screen

1export const sumItems = (cartItems) => {2 return {3 total:4 cartItems.length > 05 ? cartItems.reduce((total, item) => total + item.price, 0)6 : 0,7 }8}9const searchReducer = (searchState, action) => {10 switch (action.type) {11 case 'ADD_CATEGORIES':12 searchState.categories.push({ ...action.payload })13 return {14 ...searchState,15 }16 case 'SET_PAGE':17 searchState.page = action.payload18 return {19 ...searchState,20 }21 case 'ADD_DRONE_TYPE':22 if (searchState.droneType === '') {23 searchState.droneType = action.payload24 return {25 ...searchState,26 cartItems: [...searchState.cartItems],27 ...sumItems(searchState.cartItems),28 }29 } else {30 searchState.droneType = action.payload31 searchState.cartItems = []32 searchState.batteryType = ''33 searchState.hdOrAnalog = ''34 searchState.cartItems = ''35 searchState.motor = ''36 searchState.software = ''37 searchState.stack = ''38 searchState.esc = ''39 searchState.propeller = ''40 searchState.miscellaneous = ''41 searchState.total = 042 return {43 ...searchState,44 cartItems: [...searchState.cartItems],45 ...sumItems(searchState.cartItems),46 }47 }48 case 'ADD_ITEM':49 if (50 !searchState.cartItems.find((item) => item._id === action.payload._id)51 ) {52 searchState.cartItems.push({ ...action.payload })53 }54 return {55 ...searchState,56 cartItems: [...searchState.cartItems],57 ...sumItems(searchState.cartItems),58 }59 case 'REMOVE_ITEM':60 return {61 ...searchState,62 cartItems: [63 ...searchState.cartItems.filter(64 (item) => item._id !== action.payload._id65 ),66 ],67 ...sumItems(searchState.cartItems),68 }69 case 'ADD_FRAME':70 if (searchState.frame === '') {71 searchState.frame = action.payload72 return {73 ...searchState,74 cartItems: [...searchState.cartItems],75 ...sumItems(searchState.cartItems),76 }77 } else {78 searchState.frame = action.payload79 searchState.batteryType = ''80 searchState.hdOrAnalog = ''81 searchState.motor = ''82 searchState.software = ''83 searchState.stack = ''84 searchState.esc = ''85 searchState.vtx = ''86 searchState.camera = ''87 searchState.propeller = ''88 searchState.miscellaneous = ''89 searchState.total = 090 return {91 ...searchState,92 cartItems: [93 ...searchState.cartItems.filter(94 (item) =>95 item.productType !== 'Frame' &&96 item.productType !== 'Battery' &&97 item.productType !== 'Motor' &&98 item.productType !== 'Stack' &&99 item.productType !== 'ESC' &&100 item.productType !== 'Flight Controller' &&101 item.productType !== 'VTX' &&102 item.productType !== 'Camera' &&103 item.productType !== 'Propeller' &&104 item.productType !== 'Misecellaneous'105 ),106 ],107 ...sumItems(searchState.cartItems),108 }109 }110 case 'ADD_BATTERY_TYPE':111 if (searchState.batteryType === '') {112 searchState.batteryType = action.payload113 return {114 ...searchState,115 cartItems: [...searchState.cartItems],116 ...sumItems(searchState.cartItems),117 }118 } else {119 searchState.batteryType = action.payload120 searchState.hdOrAnalog = ''121 searchState.motor = ''122 searchState.software = ''123 searchState.stack = ''124 searchState.esc = ''125 searchState.vtx = ''126 searchState.camera = ''127 searchState.propeller = ''128 searchState.miscellaneous = ''129 searchState.total = 0130 return {131 ...searchState,132 cartItems: [133 ...searchState.cartItems.filter(134 (item) =>135 item.productType !== 'Battery' &&136 item.productType !== 'Motor' &&137 item.productType !== 'Stack' &&138 item.productType !== 'ESC' &&139 item.productType !== 'Flight Controller' &&140 item.productType !== 'VTX' &&141 item.productType !== 'Camera' &&142 item.productType !== 'Antenna' &&143 item.productType !== 'Propeller' &&144 item.productType !== 'Miscellaneous'145 ),146 ],147 ...sumItems(searchState.cartItems),148 }149 }150 case 'ADD_HD_OR_ANALOG':151 if (searchState.hdOrAnalog === '') {152 searchState.hdOrAnalog = action.payload153 return {154 ...searchState,155 cartItems: [...searchState.cartItems],156 ...sumItems(searchState.cartItems),157 }158 } else {159 searchState.hdOrAnalog = action.payload160 searchState.motor = ''161 searchState.software = ''162 searchState.stack = ''163 searchState.esc = ''164 searchState.vtx = ''165 searchState.camera = ''166 searchState.propeller = ''167 searchState.miscellaneous = ''168 searchState.total = 0169 return {170 ...searchState,171 cartItems: [172 ...searchState.cartItems.filter(173 (item) =>174 item.productType !== 'Motor' &&175 item.productType !== 'Stack' &&176 item.productType !== 'ESC' &&177 item.productType !== 'Flight Controller' &&178 item.productType !== 'VTX' &&179 item.productType !== 'Camera' &&180 item.productType !== 'Antenna' &&181 item.productType !== 'Propeller' &&182 item.productType !== 'Miscellaneous'183 ),184 ],185 ...sumItems(searchState.cartItems),186 }187 }188 case 'ADD_MOTOR':189 if (searchState.motor === '') {190 searchState.motor = action.payload191 return {192 ...searchState,193 cartItems: [...searchState.cartItems],194 ...sumItems(searchState.cartItems),195 }196 } else {197 searchState.motor = action.payload198 searchState.software = ''199 searchState.stack = ''200 searchState.esc = ''201 searchState.vtx = ''202 searchState.camera = ''203 searchState.propeller = ''204 searchState.miscellaneous = ''205 searchState.total = 0206 return {207 ...searchState,208 cartItems: [209 ...searchState.cartItems.filter(210 (item) =>211 item.productType !== 'Motor' &&212 item.productType !== 'Stack' &&213 item.productType !== 'ESC' &&214 item.productType !== 'Stack' &&215 item.productType !== 'Flight Controller' &&216 item.productType !== 'VTX' &&217 item.productType !== 'Camera' &&218 item.productType !== 'Antenna' &&219 item.productType !== 'Propeller' &&220 item.productType !== 'Miscellaneous'221 ),222 ],223 ...sumItems(searchState.cartItems),224 }225 }226 case 'ADD_SOFTWARE':227 if (searchState.software === '') {228 searchState.software = action.payload229 return {230 ...searchState,231 cartItems: [...searchState.cartItems],232 ...sumItems(searchState.cartItems),233 }234 } else {235 if (action.payload === 'Kiss') {236 searchState.software = action.payload237 searchState.stack = ''238 searchState.esc = ''239 searchState.controller = ''240 searchState.propeller = ''241 searchState.miscellaneous = ''242 return {243 ...searchState,244 cartItems: [245 ...searchState.cartItems.filter(246 (item) =>247 item.productType !== 'Stack' &&248 item.productType !== 'ESC' &&249 item.productType !== 'VTX' &&250 item.productType !== 'Flight Controller' &&251 item.productType !== 'Camera' &&252 item.productType !== 'Antenna' &&253 item.productType !== 'Propeller' &&254 item.productType !== 'Miscellaneous'255 ),256 ],257 ...sumItems(searchState.cartItems),258 }259 }260 if (action.payload === 'Betaflight') {261 searchState.software = action.payload262 return {263 ...searchState,264 cartItems: [265 ...searchState.cartItems.filter(266 (item) =>267 item.productType !== 'ESC' &&268 item.productType !== 'Stack' &&269 item.productType !== 'ESC' &&270 item.productType !== 'Flight Controller' &&271 item.productType !== 'VTX' &&272 item.productType !== 'Camera' &&273 item.productType !== 'Antenna' &&274 item.productType !== 'Propeller' &&275 item.productType !== 'Miscellaneous'276 ),277 ],278 ...sumItems(searchState.cartItems),279 }280 }281 }282 case 'ADD_STACK':283 if (searchState.stack === '') {284 searchState.stack = action.payload285 searchState.esc = ''286 searchState.controller = ''287 searchState.propeller = ''288 searchState.miscellaneous = ''289 searchState.total = 0290 return {291 ...searchState,292 cartItems: [...searchState.cartItems],293 ...sumItems(searchState.cartItems),294 }295 } else {296 searchState.stack = action.payload297 searchState.esc = ''298 searchState.controller = ''299 searchState.propeller = ''300 searchState.miscellaneous = ''301 searchState.total = 0302 return {303 ...searchState,304 cartItems: [305 ...searchState.cartItems.filter(306 (item) =>307 item.productType !== 'Stack' &&308 item.productType !== 'ESC' &&309 item.productType !== 'VTX' &&310 item.productType !== 'Flight Controller' &&311 item.productType !== 'Camera' &&312 item.productType !== 'Antenna' &&313 item.productType !== 'Propeller' &&314 item.productType !== 'Miscellaneous'315 ),316 ],317 ...sumItems(searchState.cartItems),318 }319 }320 case 'ADD_ESC':321 if (searchState.esc === '') {322 searchState.esc = action.payload323 searchState.stack = ''324 return {325 ...searchState,326 cartItems: [...searchState.cartItems],327 ...sumItems(searchState.cartItems),328 }329 } else {330 searchState.esc = action.payload331 searchState.stack = ''332 return {333 ...searchState,334 cartItems: [335 ...searchState.cartItems.filter(336 (item) =>337 item.productType !== 'ESC' &&338 item.productType !== 'VTX' &&339 item.productType !== 'Camera' &&340 item.productType !== 'Antenna' &&341 item.productType !== 'Propeller' &&342 item.productType !== 'Miscellaneous'343 ),344 ],345 ...sumItems(searchState.cartItems),346 }347 }348 case 'ADD_CONTROLLER':349 if (searchState.controller === '') {350 searchState.controller = action.payload351 searchState.stack = ''352 return {353 ...searchState,354 cartItems: [...searchState.cartItems],355 ...sumItems(searchState.cartItems),356 }357 } else {358 searchState.controller = action.payload359 searchState.vtx = ''360 searchState.camera = ''361 searchState.antenna = ''362 searchState.propeller = ''363 searchState.miscellaneous = ''364 return {365 ...searchState,366 cartItems: [367 ...searchState.cartItems.filter(368 (item) =>369 item.productType !== 'Controller' &&370 item.productType !== 'VTX' &&371 item.productType !== 'Camera' &&372 item.productType !== 'Propeller' &&373 item.productType !== 'Miscellaneous'374 ),375 ],376 ...sumItems(searchState.cartItems),377 }378 }379 case 'ADD_VTX':380 if (searchState.vtx === '') {381 searchState.vtx = action.payload382 return {383 ...searchState,384 cartItems: [...searchState.cartItems],385 ...sumItems(searchState.cartItems),386 }387 } else {388 searchState.vtx = action.payload389 searchState.camera = ''390 searchState.antenna = ''391 searchState.propeller = ''392 searchState.miscellaneous = ''393 return {394 ...searchState,395 cartItems: [396 ...searchState.cartItems.filter(397 (item) =>398 item.productType !== 'VTX' &&399 item.productType !== 'Camera' &&400 item.productType !== 'Antenna' &&401 item.productType !== 'Propeller' &&402 item.productType !== 'Miscellaneous'403 ),404 ],405 ...sumItems(searchState.cartItems),406 }407 }408 case 'ADD_CAMERA':409 if (searchState.camera === '') {410 searchState.camera = action.payload411 searchState.miscellaneous = ''412 return {413 ...searchState,414 cartItems: [...searchState.cartItems],415 ...sumItems(searchState.cartItems),416 }417 } else {418 searchState.camera = action.payload419 searchState.antenna = ''420 searchState.propeller = ''421 searchState.miscellaneous = ''422 return {423 ...searchState,424 cartItems: [425 ...searchState.cartItems.filter(426 (item) =>427 item.productType !== 'Camera' &&428 item.productType !== 'Antenna' &&429 item.productType !== 'Propeller' &&430 item.productType !== 'Miscellaneous'431 ),432 ],433 ...sumItems(searchState.cartItems),434 }435 }436 case 'ADD_ANTENNA':437 if (searchState.antenna === '') {438 searchState.antenna = action.payload439 return {440 ...searchState,441 cartItems: [...searchState.cartItems],442 ...sumItems(searchState.cartItems),443 }444 } else {445 searchState.antenna = action.payload446 searchState.propeller = ''447 searchState.miscellaneous = ''448 return {449 ...searchState,450 cartItems: [451 ...searchState.cartItems.filter(452 (item) =>453 item.productType !== 'Antenna' &&454 item.productType !== 'Propeller'455 ),456 ],457 ...sumItems(searchState.cartItems),458 }459 }460 case 'ADD_PROPELLER':461 if (searchState.propeller === '') {462 searchState.propeller = action.payload463 searchState.miscellaneous = ''464 return {465 ...searchState,466 cartItems: [...searchState.cartItems],467 ...sumItems(searchState.cartItems),468 }469 } else {470 searchState.propeller = action.payload471 searchState.miscellaneous = ''472 return {473 ...searchState,474 cartItems: [475 ...searchState.cartItems.filter(476 (item) =>477 item.productType !== 'propeller' &&478 item.productType !== 'miscellaneous'479 ),480 ],481 ...sumItems(searchState.cartItems),482 }483 }484 case 'ADD_MISCELLANEOUS':485 if (searchState.miscellaneous === '') {486 searchState.miscellaneous = action.payload487 return {488 ...searchState,489 cartItems: [...searchState.cartItems],490 ...sumItems(searchState.cartItems),491 }492 } else {493 searchState.miscellaneous = action.payload494 return {495 ...searchState,496 cartItems: [497 ...searchState.cartItems.filter(498 (item) => item.productType !== 'miscellaneous'499 ),500 ],501 ...sumItems(searchState.cartItems),502 }503 }504 default:505 return searchState506 }507}...

Full Screen

Full Screen

withUrlSync.js

Source:withUrlSync.js Github

copy

Full Screen

...39 }40 componentDidMount() {41 this.originalPathName = window.location.pathname;42 this.originalHref = window.location.href;43 this.setState({searchState: queryStringToSearchState(window.location.search.slice(1))});44 window.addEventListener(`popstate`, ({state: searchState}) => {45 // check we are on a search result46 if (searchState !== null) {47 this.setState({searchState});48 return;49 }50 this.setState({searchState: {query: ``, page: 1}});51 });52 }53 componentWillReceiveProps(nextProps) {54 if (nextProps.location.key !== this.props.location.key) {55 this.originalPathName = window.location.pathname;56 this.originalHref = window.location.href;57 this.setState({searchState: {query: ``, page: 1}});...

Full Screen

Full Screen

search-state-service.spec.js

Source:search-state-service.spec.js Github

copy

Full Screen

1/* ============================================================================2 Copyright (C) 2006-2018 Talend Inc. - www.talend.com3 This source code is available under agreement available at4 https://github.com/Talend/data-prep/blob/master/LICENSE5 You should have received a copy of the agreement6 along with this program; if not, write to Talend SA7 9 rue Pages 92150 Suresnes, France8 ============================================================================*/9describe('Search', () => {10 beforeEach(angular.mock.module('data-prep.services.state'));11 describe('state service', () => {12 it('should toggle', inject((searchState, SearchStateService) => {13 // given14 expect(searchState.searchToggle).toBeTruthy();15 // when16 SearchStateService.toggle();17 // then18 expect(searchState.searchToggle).toBeFalsy();19 }));20 it('should set searching', inject((searchState, SearchStateService) => {21 // given22 expect(searchState.searching).toBeFalsy();23 // when24 SearchStateService.setSearching(true);25 // then26 expect(searchState.searching).toBeTruthy();27 }));28 it('should set input search', inject((searchState, SearchStateService) => {29 // given30 expect(searchState.searchInput).toBeNull();31 // when32 SearchStateService.setSearchInput('lorem ipsum');33 // then34 expect(searchState.searchInput).toBe('lorem ipsum');35 }));36 it('should set search results', inject((searchState, SearchStateService) => {37 // given38 expect(searchState.searchResults).toBeNull();39 // when40 SearchStateService.setSearchResults([1, 2, 3]);41 // then42 expect(searchState.searchResults).toEqual([1, 2, 3]);43 }));44 it('should set search categories', inject((searchState, SearchStateService) => {45 // given46 expect(searchState.searchCategories).toBeNull();47 // when48 SearchStateService.setSearchCategories([1, 2, 3]);49 // then50 expect(searchState.searchCategories).toEqual([1, 2, 3]);51 }));52 it('should set focused section index', inject((searchState, SearchStateService) => {53 // given54 expect(searchState.focusedSectionIndex).toBeNull();55 // when56 SearchStateService.setFocusedSectionIndex(1);57 // then58 expect(searchState.focusedSectionIndex).toBe(1);59 }));60 it('should set focused item index', inject((searchState, SearchStateService) => {61 // given62 expect(searchState.focusedItemIndex).toBeNull();63 // when64 SearchStateService.setFocusedItemIndex(1);65 // then66 expect(searchState.focusedItemIndex).toBe(1);67 }));68 });...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run redwood automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful