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

Start working on projects front end #1107

Merged
merged 3 commits into from
Jul 26, 2016
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
16 changes: 16 additions & 0 deletions components/builder-web/app/BuilderApiClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,22 @@ export class BuilderApiClient {
});
}

public createProject(project) {
return new Promise((resolve, reject) => {
fetch(`${this.urlPrefix}/projects`, {
body: JSON.stringify(project),
headers: this.headers,
method: "POST",
}).then(response => {
if (response.ok) {
resolve(response.json());
} else {
reject(new Error(response.statusText));
}
}).catch(error => reject(error));
});
}

public getMyOriginInvitations() {
return new Promise((resolve, reject) => {
fetch(`${this.urlPrefix}/user/invitations`, {
Expand Down
17 changes: 12 additions & 5 deletions components/builder-web/app/actions/projects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import * as fakeApi from "../fakeApi";
import {Observable} from "rxjs";
import {BuilderApiClient} from "../BuilderApiClient";
import {addNotification} from "./notifications";
import {DANGER, INFO, SUCCESS, WARNING} from "./notifications";
import {requestRoute} from "./router";
Expand All @@ -32,16 +33,22 @@ export const POPULATE_PROJECT = "POPULATE_PROJECT";
export const SET_CURRENT_PROJECT = "SET_CURRENT_PROJECT";
export const SET_PROJECTS = "SET_PROJECTS";

export function addProject(project) {
export function addProject(project: Object, token: string) {
return dispatch => {
// TODO: put this back. Removed the method when the builder api client
// underwhen some changes
new BuilderApiClient(token).createProject(project).then(response => {
dispatch(addNotification({
title: "Project created",
body: `Created ${project["name"]}.`,
type: SUCCESS,
}));
console.log(response);
}).catch(error => {
dispatch(addNotification({
title: "Failed to Create project",
body: "",
body: error.message,
type: DANGER,
}));

});
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import {Component, OnInit} from "angular2/core";
import {ControlGroup, FormBuilder, Validators} from "angular2/common";
import {RouteParams, RouterLink} from "angular2/router";
import {addProject} from "../actions/index";
import {addProject, fetchMyOrigins} from "../actions/index";
import {AppStore} from "../AppStore";
import {CheckingInputComponent} from "../CheckingInputComponent";
import {GitHubApiClient} from "../GitHubApiClient";
Expand Down Expand Up @@ -60,13 +60,21 @@ import {requireSignIn} from "../util";
</div>
<div class="name">
<label for="name">Project Name</label>
<hab-checking-input autofocus=true
<small>Must be unique, contain no spaces, and begin with a lowercase letter or number.</small>
<small>
Allowed characters include
<em>a&thinsp;&ndash;&thinsp;z</em>,
<em>0&thinsp;&ndash;&thinsp;9</em>,
<em>_</em>, and <em>-</em>.
No more than {{maxNameLength}} characters.
</small>
<hab-checking-input autofocus=true
displayName="Name"
[form]="form"
id="name"
[isAvailable]="false"
name="name"
placeholder="Required. Max 40 characters."
placeholder="Required. Max {{maxNameLength}} characters."
[value]="repo">
</hab-checking-input>
</div>
Expand All @@ -79,7 +87,7 @@ import {requireSignIn} from "../util";
id="plan"
[isAvailable]="doesFileExist"
[maxLength]="false"
name="plan"
name="plan_path"
notAvailableMessage="does not exist in repository"
[pattern]="false"
value="/plan.sh">
Expand All @@ -101,14 +109,15 @@ export class ProjectCreatePageComponent implements OnInit {
private doesFileExist: Function;
private form: ControlGroup;
private isProjectAvailable: Function;
private maxNameLength: Number = 255;

constructor(private formBuilder: FormBuilder,
private routeParams: RouteParams, private store: AppStore) {
this.form = formBuilder.group({
repo: [this.repo || "", Validators.required],
origin: [this.store.getState().origins.current.name,
origin: [this.myOrigins.first(),
Validators.required],
plan: ["/plan.sh", Validators.required],
plan_path: ["/plan.sh", Validators.required],
});

this.doesFileExist = function (path) {
Expand Down Expand Up @@ -141,19 +150,27 @@ export class ProjectCreatePageComponent implements OnInit {
return (this.ownerAndRepo || "").split("/")[1];
}

get token() {
return this.store.getState().gitHub.authToken;
}

private addProject(values) {
this.store.dispatch(addProject(values));
// Change the format to match what the server wants
values.vcs = { url: values.repo };
delete values.repo;

this.store.dispatch(addProject(values, this.token));
return false;
}

public ngOnInit() {
requireSignIn(this);

this.store.dispatch(fetchMyOrigins(this.token));
// Wait a second to set the fields as dirty to do validation on page
// load. Doing this later in the lifecycle causes a changed after it was
// checked error.
setTimeout(() => {
this.form.controls["plan"].markAsDirty();
this.form.controls["plan_path"].markAsDirty();
this.form.controls["name"].markAsDirty();
} , 1000);
}
Expand Down