<commit_before>import { campaignsRef, usersRef, Timestamp } from '../../firebase';
import { firebaseFetchUserData } from '../actions/firebaseAuth';
import {
  firebasePopulateCampaignById,
  firebaseFetchCampaignByIdError
} from '../actions/firebaseActiveCampaign';

// FETCH SIGNATURE ACTIONS
// FETCH FIREBASE USER SIGNATURES REQUEST
export const FETCH_FIREBASE_SIGNATURES_REQUEST = 'FETCH_FIREBASE_SIGNATURES_REQUEST';
export const firebaseFetchUserSignaturesRequest = () => ({
  type: FETCH_FIREBASE_SIGNATURES_REQUEST
});

// POPULATE FIREBASE USER SIGNATURES (SUCCESS)
// TODO UPDATE FUNCTION NAME
export const FETCH_FIREBASE_SIGNATURES_SUCCESS = 'FETCH_FIREBASE_SIGNATURES_SUCCESS';
export const populateFirebaseUserSignatures = firebaseUserSignatures => ({
  type: FETCH_FIREBASE_SIGNATURES_SUCCESS,
  response: firebaseUserSignatures
});

// THUNK FOR FIREBASE SIGNATURES
export const firebaseFetchUserSignatures = firebaseUserSignatures => dispatch => {
  dispatch(firebaseFetchUserData());
  dispatch(populateFirebaseUserSignatures(firebaseUserSignatures));
};

// START LISTENING FOR NEW SIGNATURES
// export const startListeningForSignatures = () => dispatch => {
//   signaturesRef.onSnapshot(querySnapshot => {
//     const signatures = [];
//     querySnapshot.forEach(doc => {
//       signatures.push({ userId: doc.data().userId, campaignId: doc.data().campaignId });
//     });
//     dispatch(firebaseFetchUserSignatures(signatures));
//   });
// };

// ADD SIGNATURE ACTIONS
// FIREBASE ADD SIGNATURE TO CAMPAIGN REQUEST
export const FIREBASE_ADD_SIGNATURE_TO_CAMPAIGN_REQUEST =
  'FIREBASE_ADD_SIGNATURE_TO_CAMPAIGN_REQUEST';
export const firebaseAddSignatureToCampaignRequest = firebaseUserSignatures => ({
  type: FIREBASE_ADD_SIGNATURE_TO_CAMPAIGN_REQUEST
});

// FIREBASE ADD SIGNATURE SUCCESS
export const FIREBASE_ADD_SIGNATURE_TO_CAMPAIGN_SUCCESS =
  'FIREBASE_ADD_SIGNATURE_TO_CAMPAIGN_SUCCESS';
export const firebaseAddSignatureToCampaignSuccess = addSignatureSuccessObject => ({
  type: FIREBASE_ADD_SIGNATURE_TO_CAMPAIGN_SUCCESS,
  response: addSignatureSuccessObject
});

// FIREBASE ADD SIGNATURE ERROR
export const FIREBASE_ADD_SIGNATURE_TO_CAMPAIGN_ERROR = 'FIREBASE_ADD_SIGNATURE_TO_CAMPAIGN_ERROR';
export const firebaseAddSignatureToCampaignError = addSignatureError => ({
  type: FIREBASE_ADD_SIGNATURE_TO_CAMPAIGN_ERROR,
  error: addSignatureError
});

