forked from geosolutions-it/geonode-mapstore-client
-
Notifications
You must be signed in to change notification settings - Fork 113
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Client - Switch between grid and list of cards (#128)
- Loading branch information
1 parent
a9bca24
commit 752f8d3
Showing
8 changed files
with
249 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
geonode_mapstore_client/client/js/hooks/__tests__/useLocalStorage-test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
|
||
/* | ||
* Copyright 2021, GeoSolutions Sas. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
import React from 'react'; | ||
import ReactDOM from 'react-dom'; | ||
import expect from 'expect'; | ||
import useLocalStorage from '@js/hooks/useLocalStorage'; | ||
|
||
function MockApp({ key, value }) { | ||
|
||
const [inTest, setInTest] = useLocalStorage(key); | ||
setInTest(value); | ||
return ( | ||
<div className="MockApp"> | ||
<p id="lsValue" >{inTest}</p> | ||
</div> | ||
); | ||
|
||
} | ||
export default MockApp; | ||
|
||
|
||
describe('Test useLocalStorage', () => { | ||
beforeEach((done) => { | ||
document.body.innerHTML = '<div id="container"></div>'; | ||
setTimeout(done); | ||
}); | ||
afterEach((done) => { | ||
ReactDOM.unmountComponentAtNode(document.getElementById("container")); | ||
document.body.innerHTML = ''; | ||
setTimeout(done); | ||
}); | ||
|
||
it('Test componet is rendered', () => { | ||
ReactDOM.render(<MockApp key="test_key" value="test_value" /> | ||
, document.getElementById("container")); | ||
const container = document.getElementById('container'); | ||
const el = container.querySelector('.MockApp'); | ||
expect(el).toExist(); | ||
}); | ||
|
||
it('Test component localStorage props', () => { | ||
ReactDOM.render(<MockApp key="test_key" value="test_value" />, document.getElementById("container")); | ||
const el = document.getElementsByClassName('MockApp'); | ||
expect(el).toExist(); | ||
const element = el[0].childNodes[0]; | ||
expect(element).toExist(); | ||
expect(element.innerHTML).toBe('test_value'); | ||
}); | ||
|
||
|
||
}); |
52 changes: 52 additions & 0 deletions
52
geonode_mapstore_client/client/js/hooks/useLocalStorage.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { useEffect, useState } from 'react'; | ||
|
||
export default (key, initialValue) => { | ||
|
||
const getValue = () => { | ||
if (typeof window === 'undefined') { | ||
return initialValue; | ||
} | ||
|
||
try { | ||
const item = window.localStorage.getItem(key); | ||
return item ? JSON.parse(item) : initialValue; | ||
} catch (error) { | ||
// Todo log error in persistent solution | ||
console.log(`Error to get item key “${key}”:`, error); | ||
return initialValue; | ||
} | ||
}; | ||
|
||
const [storedValue, setStoredValue] = useState(getValue); | ||
|
||
const setValue = (value) => { | ||
|
||
try { | ||
const newValue = value instanceof Function ? value(storedValue) : value; | ||
setStoredValue(newValue); | ||
window.localStorage.setItem(key, JSON.stringify(newValue)); | ||
window.dispatchEvent(new Event('localStorage')); | ||
} catch (error) { | ||
// Todo log error in persistent solution | ||
console.log(`Error “${key}”:`, error); | ||
} | ||
}; | ||
|
||
useEffect(() => { | ||
setStoredValue(getValue()); | ||
}, []); | ||
|
||
useEffect(() => { | ||
const handleStoreChange = () => { | ||
setStoredValue(getValue()); | ||
}; | ||
window.addEventListener('localStorage', handleStoreChange); | ||
|
||
return () => { | ||
window.removeEventListener('localStorage', handleStoreChange); | ||
}; | ||
}, []); | ||
|
||
|
||
return [storedValue, setValue]; | ||
}; |
Oops, something went wrong.