Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: adjust code fetching all projects #989

Merged
merged 2 commits into from
Jul 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 15 additions & 26 deletions src/api-client/project.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ function getFilesTreeLazy(client, files, projectId, openFilePath, lfsFiles) {

function addProjectMethods(client) {

client.getProjects = (queryParams = {}) => {
client.getProjects = async (queryParams = {}) => {
let headers = client.getBasicHeaders();
return client.clientFetch(`${client.baseUrl}/projects`, {
method: "GET",
Expand All @@ -107,33 +107,22 @@ function addProjectMethods(client) {
});
};

client.getAllProjects = (extraParams = [],
{ recursive = false, per_page = 100, page = 1, previousResults = [] } = {}
) => {
let headers = client.getBasicHeaders();
client.getAllProjects = async (queryParams = {}) => {
let projects = [];
let page = 1;
let finished = false;

const queryParams = {
recursive,
per_page,
page,
...extraParams
};
while (!finished) {
const resp = await client.getProjects({ ...queryParams, page });
projects = [...projects, ...resp.data];

return client.clientFetch(`${client.baseUrl}/projects`, {
method: "GET",
headers,
queryParams,
}).then(response => {
if (response.pagination.nextPageLink !== undefined) {
return client.getAllProjects(extraParams, {
recursive,
per_page,
previousResults: previousResults.concat(response.data),
page: response.pagination.nextPage
});
}
return previousResults.concat(response.data);
});
if (!resp.pagination.nextPageLink)
finished = true;
else
page = resp.pagination.nextPage;
}

return projects;
};

client.getAvatarForNamespace = (namespaceId = {}) => {
Expand Down
3 changes: 2 additions & 1 deletion src/landing/Landing.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ class HomeProjects extends Component {
}

componentDidMount() {
this.projectsCoordinator.getFeatured();
if (this.props.user.logged)
this.projectsCoordinator.getFeatured();
}

mapStateToProps(state, ownProps) {
Expand Down
4 changes: 2 additions & 2 deletions src/landing/NavBar.js
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ class LoggedInNavBar extends Component {
</button>

<div className="collapse navbar-collapse flex-wrap" id="navbarSupportedContent">
<QuickNav client={this.props.client} model={this.props.model} />
<QuickNav client={this.props.client} model={this.props.model} user={this.props.user} />

<div className="d-flex flex-grow-1">
<ul className="navbar-nav mr-auto">
Expand Down Expand Up @@ -261,7 +261,7 @@ class AnonymousNavBar extends Component {
</button>

<div className="collapse navbar-collapse flex-wrap" id="navbarSupportedContent">
<QuickNav client={this.props.client} model={this.props.model} />
<QuickNav client={this.props.client} model={this.props.model} user={this.props.user} />

<div className="d-flex flex-grow-1">
<ul className="navbar-nav mr-auto">
Expand Down
8 changes: 5 additions & 3 deletions src/project/Project.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,11 @@ class View extends Component {
this.projectCoordinator = new ProjectCoordinator(props.client, props.model.subModel("project"));

// fetch useful projects data in not yet loaded
const featured = props.model.get("projects.featured");
if (!featured.fetched && !featured.fetching)
this.projectsCoordinator.getFeatured();
if (this.props.user.logged) {
const featured = props.model.get("projects.featured");
if (!featured.fetched && !featured.fetching)
this.projectsCoordinator.getFeatured();
}
}

componentDidMount() {
Expand Down
28 changes: 10 additions & 18 deletions src/project/shared/Projects.state.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,25 +51,17 @@ class ProjectsCoordinator {
}

async getFeatured() {
if (this.model.get("featured.fetching")) return;
// set status to fetching and invoke both APIs
if (this.model.get("featured.fetching"))
return;
// set status to fetching, get all the projects and filter and invoke both APIs
this.model.set("featured.fetching", true);
const promiseStarred = this.client.getProjects({ starred: true, order_by: "last_activity_at", per_page: 100 })
.then((projectResponse) => {
const projects = projectResponse.data.map((project) => this._starredProjectMetadata(project));
return projects;
})
.catch((error) => {
this.model.set("starredProjects", []);
});
const promiseMember = this.client.getAllProjects({ membership: true, order_by: "last_activity_at", per_page: 100 })
.then((projectResponse) => {
const projects = projectResponse.map((project) => this._starredProjectMetadata(project));
return projects;
})
.catch((error) => {
this.model.set("memberProjects", []);
});
const params = { order_by: "last_activity_at", per_page: 100 };
const promiseStarred = this.client.getAllProjects({ ...params, starred: true })
.then(resp => resp.map((project) => this._starredProjectMetadata(project)))
.catch(error => []);
const promiseMember = this.client.getAllProjects({ ...params, membership: true })
.then(resp => resp.map((project) => this._starredProjectMetadata(project)))
.catch(error => []);

// set `featured` content and return only `starred` and `member` projects data
return Promise.all([promiseStarred, promiseMember]).then(values => {
Expand Down
8 changes: 5 additions & 3 deletions src/utils/quicknav/QuickNav.container.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,11 @@ class QuickNavContainerWithRouter extends Component {
super(props);
this.bar = new SearchBarModel(StateKind.REACT, this);
this.projectsCoordinator = new ProjectsCoordinator(props.client, props.model.subModel("projects"));
const featured = this.projectsCoordinator.model.get("featured");
if (!featured.featured && !featured.fetching)
this.projectsCoordinator.getFeatured();
if (this.props.user.logged) {
const featured = this.projectsCoordinator.model.get("featured");
if (!featured.featured && !featured.fetching)
this.projectsCoordinator.getFeatured();
}

this.callbacks = {
onChange: this.onChange.bind(this),
Expand Down