Skip to content

Commit

Permalink
complete pattern refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Pandelis Symeonidis committed Nov 29, 2023
1 parent 2058e70 commit 10107ea
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 33 deletions.
2 changes: 0 additions & 2 deletions frontend/src/components/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ export * from "./item-card";
export * from "./input-fields";
export * from "./switch-selector";
export * from "./navigation-tab";
export * from "./menu-card";
export * from "./menu-item";
export * from "./price-in-ist";
export * from "./base-route";
export * from "./equipped-item-card";
Expand Down
4 changes: 2 additions & 2 deletions frontend/src/components/menu-card/menu-card.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,11 @@ export const MenuCard: FC<MenuCardProps> = ({ title, category, equippedItemProp,
const unequipItem = useUnequipItem();

const handleEquipResult: MakeOfferCallback = {
accepted: () => setEquippedItem(undefined),
accepted: setEquippedItem,
};

const handleUnequipResult: MakeOfferCallback = {
accepted: setEquippedItem,
accepted: () => setEquippedItem(undefined),
};

const allItems = useMemo(() => {
Expand Down
15 changes: 11 additions & 4 deletions frontend/src/pages/buy/item-buy.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,17 @@ export const ItemBuy = () => {

const handleSubmit = async () => {
setIsAwaitingApproval(true);
await buyItem.sendOffer(() => {
setIsOfferAccepted(true);
setIsAwaitingApproval(false);
}, {});
await buyItem.sendOffer({
refunded: () => {
setIsOfferAccepted(true);
},
accepted: () => {
setIsOfferAccepted(true);
},
settled: () => {
setIsAwaitingApproval(false);
}
});
};

if (!data) return <ErrorView />;
Expand Down
8 changes: 6 additions & 2 deletions frontend/src/pages/sell/item-sell.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,21 @@ import { ErrorView } from "../../components";
import { useSellItem } from "../../service";
import { Sell } from "./sell";
import { SellData } from "./types";
import { Category, isItemCategory } from "../../interfaces";
import { Category, MakeOfferCallback, isItemCategory } from "../../interfaces";

export const ItemSell = () => {
const { name, category } = useParams<"category" | "name">();
const [isPlacedInShop, setIsPlacedInShop] = useState(false);
const sellItem = useSellItem(name, category as Category);
const [data, setData] = useState<SellData>({ price: 0 });

const handleResult: MakeOfferCallback = {
settled: () => setIsPlacedInShop(true),
};

const sendOfferHandler = async (data: SellData) => {
if (data.price < 1) return; // We don't want to sell for free in case someone managed to fool the frontend
await sellItem.sendOffer(data.price, () => setIsPlacedInShop(true), {});
await sellItem.sendOffer(data.price, handleResult);
};

if (!data || !isItemCategory(category)) return <ErrorView />;
Expand Down
4 changes: 2 additions & 2 deletions frontend/src/service/character.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,13 +244,13 @@ export const useBuyCharacter = (characterId: string) => {
callback: {
...callback,
refunded: () => {
if (callback.refunded) callback.refunded();
console.info("BuyCharacter call settled");
if (callback.refunded) callback.refunded();
setIsLoading(false);
},
accepted: () => {
if (callback.accepted) callback.accepted();
console.info("BuyCharacter call settled");
if (callback.accepted) callback.accepted();
setIsLoading(false);
},
setIsLoading: setIsLoading,
Expand Down
39 changes: 18 additions & 21 deletions frontend/src/service/items.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ export const useSellItem = (itemName: string | undefined, itemCategory: Category
const [isLoading, setIsLoading] = useState(false);

const sendOffer = useCallback(
async (price: number, setPlacedInShop: () => void, callback: MakeOfferCallback) => {
async (price: number, callback: MakeOfferCallback) => {
try {
const found = items.find((item) => item.name === itemName && item.category === itemCategory);
if (!found) return;
Expand All @@ -163,20 +163,14 @@ export const useSellItem = (itemName: string | undefined, itemCategory: Category
},
callback: {
...callback,
refunded: () => {
if (callback.refunded) callback.refunded();
console.info("SellItem call settled");
setIsLoading(false);
},
accepted: () => {
if (callback.accepted) callback.accepted();
seated: () => {
console.info("SellItem call settled");
if (callback.seated) callback.seated();
setIsLoading(false);
},
setIsLoading: setIsLoading
}
});
setPlacedInShop();
return true;
} catch (error) {
console.warn(error);
Expand All @@ -202,7 +196,7 @@ export const useBuyItem = (itemToBuy: ItemInMarket | undefined) => {
const istBrand = service.tokenInfo.ist.brand;

const sendOffer = useCallback(
async (setIsAwaitingApprovalToFalse: () => void, callback: MakeOfferCallback) => {
async (callback: MakeOfferCallback) => {
try {
if (!itemToBuy) return;
const { forSale, equippedTo, activity, ...itemObject } = itemToBuy.item;
Expand All @@ -221,24 +215,21 @@ export const useBuyItem = (itemToBuy: ItemInMarket | undefined) => {
callback: {
...callback,
refunded: () => {
if (callback.refunded) callback.refunded();
console.info("BuyItem call settled");
if (callback.refunded) callback.refunded();
setIsLoading(false);
setIsAwaitingApprovalToFalse();
},
accepted: () => {
if (callback.accepted) callback.accepted();
console.info("BuyItem call settled");
if (callback.accepted) callback.accepted();
setIsLoading(false);
setIsAwaitingApprovalToFalse();
},
setIsLoading: setIsLoading
}
});
} catch (error) {
console.warn(error);
setIsError(true);
setIsAwaitingApprovalToFalse();
}
},
[itemToBuy, items, wallet, service],
Expand Down Expand Up @@ -282,9 +273,11 @@ export const useEquipItem = () => {
},
callback: {
...body.callback,
accepted: () => {
seated: () => {
console.info("Swap call settled");
if (body.callback.accepted) body.callback.accepted();
if (body.callback.seated) body.callback.seated();
},
settled: () => {
setTimeout(() => userStateDispatch({ type: "END_INVENTORY_CALL" }), INVENTORY_CALL_FETCH_DELAY);
}
}
Expand All @@ -301,9 +294,11 @@ export const useEquipItem = () => {
},
callback: {
...body.callback,
accepted: () => {
seated: () => {
console.info("Equip call settled");
if (body.callback.accepted) body.callback.accepted();
if (body.callback.seated) body.callback.seated();
},
settled: () => {
setTimeout(() => userStateDispatch({ type: "END_INVENTORY_CALL" }), INVENTORY_CALL_FETCH_DELAY);
}
},
Expand Down Expand Up @@ -342,9 +337,11 @@ export const useUnequipItem = () => {
},
callback: {
...body.callback,
accepted: () => {
seated: () => {
console.info("Unequip call settled");
if (body.callback.accepted) body.callback.accepted();
if (body.callback.seated) body.callback.seated();
},
settled: () => {
setTimeout(() => userStateDispatch({ type: "END_INVENTORY_CALL" }), INVENTORY_CALL_FETCH_DELAY);
}
}
Expand Down

0 comments on commit 10107ea

Please sign in to comment.