Skip to content

Commit

Permalink
Merge pull request #114 from Crossbell-Box/fix/xsync
Browse files Browse the repository at this point in the history
  • Loading branch information
songkeys authored Dec 6, 2022
2 parents 7dd6616 + 4f188d9 commit 89588a4
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 12 deletions.
4 changes: 2 additions & 2 deletions components/pages/sync/hooks/use-toggle-operator.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from "react";

import { useSetCharacterOperator } from "@/utils/apis/contract";
import { useToggleSyncOperator } from "@/utils/apis/contract";
import { isAddressEqual } from "@/utils/ethers";
import { OPERATOR_ADDRESS } from "@/utils/apis/operator-sync";
import {
Expand All @@ -16,7 +16,7 @@ export function useToggleOperator() {
const { data: operator } = useCharacterOperator(character?.characterId);
const [isTogglingOperator, setIsTogglingOperator] = React.useState(false);

const setOperator = useSetCharacterOperator(OPERATOR_ADDRESS);
const setOperator = useToggleSyncOperator("add");

return React.useMemo(() => {
const hasOperator = isAddressEqual(operator, OPERATOR_ADDRESS);
Expand Down
4 changes: 2 additions & 2 deletions components/pages/sync/modals/remove-operator-modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { useClickOutside } from "@mantine/hooks";

import { openBorderlessModal } from "@/components/common/Modal";
import Image from "@/components/common/Image";
import { useSetCharacterOperator } from "@/utils/apis/contract";
import { useToggleSyncOperator } from "@/utils/apis/contract";
import { NIL_ADDRESS } from "@/utils/ethers";
import { useCurrentCharacter } from "@/utils/apis/indexer";
import { useCharacterBoundAccounts } from "@/utils/apis/operator-sync";
Expand Down Expand Up @@ -43,7 +43,7 @@ export function RemoveOperatorModal() {
const [scene, setScene] = React.useState(Scene.wannaRemoveTips);

const { mutate: removeOperator_, isLoading: isRemoving } =
useSetCharacterOperator(NIL_ADDRESS);
useToggleSyncOperator("remove");

const removeOperator = React.useCallback(() => {
if (!isRemoving && character?.characterId) {
Expand Down
4 changes: 2 additions & 2 deletions components/pages/sync/operator-sync-welcome/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
useActivateCharacter,
} from "@/utils/apis/operator-sync";
import { useLoginChecker } from "@/utils/wallet/hooks";
import { useSetCharacterOperator } from "@/utils/apis/contract";
import { useToggleSyncOperator } from "@/utils/apis/contract";
import { isAddressEqual, NIL_ADDRESS } from "@/utils/ethers";

import { SNSIcons } from "./sns-icons";
Expand All @@ -21,7 +21,7 @@ export default function OperatorSyncWelcome() {
const { data: character } = useCurrentCharacter();
const { validate } = useLoginChecker();
const activate = useActivateCharacter(character?.characterId);
const setOperator = useSetCharacterOperator(OPERATOR_ADDRESS);
const setOperator = useToggleSyncOperator("add");
const { data: operator } = useCharacterOperator(character?.characterId);
const { openConnectModal } = useConnectModal();

Expand Down
14 changes: 8 additions & 6 deletions utils/apis/contract/operator.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
import { useContract } from "@/utils/crossbell.js";
import { showNotification } from "@mantine/notifications";
import { useMutation, useQueryClient } from "@tanstack/react-query";
import { SCOPE_KEY_CHARACTER_OPERATOR } from "../indexer";
import { OPERATOR_ADDRESS } from "@/utils/apis/operator-sync";

export function useSetCharacterOperator(operator: string) {
import { SCOPE_KEY_CHARACTER } from "../indexer";

export function useToggleSyncOperator(type: "add" | "remove") {
const contract = useContract();
const queryClient = useQueryClient();

return useMutation(
async (characterId: number) => {
return contract.setOperator(characterId, operator);
return type === "add"
? contract.addOperator(characterId, OPERATOR_ADDRESS)
: contract.removeOperator(characterId, OPERATOR_ADDRESS);
},
{
onSuccess: (_, characterId) => {
return Promise.all([
queryClient.invalidateQueries(
SCOPE_KEY_CHARACTER_OPERATOR(characterId)
),
queryClient.invalidateQueries(SCOPE_KEY_CHARACTER(characterId)),
]);
},
onError: (err: any) => {
Expand Down

1 comment on commit 89588a4

@vercel
Copy link

@vercel vercel bot commented on 89588a4 Dec 6, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

crossbell-io – ./

crossbell-io-crossbell.vercel.app
crossbell.vercel.app
crossbell-io-git-main-crossbell.vercel.app

Please sign in to comment.