#4213 FIX - Kundensuche Trefferliste verschwindet nach Tab wechseln

This commit is contained in:
Lorenz Hilpert
2023-07-20 10:07:22 +02:00
parent ce5388be61
commit c9fbbd78a8
3 changed files with 41 additions and 38 deletions

View File

@@ -15,6 +15,7 @@ import {
debounceTime,
take,
first,
map,
} from 'rxjs/operators';
import { CrmCustomerService } from '@domain/crm';
import { Result } from '@domain/defs';
@@ -23,6 +24,7 @@ import { Filter } from '@shared/components/filter';
import { isEmpty } from 'lodash';
import { DomainOmsService } from '@domain/oms';
import { OrderDTO, OrderItemDTO, OrderListItemDTO } from '@swagger/oms';
import { hash } from '@utils/common';
@Injectable()
export class CustomerSearchStore extends ComponentStore<CustomerSearchState> implements OnStoreInit, OnDestroy {
@@ -302,9 +304,11 @@ export class CustomerSearchStore extends ComponentStore<CustomerSearchState> imp
first()
)
),
withLatestFrom(this.filter$, this.processId$, this.restoreSearchResult()),
tap(() => {
withLatestFrom(this.filter$, this.processId$),
map(([a1, a2, a3]) => {
this.patchState({ fetchingCustomerList: true, customerList: [], customerListCount: 0 });
return [a1, a2, a3, this.restoreSearchResult()] as [{ ignoreRestore?: boolean }, Filter, number, boolean];
}),
switchMap(([{ ignoreRestore }, filter, processId, restored]) =>
this._customerService
@@ -393,52 +397,36 @@ export class CustomerSearchStore extends ComponentStore<CustomerSearchState> imp
this.patchState({ queryParams });
}
async _getSearchResultKey() {
const _filter = await this.filter$
.pipe(
filter((f) => !!f),
take(1)
)
.toPromise();
_getSearchResultKey() {
const jsonStr = JSON.stringify(this.queryParams);
const queryParams = _filter.getQueryParams();
const encoder = new TextEncoder();
const data = encoder.encode(JSON.stringify(queryParams));
const hash = await crypto.subtle.digest('SHA-1', data);
return Array.from(new Uint8Array(hash))
.map((b) => b.toString(16))
.join('');
return hash(jsonStr);
}
cacheSearchResult() {
const customerList = this.customerList;
const customerListCount = this.customerListCount;
this._getSearchResultKey().then((key) => {
window.sessionStorage.setItem(
String(key),
JSON.stringify({
customerList,
customerListCount,
})
);
});
const key = this._getSearchResultKey();
window.sessionStorage.setItem(
String(key),
JSON.stringify({
customerList,
customerListCount,
})
);
}
restoreSearchResult() {
return this._getSearchResultKey().then((key) => {
const cache = window.sessionStorage.getItem(String(key));
const key = this._getSearchResultKey();
const cache = window.sessionStorage.getItem(String(key));
if (cache) {
const { customerList, customerListCount } = JSON.parse(cache);
this.patchState({ customerList, customerListCount, fetchingCustomerList: false });
this._customerListRestored.next();
return true;
}
return false;
});
if (cache) {
const { customerList, customerListCount } = JSON.parse(cache);
this.patchState({ customerList, customerListCount, fetchingCustomerList: false });
this._customerListRestored.next();
return true;
}
return false;
}
cacheCustomerInfo() {

View File

@@ -0,0 +1,14 @@
export function hash(value: any): string {
const jsonStr = JSON.stringify(value);
let hash = 0;
if (jsonStr.length == 0) return hash.toString(16);
for (let i = 0; i < jsonStr.length; i++) {
const char = jsonStr.charCodeAt(i);
hash = (hash << 5) - hash + char;
hash = hash & hash; // Convert to 32bit integer
}
return hash.toString(16);
}

View File

@@ -1,5 +1,6 @@
export * from './contains-element';
export * from './geo-distance';
export * from './hash';
export * from './is-array';
export * from './is-boolean';
export * from './is-null-or-undefined';