diff --git a/src/components/user/User.tsx b/src/components/user/User.tsx index d776094..c5efe0d 100644 --- a/src/components/user/User.tsx +++ b/src/components/user/User.tsx @@ -91,6 +91,14 @@ const User: Component = (props) => { return { username, ...userDocument }; }; + const removeSubscription = (userDocument: any) => { + const subscriptions = userDocument.subscriptions || []; + userDocument.subscriptions = subscriptions.filter( + (subscription: any) => subscription.username !== username + ); + return { username, ...userDocument }; + }; + const defaultUserDocument = { database, }; @@ -98,7 +106,7 @@ const User: Component = (props) => { for (let i = 0; i < updatedSubscriptions.length; i++) { let subscription = updatedSubscriptions[i]; if (!isIn(subscription.username, subscriptions)) { - update( + await update( userId(subscription.username, database), addSubscription, defaultUserDocument @@ -115,6 +123,11 @@ const User: Component = (props) => { for (let i = 0; i < subscriptions.length; i++) { let subscription = subscriptions[i]; if (!isIn(subscription.username, updatedSubscriptions)) { + await update( + userId(subscription.username, database), + removeSubscription, + defaultUserDocument + ); console.log({ caller: 'User / submitHandler / deleted subscription', username: subscription.username,