Skip to content

Instantly share code, notes, and snippets.

@k0t0vich
Created August 17, 2019 15:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save k0t0vich/170a9658c6e80950a3205c341df396d3 to your computer and use it in GitHub Desktop.
Save k0t0vich/170a9658c6e80950a3205c341df396d3 to your computer and use it in GitHub Desktop.
import RecordData from "../data/RecordData";
import { observable, computed, action } from "mobx";
import DataLoader from "../services/DataLoader";
import TableData from './TableData';
export default class Store {
@observable records: RecordData[] = [];
@observable searchString: string = "";
@observable tableData:TableData = new TableData();
loadData(){
DataLoader.load<RecordData[]>("/testData.json").then((data: RecordData[]) =>
this.initRecords(data)
);
}
@action initRecords(records: RecordData[]) {
console.log("initRecords");
this.records = records;
}
@action changeSearchString(searchString:string){
this.searchString = searchString;
}
@computed get logged(){
return this.user !== null;
}
@computed get user(){
return localStorage.getItem("user");
}
@action saveUser(user:string){
localStorage.setItem("user", user);
}
@action clearUser(){
localStorage.removeItem("user");
}
@computed get filteredRecords(): RecordData[] {
if (this.searchString === "") return this.records;
return this.records.filter(
(record: RecordData, index: number) =>
record.accountId.indexOf(this.searchString) !== -1 ||
record.caseUid.indexOf(this.searchString) !== -1 ||
record.creationDate.indexOf(this.searchString) !== -1 ||
record.publicId.indexOf(this.searchString) !== -1 ||
record.reference.indexOf(this.searchString) !== -1 ||
record.status.indexOf(this.searchString) !== -1
);
}
@computed get visibleRecords():RecordData[] {
return this.filteredRecords.slice(this.tableData.start,this.tableData.end);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment