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

Feature/code improvements n best practices #87

Merged
merged 14 commits into from
Jul 7, 2017
Merged

Feature/code improvements n best practices #87

merged 14 commits into from
Jul 7, 2017

Conversation

hardikpthv
Copy link
Contributor

@hardikpthv hardikpthv commented Jul 1, 2017

#75 implemented

  • fix: Updated header format

  • refactor: Updated @Output label, import statements, members and methods sequence are alphabetized as per style guide.

  • fix: Moved service file into feature folder along with component

  • docs: Updated readme

@UncleDave UncleDave added this to the v1.0.0 milestone Jul 1, 2017
README.md Outdated
@@ -62,27 +62,25 @@ you can use `[headers]` directive like this.

#### Callbacks
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While we're here - though it's not strictly incorrect, I would rename this to either "Outputs" or "Events".

README.md Outdated
@@ -62,27 +62,25 @@ you can use `[headers]` directive like this.

#### Callbacks

`(onFileUploadFinish)="imageUploaded($event)"`. If `[url]` is specified this event is fired when component gets a response from the server, also in this case event has field `serverResponse` which contains the status code and response from the server `{status, response}`. If `[url]` is not specified it's fired immediately after an image(s) dropped into file-drop zone of choosed in file browser. So what you can do, is not specify `[url]` to handle upload yourself, for exapmple send the image into firebase storage. To get file use `event.file`.
`(uploadFinish)="imageUploaded($event)"`. If `[url]` is specified this event is fired when component gets a response from the server, also in this case event has field `serverResponse` which contains the status code and response from the server `{status, response}`. If `[url]` is not specified it's fired immediately after an image(s) dropped into file-drop zone of choosed in file browser. So what you can do, is not specify `[url]` to handle upload yourself, for exapmple send the image into firebase storage. To get file use `event.file`.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By convention I think we want to go with past tense here: uploadFinished - the event has already occurred.
The example function here should also by convention be suggested as onUploadFinished($event)

All in all:

(uploadFinished)="onUploadFinished($event)"

README.md Outdated

`(onRemove)="imageRemoved($event)"` - this event is fired when remove button was clicked and the image preview was removed. *Note that this library doesn't handle deletion from server so you should do it yourself*. Event passed as the argument is the exact same object that was passed to the `(imageUploaded)` callback when image was added so you can access `serverResponse` to get a key to delete your image from server.
`(remove)="imageRemoved($event)"` - this event is fired when remove or clear button was clicked and the image preview was removed. *Note that this library doesn't handle deletion from server so you should do it yourself*. Event passed as the argument is the exact same object that was passed to the `(imageUploaded)` callback when image was added so you can access `serverResponse` to get a key to delete your image from server.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above but (removed)="onRemoved($event)"

README.md Outdated

`(isPending)="disableSendButton($event)"` - this event is fired when pending state was changed. Event is just a boolean that represents the pending state. Pending state is `true` when and only when component avaits a response from the server, and `false` othervise. You can use it, for example, to disable send button in your form until all images are uploaded.
`(uploadStateChange)="disableSendButton($event)"` - this event is fired when image upload state was changed. Event is just a boolean that represents the uploading state. Image upload state is `true` when and only when component awaits the response from the server, and `false` otherwise. You can use it, for example, to disable send button in your form until all images are uploaded.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above but (uploadStateChanged)="onUploadStateChanged($event)"


@Directive({
selector: '[fileDrop]'
})
export class FileDropDirective {
@Input() accept: string[];
@Output() isFileOver: EventEmitter<boolean> = new EventEmitter<boolean>();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again for conventions I would go with fileOver


constructor(private http: Http) { }

public postImage(url: string, image: File, headers?: any[], partName: string = 'image',
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Headers shouldn't be any.

export interface Header {
    [name: string]: any;
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, let it be as Header itself.

let key = Object.keys(header)[0];
options.headers.append(key, header[key]);
}
}
Copy link
Collaborator

@UncleDave UncleDave Jul 1, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At this point we can probably just get rid of lines 22 - 28 and replace with options.headers = headers - if that doesn't work (i'm unsure) then options.headers = new Headers(headers) should.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, this options.headers = headers is not working since Headers class has a few methods in it so continue with options.headers = new Headers() but again need to set header in expected format otherwise it goes like this 0:[object Object].

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe the Headers constructor accepts { [name: string]: any }[]?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay I've been poking around in angular - it expects either a Headers object or a dictionary, which is why it wasn't happy with the array.

I suggest we just use this signature wherever we need headers, and get rid of the interface (sorry, I know I asked you to do it):

Headers|{[name: string]: any}

Then the following will work:

myFunc(headers: Headers|{[name: string]: any}) {
  const options: any = {};
  options.headers = new Headers(headers);
}

The user can pass this in by instantiating their own Headers object or something like:

const headers: {[name: string]: any} = {
  potato: 'cheese',
  cheese: 'potato'
};

@Input() buttonCaption: string = 'Select Images';
@Input() dropBoxMessage: string = 'Drop your images here!';
@Input() fileTooLargeMessage: string;
@Input() headers: any;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't be any - details in image.service.ts comment.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alright, i will set it as Header.

@UncleDave
Copy link
Collaborator

Note to merger: Squash into a conventional commit.

Copy link
Owner

@aberezkin aberezkin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same remarks as UncleDave's

@UncleDave
Copy link
Collaborator

Thank you for the changes so far, that all looks good - just the Headers issue to address.

let key = Object.keys(header)[0];
options.headers.append(key, header[key]);
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay I've been poking around in angular - it expects either a Headers object or a dictionary, which is why it wasn't happy with the array.

I suggest we just use this signature wherever we need headers, and get rid of the interface (sorry, I know I asked you to do it):

Headers|{[name: string]: any}

Then the following will work:

myFunc(headers: Headers|{[name: string]: any}) {
  const options: any = {};
  options.headers = new Headers(headers);
}

The user can pass this in by instantiating their own Headers object or something like:

const headers: {[name: string]: any} = {
  potato: 'cheese',
  cheese: 'potato'
};

Copy link
Collaborator

@UncleDave UncleDave left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Last bit of header tidy!

@hardikpthv
Copy link
Contributor Author

Need to check with proposed changes. Since let options: RequestOptionsArgs = new RequestOptions(); is declared then const options: any = {}; will reset format to be sent.

@hardikpthv
Copy link
Contributor Author

Changing the format from array to object { potato: 'cheese', cheese: 'potato' } solves the problem, nothing else is required. Let me finally update...

@UncleDave
Copy link
Collaborator

const options: any = {}; was just an example, I don't mean for you to do that bit 😛

@hardikpthv
Copy link
Contributor Author

Alrighty then! 😆

@hardikpthv
Copy link
Contributor Author

hardikpthv commented Jul 7, 2017

@UncleDave, demo source is updated, need to generate new bundle.

@UncleDave
Copy link
Collaborator

Okay I'm going to merge this but I still have some changes I'd like to make to headers, don't get offended 😄

@UncleDave UncleDave merged commit 29d288e into aberezkin:development Jul 7, 2017
@hardikpthv hardikpthv deleted the feature/code_improvements_n_best_practices branch July 7, 2017 11:25
@hardikpthv
Copy link
Contributor Author

That's fine @UncleDave 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants