diff --git a/firestore.rules b/firestore.rules index d52569cd..a07c5aed 100644 --- a/firestore.rules +++ b/firestore.rules @@ -45,7 +45,7 @@ service cloud.firestore { match /folds/{foldId} { allow read; - allow update: if request.auth.uid == resource.data.curatorId; + allow update, delete: if request.auth.uid == resource.data.curatorId; } match /folds/{foldId}/followers/{userId} { diff --git a/functions/src/index.ts b/functions/src/index.ts index f14b4100..df9ec9ab 100644 --- a/functions/src/index.ts +++ b/functions/src/index.ts @@ -12,6 +12,7 @@ export * from './create-contract' export * from './create-user' export * from './create-fold' export * from './on-fold-follow' +export * from './on-fold-delete' export * from './unsubscribe' export * from './update-contract-metrics' export * from './update-user-metrics' diff --git a/functions/src/on-fold-delete.ts b/functions/src/on-fold-delete.ts new file mode 100644 index 00000000..7762e113 --- /dev/null +++ b/functions/src/on-fold-delete.ts @@ -0,0 +1,10 @@ +import * as functions from 'firebase-functions' + +export const onFoldDelete = functions.firestore + .document('folds/{foldId}') + .onDelete(async (change, context) => { + const snapshot = await change.ref.collection('followers').get() + + // Delete followers sub-collection. + await Promise.all(snapshot.docs.map((doc) => doc.ref.delete())) + }) diff --git a/web/components/edit-fold-button.tsx b/web/components/edit-fold-button.tsx index 2176e165..64ffc2fe 100644 --- a/web/components/edit-fold-button.tsx +++ b/web/components/edit-fold-button.tsx @@ -5,13 +5,16 @@ import { PencilIcon } from '@heroicons/react/outline' import { Fold } from '../../common/fold' import { parseWordsAsTags } from '../../common/util/parse' -import { updateFold } from '../lib/firebase/folds' +import { deleteFold, updateFold } from '../lib/firebase/folds' import { toCamelCase } from '../lib/util/format' import { Spacer } from './layout/spacer' import { TagsList } from './tags-list' +import { useRouter } from 'next/router' export function EditFoldButton(props: { fold: Fold; className?: string }) { const { fold, className } = props + const router = useRouter() + const [name, setName] = useState(fold.name) const [about, setAbout] = useState(fold.about ?? '') @@ -106,6 +109,20 @@ export function EditFoldButton(props: { fold: Fold; className?: string }) {
+ diff --git a/web/lib/firebase/folds.ts b/web/lib/firebase/folds.ts index 63d67806..d760fc16 100644 --- a/web/lib/firebase/folds.ts +++ b/web/lib/firebase/folds.ts @@ -26,6 +26,10 @@ export function updateFold(fold: Fold, updates: Partial) { return updateDoc(doc(foldCollection, fold.id), updates) } +export function deleteFold(fold: Fold) { + return deleteDoc(doc(foldCollection, fold.id)) +} + export async function listAllFolds() { return getValues(foldCollection) }