-
Notifications
You must be signed in to change notification settings - Fork 1
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 #10 from ecency/bugfix/review-issues-3
Bugfix/review issues 3
- Loading branch information
Showing
45 changed files
with
1,042 additions
and
553 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
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
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,53 @@ | ||
import { useMutation, useQueryClient } from "@tanstack/react-query"; | ||
import { Community, Subscription } from "@/entities"; | ||
import { useGlobalStore } from "@/core/global-store"; | ||
import { broadcastPostingJSON, formatError } from "@/api/operations"; | ||
import { error } from "@/features/shared"; | ||
import { QueryIdentifiers } from "@/core/react-query"; | ||
|
||
export function useSubscribeToCommunity(community: Community) { | ||
const activeUser = useGlobalStore((s) => s.activeUser); | ||
const queryClient = useQueryClient(); | ||
|
||
return useMutation({ | ||
mutationKey: ["subscribeToCommunity", activeUser?.username, community?.name], | ||
mutationFn: async ({ isSubscribe }: { isSubscribe: boolean }) => { | ||
if (!activeUser) { | ||
throw new Error("Can`t subscribe w/o active user"); | ||
} | ||
|
||
if (isSubscribe) { | ||
return [ | ||
isSubscribe, | ||
await broadcastPostingJSON(activeUser?.username, "community", [ | ||
"subscribe", | ||
{ community: community.name } | ||
]) | ||
] as const; | ||
} else { | ||
return [ | ||
isSubscribe, | ||
await broadcastPostingJSON(activeUser?.username, "community", [ | ||
"unsubscribe", | ||
{ community: community.name } | ||
]) | ||
] as const; | ||
} | ||
}, | ||
onSuccess: ([isSubscribe]) => { | ||
queryClient.setQueryData<Subscription[]>( | ||
[QueryIdentifiers.SUBSCRIPTIONS, activeUser?.username], | ||
(data) => { | ||
if (!data) { | ||
return data; | ||
} | ||
|
||
return isSubscribe | ||
? [...data, [community.name, community.title, "guest", ""]] | ||
: data.filter(([u]) => u !== community.name); | ||
} | ||
); | ||
}, | ||
onError: (err) => error(...formatError(err)) | ||
}); | ||
} |
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
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
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
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.