Skip to content

Fix google file picker #2569

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

Merged
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import Intl from 'ember-intl/services/intl';

const {
GOOGLE_FILE_PICKER_SCOPES,
GOOGLE_FILE_PICKER_CLIENT_ID,
GOOGLE_FILE_PICKER_API_KEY,
GOOGLE_FILE_PICKER_APP_ID,
} = config.OSF.googleFilePicker;
Expand Down Expand Up @@ -68,12 +67,9 @@ export default class GoogleFilePickerWidget extends Component<Args> {
@tracked openGoogleFilePicker = false;
@tracked visible = false;
pickerInited = false;
gisInited = false;
selectFolder: any = undefined;
tokenClient: any = undefined;
accessToken!: string;
scopes = GOOGLE_FILE_PICKER_SCOPES;
clientId = GOOGLE_FILE_PICKER_CLIENT_ID;
apiKey = GOOGLE_FILE_PICKER_API_KEY;
appId = GOOGLE_FILE_PICKER_APP_ID;
mimeTypes = '';
Expand Down Expand Up @@ -137,12 +133,12 @@ export default class GoogleFilePickerWidget extends Component<Args> {
* @param file - The file object selected (format determined by external API)
*/
@action
filePickerCallback(file: any) {
filePickerCallback(data: any) {
if (this.selectFolder !== undefined) {
this.folderName = file.name;
this.folderName = data.name;
this.selectFolder({
itemName: file.name,
itemId: file.id,
itemName: data.name,
itemId: data.id,
});
} else {
this.args.manager.reload();
Expand All @@ -166,9 +162,7 @@ export default class GoogleFilePickerWidget extends Component<Args> {

willDestroy() {
super.willDestroy();
delete this.tokenClient;
this.pickerInited = false;
this.gisInited = false;
}


Expand All @@ -184,31 +178,15 @@ export default class GoogleFilePickerWidget extends Component<Args> {
* discovery doc to initialize the API.
*/
async initializePicker() {
await window.gapi.client.load('https://www.googleapis.com/discovery/v1/apis/drive/v3/rest');
this.pickerInited = true;
this.maybeEnableButtons();
}

/**
* Callback after Google Identity Services are loaded.
*/
gisLoaded() {
if (!this.tokenClient) {
this.tokenClient = window.google?.accounts?.oauth2.initTokenClient({
client_id: this.clientId,
scope: this.scopes,
callback: '', // defined later
});
}
this.gisInited = true;
this.maybeEnableButtons();
}

/**
* Enables user interaction after all libraries are loaded.
*/
maybeEnableButtons() {
if (this.pickerInited && this.gisInited && this.isFolderPicker) {
if (this.pickerInited && this.isFolderPicker) {
this.visible = true;
}
}
Expand All @@ -218,21 +196,7 @@ export default class GoogleFilePickerWidget extends Component<Args> {
*/
@action
handleAuthClick() {
this.tokenClient.callback = async (response: any) => {
if (response.error !== undefined) {
throw (response);
}
await this.createPicker();
};

if (this.accessToken === null) {
// Prompt the user to select a Google Account and ask for consent to share their data
// when establishing a new session.
this.tokenClient?.requestAccessToken({prompt: 'consent'});
} else {
// Skip display of account chooser and consent dialog for an existing session.
this.tokenClient?.requestAccessToken({prompt: ''});
}
this.createPicker();
}

/**
Expand Down Expand Up @@ -263,14 +227,7 @@ export default class GoogleFilePickerWidget extends Component<Args> {
*/
async pickerCallback(data: any) {
if (data.action === window.google.picker.Action.PICKED) {
const document = data[window.google.picker.Response.DOCUMENTS][0];
const fileId = document[window.google.picker.Document.ID];
const res = await window.gapi.client.drive.files.get({
fileId,
fields: '*',
});
// Correctly call the Ember method
this.filePickerCallback(res.result);
this.filePickerCallback(data.docs[0]);
}
}
}
Expand Down