Skip to content

Commit

Permalink
Fix pr #390: Fix issue #389: Jutsu Level Transfer
Browse files Browse the repository at this point in the history
  • Loading branch information
openhands-agent committed Feb 21, 2025
1 parent cb08cae commit 825f15a
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 41 deletions.
13 changes: 6 additions & 7 deletions app/src/app/jutsus/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,7 @@ export default function MyJutsu() {
const [userjutsu, setUserJutsu] = useState<(Jutsu & UserJutsu) | undefined>(
undefined,
);
const [transferTarget, setTransferTarget] = useState<(Jutsu & UserJutsu) | undefined>(undefined);
const [transferLevels, setTransferLevels] = useState<number>(1);


// User Jutsus & items
const { data: userJutsus, isFetching: l1 } = api.jutsu.getUserJutsus.useQuery(
Expand All @@ -80,7 +79,7 @@ export default function MyJutsu() {
document.body.style.cursor = "default";
setIsOpen(false);
setUserJutsu(undefined);
setTransferTarget(undefined);

};

// Mutations
Expand Down Expand Up @@ -362,8 +361,8 @@ export default function MyJutsu() {
variant="secondary"
onClick={() => {
setIsOpen(false);
setTransferTarget(undefined);
setTransferLevels(1);


const transferModal = document.createElement('div');
transferModal.innerHTML = `
<div class="fixed inset-0 bg-black bg-opacity-50 flex items-center justify-center z-50">
Expand Down Expand Up @@ -438,7 +437,7 @@ export default function MyJutsu() {
if (selected) {
transferDetails?.classList.remove('hidden');
const levels = parseInt((document.getElementById('transfer-levels') as HTMLInputElement).value);
transferInfo.innerHTML = `
if (transferInfo) transferInfo.innerHTML = `
<p>Transfer ${levels} level(s) from ${userjutsu.name} to ${selected.name}?</p>
<p>This will reduce ${userjutsu.name} to level ${userjutsu.level - levels} and increase ${selected.name} to level ${selected.level + levels}.</p>
`;
Expand All @@ -451,7 +450,7 @@ export default function MyJutsu() {
);
if (selected) {
const levels = parseInt((e.target as HTMLInputElement).value);
transferInfo.innerHTML = `
if (transferInfo) transferInfo.innerHTML = `
<p>Transfer ${levels} level(s) from ${userjutsu.name} to ${selected.name}?</p>
<p>This will reduce ${userjutsu.name} to level ${userjutsu.level - levels} and increase ${selected.name} to level ${selected.level + levels}.</p>
`;
Expand Down
56 changes: 22 additions & 34 deletions app/src/libs/jutsu.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
import { calculateContentDiff } from "@/utils/diff";
import { useForm } from "react-hook-form";
import { api } from "@/app/_trpc/client";
import { zodResolver } from "@hookform/resolvers/zod";
import { JutsuValidator } from "@/libs/combat/types";
import { AttackTargets } from "@/drizzle/constants";
import { AttackMethods } from "@/drizzle/constants";
import { LetterRanks } from "@/drizzle/constants";
import { WeaponTypes } from "@/drizzle/constants";
import { JutsuTypes } from "@/drizzle/constants";
import { UserRanks } from "@/drizzle/constants";
import { StatTypes } from "@/drizzle/constants";
import { showMutationToast, showFormErrorsToast } from "@/libs/toast";
import { showMutationToast } from "@/libs/toast";
import { JUTSU_TRANSFER_FREE_AMOUNT } from "@/drizzle/constants";
import { JUTSU_TRANSFER_FREE_NORMAL } from "@/drizzle/constants";
import { JUTSU_TRANSFER_FREE_SILVER } from "@/drizzle/constants";
Expand All @@ -27,16 +24,7 @@ import type { Jutsu } from "@/drizzle/schema";
*/
export const useJutsuEditForm = (data: Jutsu, refetch: () => void) => {
// Case type
const jutsu = { ...data, effects: data.effects };

// Form handling
const form = useForm<ZodJutsuType>({
mode: "all",
criteriaMode: "all",
values: jutsu as ZodJutsuType,
defaultValues: jutsu as ZodJutsuType,
resolver: zodResolver(JutsuValidator),
});
const jutsu = { ...data, effects: data.effects || [] };

// Query for bloodlines and villages
const { data: bloodlines, isPending: l1 } =
Expand All @@ -52,30 +40,23 @@ export const useJutsuEditForm = (data: Jutsu, refetch: () => void) => {
});

// Form submission
const handleJutsuSubmit = form.handleSubmit(
(data: ZodJutsuType) => {
const newJutsu = { ...jutsu, ...data };
const diff = calculateContentDiff(jutsu, newJutsu);
if (diff.length > 0) {
updateJutsu({ id: jutsu.id, data: newJutsu });
}
},
(errors) => showFormErrorsToast(errors),
);

// Watch the effects
const effects = form.watch("effects");

// Handle updating of effects
const setEffects = (newEffects: ZodAllTags[]) => {
form.setValue("effects", newEffects, { shouldDirty: true });
const handleJutsuSubmit = (values: typeof jutsu) => {
const newJutsu = { ...jutsu, ...values };
const diff = calculateContentDiff(jutsu, newJutsu);
if (diff.length > 0) {
updateJutsu({ id: jutsu.id, data: newJutsu });
}
};

// Are we loading data
const loading = l1 || l2 || l3;

// Watch for changes to avatar
const imageUrl = form.watch("image");
// Form state
const effects = jutsu.effects;
const imageUrl = jutsu.image || "";
const setEffects = (newEffects: ZodAllTags[]) => {
jutsu.effects = newEffects;
};

// Object for form values
const formData: FormEntry<keyof ZodJutsuType>[] = [
Expand Down Expand Up @@ -106,7 +87,14 @@ export const useJutsuEditForm = (data: Jutsu, refetch: () => void) => {
{ id: "hidden", type: "boolean" },
];

return { jutsu, effects, form, formData, loading, setEffects, handleJutsuSubmit };
return {
jutsu,
effects,
formData,
loading,
setEffects,
handleJutsuSubmit
};
};

/**
Expand Down

0 comments on commit 825f15a

Please sign in to comment.