fix(user): mobile announcement close + payment method selection (Fixes #8)
This commit is contained in:
parent
fee44fa1b2
commit
b32ba55ab8
@ -9,7 +9,7 @@ import {
|
|||||||
import { cn } from "@workspace/ui/lib/utils";
|
import { cn } from "@workspace/ui/lib/utils";
|
||||||
import { getAvailablePaymentMethods } from "@workspace/ui/services/user/portal";
|
import { getAvailablePaymentMethods } from "@workspace/ui/services/user/portal";
|
||||||
import type React from "react";
|
import type React from "react";
|
||||||
import { memo } from "react";
|
import React, { memo } from "react";
|
||||||
import { useTranslation } from "react-i18next";
|
import { useTranslation } from "react-i18next";
|
||||||
|
|
||||||
interface PaymentMethodsProps {
|
interface PaymentMethodsProps {
|
||||||
@ -30,12 +30,21 @@ const PaymentMethods: React.FC<PaymentMethodsProps> = ({
|
|||||||
queryFn: async () => {
|
queryFn: async () => {
|
||||||
const { data } = await getAvailablePaymentMethods();
|
const { data } = await getAvailablePaymentMethods();
|
||||||
const list = data.data?.list || [];
|
const list = data.data?.list || [];
|
||||||
const methods = balance ? list : list.filter((item) => item.id !== -1);
|
return balance ? list : list.filter((item) => item.id !== -1);
|
||||||
const defaultMethod = methods.find((item) => item.id)?.id;
|
|
||||||
if (defaultMethod) onChange(defaultMethod);
|
|
||||||
return methods;
|
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Only set a default when the current value is not a valid option.
|
||||||
|
// This avoids resetting the user's selection on refetch (common on mobile).
|
||||||
|
// Prefer non-balance methods when possible.
|
||||||
|
React.useEffect(() => {
|
||||||
|
if (!data || data.length === 0) return;
|
||||||
|
const valid = data.some((m) => String(m.id) === String(value));
|
||||||
|
if (valid) return;
|
||||||
|
|
||||||
|
const preferred = data.find((m) => m.id !== -1)?.id ?? data[0]!.id;
|
||||||
|
onChange(preferred);
|
||||||
|
}, [data, onChange, value]);
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<div className="font-semibold">
|
<div className="font-semibold">
|
||||||
@ -44,7 +53,6 @@ const PaymentMethods: React.FC<PaymentMethodsProps> = ({
|
|||||||
<RadioGroup
|
<RadioGroup
|
||||||
className="grid grid-cols-2 gap-2 md:grid-cols-5"
|
className="grid grid-cols-2 gap-2 md:grid-cols-5"
|
||||||
onValueChange={(val) => {
|
onValueChange={(val) => {
|
||||||
console.log(val);
|
|
||||||
onChange(Number(val));
|
onChange(Number(val));
|
||||||
}}
|
}}
|
||||||
value={String(value)}
|
value={String(value)}
|
||||||
|
|||||||
@ -11,6 +11,7 @@ import Empty from "@workspace/ui/composed/empty";
|
|||||||
import { Icon } from "@workspace/ui/composed/icon";
|
import { Icon } from "@workspace/ui/composed/icon";
|
||||||
import { Markdown } from "@workspace/ui/composed/markdown";
|
import { Markdown } from "@workspace/ui/composed/markdown";
|
||||||
import { queryAnnouncement } from "@workspace/ui/services/user/announcement";
|
import { queryAnnouncement } from "@workspace/ui/services/user/announcement";
|
||||||
|
import { useEffect, useState } from "react";
|
||||||
import { useTranslation } from "react-i18next";
|
import { useTranslation } from "react-i18next";
|
||||||
import { useGlobalStore } from "@/stores/global";
|
import { useGlobalStore } from "@/stores/global";
|
||||||
|
|
||||||
@ -39,10 +40,16 @@ export default function Announcement({ type }: { type: "popup" | "pinned" }) {
|
|||||||
|
|
||||||
if (!data) return null;
|
if (!data) return null;
|
||||||
|
|
||||||
|
const [open, setOpen] = useState(false);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (type === "popup" && !!data) setOpen(true);
|
||||||
|
}, [data, type]);
|
||||||
|
|
||||||
if (type === "popup") {
|
if (type === "popup") {
|
||||||
return (
|
return (
|
||||||
<Dialog defaultOpen={!!data}>
|
<Dialog onOpenChange={setOpen} open={open}>
|
||||||
<DialogContent className="sm:max-w-[425px]">
|
<DialogContent className="max-h-[85vh] overflow-auto sm:max-w-[425px]">
|
||||||
<DialogHeader>
|
<DialogHeader>
|
||||||
<DialogTitle>{data?.title}</DialogTitle>
|
<DialogTitle>{data?.title}</DialogTitle>
|
||||||
</DialogHeader>
|
</DialogHeader>
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user