Skip to content

Commit

Permalink
Hook up new API endpoint to the front-end
Browse files Browse the repository at this point in the history
Signed-off-by: Josh Black <raskchanky@gmail.com>
  • Loading branch information
raskchanky committed Nov 28, 2016
1 parent 8cdad55 commit 4ad3397
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 25 deletions.
11 changes: 6 additions & 5 deletions components/builder-depot/src/data_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,11 +170,12 @@ impl PackagesIndex {
Ok(ids) => {
// JW TODO: This in-memory sorting logic can be removed once the Redis sorted set
// is pre-sorted on write. For now, we'll do it on read each time.
let mut ids: Vec<package::PackageIdent> =
ids.iter().map(|id| package::PackageIdent::from_str(id).unwrap()).collect();
ids.sort();
let ids = ids.into_iter().map(|id| depotsrv::PackageIdent::from(id)).collect();
Ok(ids)
let mut idz: Vec<package::PackageIdent> = ids.iter()
.map(|iz| package::PackageIdent::from_str(&format!("{}/{}", &id, &iz)).unwrap())
.collect();
idz.sort();
let new_ids = idz.into_iter().map(|zd| depotsrv::PackageIdent::from(zd)).collect();
Ok(new_ids)
}
Err(e) => Err(Error::from(e)),
}
Expand Down
3 changes: 1 addition & 2 deletions components/builder-depot/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1049,8 +1049,7 @@ pub fn router(depot: Depot) -> Result<Chain> {

package_search: get "/pkgs/search/:query" => search_packages,
packages: get "/pkgs/:origin" => list_packages,
// JB TODO - this URL needs to change otherwise no one will be able to upload a package called "unique"
packages_unique: get "/pkgs/:origin/unique" => list_unique_packages,
packages_unique: get "/pkgs-unique/:origin" => list_unique_packages,
packages_pkg: get "/pkgs/:origin/:pkg" => list_packages,
package_pkg_latest: get "/pkgs/:origin/:pkg/latest" => show_package,
packages_version: get "/pkgs/:origin/:pkg/:version" => list_packages,
Expand Down
1 change: 1 addition & 0 deletions components/builder-web/app/actions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ export const populateExplore = packageActions.populateExplore;
export const setCurrentPackage = packageActions.setCurrentPackage;
export const setPackagesSearchQuery = packageActions.setPackagesSearchQuery;
export const setVisiblePackages = packageActions.setVisiblePackages;
export const getUniquePackages = packageActions.getUniquePackages;

export const addProject = projectActions.addProject;
export const fetchBuilds = projectActions.fetchBuilds;
Expand Down
29 changes: 22 additions & 7 deletions components/builder-web/app/actions/packages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,31 @@ export function fetchPackage(pkg) {
};
}

export function getUniquePackages(
origin: string,
nextRange: number = 0,
token: string = ""
) {
return dispatch => {
if (nextRange === 0) {
dispatch(clearPackages());
}

depotApi.getUnique(origin, nextRange).then(response => {
dispatch(setVisiblePackages(response["results"]));
dispatch(setPackagesTotalCount(response["totalCount"]));
dispatch(setPackagesNextRange(response["nextRange"]));
dispatch(fetchProjectsForPackages(response["results"], token));
}).catch(error => {
dispatch(setVisiblePackages(undefined, error));
});
};
}

export function filterPackagesBy(
params,
query: string,
nextRange: number = 0,
fetchProjects: boolean = false,
token: string = ""
nextRange: number = 0
) {
return dispatch => {
if (nextRange === 0) {
Expand All @@ -70,10 +89,6 @@ export function filterPackagesBy(
dispatch(setVisiblePackages(response["results"]));
dispatch(setPackagesTotalCount(response["totalCount"]));
dispatch(setPackagesNextRange(response["nextRange"]));

if (fetchProjects) {
dispatch(fetchProjectsForPackages(response["results"], token));
}
}).catch(error => {
dispatch(setVisiblePackages(undefined, error));
});
Expand Down
28 changes: 28 additions & 0 deletions components/builder-web/app/depotApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,34 @@ import {packageString} from "./util";

const urlPrefix = config["habitat_api_url"] || "";

export function getUnique(origin: string, nextRange: number = 0) {
const url = `${urlPrefix}/depot/pkgs-unique/${origin}?range=${nextRange}`;

return new Promise((resolve, reject) => {
fetch(url).then(response => {
if (response.status >= 400) {
reject(new Error(response.statusText));
}

response.json().then(resultsObj => {
let results;

const endRange = parseInt(resultsObj.range_end, 10);
const totalCount = parseInt(resultsObj.total_count, 10);
const nextRange = totalCount > (endRange + 1) ? endRange + 1 : 0;

if (resultsObj["package_list"]) {
results = resultsObj["package_list"];
} else {
results = resultsObj;
}

resolve({ results, totalCount, nextRange });
});
}).catch(error => reject(error));
});
}

export function get(params, nextRange: number = 0) {
const url = `${urlPrefix}/depot/pkgs/` +
(params["query"] ? `search/${params["query"]}`
Expand Down
18 changes: 7 additions & 11 deletions components/builder-web/app/origin-page/OriginPageComponent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ import {fetchOrigin, fetchOriginInvitations, fetchOriginMembers,
fetchOriginPublicKeys, inviteUserToOrigin, setCurrentOriginAddingPublicKey,
setCurrentOriginAddingPrivateKey, uploadOriginPrivateKey,
uploadOriginPublicKey, filterPackagesBy, fetchMyOrigins,
setProjectHint, requestRoute, setCurrentProject} from "../actions/index";
setProjectHint, requestRoute, setCurrentProject, getUniquePackages} from "../actions/index";
import config from "../config";
import {KeyAddFormComponent} from "./KeyAddFormComponent";
import {KeyListComponent} from "./KeyListComponent";
import {Origin} from "../records/Origin";
import {OriginMembersTabComponent} from "./OriginMembersTabComponent";
import {TabComponent} from "../TabComponent";
import {TabsComponent} from "../TabsComponent";
import {requireSignIn} from "../util";
import {requireSignIn, packageString} from "../util";
import {PackagesListComponent} from "../packages-list/PackagesListComponent";
import {Subscription} from "rxjs/Subscription";
import {FeatureFlags} from "../Privilege";
Expand Down Expand Up @@ -392,19 +392,15 @@ export class OriginPageComponent implements OnInit, OnDestroy {
}

public getPackages() {
this.store.dispatch(filterPackagesBy(
{origin: this.origin.name}, "", 0, true, this.gitHubAuthToken
));
this.store.dispatch(getUniquePackages(this.origin.name, 0, this.gitHubAuthToken));
}

private fetchMorePackages() {
this.store.dispatch(filterPackagesBy(
{origin: this.origin.name},
"",
this.store.dispatch(getUniquePackages(
this.origin.name,
this.store.getState().packages.nextRange,
true,
this.gitHubAuthToken));

this.gitHubAuthToken
));
return false;
}

Expand Down

0 comments on commit 4ad3397

Please sign in to comment.