All files / src/components/share-button share-button.ts

100% Statements 35/35
85.71% Branches 12/14
100% Functions 10/10
100% Lines 32/32
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80                        1x   1x 1x 1x 1x 1x   6x 6x 6x 6x 6x     3x 3x 2x 1x 1x     1x       1x 1x 1x   1x 1x 1x           3x 3x               2x 1x             1x   1x 1x 1x             1x  
import { Component, Input } from '@angular/core';
import { GoogleAnalytics } from '@ionic-native/google-analytics';
import { SocialSharing } from '@ionic-native/social-sharing';
import { PromptService } from '../../providers/prompt-service/prompt-service';
import { TrackerService } from '../../providers/tracker-service/tracker-service';
import { Screenshot } from '@ionic-native/screenshot';
 
@Component( {
    selector: 'share-button',
    templateUrl: 'share-button.html'
} )
 
export class ShareButtonComponent {
 
    @Input() private options: any;
    @Input() private trackingOptions: any;
    @Input() private hasLabel: boolean;
    @Input() private doScreenShot: boolean;
    @Input() private content: boolean;
 
    constructor ( private ga: GoogleAnalytics,
                private socialSharing: SocialSharing,
                private prompt: PromptService,
                private tracker: TrackerService,
                private screenshot: Screenshot ) {
    }
 
    private onClick () {
        if ( this.doScreenShot ) {
            this.screenshot.URI( 100 ).then(
                result => { this.onScreenshotComplete( result ); },
                error => { this.onScreenshotError( error ); }
            );
        } else {
            this.shareIt( this.options );
        }
    }
 
    private onScreenshotComplete ( result ) {
        this.options.image = result.URI;
        this.shareIt( this.options );
    }
    private onScreenshotError ( error ) {
        console.log( 'Error: ', error );
        this.prompt.presentMessage( {
            classNameCss: 'error',
            message: `Une erreur s'est produite lors de la screenshot : \n ${ error.toString() }`
        } );
    }
 
    private shareIt ( options ) {
        this.socialSharing
            .share(
            options.message || null,
            options.subject || null,
            options.image || null,
            options.url || null
            )
            .then( () => {
                if ( this.trackingOptions ) {
                    this.tracker.trackEventWithData(
                        this.trackingOptions.category,
                        this.trackingOptions.action,
                        this.trackingOptions.label );
                }
            } )
            .catch( e => {
                Eif ( this.trackingOptions ) {
                    // TODO translate
                    const lbl = 'Une erreur s\'est produite pour partager';
                    const msg = `${ lbl } ${ this.trackingOptions.label }: \n ${ e.toString() }`;
                    this.prompt.presentMessage( {
                        classNameCss: 'error',
                        message: msg
                    } );
                }
            } );
    }
}