// FIREBASE ADD SIGNATURE THUNK
export const firebaseAddSignatureToCampaign = signatureObject => async dispatch => {
  dispatch(firebaseAddSignatureToCampaignRequest());
  const { campaignId, uid, displayName, signerMessage, keepMeUpdated } = signatureObject;
  const addSignatureRef = campaignsRef
    .doc(campaignId)
    .collection('signatures')
    .doc(uid);
  //  USE UID TO DEFINE SIGNATURE DOC
  addSignatureRef
    .set(
      // IF DOCUMENT EXISTS, OVERWRITE, IF DOESN'T EXIST, CREATE
      {
        uid,
        displayName,
        signerMessage,
        keepMeUpdated,
        createdAt: Timestamp,
        modifiedAt: Timestamp
      }
    )
    // TODO REMOVE AFTER TESTING
    /* eslint-disable no-console */
    .then(dispatch(firebasePopulateCampaignById(campaignId)))
    .catch(err => {
      dispatch(firebaseFetchCampaignByIdError(err));
    });
  const addSignatureUserRef = usersRef.doc(uid);
  addSignatureUserRef
    .set(
      {
        signedCampaignId: campaignId,
        signedCampaignTimestamp: Timestamp<commit_msg>Add Keep Me Updated to DB Patch (#422)<commit_after>import { campaignsRef, usersRef, Timestamp } from '../../firebase';
import { firebaseFetchUserData } from '../actions/firebaseAuth';
import {
  firebasePopulateCampaignById,
  firebaseFetchCampaignByIdError
} from '../actions/firebaseActiveCampaign';

// FETCH SIGNATURE ACTIONS
// FETCH FIREBASE USER SIGNATURES REQUEST
export const FETCH_FIREBASE_SIGNATURES_REQUEST = 'FETCH_FIREBASE_SIGNATURES_REQUEST';
export const firebaseFetchUserSignaturesRequest = () => ({
  type: FETCH_FIREBASE_SIGNATURES_REQUEST
});

// POPULATE FIREBASE USER SIGNATURES (SUCCESS)
// TODO UPDATE FUNCTION NAME
export const FETCH_FIREBASE_SIGNATURES_SUCCESS = 'FETCH_FIREBASE_SIGNATURES_SUCCESS';
export const populateFirebaseUserSignatures = firebaseUserSignatures => ({
  type: FETCH_FIREBASE_SIGNATURES_SUCCESS,
  response: firebaseUserSignatures
});

// THUNK FOR FIREBASE SIGNATURES
export const firebaseFetchUserSignatures = firebaseUserSignatures => dispatch => {
  dispatch(firebaseFetchUserData());
  dispatch(populateFirebaseUserSignatures(firebaseUserSignatures));
};

// START LISTENING FOR NEW SIGNATURES
// export const startListeningForSignatures = () => dispatch => {
//   signaturesRef.onSnapshot(querySnapshot => {
//     const signatures = [];
//     querySnapshot.forEach(doc => {
//       signatures.push({ userId: doc.data().userId, campaignId: doc.data().campaignId });
//     });
//     dispatch(firebaseFetchUserSignatures(signatures));
//   });
// };

// ADD SIGNATURE ACTIONS
// FIREBASE ADD SIGNATURE TO CAMPAIGN REQUEST
export const FIREBASE_ADD_SIGNATURE_TO_CAMPAIGN_REQUEST =
  'FIREBASE_ADD_SIGNATURE_TO_CAMPAIGN_REQUEST';
export const firebaseAddSignatureToCampaignRequest = firebaseUserSignatures => ({
  type: FIREBASE_ADD_SIGNATURE_TO_CAMPAIGN_REQUEST
});

// FIREBASE ADD SIGNATURE SUCCESS
export const FIREBASE_ADD_SIGNATURE_TO_CAMPAIGN_SUCCESS =
  'FIREBASE_ADD_SIGNATURE_TO_CAMPAIGN_SUCCESS';
export const firebaseAddSignatureToCampaignSuccess = addSignatureSuccessObject => ({
  type: FIREBASE_ADD_SIGNATURE_TO_CAMPAIGN_SUCCESS,
  response: addSignatureSuccessObject
});

// FIREBASE ADD SIGNATURE ERROR
export const FIREBASE_ADD_SIGNATURE_TO_CAMPAIGN_ERROR = 'FIREBASE_ADD_SIGNATURE_TO_CAMPAIGN_ERROR';
export const firebaseAddSignatureToCampaignError = addSignatureError => ({
  type: FIREBASE_ADD_SIGNATURE_TO_CAMPAIGN_ERROR,
  error: addSignatureError
});

// FIREBASE ADD SIGNATURE THUNK
export const firebaseAddSignatureToCampaign = signatureObject => async dispatch => {
  dispatch(firebaseAddSignatureToCampaignRequest());
  const { campaignId, uid, displayName, signerMessage, keepMeUpdated } = signatureObject;
  const addSignatureRef = campaignsRef
    .doc(campaignId)
    .collection('signatures')
    .doc(uid);
  //  USE UID TO DEFINE SIGNATURE DOC
  addSignatureRef
    .set(
      // IF DOCUMENT EXISTS, OVERWRITE, IF DOESN'T EXIST, CREATE
      {
        uid,
        displayName,
        signerMessage,
        keepMeUpdated,
        createdAt: Timestamp,
        modifiedAt: Timestamp
      }
    )
    // TODO REMOVE AFTER TESTING
    /* eslint-disable no-console */
    .then(dispatch(firebasePopulateCampaignById(campaignId)))
    .catch(err => {
      dispatch(firebaseFetchCampaignByIdError(err));
    });
  const addSignatureUserRef = usersRef.doc(uid);
  addSignatureUserRef
    .set(
      {
        signedCampaignId: campaignId,
        signedCampaignTimestamp: Timestamp,
        keepMeUpdated
{'fraction_non_alphanumeric': 0.09020172910662824, 'fraction_numerical': 0.0004322766570605187, 'mean_word_length': 4.837678721614802, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 31, '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': '6879898', 'n_tokens_mistral': 2176, 'n_tokens_neox': 2150, 'n_words': 407}