fix some bugs

This commit is contained in:
2026-06-23 22:31:21 +03:30
parent 71764f7e9a
commit 69da635247
9 changed files with 156 additions and 239 deletions
@@ -6,18 +6,6 @@
class="wallet-charge-form"
:class="formClass"
>
<a-form-item label="کیف پول / محصول" name="chargeTargetKey" :rules="targetRules">
<a-select
v-model:value="modelValue.chargeTargetKey"
show-search
placeholder="انتخاب کنید"
class="w-full"
:options="targetOptions"
:filter-option="filterTarget"
@change="onTargetChange"
/>
</a-form-item>
<a-form-item label="نوع عملیات" name="direction">
<a-radio-group
v-model:value="modelValue.direction"
@@ -48,10 +36,10 @@
<BasicInput
v-model:value="modelValue.amountAbs"
:addon-before="unit"
:type="isMoney ? 'price' : 'number'"
:type="variant === 'money' ? 'price' : 'number'"
:place-holder="amountPlaceholder"
/>
<div v-if="isMoney && Number(modelValue.amountAbs) > 10" class="wallet-charge-form__hint">
<div v-if="variant === 'money' && Number(modelValue.amountAbs) > 10" class="wallet-charge-form__hint">
معادل {{ usePrice(Number(modelValue.amountAbs) / 10, true, 'تومان') }}
</div>
</a-form-item>
@@ -63,46 +51,23 @@
</template>
<script setup lang="ts">
import { computed, ref, watch } from 'vue'
import { computed, ref } from 'vue'
import type { FormInstance } from 'ant-design-vue'
import type { Charge, WalletChargeTarget } from '@/model/wallet'
import type { Charge } from '@/model/wallet'
import { inputRules } from '@/utils/inputRules'
import { usePrice } from '@core/utils/usePrice'
import { applyChargeTarget, resolveChargeTarget, walletPreviewLabel } from '../utils/walletChargeForm'
import { m } from '@/utils/metalLabels'
const props = defineProps<{
variant: 'money' | 'gold'
modelValue: Charge
targets: WalletChargeTarget[]
}>()
const formRef = ref<FormInstance>()
const selectedTarget = computed(() =>
resolveChargeTarget(props.targets, props.modelValue.chargeTargetKey)
)
const isMoney = computed(() => selectedTarget.value?.kind === 'money')
const unit = computed(() => selectedTarget.value?.unit ?? 'ریال')
const unit = computed(() => (props.variant === 'money' ? 'ریال' : 'گرم'))
const isDischarge = computed(() => props.modelValue.direction === 'discharge')
const targetOptions = computed(() =>
props.targets.map((target) => ({
value: target.key,
label: target.label
}))
)
const targetRules = [
{
validator: (_rule: unknown, value: string | undefined) => {
if (!value || !resolveChargeTarget(props.targets, value)) {
return Promise.reject('کیف پول یا محصول را انتخاب کنید')
}
return Promise.resolve()
}
}
]
const formClass = computed(() =>
isDischarge.value ? 'wallet-charge-form--discharge' : 'wallet-charge-form--charge'
)
@@ -117,7 +82,8 @@ const previewSign = computed(() => (isDischarge.value ? '−' : '+'))
const previewText = computed(() => {
const action = isDischarge.value ? 'کسر از' : 'افزایش'
return `${action} ${walletPreviewLabel(selectedTarget.value)}`
const wallet = props.variant === 'money' ? 'موجودی ریالی' : m('موجودی طلایی')
return `${action} ${wallet} کاربر`
})
const amountLabel = computed(() =>
@@ -127,7 +93,7 @@ const amountLabel = computed(() =>
const noteLabel = computed(() => (isDischarge.value ? 'علت دشارژ' : 'علت شارژ'))
const amountPlaceholder = computed(() =>
isMoney.value ? 'مثلاً ۱۰۰٬۰۰۰' : 'مثلاً ۰.۵'
props.variant === 'money' ? 'مثلاً ۱۰۰٬۰۰۰' : 'مثلاً ۰.۵'
)
const notePlaceholder = computed(() =>
@@ -147,28 +113,6 @@ const amountRules = [
}
]
function filterTarget(input: string, option?: { label?: string }) {
return String(option?.label ?? '').toLowerCase().includes(input.toLowerCase())
}
function onTargetChange(key: string) {
const target = resolveChargeTarget(props.targets, key)
if (!target) return
Object.assign(props.modelValue, applyChargeTarget(props.modelValue, target))
}
watch(
() => props.targets,
(targets) => {
if (!targets.length) return
const current = resolveChargeTarget(targets, props.modelValue.chargeTargetKey)
if (!current) {
Object.assign(props.modelValue, applyChargeTarget(props.modelValue, targets[0]))
}
},
{ immediate: true }
)
defineExpose({
validateFields: () => formRef.value?.validateFields()
})