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: 6, onlyTargets: true, // true: "y", false: "n" title: 'Raport bez tytułu z dnia ' + CURRENT_DATE, } } function getLocalStorageState() { DBG && console.log('DBG: anton1 store INIT'); var defaultState = getDefaultState(); var defaultOnlyTargetsString = defaultState.onlyTargets ? "y" : "n" var pozostaleOd = getListFromLocalStorage(getSelectedListLocalStorageKey('OD')); var pozostaleDo = getListFromLocalStorage(getSelectedListLocalStorageKey('DO')); var pracownicyIds = getListFromLocalStorage('Anton1.biAuditForm.pracownicyIds'); var kontrahenciIds = getListFromLocalStorage('Anton1.biAuditForm.kontrahenciIds'); return { totalOdPracownicy: pracownicyIds.length, totalOdPozostale: pozostaleOd.length, totalDoKontrahenci: kontrahenciIds.length, totalDoPozostale: pozostaleDo.length, pozostaleOd: pozostaleOd, pozostaleDo: pozostaleDo, depth: getSignedIntFromLocalStorage('Anton1.biAuditForm.depth', defaultState.depth), onlyTargets: "n" === getStringFromLocalStorage('Anton1.biAuditForm.analizaOnlyTargets', defaultOnlyTargetsString) ? false : true, title: getStringFromLocalStorage('Anton1.biAuditForm.smadReaportTitle', defaultState.title), } } function anton1RaportStore(state, action) { var prevState = state || getLocalStorageState(); DBG && console.warn('DBG: anton1 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('Anton1.biAuditForm.depth', action.depth) return Object.assign(prevState, { depth: action.depth, }) } case 'SET_ONLY_TARGETS': { localStorage.setItem('Anton1.biAuditForm.analizaOnlyTargets', action.onlyTargets ? '"y"' : '"n"') return Object.assign(prevState, { onlyTargets: action.onlyTargets, }) } case 'SET_TITLE': { localStorage.setItem('Anton1.biAuditForm.smadReaportTitle', '"' + action.title + '"') return Object.assign(prevState, { title: action.title, }) } case 'CLEAR_ALL': { var defaultState = getDefaultState(); var defaultOnlyTargetsString = defaultState.onlyTargets ? "y" : "n" setItemLocalStorage('Anton1.biAuditForm.pracownicyIds', []); setItemLocalStorage('Anton1.biAuditForm.kontrahenciIds', []); setItemLocalStorage(getSelectedListLocalStorageKey('OD'), []); setItemLocalStorage(getSelectedListLocalStorageKey('DO'), []); setItemLocalStorage('Anton1.biAuditForm.pracownicy.filterIdGroup', 0); setItemLocalStorage('Anton1.biAuditForm.kontrahenci.filterIdGroup', 0); setItemLocalStorage('Anton1.biAuditForm.pracownicy.pagination.page', 1); setItemLocalStorage('Anton1.biAuditForm.kontrahenci.pagination.page', 1); setItemLocalStorage('Anton1.biAuditForm.analizaOnlyTargets', defaultOnlyTargetsString); setItemLocalStorage('Anton1.biAuditForm.depth', defaultState.depth); setItemLocalStorage('Anton1.biAuditForm.smadReaportTitle', defaultState.title); return Object.assign(prevState, defaultState) } default: return prevState; } } function getSelectedListLocalStorageKey(from) { // from: ( OD | DO ) switch (from) { case 'OD': return 'Anton1.biAuditForm.odFeatureKeys'; case 'DO': return 'Anton1.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(anton1RaportStore); global[STORE_NAME + '__defaults'] = getDefaultState;