-
Notifications
You must be signed in to change notification settings - Fork 769
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support custom subscription config (#24)
- Loading branch information
1 parent
aacc6b9
commit ed74b6b
Showing
27 changed files
with
1,198 additions
and
107 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { Plan } from "@/api/types"; | ||
import axios from "axios"; | ||
import { CommonResponse } from "@/admin/utils.ts"; | ||
import { getErrorMessage } from "@/utils/base.ts"; | ||
|
||
export type PlanConfig = { | ||
enabled: boolean; | ||
plans: Plan[]; | ||
}; | ||
|
||
export async function getPlanConfig(): Promise<PlanConfig> { | ||
try { | ||
const response = await axios.get("/admin/plan/view"); | ||
const conf = response.data as PlanConfig; | ||
conf.plans = (conf.plans || []).filter((item) => item.level > 0); | ||
if (conf.plans.length === 0) | ||
conf.plans = [1, 2, 3].map( | ||
(level) => ({ level, price: 0, items: [] }) as Plan, | ||
); | ||
return conf; | ||
} catch (e) { | ||
console.warn(e); | ||
return { enabled: false, plans: [] }; | ||
} | ||
} | ||
|
||
export async function setPlanConfig( | ||
config: PlanConfig, | ||
): Promise<CommonResponse> { | ||
try { | ||
const response = await axios.post(`/admin/plan/update`, config); | ||
return response.data as CommonResponse; | ||
} catch (e) { | ||
return { status: false, error: getErrorMessage(e) }; | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
import React from "react"; | ||
|
||
import { cn } from "@/components/ui/lib/utils"; | ||
import { Button } from "@/components/ui/button"; | ||
import { | ||
Command, | ||
CommandEmpty, | ||
CommandInput, | ||
CommandItem, | ||
CommandList, | ||
} from "@/components/ui/command"; | ||
import { | ||
Popover, | ||
PopoverContent, | ||
PopoverTrigger, | ||
} from "@/components/ui/popover"; | ||
import { Check, ChevronsUpDown } from "lucide-react"; | ||
import { useTranslation } from "react-i18next"; | ||
|
||
type MultiComboBoxProps = { | ||
value: string[]; | ||
onChange: (value: string[]) => void; | ||
list: string[]; | ||
placeholder?: string; | ||
searchPlaceholder?: string; | ||
defaultOpen?: boolean; | ||
className?: string; | ||
align?: "start" | "end" | "center" | undefined; | ||
}; | ||
|
||
export function MultiCombobox({ | ||
value, | ||
onChange, | ||
list, | ||
placeholder, | ||
searchPlaceholder, | ||
defaultOpen, | ||
className, | ||
align, | ||
}: MultiComboBoxProps) { | ||
const { t } = useTranslation(); | ||
const [open, setOpen] = React.useState(defaultOpen ?? false); | ||
const valueList = React.useMemo((): string[] => { | ||
// list set | ||
const set = new Set(list); | ||
return [...set]; | ||
}, [list]); | ||
|
||
return ( | ||
<Popover open={open} onOpenChange={setOpen}> | ||
<PopoverTrigger asChild> | ||
<Button | ||
variant="outline" | ||
role="combobox" | ||
aria-expanded={open} | ||
className={cn("w-[320px] max-w-[60vw] justify-between", className)} | ||
> | ||
<Check className="mr-2 h-4 w-4 shrink-0 opacity-50" /> | ||
{placeholder ?? `${value.length} Items Selected`} | ||
<ChevronsUpDown className="ml-2 h-4 w-4 shrink-0 opacity-50" /> | ||
</Button> | ||
</PopoverTrigger> | ||
<PopoverContent className="w-[320px] max-w-[60vw] p-0" align={align}> | ||
<Command> | ||
<CommandInput placeholder={searchPlaceholder} /> | ||
<CommandEmpty>{t("admin.empty")}</CommandEmpty> | ||
<CommandList> | ||
{valueList.map((key) => ( | ||
<CommandItem | ||
key={key} | ||
value={key} | ||
onSelect={(current) => { | ||
if (value.includes(current)) { | ||
onChange(value.filter((item) => item !== current)); | ||
} else { | ||
onChange([...value, current]); | ||
} | ||
}} | ||
> | ||
<Check | ||
className={cn( | ||
"mr-2 h-4 w-4", | ||
value.includes(key) ? "opacity-100" : "opacity-0", | ||
)} | ||
/> | ||
{key} | ||
</CommandItem> | ||
))} | ||
</CommandList> | ||
</Command> | ||
</PopoverContent> | ||
</Popover> | ||
); | ||
} |
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
Oops, something went wrong.