Skip to content

Commit

Permalink
feat: infer percent or price
Browse files Browse the repository at this point in the history
  • Loading branch information
yjl9903 committed Mar 5, 2024
1 parent ae51d02 commit 0b0947a
Showing 1 changed file with 41 additions and 4 deletions.
45 changes: 41 additions & 4 deletions components/CreateLog.vue
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ const formSchema = toTypedSchema(
z.object({
targetCity: z.string(),
price: z.number().gt(0),
percent: z.array(z.number().gte(0).lte(200)),
percent: z.array(z.number().gt(0).lt(200)),
trend: z.enum(['up', 'same', 'down'])
})
);
Expand All @@ -62,6 +62,43 @@ watch(open, (open) => {
}
});
watch(
() => [form.values.targetCity, form.values.price] as const,
([_, price]) => {
if (price === '') form.resetField('price');
if (price === undefined) return;
if (form.isFieldDirty('percent')) return;
if (!form.values.targetCity) return;
const transaction = props.product.transactions.find(
(tr) => tr.targetCity === form.values.targetCity
);
if (transaction && transaction.basePrice > 0) {
const percent = +((100 * price) / transaction.basePrice).toFixed(0);
if (percent > 0 && percent < 200) {
form.resetField('percent', { value: [percent] });
}
}
}
);
watch(
() => [form.values.targetCity, form.values.percent] as const,
([_, percent]) => {
if (percent === undefined) return;
if (form.isFieldDirty('price')) return;
if (!form.values.targetCity) return;
const transaction = props.product.transactions.find(
(tr) => tr.targetCity === form.values.targetCity
);
if (transaction && transaction.basePrice > 0) {
const price = +((percent[0] / 100.0) * transaction.basePrice).toFixed(0);
if (price > 0) {
form.resetField('price', { value: price });
}
}
}
);
const store = useLatestLogs();
const onSubmit = form.handleSubmit(async (values) => {
Expand Down Expand Up @@ -166,14 +203,14 @@ const onSubmit = form.handleSubmit(async (values) => {
<Slider
v-bind="componentField"
:default-value="[100]"
:max="150"
:min="50"
:max="199"
:min="1"
:step="1"
/>
<FormDescription class="flex justify-between">
<span
>{{ form.values.targetCity !== city.name ? '售出价位' : '买入价位' }}
{{ form.values.percent[0] }}%</span
{{ form.values.percent?.[0] ?? 100 }}%</span
>
</FormDescription>
</FormControl>
Expand Down

0 comments on commit 0b0947a

Please sign in to comment.