Skip to content

Commit

Permalink
#68 Updated pageInfo, added total count of items.
Browse files Browse the repository at this point in the history
  • Loading branch information
artzub committed Feb 3, 2022
1 parent 8f3c7c7 commit dc335fd
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 7 deletions.
9 changes: 9 additions & 0 deletions src/redux/api/github/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ export const parseRateLimit = (headers = {}) => ({
});

const reg = /page=(\d+)>; rel="next"/;
const regPerPage = /per_page=(\d+)/;
const regPageCount = /page=(\d+)>; rel="last"/;
export const parsePageInfo = ({ link = '' } = {}) => {
const hasNextPage = Boolean(link && link.includes('rel="next"'));
let nextPage;
Expand All @@ -13,7 +15,14 @@ export const parsePageInfo = ({ link = '' } = {}) => {
nextPage = +link.match(reg)[1];
}

let total = 0;
if (Boolean(link && link.includes('rel="last"'))) {
total = +link.match(regPerPage)[1];
total *= +link.match(regPageCount)[1];
}

return {
total,
nextPage,
hasNextPage,
};
Expand Down
4 changes: 2 additions & 2 deletions src/redux/api/githubGQL/getBranches/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { addCursorAfter } from "@/redux/api/githubGQL/utils";
import { addCursorAfter, parsePageInfo } from "@/redux/api/githubGQL/utils";
import { withCancellation } from "@/redux/utils";
import getClient from '../getClient';
import query from './query.graphql';
Expand Down Expand Up @@ -29,7 +29,7 @@ export const getBranches = ({ owner, repo, page = '', perPage = 10 }) =>

return {
data: data?.repository?.refs?.nodes,
pageInfo: data?.repository?.refs?.pageInfo,
pageInfo: parsePageInfo(data?.repository?.refs),
rateLimit: data?.rateLimit,
};
});
8 changes: 5 additions & 3 deletions src/redux/api/githubGQL/getCommits/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { withCancellation } from "@/redux/utils";
import getClient from '../getClient';
import { addCursorAfter } from "../utils";
import { addCursorAfter, parsePageInfo } from "../utils";
import query from './query.graphql';

/**
Expand Down Expand Up @@ -29,7 +29,9 @@ export const getCommits = ({ owner, repo, branch, page = '', perPage = 10 }) =>
},
});

const ids = data?.repository?.ref?.target?.history?.nodes?.map(({ oid }) => oid) || [];
const history = data?.repository?.ref?.target?.history || {};

const ids = history.nodes?.map(({ oid }) => oid) || [];

const commits = await Promise.all(ids.map(async (ref) => {
const d = await client.repos.getCommit({
Expand All @@ -45,7 +47,7 @@ export const getCommits = ({ owner, repo, branch, page = '', perPage = 10 }) =>

return {
data: commits,
pageInfo: data?.repository?.ref?.target?.history?.pageInfo,
pageInfo: parsePageInfo(history),
rateLimit: data?.rateLimit,
};
});
1 change: 1 addition & 0 deletions src/redux/api/githubGQL/getCommits/query.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ query($owner:String!, $repo:String!, $branch: String!, $perPage: Int!, ${this.cu
nodes {
oid
}
totalCount
pageInfo {
nextPage: endCursor,
hasNextPage
Expand Down
4 changes: 2 additions & 2 deletions src/redux/api/githubGQL/getRepositories/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { withCancellation } from "@/redux/utils";
import getClient from '../getClient';
import { addCursorAfter } from "../utils";
import { addCursorAfter, parsePageInfo } from "../utils";
import query from './query.graphql';

/**
Expand All @@ -27,7 +27,7 @@ export const getRepositories = ({ owner, page = '', perPage = 10 }) =>

return {
data: data?.profile?.repositories?.nodes,
pageInfo: data?.profile?.repositories?.pageInfo,
pageInfo: parsePageInfo(data?.profile?.repositories),
rateLimit: data?.rateLimit,
};
});
1 change: 1 addition & 0 deletions src/redux/api/githubGQL/getRepositories/query.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ query($owner: String!, $perPage: Int!, ${this.cursorArgument}) {
}
profile: repositoryOwner(login: $owner) {
repositories(first: $perPage, ${this.after}) {
totalCount
pageInfo {
nextPage: endCursor,
hasNextPage
Expand Down
5 changes: 5 additions & 0 deletions src/redux/api/githubGQL/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,8 @@ export const parseRateLimit = ({ cost, remaining, resetAt } = {}) => ({
cost,
resetAt: resetAt && +(new Date(resetAt)),
});

export const parsePageInfo = ({ pageInfo, totalCount } = {}) => ({
...(pageInfo || {}),
total: totalCount ?? 0,
});

0 comments on commit dc335fd

Please sign in to comment.