I have tried a lot of ways to show the records in below format.
If, I create "BASIC" subscriber then its "value = 0" and for "PREMIUM" its "value = 1".
For "BASIC" subscriber I want to show Latest 30 records. Like:
For "PREMIUM" subscriber I want to show Latest 50 records with pageSize of 30. Like:
Below, Is my code, Please correct me where i am wrong. Thank you for any help.
Condition:
// const pageSize = parseInt(50);
this.state = {
pageInfo: {
offset: 0,
limit: (userType === 1)? premium: basic,
total: -1
},
currentPage: 1,
totalRecords: null,
currentRecords: [],
logRecords: [],
}
// Here, I am fetching data from API using ajax request
myUserRecords(){
var pageInfo = {};
pageInfo = this.state.pageInfo;
pageInfo = { ...pageInfo, total: -1 }
if (this.state.currentRecords.length !== 0 && ((this.state.currentPage *
this.pageLimit) - this.pageLimit) > this.state.currentRecords.length) {
this.setState({ currentPage: 1 })
pageInfo = { ...pageInfo, offset: 0 }
}
$.ajax({
method: "GET",
url: API + encodeURIComponent(JSON.stringify(filter)),
dataType: 'json',
})
.done(function (result) {
this.setState({ logRecords : result.records, pageInfo : result.pageInfo });
let currentPage = this.state.currentPage;
if (result.records.length !== 0 && this.state.currentPage === 0) {
currentPage = 1;
}
if(currentPage>1){
self.setState({
currentPage: currentPage, records: resultObj.records,
pageInfo: resultObj.pageInfo
});
}else{
self.setState({
currentPage: currentPage, totalRecords: resultObj.pageInfo.total, records: resultObj.records,
pageInfo: resultObj.pageInfo
});
}
})
.fail(function (error) {
if(error == "error"){ return }
});
}
getStartEndValue = () => {
let start =((this.state.currentPage-1) * this.pageLimit) + 1;
let end = this.state.currentPage * this.pageLimit;
if(end > this.state.userRecords)
{
end = this.state.userRecords
}
return { start , end }
}
onPageChanged = (data) => {
const { records } = this.state;
const { currentPage, totalPages, pageLimit } = data;
let new_offset = (currentPage * pageLimit) - pageLimit
let pageInfo = { ...this.state.pageInfo, offset: new_offset }
if (currentPage === 0 && this.state.currentRecords.length > 0) {
currentPage = 1
}
const offset = (currentPage - 1) * pageLimit;
const currentRecords = records.slice(offset, offset + pageLimit);
this.setState({ currentPage, currentRecords, totalPages, pageInfo }, () => {
this.myUserRecords();
});
};
render() {
let { start , end } = this.getStartEndValue();
<div className="paginationNew">
<div className="recordCount">
{this.state.totalRecords > 0 ?
<h2 className={"text-dark"}>
{/* <strong className="text-secondary"> {start} - {end}
out of {this.state.totalRecords}</strong>{" "} */}
<strong className="text-secondary"> {start} - {end} out of
{this.state.pageInfo.limit}</strong>{" "}
</h2> : "No records found" }
</div>
<div className="paginationComp">
<NewPagination
totalRecords={this.state.totalRecords}
pageLimit={(userType === 1)? premium: basic}
pageNeighbours={1}
onPageChanged={this.onPageChanged}
current_page={this.state.currentPage}
/>
</div>
</div>
}
const pageSize = parseInt(30);
constructor() {
super();
this.state = {
pageSize: (userType === 1)? pageSize: basic,
pageInfo: {
offset: 0,
limit: (userType === 1)? pageSize: basic,
total: 0
},
totalRecords: null,
totalCount: (userType === 1)? premium: basic,
}
this.pageLimit = this.state.pageSize;
}
In loadRecords(), i give condition after API call inside the .done function.
If totalRecords is > then (Basic/Premium) limit value the it will
give only logCount values of record which is already assign for (BASIC/Premium), otherwise, giving totalRecords.
loadRecords(){
.done{
const totalRecords = (result.pageInfo.total > logCount)? logCount: result.pageInfo.total;
this.setState({ currentPage : currentPage, totalRecords : totalRecords, records: result.records, pageInfo : result.pageInfo});
}
}