import { createFileRoute } from '@tanstack/react-router' import React from 'react' import { Formik } from 'formik' import { AppContainer, Checkbox, MyButton, p as MyText, pInput as PInput } from 'web-components' import { ChevronRight, ListOrdered, Pencil } from 'lucide-react' import { trpc } from '../lib/trpc-client' import { ProtectedRoute } from '../components/ProtectedRoute' type ConstantFormData = Record const CONST_LABELS: Record = { minRegularOrderValue: 'Minimum Regular Order Value', freeDeliveryThreshold: 'Free Delivery Threshold', deliveryCharge: 'Delivery Charge', flashFreeDeliveryThreshold: 'Flash Free Delivery Threshold', flashDeliveryCharge: 'Flash Delivery Charge', platformFeePercent: 'Platform Fee Percent', taxRate: 'Tax Rate', minOrderAmountForCoupon: 'Minimum Order Amount for Coupon', maxCouponDiscount: 'Maximum Coupon Discount', flashDeliverySlotId: 'Flash Delivery Slot ID', readableOrderId: 'Readable Order ID', versionNum: 'Version Number', playStoreUrl: 'Play Store URL', appStoreUrl: 'App Store URL', popularItems: 'Popular Items', allItemsOrder: 'All Items Order', isFlashDeliveryEnabled: 'Enable Flash Delivery', supportMobile: 'Support Mobile', supportEmail: 'Support Email', internalAppVersion: 'Internal App Version', } const CONST_VISIBILITY: Record = { minRegularOrderValue: true, freeDeliveryThreshold: true, deliveryCharge: true, flashFreeDeliveryThreshold: true, flashDeliveryCharge: true, platformFeePercent: true, taxRate: false, minOrderAmountForCoupon: true, maxCouponDiscount: false, flashDeliverySlotId: true, readableOrderId: false, versionNum: true, playStoreUrl: true, appStoreUrl: true, popularItems: true, allItemsOrder: true, isFlashDeliveryEnabled: true, supportMobile: true, supportEmail: true, internalAppVersion: true, tester: false, } interface ConstantInputProps { constantKey: string value: any setFieldValue: (field: string, value: any) => void } const ConstantInput: React.FC = ({ constantKey, value, setFieldValue }) => { const fieldName = constantKey if (constantKey === 'popularItems') { return (
{CONST_LABELS[constantKey] || constantKey}
) } if (constantKey === 'allItemsOrder') { return (
{CONST_LABELS[constantKey] || constantKey}
) } if (typeof value === 'boolean') { return (
{CONST_LABELS[constantKey] || constantKey}
setFieldValue(fieldName, !value)} size={28} /> {value ? 'Enabled' : 'Disabled'}
) } if (typeof value === 'number') { return ( { const numValue = parseFloat(e.target.value) setFieldValue(fieldName, isNaN(numValue) ? 0 : numValue) }} placeholder="Enter number" /> ) } if (Array.isArray(value)) { return ( { const arrayValue = e.target.value .split(',') .map((s) => s.trim()) .filter((s) => s.length > 0) setFieldValue(fieldName, arrayValue) }} placeholder="Enter comma separated values" /> ) } return ( setFieldValue(fieldName, e.target.value)} placeholder="Enter value" /> ) } function CustomizeApp() { const { data: constants, isLoading: isLoadingConstants, refetch } = trpc.admin.const.getConstants.useQuery() const { mutate: updateConstants, isPending: isUpdating } = trpc.admin.const.updateConstants.useMutation() const handleSubmit = (values: ConstantFormData) => { const changedConstants = (constants || []) .filter((constant) => { const nextValue = values[constant.key] return JSON.stringify(nextValue) !== JSON.stringify(constant.value) }) .map((constant) => ({ key: constant.key, value: values[constant.key], })) if (changedConstants.length === 0) { window.alert('No Changes\n\nNo constants were modified.') return } updateConstants( { constants: changedConstants }, { onSuccess: (response) => { window.alert(`Success\n\nUpdated ${response.updatedCount} constants successfully.`) refetch() }, onError: (error) => { window.alert('Error\n\nFailed to update constants. Please try again.') console.error('Update constants error:', error) }, } ) } if (isLoadingConstants) { return (
Loading constants...
) } if (!constants) { return (
Failed to load constants
) } const initialValues: ConstantFormData = constants.reduce((acc, constant) => { acc[constant.key] = constant.value ?? '' return acc }, {} as ConstantFormData) return (
Customize App Constants Modify application constants. Changes will take effect immediately. {({ handleSubmit, values, setFieldValue }) => (
{constants.map((constant) => { if (!CONST_VISIBILITY[constant.key]) { return null } return (
) })} {isUpdating ? 'Updating...' : 'Save Changes'}
)}
) } export const Route = createFileRoute('/constants')({ component: () => ( ), })