var DBG = DBG || 0; var DBG1 = true; if (!STORE_NAME) throw "Store name not defined"; var Redux = global.p5VendorJs.Redux; var ReduxThunk = global.p5VendorJs.ReduxThunk; var createStoreWithThunkMiddleware = Redux.applyMiddleware(ReduxThunk)(Redux.createStore); // TODO: to vendor.js function getDefaultState() { return { totalOdPracownicy: 0, totalOdPozostale: 0, totalDoKontrahenci: 0, totalDoPozostale: 0, pozostaleOd: [], pozostaleDo: [], depth: DEFAULT_DEPTH || 5, onlyTargets: true, // true: "y", false: "n" title: 'Raport bez tytułu z dnia ' + CURRENT_DATE, } } function getLocalStorageState() { DBG && console.log('DBG: bocian store INIT'); var defaultState = getDefaultState(); var defaultOnlyTargetsString = defaultState.onlyTargets ? "y" : "n" var pozostaleOd = getListFromLocalStorage(getSelectedListLocalStorageKey('OD')); var pozostaleDo = getListFromLocalStorage(getSelectedListLocalStorageKey('DO')); var pracownicyIds = getListFromLocalStorage('Bocian.biAuditForm.pracownicyIds'); var kontrahenciIds = getListFromLocalStorage('Bocian.biAuditForm.kontrahenciIds'); return { totalOdPracownicy: pracownicyIds.length, totalOdPozostale: pozostaleOd.length, totalDoKontrahenci: kontrahenciIds.length, totalDoPozostale: pozostaleDo.length, pozostaleOd: pozostaleOd, pozostaleDo: pozostaleDo, depth: getSignedIntFromLocalStorage('Bocian.biAuditForm.depth', defaultState.depth), onlyTargets: "n" === getStringFromLocalStorage('Bocian.biAuditForm.analizaOnlyTargets', defaultOnlyTargetsString) ? false : true, title: getStringFromLocalStorage('Bocian.biAuditForm.smadReaportTitle', defaultState.title), } } function bocianRaportStore(state, action) { var prevState = state || getLocalStorageState(); DBG && console.warn('DBG: bocian store', { prevState, action, actionType: action.type }); switch (action.type) { case 'UPDATE': return Object.assign(prevState, { totalOdPracownicy: action.totalOdPracownicy, totalDoKontrahenci: action.totalDoKontrahenci, }); case 'ADD_OD': { var listPozostaleOd = prevState.pozostaleOd.concat(action.featureKey); localStorage.setItem(getSelectedListLocalStorageKey('OD'), JSON.stringify(listPozostaleOd)) return Object.assign(prevState, { pozostaleOd: listPozostaleOd, totalOdPozostale: listPozostaleOd.length, }) } case 'ADD_DO': { var listPozostaleDo = prevState.pozostaleDo.concat(action.featureKey); localStorage.setItem(getSelectedListLocalStorageKey('DO'), JSON.stringify(listPozostaleDo)) return Object.assign(prevState, { pozostaleDo: listPozostaleDo, totalDoPozostale: listPozostaleDo.length, }) } case 'RM_OD': { var featureKey = action.featureKey; var listPozostaleOd = prevState.pozostaleOd.filter(function (item) { return ( item !== featureKey ); }) localStorage.setItem(getSelectedListLocalStorageKey('OD'), JSON.stringify(listPozostaleOd)) return Object.assign(prevState, { pozostaleOd: listPozostaleOd, totalOdPozostale: listPozostaleOd.length, }) } case 'RM_DO': { var featureKey = action.featureKey; var listPozostaleDo = prevState.pozostaleDo.filter(function (item) { return ( item !== featureKey ); }) localStorage.setItem(getSelectedListLocalStorageKey('DO'), JSON.stringify(listPozostaleDo)) return Object.assign(prevState, { pozostaleDo: listPozostaleDo, totalDoPozostale: listPozostaleDo.length, }) } case 'CLEAR_POZOSTALE_OD': { localStorage.setItem(getSelectedListLocalStorageKey('OD'), JSON.stringify([])) return Object.assign(prevState, { pozostaleOd: [], totalOdPozostale: 0, }) } case 'CLEAR_POZOSTALE_DO': { localStorage.setItem(getSelectedListLocalStorageKey('DO'), JSON.stringify([])) return Object.assign(prevState, { pozostaleDo: [], totalDoPozostale: 0, }) } case 'SET_DEPTH': { localStorage.setItem('Bocian.biAuditForm.depth', action.depth) return Object.assign(prevState, { depth: action.depth, }) } case 'SET_ONLY_TARGETS': { localStorage.setItem('Bocian.biAuditForm.analizaOnlyTargets', action.onlyTargets ? '"y"' : '"n"') return Object.assign(prevState, { onlyTargets: action.onlyTargets, }) } case 'SET_TITLE': { localStorage.setItem('Bocian.biAuditForm.smadReaportTitle', '"' + action.title + '"') return Object.assign(prevState, { title: action.title, }) } case 'CLEAR_ALL': { var defaultState = getDefaultState(); var defaultOnlyTargetsString = defaultState.onlyTargets ? "y" : "n" setItemLocalStorage('Bocian.biAuditForm.pracownicyIds', []); setItemLocalStorage('Bocian.biAuditForm.kontrahenciIds', []); setItemLocalStorage(getSelectedListLocalStorageKey('OD'), []); setItemLocalStorage(getSelectedListLocalStorageKey('DO'), []); setItemLocalStorage('Bocian.biAuditForm.pracownicy.filterIdGroup', 0); setItemLocalStorage('Bocian.biAuditForm.kontrahenci.filterIdGroup', 0); setItemLocalStorage('Bocian.biAuditForm.pracownicy.pagination.page', 1); setItemLocalStorage('Bocian.biAuditForm.kontrahenci.pagination.page', 1); setItemLocalStorage('Bocian.biAuditForm.analizaOnlyTargets', defaultOnlyTargetsString); setItemLocalStorage('Bocian.biAuditForm.depth', defaultState.depth); setItemLocalStorage('Bocian.biAuditForm.smadReaportTitle', defaultState.title); return Object.assign(prevState, defaultState) } default: return prevState; } } function getSelectedListLocalStorageKey(from) { // from: ( OD | DO ) switch (from) { case 'OD': return 'Bocian.biAuditForm.odFeatureKeys'; case 'DO': return 'Bocian.biAuditForm.doFeatureKeys'; default: throw "Not implemented from selected list '" + from + "' - expected 'OD' or 'DO'"; } } function getListFromLocalStorage(key) { return JSON.parse( localStorage.getItem(key) || '[]' ); } function getSignedIntFromLocalStorage(key, defaultValue) { return JSON.parse( localStorage.getItem(key) || defaultValue ); } function getStringFromLocalStorage(key, defaultValue) { var value = localStorage.getItem(key); value = (value) ? JSON.parse(value) : ''; return (value) ? value : defaultValue; } function setItemLocalStorage(key, value) { localStorage.setItem( key, JSON.stringify(value) ); } function getItemLocalStorage(key) { return JSON.parse( localStorage.getItem(key) ); } global[STORE_NAME] = createStoreWithThunkMiddleware(bocianRaportStore); global[STORE_NAME + '__defaults'] = getDefaultState;