diff --git a/src/views/components/database/quest/QuestEarnings.tsx b/src/views/components/database/quest/QuestEarnings.tsx
index beec9c4c..9d0eb4b5 100644
--- a/src/views/components/database/quest/QuestEarnings.tsx
+++ b/src/views/components/database/quest/QuestEarnings.tsx
@@ -15,6 +15,12 @@ type QuestEarningsProps = {
export const QuestEarnings = ({ quest, dialogsRef, setEarningIndex }: QuestEarningsProps) => {
const { projectDataValues: quests } = useProjectQuests();
const { t } = useTranslation('database_quests');
+
+ const editEarning = (index: number) => {
+ dialogsRef.current?.openDialog('earning');
+ setEarningIndex(index);
+ };
+
return (
-
+
);
};
diff --git a/src/views/components/database/quest/editors/QuestEarningEditor.tsx b/src/views/components/database/quest/editors/QuestEarningEditor.tsx
index fa54d5f5..7a106ff9 100644
--- a/src/views/components/database/quest/editors/QuestEarningEditor.tsx
+++ b/src/views/components/database/quest/editors/QuestEarningEditor.tsx
@@ -1,51 +1,94 @@
-import { useRefreshUI } from '@components/editor';
import { Editor } from '@components/editor/Editor';
import { InputContainer, InputWithTopLabelContainer, Label } from '@components/inputs';
-import { SelectCustomSimple } from '@components/SelectCustom';
-import { QUEST_EARNINGS, StudioQuest, StudioQuestEarningType } from '@modelEntities/quest';
-import { createQuestEarning } from '@utils/entityCreation';
+import { QUEST_EARNINGS, StudioQuestEarningType } from '@modelEntities/quest';
import { padStr } from '@utils/PadStr';
-import React, { useMemo } from 'react';
import { useTranslation } from 'react-i18next';
import { TFunction } from 'i18next';
import { QuestEarningItem, QuestEarningMoney, QuestEarningPokemon } from './earnings';
+import { EditorHandlingClose, useEditorHandlingClose } from '@components/editor/useHandleCloseEditor';
+import { useQuestPage } from '@src/hooks/usePage';
+import { useUpdateQuest } from './useUpdateQuest';
+import { useEarningQuest } from './useEarningQuest';
+import { cloneEntity } from '@utils/cloneEntity';
+import { assertUnreachable } from '@utils/assertUnreachable';
+import { cleanNaNValue } from '@utils/cleanNaNValue';
+import { Select } from '@ds/Select';
+import React, { forwardRef, useMemo } from 'react';
const earningCategoryEntries = (t: TFunction<'database_quests'>) => QUEST_EARNINGS.map((earning) => ({ value: earning, label: t(earning) }));
type QuestEarningEditorProps = {
- quest: StudioQuest;
earningIndex: number;
};
-export const QuestEarningEditor = ({ quest, earningIndex }: QuestEarningEditorProps) => {
+export const QuestEarningEditor = forwardRef(({ earningIndex }, ref) => {
const { t } = useTranslation('database_quests');
- const refreshUI = useRefreshUI();
+ const { quest } = useQuestPage();
+ const updateQuest = useUpdateQuest(quest);
+ const { earning, refs, updateEarning, isValid } = useEarningQuest(quest.earnings[earningIndex]);
const earningOptions = useMemo(() => earningCategoryEntries(t), [t]);
- const earning = quest.earnings[earningIndex];
+ const earningMethodName = earning.earningMethodName;
const changeEarning = (value: StudioQuestEarningType) => {
- if (value === quest.earnings[earningIndex].earningMethodName) return;
- quest.earnings[earningIndex] = createQuestEarning(value);
+ if (value === earningMethodName) return;
+
+ updateEarning(value);
+ };
+
+ const canClose = () => isValid;
+
+ const onClose = () => {
+ if (!canClose()) return;
+
+ const newEarning = cloneEntity(earning);
+ const oldEarning = quest.earnings[earningIndex];
+ const isSameMethodName = newEarning.earningMethodName === oldEarning.earningMethodName;
+ switch (earningMethodName) {
+ case 'earning_money': {
+ if (!refs.inputRef.current) return;
+
+ const backupValue = isSameMethodName ? (oldEarning.earningArgs[0] as number) : 100;
+ newEarning.earningArgs[0] = cleanNaNValue(refs.inputRef.current.valueAsNumber, backupValue);
+ break;
+ }
+ case 'earning_item': {
+ if (!refs.entityRef.current || !refs.inputRef.current) return;
+
+ const backupValue = isSameMethodName ? (oldEarning.earningArgs[1] as number) : 1;
+ newEarning.earningArgs[0] = refs.entityRef.current;
+ newEarning.earningArgs[1] = cleanNaNValue(refs.inputRef.current.valueAsNumber, backupValue);
+ break;
+ }
+ case 'earning_pokemon':
+ case 'earning_egg': {
+ if (!refs.entityRef.current) return;
+
+ newEarning.earningArgs[0] = refs.entityRef.current;
+ break;
+ }
+ default:
+ assertUnreachable(earningMethodName);
+ }
+ const updatedEarnings = cloneEntity(quest.earnings);
+ updatedEarnings[earningIndex] = cloneEntity(newEarning);
+ updateQuest({ earnings: updatedEarnings });
};
+ useEditorHandlingClose(ref, onClose, canClose);
+
return (
-
- refreshUI(changeEarning(value as StudioQuestEarningType))}
- noTooltip
- />
+
+
- {earning.earningMethodName === 'earning_money' && }
- {earning.earningMethodName === 'earning_item' && }
- {earning.earningMethodName === 'earning_pokemon' && }
- {earning.earningMethodName === 'earning_egg' && }
+ {earningMethodName === 'earning_money' && }
+ {earningMethodName === 'earning_item' && }
+ {earningMethodName === 'earning_pokemon' && }
+ {earningMethodName === 'earning_egg' && }
);
-};
+});
+QuestEarningEditor.displayName = 'QuestEarningEditor';
diff --git a/src/views/components/database/quest/editors/QuestNewEarningEditor.tsx b/src/views/components/database/quest/editors/QuestNewEarningEditor.tsx
index 88717c5d..6e3baa31 100644
--- a/src/views/components/database/quest/editors/QuestNewEarningEditor.tsx
+++ b/src/views/components/database/quest/editors/QuestNewEarningEditor.tsx
@@ -2,7 +2,6 @@ import { DarkButton, PrimaryButton } from '@components/buttons';
import { Editor } from '@components/editor/Editor';
import { InputContainer, InputWithTopLabelContainer, Label } from '@components/inputs';
import { QUEST_EARNINGS, StudioQuestEarningType } from '@modelEntities/quest';
-import { createQuestEarning } from '@utils/entityCreation';
import { useTranslation } from 'react-i18next';
import { TFunction } from 'i18next';
import { QuestEarningItem, QuestEarningMoney, QuestEarningPokemon } from './earnings';
@@ -35,7 +34,7 @@ export const QuestNewEarningEditor = forwardRef earningCategoryEntries(t), [t]);
- const { earning, refs, updateEarning, checkIsValid, isValid } = useEarningQuest(createQuestEarning('earning_money'));
+ const { earning, refs, updateEarning, checkIsValid, isValid } = useEarningQuest();
const earningMethodName = earning.earningMethodName;
useEditorHandlingClose(ref);
@@ -43,7 +42,7 @@ export const QuestNewEarningEditor = forwardRef {
if (value === earning.earningMethodName) return;
- updateEarning(createQuestEarning(value));
+ updateEarning(value);
};
const onClickNew = () => {
@@ -82,8 +81,8 @@ export const QuestNewEarningEditor = forwardRef
-
-
+
+
{earningMethodName === 'earning_money' && }
{earningMethodName === 'earning_item' && }
diff --git a/src/views/components/database/quest/editors/useEarningQuest.ts b/src/views/components/database/quest/editors/useEarningQuest.ts
index 852a1ba3..f1560de0 100644
--- a/src/views/components/database/quest/editors/useEarningQuest.ts
+++ b/src/views/components/database/quest/editors/useEarningQuest.ts
@@ -1,10 +1,16 @@
import { DbSymbol } from '@modelEntities/dbSymbol';
-import { StudioQuestEarning } from '@modelEntities/quest';
+import { StudioQuestEarning, StudioQuestEarningType } from '@modelEntities/quest';
import { assertUnreachable } from '@utils/assertUnreachable';
+import { cloneEntity } from '@utils/cloneEntity';
+import { createQuestEarning } from '@utils/entityCreation';
import { useRef, useState } from 'react';
-export const useEarningQuest = (initialEarning: StudioQuestEarning) => {
- const [earning, setEarning] = useState(initialEarning);
+const initializeEarning = (initialEarning?: StudioQuestEarning): StudioQuestEarning => {
+ return initialEarning ? cloneEntity(initialEarning) : createQuestEarning('earning_money');
+};
+
+export const useEarningQuest = (initialEarning?: StudioQuestEarning) => {
+ const [earning, setEarning] = useState(initializeEarning(initialEarning));
const [isValid, setIsValid] = useState(true);
const entityRef = useRef();
const inputRef = useRef(null);
@@ -24,8 +30,8 @@ export const useEarningQuest = (initialEarning: StudioQuestEarning) => {
return setIsValid(true);
};
- const updateEarning = (earning: StudioQuestEarning) => {
- setEarning(earning);
+ const updateEarning = (earningMethod: StudioQuestEarningType) => {
+ setEarning(createQuestEarning(earningMethod));
setIsValid(true);
};
diff --git a/src/views/components/database/quest/tables/QuestEarningsTable.tsx b/src/views/components/database/quest/tables/QuestEarningsTable.tsx
index 3b170dbb..78b4c0aa 100644
--- a/src/views/components/database/quest/tables/QuestEarningsTable.tsx
+++ b/src/views/components/database/quest/tables/QuestEarningsTable.tsx
@@ -8,10 +8,10 @@ import { useUpdateQuest } from '../editors/useUpdateQuest';
type QuestEarningsTableProps = {
quest: StudioQuest;
- setEarningIndex: (index: number) => void;
+ editEarning: (index: number) => void;
};
-export const QuestEarningsTable = ({ quest, setEarningIndex }: QuestEarningsTableProps) => {
+export const QuestEarningsTable = ({ quest, editEarning }: QuestEarningsTableProps) => {
const updateQuest = useUpdateQuest(quest);
const { t } = useTranslation('database_quests');
@@ -27,7 +27,7 @@ export const QuestEarningsTable = ({ quest, setEarningIndex }: QuestEarningsTabl
setEarningIndex(index)}
+ onClickEdit={() => editEarning(index)}
onClickDelete={() => {
const newEarnings = cloneEntity(quest.earnings);
newEarnings.splice(index, 1);