-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
92 additions
and
38 deletions.
There are no files selected for viewing
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
91 changes: 67 additions & 24 deletions
91
src/views/components/database/quest/editors/QuestEarningEditor.tsx
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 |
---|---|---|
@@ -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<EditorHandlingClose, QuestEarningEditorProps>(({ 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 ( | ||
<Editor type="edit" title={t('earning_title', { id: padStr(earningIndex + 1, 2) })}> | ||
<InputContainer> | ||
<InputWithTopLabelContainer> | ||
<Label htmlFor="earning-type">{t('earning_type')}</Label> | ||
<SelectCustomSimple | ||
id={'earning-type-select'} | ||
value={earning.earningMethodName} | ||
options={earningOptions} | ||
onChange={(value) => refreshUI(changeEarning(value as StudioQuestEarningType))} | ||
noTooltip | ||
/> | ||
<Label htmlFor="earning-method-name">{t('earning_type')}</Label> | ||
<Select name="earning-method-name" value={earning.earningMethodName} options={earningOptions} onChange={changeEarning} /> | ||
</InputWithTopLabelContainer> | ||
{earning.earningMethodName === 'earning_money' && <QuestEarningMoney earning={earning} />} | ||
{earning.earningMethodName === 'earning_item' && <QuestEarningItem earning={earning} />} | ||
{earning.earningMethodName === 'earning_pokemon' && <QuestEarningPokemon earning={earning} />} | ||
{earning.earningMethodName === 'earning_egg' && <QuestEarningPokemon earning={earning} />} | ||
{earningMethodName === 'earning_money' && <QuestEarningMoney earning={earning} refs={refs} />} | ||
{earningMethodName === 'earning_item' && <QuestEarningItem earning={earning} refs={refs} />} | ||
{earningMethodName === 'earning_pokemon' && <QuestEarningPokemon earning={earning} refs={refs} />} | ||
{earningMethodName === 'earning_egg' && <QuestEarningPokemon earning={earning} refs={refs} />} | ||
</InputContainer> | ||
</Editor> | ||
); | ||
}; | ||
}); | ||
QuestEarningEditor.displayName = 'QuestEarningEditor'; |
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