-
Notifications
You must be signed in to change notification settings - Fork 0
Delete Trips #82
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
Delete Trips #82
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
51a2fc9
Implement deleting a trip and its corresponding sub-collection of act…
zghera cf55408
Add documentation in delete-trip-button.
zghera 1f1c1bc
Add then and catch statements to deleteTripActivities return.
zghera 0f4efc6
Change choice of words in JSDoc for code that was used from the docum…
zghera 14ee650
Add confirmation method before deleting.
zghera c6eaa64
Remove magic number for limiting queries.
zghera c0efd65
Merge branch 'edit-trip-implementation' into delete-trip
zghera 0bbf697
Merge branch 'edit-trip-implementation' into delete-trip
zghera da1be2c
Merge branch 'edit-trip-implementation' into delete-trip
zghera b64b73e
Merge branch 'edit-trip-implementation' into delete-trip
zghera 57a97fb
Fix typos in JSDoc and comments.
zghera 6d2aa6d
Merge branch 'master' into delete-trip
zghera 3100a4e
Add top-level description to getCollaboratorEmails JSDoc.
zghera ff8a21d
Merge branch 'master' into delete-trip
zghera de2807e
Add comma on dep in package.json to fix broken build.
zghera d10c6ee
Delete unused imports to fix warnings in local build.
zghera File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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
119 changes: 119 additions & 0 deletions
119
frontend/src/components/ViewTrips/delete-trip-button.js
This file contains hidden or 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,119 @@ | ||
| import React from 'react'; | ||
|
|
||
| import app from '../Firebase/'; | ||
| import Button from 'react-bootstrap/Button'; | ||
|
|
||
| import * as DB from '../../constants/database.js'; | ||
|
|
||
| const db = app.firestore(); | ||
| const LIMIT_QUERY_DOCS_RETRIEVED = 5; | ||
|
|
||
| /** | ||
| * Component used to delete a Trip. | ||
| * | ||
| * | ||
| * @param {Object} props These are the props for this component: | ||
| * - tripId: Document ID for the current Trip document. | ||
| * - refreshTripsContainer: Handler that refreshes the TripsContainer | ||
| * component upon trip creation (Remove when fix Issue #62). | ||
| */ | ||
| const DeleteTripsButton = (props) => { | ||
| /** | ||
| * Deletes documents in query with a batch delete. | ||
| * | ||
| * This was taken from the delete collection snippets in the documentation | ||
| * at https://firebase.google.com/docs/firestore/manage-data/delete-data. | ||
| * | ||
| * @param {firebase.firestore.Firestore} db Firestore database instance. | ||
| * @param {firebase.firestore.Query} query Query containing documents from | ||
| * the activities subcollection of a trip documents. | ||
| * @param {Function} resolve Resolve function that returns a void Promise. | ||
| */ | ||
| async function deleteQueryBatch(db, query, resolve) { | ||
|
zghera marked this conversation as resolved.
|
||
| const snapshot = await query.get(); | ||
|
|
||
| const batchSize = snapshot.size; | ||
| if (batchSize === 0) { | ||
| // When there are no documents left, we are done. | ||
| resolve(); | ||
| return; | ||
| } | ||
|
|
||
| // Delete documents in a batch. | ||
| const batch = db.batch(); | ||
| snapshot.docs.forEach((doc) => { | ||
| batch.delete(doc.ref); | ||
| }); | ||
| await batch.commit(); | ||
|
|
||
| // Recurse on the next process tick, to avoid | ||
| // exploding the stack. | ||
| process.nextTick(() => { | ||
| deleteQueryBatch(db, query, resolve); | ||
| }); | ||
| } | ||
|
|
||
| /** | ||
| * Deletes a trip's subcollection of activities corresponding to the | ||
| * `tripId` prop. | ||
| * | ||
| * This was adapted from the delete collection snippets in the documentation | ||
| * at https://firebase.google.com/docs/firestore/manage-data/delete-data. | ||
| * | ||
| * TODO(Issue #81): Consider deleting data with callable cloud function | ||
| * https://firebase.google.com/docs/firestore/solutions/delete-collections. | ||
| */ | ||
| async function deleteTripActivities() { | ||
| const query = db.collection(DB.COLLECTION_TRIPS) | ||
| .doc(props.tripId) | ||
| .collection(DB.COLLECTION_ACTIVITIES) | ||
| .orderBy(DB.ACTIVITIES_TITLE) | ||
| .limit(LIMIT_QUERY_DOCS_RETRIEVED); | ||
|
|
||
| return new Promise((resolve, reject) => { | ||
| deleteQueryBatch(db, query, resolve).catch(reject); | ||
| }); | ||
| } | ||
|
|
||
| /** | ||
| * Deletes a trip and its subcollection of activities corrsponding to the | ||
| * `tripId` prop and then refreshes the TripsContainer component. | ||
| * | ||
| * TODO(Issue #62): Remove refreshTripsContainer. | ||
| */ | ||
| async function deleteTrip() { | ||
| if (window.confirm('Are you sure you want to delete this trip? This' + | ||
| ' action cannot be undone!')) { | ||
| await deleteTripActivities() | ||
| .then(() => { | ||
| console.log("Activity subcollection successfully deleted for trip" + | ||
| " with id: ", props.tripId); | ||
|
anan-ya-y marked this conversation as resolved.
|
||
| }) | ||
| .catch(error => { | ||
| console.error("Error deleting activities subcollection: ", error); | ||
|
zghera marked this conversation as resolved.
|
||
| }); | ||
|
|
||
| db.collection(DB.COLLECTION_TRIPS) | ||
| .doc(props.tripId) | ||
| .delete() | ||
| .then(() => { | ||
| console.log("Document successfully deleted with id: ", props.tripId); | ||
|
zghera marked this conversation as resolved.
|
||
| }).catch(error => { | ||
| console.error("Error removing document: ", error); | ||
| }); | ||
|
|
||
| props.refreshTripsContainer(); | ||
| } | ||
| } | ||
|
|
||
| return ( | ||
| <Button | ||
| type='button' | ||
| variant='primary' | ||
| onClick={deleteTrip} > | ||
| Delete Trip | ||
| </Button> | ||
| ); | ||
| } | ||
|
|
||
| export default DeleteTripsButton; | ||
This file contains hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.