import moment from 'moment' ; import { Task , TaskList } from '../../types/google' ; export class GoogleTasksService { private readonly clientId : string = '721709625729-0jp536rce8pn3i5ie0pg213d2t55mu55.apps.googleusercontent.com' ; private readonly scopes : string = 'https://www.googleapis.com/auth/tasks' ; private readonly discoveryDocs = [ 'https://www.googleapis.com/discovery/v1/apis/tasks/v1/rest' ] ; private isLoaded : boolean = false ; private auth : any ; private google = window . gapi ; /**
     * Loads the client library and gets all the api required information from google servers
     *
     */ loadScript ( ) { if ( this . isLoaded ) return Promise . resolve ( ) ; const self = this ; return new Promise ( ( resolve , reject ) => { const script = document . createElement ( 'script' ) ; script . src = 'https://apis.google.com/js/api.js' ; script . async = true ; script . defer = true ; script . onerror = ( event , source ? , lineno ? , colno ? , error ? ) => { console . error ( 'Error loading GAPI' ) ; console . error ( event ) ; reject ( error || 'Error loading GAPI' ) ; } ; script . onload = ( ) => { gapi . load ( 'client:auth2' , async ( ) => { const gapiInit = async ( ) => { await gapi . client . init ( { clientId : this . clientId , discoveryDocs : this . discoveryDocs , scope : this . scopes , } ) . then ( ( ) => { this . google = gapi ; self . isLoaded = true ; resolve ( ) ; } ) ; } ; try { await gapiInit ( ) ; } catch ( e ) { console . error ( 'Error initializing GAPI' ) ; console . error ( e ) ; const retry = async ( ) => { try { await gapiInit ( ) ; } catch ( e2 ) { reject ( e2 ) ; } } ; setTimeout ( retry , 2000 ) ; } } ) ; } ; document . body . appendChild ( script ) ; } ) ; } /**
     * Gets the client authorization to query google's API
     *
     */ async load ( callback ) { await this . loadScript ( ) ; this . auth = this . google . auth2 . getAuthInstance ( ) ; this . subscribeSigninStatus ( callback ) ; } /**
     * Returns whether the current session is signed in or not
     *
     */ isSignedIn ( ) { if ( ! this . auth ) return false ; return this . auth . isSignedIn . get ( ) ; } param subscriber subscribeSigninStatus ( subscriber ) { if ( ! this . auth ) return false ; subscriber ( this . isSignedIn ( ) ) ; return this . auth . isSignedIn . listen ( subscriber ) ; } /**
     * Starts the sign in process against your Google Account
     *
     */ signIn ( ) { this . auth . signIn ( ) ; } /**
     * Starts the sign out process against your Google Account
     *
     */ signOut ( ) { this . auth . signOut ( ) ; } returns async listTaskLists ( ) { const response = await this . google . client . tasks . tasklists . list ( ) ; return response . result . items . map ( ( item ) : TaskList => ( { id : item . id , title : item . title , updatedAt : moment ( item . updated ) , } as TaskList ) ) ; } returns async listTasks ( taskListId , pageToken = '' ) { const response = await this . google . client . tasks . tasks . list ( { tasklist : taskListId , showCompleted : false , showHidden : true , pageToken , } ) ; let result = [ ] ; const { items } = response . result ; if ( ! items ) return result ; const nextPageToken = response . result . nextPageToken as string ; const mapItems = ( tasks ) : Task [ ] => { return tasks . map ( ( item ) : Task => ( { id : item . id , title : item . title ? item . title : '' , notes : item . notes ? item . notes : '' , dueAt : item . due ? moment ( item . due ) : undefined , parent : item . parent , completed : item . status === 'completed' , completedAt : item . completed ? moment ( item . completed ) : undefined , updatedAt : moment ( item . updated ) , listId : taskListId , status : item . status , isDirty : false , subtasks : mapItems ( items . filter ( ( subitem ) => subitem . parent === item . id ) ) , } as Task ) ) ; } ; result = result . concat ( mapItems ( items . filter ( ( subitem ) => ! subitem . parent ) ) ) ; if ( nextPageToken ) { result = result . concat ( await this . listTasks ( taskListId , nextPageToken ) ) ; } return result ; } param taskList insertTaskList ( taskList ) { return this . google . client . tasks . tasklists . insert ( { title : taskList . title , } ) ; } param task param tasklist param completed updateTaskCompletion ( task , tasklist , completed ) { return this . google . client . tasks . tasks . update ( { tasklist , task , id : task , status : completed ? 'completed' : 'needsAction' , } ) ; } param task insertTask ( task ) { return this . google . client . tasks . tasks . insert ( { tasklist : task . listId , task : task . id , id : task . id , title : task . title ? task . title : '' , notes : task . notes ? task . notes : '' , due : task . dueAt ? task . dueAt . format ( ) : null , status : task . completed ? 'completed' : 'needsAction' , parent : task . parent , } ) ; } param task updateTask ( task ) { return this . google . client . tasks . tasks . update ( { tasklist : task . listId , task : `dasdasdasdas${ task . id }` , id : `dasdasdasdas${ task . id }` , title : task . title , notes : task . notes , due : task . dueAt ? task . dueAt . format ( ) : null , status : task . completed ? 'completed' : 'needsAction' , } ) ; } param task param tasklist deleteTask ( task , tasklist ) { return this . google . client . tasks . tasks . delete ( { tasklist , task , } ) ; } /**
     * Only used for testing purposes, view mock file
     */ reset ( ) { } } const r = new GoogleTasksService ( ) ; export default r ;