Router done
This commit is contained in:
@@ -1,31 +1,13 @@
|
||||
class ToggleHash {
|
||||
constructor(data, key) {
|
||||
this.data = data;
|
||||
this.key = key;
|
||||
this.data[key] = {};
|
||||
}
|
||||
|
||||
toggle (field) {
|
||||
if (!field) {
|
||||
return;
|
||||
}
|
||||
|
||||
const val = this.data[this.key][field];
|
||||
this.data[this.key][field] = !val;
|
||||
}
|
||||
}
|
||||
import clone from 'lodash.clonedeep';
|
||||
|
||||
export default class Router {
|
||||
constructor(comp, initialState) {
|
||||
this.component = comp;
|
||||
this.state = initialState || {};
|
||||
this.state = clone(initialState) || {};
|
||||
|
||||
this.roomsToggler = new ToggleHash(this.state, 'rooms');
|
||||
this.categoryToggler = new ToggleHash(this.state, 'category');
|
||||
|
||||
window.onpopstate = (event) => {
|
||||
const state = event.state;
|
||||
console.log('POPING STATE', state);
|
||||
if (state) {
|
||||
if (state.toDispatch) {
|
||||
this.component.dispatch(state.toDispatch);
|
||||
@@ -38,16 +20,22 @@ export default class Router {
|
||||
const params = [];
|
||||
if (state.params) {
|
||||
|
||||
this.roomsToggler.toggle(state.params.rooms);
|
||||
this.categoryToggler.toggle(state.params.category);
|
||||
let cloned = clone(state);
|
||||
|
||||
delete state.params['rooms'];
|
||||
delete state.params['category'];
|
||||
if (cloned.params.rooms != null) {
|
||||
this.state.rooms[cloned.params.rooms] = !this.state.rooms[cloned.params.rooms];
|
||||
}
|
||||
|
||||
this.state = Object.assign(this.state, state.params);
|
||||
if (cloned.params.category != null) {
|
||||
this.state.category[cloned.params.category] = !this.state.category[cloned.params.category];
|
||||
}
|
||||
|
||||
console.log('router state is: ', this.state);
|
||||
const {listingId, bounds, sort, rooms = {}, category = {}} = this.state;
|
||||
delete cloned.params['rooms'];
|
||||
delete cloned.params['category'];
|
||||
|
||||
this.state = Object.assign(this.state, cloned.params);
|
||||
|
||||
const {listingId, bounds, sort, rooms = {}, category = {}, zoom} = this.state;
|
||||
|
||||
if (listingId) {
|
||||
params.push(`listingId=${listingId}`);
|
||||
@@ -55,22 +43,20 @@ export default class Router {
|
||||
|
||||
params.push(`sort=${sort}`);
|
||||
params.push(`bounds=${bounds}`);
|
||||
params.push(`zoom=${zoom}`);
|
||||
params.push(`rooms=${Object.keys(rooms).filter(v => rooms[v]).join(",")}`);
|
||||
params.push(`category=${Object.keys(category).filter(v => category[v]).join(",")}`);
|
||||
}
|
||||
|
||||
if (state.toDispatch) {
|
||||
console.log('PUSHING STATE', state);
|
||||
window.history.pushState(state, '', `/?${params.join("&")}`);
|
||||
} else {
|
||||
const oldState = window.history.state;
|
||||
if (oldState) {
|
||||
const newState = Object.assign(oldState, state);
|
||||
console.log('REPLACING STATE', newState);
|
||||
window.history.replaceState(newState, '',`/?${params.join("&")}`);
|
||||
} else {
|
||||
|
||||
console.log('PUSHING STATE', state);
|
||||
window.history.replaceState(state, '',`/?${params.join("&")}`);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user