-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #54 from googleinterns/add-trip-functionality
Add Trip Functionality
- Loading branch information
Showing
11 changed files
with
474 additions
and
125 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import * as firebase from 'firebase/app'; | ||
|
||
import { getCurUserEmail, getUserUidFromUserEmail } from './temp-auth-utils.js' | ||
import { getTimestampFromDateString } from './time.js' | ||
|
||
/** | ||
* Return a string containing the cleaned text input. | ||
* | ||
* @param {string} rawInput String containing raw form input. | ||
* @return {string} Cleaned string. | ||
*/ | ||
export function getCleanedTextInput(rawInput, defaultValue) { | ||
return rawInput === '' ? defaultValue : rawInput; | ||
} | ||
|
||
/** | ||
* Return an array of collaborator uids given the emails provided in the | ||
* add trip form. | ||
* | ||
* TODO(#72 & #67): Remove 'remove empty fields' once there is better way to | ||
* remove collaborators (#72) and there is email validation (#67). | ||
* | ||
* @param {!Array{string}} collaboratorEmailsArr Array of emails corresponding | ||
* to the collaborators of the trip (not including the trip creator email). | ||
* @return {!Array{string}} Array of all collaborator uids (including trip | ||
* creator uid). | ||
*/ | ||
export function getCollaboratorUidArray(collaboratorEmailArr) { | ||
collaboratorEmailArr = [getCurUserEmail()].concat(collaboratorEmailArr); | ||
|
||
// Removes empty fields (temporary until fix #67 & #72). | ||
while (collaboratorEmailArr.includes('')) { | ||
const emptyStrIdx = collaboratorEmailArr.indexOf(''); | ||
collaboratorEmailArr.splice(emptyStrIdx, 1); | ||
} | ||
return collaboratorEmailArr | ||
.map(userEmail => getUserUidFromUserEmail(userEmail)); | ||
} | ||
|
||
/** | ||
* Returns a formatted and cleaned trip object that will be used as the data | ||
* for the created Trip document. | ||
* | ||
* We know that rawTripObj will contain all of the necessary fields because each | ||
* key-value pair is explicitly included. This means, only the value | ||
* corresponding to each key needs to be checked. | ||
* For text element inputs, React has built in protections against injection/XSS | ||
* attacks. Thus, no sanitization is needed for text inputs besides providing a | ||
* default value in a Trip field where applicable. | ||
* | ||
* @param {Object} rawTripObj A JS Object containing the raw form data from the | ||
* add trip form. | ||
* @return {Object} Formatted/cleaned version of `rawTripObj` holding the data | ||
* for the new Trip document that is to be created. | ||
*/ | ||
export function formatTripData(rawTripObj) { | ||
const defaultName = "Untitled Trip"; | ||
const defaultDestination = "No Destination" | ||
|
||
const formattedTripObj = { | ||
trip_creation_time: firebase.firestore.Timestamp.now(), | ||
name: getCleanedTextInput(rawTripObj.name, defaultName), | ||
description: rawTripObj.description, | ||
destination: getCleanedTextInput(rawTripObj.destination, | ||
defaultDestination), | ||
start_date: getTimestampFromDateString(rawTripObj.startDate), | ||
end_date: getTimestampFromDateString(rawTripObj.endDate), | ||
collaborators: getCollaboratorUidArray(rawTripObj.collaboratorEmails), | ||
}; | ||
|
||
return formattedTripObj; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import { getUserUidFromUserEmail } from './temp-auth-utils'; | ||
import { getCleanedTextInput, getCollaboratorUidArray } from './filter-input.js'; | ||
|
||
describe('getCleanedTextInput tests', () => { | ||
test('No input entered in form (empty string)', () => { | ||
const testDefaultValue = 'Untitled Trip'; | ||
const testRawName = ''; | ||
const expectedTripName = testDefaultValue; | ||
|
||
const testTripName = getCleanedTextInput(testRawName, testDefaultValue); | ||
|
||
expect(testTripName).toEqual(expectedTripName); | ||
}); | ||
|
||
test('Input entered into form', () => { | ||
const testDefaultValue = 'Untitled Trip'; | ||
const testRawName = 'Trip to No Man\'s Land'; | ||
const expectedTripName = testRawName; | ||
|
||
const testTripName = getCleanedTextInput(testRawName, testDefaultValue); | ||
|
||
expect(testTripName).toEqual(expectedTripName); | ||
}); | ||
}); | ||
|
||
const mockCurUserEmail = '[email protected]'; | ||
// TODO(Issue #55): Replace mock with real auth file once integrated. | ||
jest.mock('./temp-auth-utils.js', () => ({ | ||
getCurUserEmail: () => mockCurUserEmail, | ||
getUserUidFromUserEmail: (userEmail) => '_' + userEmail + '_', | ||
})); | ||
describe('getCollaboratorUidArray tests', () => { | ||
test('No collaborators entered', () => { | ||
const expectedUidArr = [getUserUidFromUserEmail(mockCurUserEmail)]; | ||
// This is the list that is created when there are no collaborators added | ||
// (automatically one empty string from the constructor created ref). | ||
const testEmailArr = ['']; | ||
|
||
const testUidArr = getCollaboratorUidArray(testEmailArr); | ||
|
||
expect(testUidArr).toEqual(expectedUidArr); | ||
}); | ||
|
||
test('Some added collaborators', () => { | ||
const person1Email = '[email protected]'; | ||
const person2Email = '[email protected]'; | ||
const expectedUidArr = [getUserUidFromUserEmail(mockCurUserEmail), | ||
getUserUidFromUserEmail(person1Email), getUserUidFromUserEmail(person2Email)]; | ||
const testEmailArr = [person1Email, person2Email]; | ||
|
||
const testUidArr = getCollaboratorUidArray(testEmailArr); | ||
|
||
expect(testUidArr).toEqual(expectedUidArr); | ||
}); | ||
|
||
test('Some added collaborators and some blank entries', () => { | ||
const person1Email = '[email protected]'; | ||
const person2Email = '[email protected]'; | ||
const expectedUidArr = [getUserUidFromUserEmail(mockCurUserEmail), | ||
getUserUidFromUserEmail(person1Email), getUserUidFromUserEmail(person2Email)]; | ||
const testEmailArr = ['', person1Email, '', person2Email, '']; | ||
|
||
const testUidArr = getCollaboratorUidArray(testEmailArr); | ||
|
||
expect(testUidArr).toEqual(expectedUidArr); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/** | ||
* @fileoverview This is a temporary file that is used to implement 'fake' | ||
* versions of the Auth utility functions used in the ViewTrips components. | ||
* | ||
* TODO(Issue 55): Remove this whole file function and replace any imports to | ||
* this file with Auth utils. | ||
*/ | ||
|
||
|
||
/** | ||
* Temporary hardcoded function that returns the current users email. | ||
* | ||
* @return Hardcoded user email string. | ||
*/ | ||
export function getCurUserEmail() { | ||
return 'matt.murdock'; | ||
} | ||
|
||
/** | ||
* Temporary hardcoded function that returns the current users uid. | ||
* | ||
* @return Hardcoded user uid string. | ||
*/ | ||
export function getCurUserUid() { | ||
return getUserUidFromUserEmail(getCurUserEmail()); | ||
} | ||
|
||
/** | ||
* Temporary hardcoded function that returns the user's uid given the user's | ||
* email. | ||
* | ||
* @param {string} userEmail A users email. | ||
* @return {string} The 'fake' uid associated with the user email that is | ||
* created with the form '_`userEmail`_'. | ||
*/ | ||
export function getUserUidFromUserEmail(userEmail) { | ||
return '_' + userEmail + '_'; | ||
} | ||
|
||
/** | ||
* Temporary hardcoded function that returns the a user's email given the | ||
* fake uid that was stored in the Trip document. | ||
* | ||
* @param {string} uid Fake string uid that is in the form '_userEmail_'. | ||
* @return {string} The email corresponding to the fake uid. | ||
*/ | ||
export function getUserEmailFromUid(uid) { | ||
return uid.substring(1, uid.length - 1); | ||
} | ||
|
||
const authUtils = { | ||
getCurUserEmail, | ||
getCurUserUid, | ||
getUserUidFromUserEmail, | ||
getUserEmailFromUid | ||
} | ||
|
||
export default authUtils; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.