<commit_before>import { delay } from 'redux-saga';
import { call, put, takeEvery } from 'redux-saga/effects';

import {
  types,
  googleClientInitRequest,
  googleClientInitSuccess,
  googleClientInitFailure,
  loginSuccess,
  loginFailure,
  logoutSuccess,
  logoutFailure,
  fetchFormsSuccess,
  fetchFormsFailure,
} from './actions';

import gapi, { CLIENT_ID, DISCOVERY_DOCS, SCOPES, SCRIPT_ID } from '../services/google';

function* initGoogleClientSaga(action) {
  try {
    yield call(gapi.loadAsync);
    yield call(
      gapi.client.init, {
        discoveryDocs: DISCOVERY_DOCS,
        clientId: CLIENT_ID,
        scope: SCOPES
      }
    );
    yield put(googleClientInitSuccess(gapi.auth2.getAuthInstance().isSignedIn.get()));
  }
  catch(e) {
    yield put(googleClientInitFailure('Problem while loading google client. Please check your connection.'));
  }
}

function* loginSaga(action) {
  try {
    yield call(gapi.loginAsync);
    yield put(loginSuccess());
  }
  catch(e) {
    yield put(loginFailure('Authentication failed. Please try again'));
  }
}

function* logoutSaga(action) {
  try {
    yield call(gapi.logoutAsync);
    yield put(logoutSuccess());
  } catch(e) {
    yield put(logoutFailure('Logout failed. Please try again'));
  }
}

function* fetchLatestForms(action) {
  try {
    const response = yield call(gapi.client.script.scripts.run, {
      'scriptId': SCRIPT_ID,
      'resource': {
        'function': 'getLastResponsesUrl'
      }
    });
    if (response.result.error) {
      console.error(response.result.error);
      yield put(fetchFormsFailure());
    }
    yield put(fetchFormsSuccess(response.result.response.result));
  } catch(e) {
    yield put(fetchFormsFailure('The latest project forms could not be retrieved. Please check you have the permission to view them'));
  }
}

function* rootSaga() {
  yield takeEvery(types.GOOGLE_CLIENT_INIT.REQUEST, initGoogleClientSaga);
  yield takeEvery(types.LOGIN.REQUEST, loginSaga);
  yield takeEvery(types.LOGOUT.REQUEST, logoutSaga);
  yield takeEvery(types.FETCH_FORMS.REQUEST, fetchLatestForms);

  yield call(delay, 100);
  yield put(googleClientInitRequest());
}

export default rootSaga;<commit_msg>Trigger fetch data on login success<commit_after>import { delay } from 'redux-saga';
import { call, put, takeEvery, select } from 'redux-saga/effects';

import {
  types,
  googleClientInitRequest,
  googleClientInitSuccess,
  googleClientInitFailure,
  loginSuccess,
  loginFailure,
  logoutSuccess,
  logoutFailure,
  fetchFormsRequest,
  fetchFormsSuccess,
  fetchFormsFailure,
} from './actions';

import gapi, { CLIENT_ID, DISCOVERY_DOCS, SCOPES, SCRIPT_ID } from '../services/google';

function* initGoogleClientSaga(action) {
  try {
    yield call(gapi.loadAsync);
    yield call(
      gapi.client.init, {
        discoveryDocs: DISCOVERY_DOCS,
        clientId: CLIENT_ID,
        scope: SCOPES
      }
    );
    yield put(googleClientInitSuccess(gapi.auth2.getAuthInstance().isSignedIn.get()));
  }
  catch(e) {
    yield put(googleClientInitFailure('Problem while loading google client. Please check your connection.'));
  }
}

function* loginSaga(action) {
  try {
    yield call(gapi.loginAsync);
    yield put(loginSuccess());
  }
  catch(e) {
    yield put(loginFailure('Authentication failed. Please try again'));
  }
}

function* logoutSaga(action) {
  try {
    yield call(gapi.logoutAsync);
    yield put(logoutSuccess());
  } catch(e) {
    yield put(logoutFailure('Logout failed. Please try again'));
  }
}

function* fetchLatestForms(action) {
  try {
    const response = yield call(gapi.client.script.scripts.run, {
      'scriptId': SCRIPT_ID,
      'resource': {
        'function': 'getLastResponsesUrl'
      }
    });
    if (response.result.error) {
      console.error(response.result.error);
      yield put(fetchFormsFailure());
    }
    yield put(fetchFormsSuccess(response.result.response.result));
  } catch(e) {
    yield put(fetchFormsFailure('The latest project forms could not be retrieved. Please check you have the permission to view them'));
  }
}

function* triggerFetchFormSaga(action) {
  const loggedIn = yield select(state => state.loggedIn);
  if (loggedIn) yield put(fetchFormsRequest());
}

function* rootSaga() {
  yield takeEvery(types.GOOGLE_CLIENT_INIT.REQUEST, initGoogleClientSaga);
  yield takeEvery(types.LOGIN.REQUEST, loginSaga);
  yield takeEvery(types.LOGOUT.REQUEST, logoutSaga);
  yield takeEvery(types.FETCH_FORMS.REQUEST, fetchLatestForms);
  yield takeEvery([types.LOGIN.SUCCESS, types.GOOGLE_CLIENT_INIT.SUCCESS], triggerFetchFormSaga)

  yield call(delay, 100);
  yield put(googleClientInitRequest());
}

export default rootSaga;
{'fraction_non_alphanumeric': 0.1179345516362091, 'fraction_numerical': 0.0016999575010624734, 'mean_word_length': 4.077669902912621, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7276383', 'n_tokens_mistral': 1524, 'n_tokens_neox': 1494, 'n_words': 313}