@apptimate/ui 1.1.0 → 1.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +4 -3
- package/src/base-components/DateLabel.tsx +68 -0
- package/src/base-components/DateRangePicker.tsx +392 -0
- package/src/base-components/ImageDropzone.tsx +80 -0
- package/src/base-components/Modal.tsx +19 -7
- package/src/base-components/PopConfirm.tsx +15 -4
- package/src/common-components/DashboardLayout.tsx +16 -5
- package/src/common-components/charts/RAreaChart.tsx +139 -0
- package/src/common-components/charts/RBarChart.tsx +171 -0
- package/src/common-components/charts/RLineChart.tsx +156 -0
- package/src/common-components/charts/RPieChart.tsx +215 -0
- package/src/common-components/charts/RStatCard.tsx +138 -0
- package/src/common-components/charts/index.ts +7 -0
- package/src/common-components/charts/palette.ts +84 -0
- package/src/components/shared/CustomerSelectorComponent.tsx +163 -0
- package/src/components/shared/ImageUploadComponent.tsx +228 -0
- package/src/components/shared/PaymentModeComponent.tsx +421 -0
- package/src/components/shared/ProcurementManagerSelector.tsx +5 -0
- package/src/components/shared/ProductSelectorComponent.tsx +166 -0
- package/src/components/shared/SupplierSelectorComponent.tsx +5 -0
- package/src/index.tsx +6 -1
- /package/src/common-components/{Charts.tsx → charts/ApexCharts.tsx} +0 -0
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import React, { useState, useCallback } from "react";
|
|
4
|
+
import {
|
|
5
|
+
PieChart,
|
|
6
|
+
Pie,
|
|
7
|
+
Cell,
|
|
8
|
+
Tooltip,
|
|
9
|
+
Legend,
|
|
10
|
+
ResponsiveContainer,
|
|
11
|
+
Sector,
|
|
12
|
+
} from "recharts";
|
|
13
|
+
import {
|
|
14
|
+
CHART_COLORS,
|
|
15
|
+
CHART_GRADIENTS,
|
|
16
|
+
TOOLTIP_STYLE,
|
|
17
|
+
TOOLTIP_ITEM_STYLE,
|
|
18
|
+
ANIMATION_DURATION,
|
|
19
|
+
ANIMATION_EASING,
|
|
20
|
+
} from "./palette";
|
|
21
|
+
|
|
22
|
+
export interface RPieChartProps {
|
|
23
|
+
data: Record<string, any>[];
|
|
24
|
+
dataKey: string;
|
|
25
|
+
nameKey: string;
|
|
26
|
+
height?: number;
|
|
27
|
+
colors?: string[];
|
|
28
|
+
innerRadius?: number | string;
|
|
29
|
+
outerRadius?: number | string;
|
|
30
|
+
showTooltip?: boolean;
|
|
31
|
+
showLegend?: boolean;
|
|
32
|
+
legendPosition?: "top" | "bottom" | "right";
|
|
33
|
+
showLabels?: boolean;
|
|
34
|
+
labelType?: "percentage" | "value" | "name";
|
|
35
|
+
centerLabel?: string;
|
|
36
|
+
centerValue?: string | number;
|
|
37
|
+
enableGradient?: boolean;
|
|
38
|
+
tooltipFormatter?: (value: any, name: string) => [string, string];
|
|
39
|
+
className?: string;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// Custom active shape for hover-expand effect
|
|
43
|
+
function renderActiveShape(props: any) {
|
|
44
|
+
const {
|
|
45
|
+
cx, cy, innerRadius, outerRadius, startAngle, endAngle,
|
|
46
|
+
fill, payload, percent, value, nameKey, dataKey,
|
|
47
|
+
} = props;
|
|
48
|
+
|
|
49
|
+
return (
|
|
50
|
+
<g>
|
|
51
|
+
<Sector
|
|
52
|
+
cx={cx}
|
|
53
|
+
cy={cy}
|
|
54
|
+
innerRadius={innerRadius - 2}
|
|
55
|
+
outerRadius={outerRadius + 8}
|
|
56
|
+
startAngle={startAngle}
|
|
57
|
+
endAngle={endAngle}
|
|
58
|
+
fill={fill}
|
|
59
|
+
opacity={1}
|
|
60
|
+
style={{ filter: "drop-shadow(0 4px 12px rgba(0,0,0,0.15))", transition: "all 0.3s ease" }}
|
|
61
|
+
/>
|
|
62
|
+
<Sector
|
|
63
|
+
cx={cx}
|
|
64
|
+
cy={cy}
|
|
65
|
+
innerRadius={innerRadius}
|
|
66
|
+
outerRadius={outerRadius}
|
|
67
|
+
startAngle={startAngle}
|
|
68
|
+
endAngle={endAngle}
|
|
69
|
+
fill={fill}
|
|
70
|
+
/>
|
|
71
|
+
</g>
|
|
72
|
+
);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
// Percentage label renderer
|
|
76
|
+
function renderLabel(props: any) {
|
|
77
|
+
const { cx, cy, midAngle, innerRadius, outerRadius, percent, name, labelType, value } = props;
|
|
78
|
+
const RADIAN = Math.PI / 180;
|
|
79
|
+
const radius = outerRadius + 20;
|
|
80
|
+
const x = cx + radius * Math.cos(-midAngle * RADIAN);
|
|
81
|
+
const y = cy + radius * Math.sin(-midAngle * RADIAN);
|
|
82
|
+
|
|
83
|
+
if (percent < 0.04) return null; // Hide labels for tiny slices
|
|
84
|
+
|
|
85
|
+
let displayText = `${(percent * 100).toFixed(0)}%`;
|
|
86
|
+
if (labelType === "value") displayText = `${value}`;
|
|
87
|
+
if (labelType === "name") displayText = name;
|
|
88
|
+
|
|
89
|
+
return (
|
|
90
|
+
<text
|
|
91
|
+
x={x}
|
|
92
|
+
y={y}
|
|
93
|
+
fill="#64748b"
|
|
94
|
+
textAnchor={x > cx ? "start" : "end"}
|
|
95
|
+
dominantBaseline="central"
|
|
96
|
+
fontSize={11}
|
|
97
|
+
fontWeight={600}
|
|
98
|
+
>
|
|
99
|
+
{displayText}
|
|
100
|
+
</text>
|
|
101
|
+
);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
export function RPieChart({
|
|
105
|
+
data,
|
|
106
|
+
dataKey,
|
|
107
|
+
nameKey,
|
|
108
|
+
height = 350,
|
|
109
|
+
colors = [...CHART_COLORS],
|
|
110
|
+
innerRadius = "55%",
|
|
111
|
+
outerRadius = "80%",
|
|
112
|
+
showTooltip = true,
|
|
113
|
+
showLegend = true,
|
|
114
|
+
legendPosition = "bottom",
|
|
115
|
+
showLabels = false,
|
|
116
|
+
labelType = "percentage",
|
|
117
|
+
centerLabel,
|
|
118
|
+
centerValue,
|
|
119
|
+
enableGradient = true,
|
|
120
|
+
tooltipFormatter,
|
|
121
|
+
className = "",
|
|
122
|
+
}: RPieChartProps) {
|
|
123
|
+
const [activeIndex, setActiveIndex] = useState<number | undefined>(undefined);
|
|
124
|
+
const gradientId = React.useId();
|
|
125
|
+
|
|
126
|
+
const onMouseEnter = useCallback((_: any, index: number) => {
|
|
127
|
+
setActiveIndex(index);
|
|
128
|
+
}, []);
|
|
129
|
+
|
|
130
|
+
const onMouseLeave = useCallback(() => {
|
|
131
|
+
setActiveIndex(undefined);
|
|
132
|
+
}, []);
|
|
133
|
+
|
|
134
|
+
return (
|
|
135
|
+
<div className={`w-full relative ${className}`} style={{ height }}>
|
|
136
|
+
<ResponsiveContainer width="100%" height="100%">
|
|
137
|
+
<PieChart>
|
|
138
|
+
{enableGradient && (
|
|
139
|
+
<defs>
|
|
140
|
+
{data.map((_, idx) => {
|
|
141
|
+
const gradientPair = CHART_GRADIENTS[idx % CHART_GRADIENTS.length];
|
|
142
|
+
const id = `pie-gradient-${gradientId}-${idx}`;
|
|
143
|
+
return (
|
|
144
|
+
<linearGradient key={id} id={id} x1="0" y1="0" x2="1" y2="1">
|
|
145
|
+
<stop offset="0%" stopColor={gradientPair[0]} stopOpacity={1} />
|
|
146
|
+
<stop offset="100%" stopColor={gradientPair[1]} stopOpacity={0.85} />
|
|
147
|
+
</linearGradient>
|
|
148
|
+
);
|
|
149
|
+
})}
|
|
150
|
+
</defs>
|
|
151
|
+
)}
|
|
152
|
+
<Pie
|
|
153
|
+
data={data}
|
|
154
|
+
dataKey={dataKey}
|
|
155
|
+
nameKey={nameKey}
|
|
156
|
+
cx="50%"
|
|
157
|
+
cy="50%"
|
|
158
|
+
innerRadius={innerRadius}
|
|
159
|
+
outerRadius={outerRadius}
|
|
160
|
+
paddingAngle={2}
|
|
161
|
+
activeIndex={activeIndex}
|
|
162
|
+
activeShape={renderActiveShape}
|
|
163
|
+
onMouseEnter={onMouseEnter}
|
|
164
|
+
onMouseLeave={onMouseLeave}
|
|
165
|
+
isAnimationActive={true}
|
|
166
|
+
animationDuration={ANIMATION_DURATION}
|
|
167
|
+
animationEasing={ANIMATION_EASING}
|
|
168
|
+
label={showLabels ? (props: any) => renderLabel({ ...props, labelType }) : undefined}
|
|
169
|
+
>
|
|
170
|
+
{data.map((_, idx) => (
|
|
171
|
+
<Cell
|
|
172
|
+
key={`cell-${idx}`}
|
|
173
|
+
fill={enableGradient ? `url(#pie-gradient-${gradientId}-${idx})` : colors[idx % colors.length]}
|
|
174
|
+
stroke="none"
|
|
175
|
+
/>
|
|
176
|
+
))}
|
|
177
|
+
</Pie>
|
|
178
|
+
{showTooltip && (
|
|
179
|
+
<Tooltip
|
|
180
|
+
formatter={tooltipFormatter}
|
|
181
|
+
contentStyle={TOOLTIP_STYLE}
|
|
182
|
+
itemStyle={TOOLTIP_ITEM_STYLE}
|
|
183
|
+
animationDuration={300}
|
|
184
|
+
/>
|
|
185
|
+
)}
|
|
186
|
+
{showLegend && (
|
|
187
|
+
<Legend
|
|
188
|
+
verticalAlign={legendPosition === "right" ? "middle" : legendPosition}
|
|
189
|
+
align={legendPosition === "right" ? "right" : "center"}
|
|
190
|
+
layout={legendPosition === "right" ? "vertical" : "horizontal"}
|
|
191
|
+
wrapperStyle={{ fontSize: "12px", fontWeight: 700, paddingTop: legendPosition === "bottom" ? "8px" : "0" }}
|
|
192
|
+
iconType="circle"
|
|
193
|
+
iconSize={8}
|
|
194
|
+
/>
|
|
195
|
+
)}
|
|
196
|
+
</PieChart>
|
|
197
|
+
</ResponsiveContainer>
|
|
198
|
+
|
|
199
|
+
{/* Center label for donut charts */}
|
|
200
|
+
{(centerLabel || centerValue) && (
|
|
201
|
+
<div
|
|
202
|
+
className="absolute inset-0 flex flex-col items-center justify-center pointer-events-none"
|
|
203
|
+
style={{ paddingBottom: showLegend && legendPosition === "bottom" ? "40px" : "0" }}
|
|
204
|
+
>
|
|
205
|
+
{centerValue && (
|
|
206
|
+
<span className="text-2xl font-bold text-[#2D3142] tracking-tight">{centerValue}</span>
|
|
207
|
+
)}
|
|
208
|
+
{centerLabel && (
|
|
209
|
+
<span className="text-xs font-semibold text-gray-400 uppercase tracking-wider">{centerLabel}</span>
|
|
210
|
+
)}
|
|
211
|
+
</div>
|
|
212
|
+
)}
|
|
213
|
+
</div>
|
|
214
|
+
);
|
|
215
|
+
}
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import React, { useEffect, useState, useRef } from "react";
|
|
4
|
+
import { TrendingUp, TrendingDown, Minus } from "lucide-react";
|
|
5
|
+
import { SEMANTIC_COLORS } from "./palette";
|
|
6
|
+
|
|
7
|
+
export interface RStatCardProps {
|
|
8
|
+
label: string;
|
|
9
|
+
value: number | string;
|
|
10
|
+
prefix?: string;
|
|
11
|
+
suffix?: string;
|
|
12
|
+
decimals?: number;
|
|
13
|
+
trend?: "up" | "down" | "neutral";
|
|
14
|
+
trendValue?: string;
|
|
15
|
+
subtitle?: string;
|
|
16
|
+
icon?: React.ReactNode;
|
|
17
|
+
color?: string;
|
|
18
|
+
animate?: boolean;
|
|
19
|
+
className?: string;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
// Animated count-up hook
|
|
23
|
+
function useCountUp(target: number, duration: number = 1000, enabled: boolean = true) {
|
|
24
|
+
const [current, setCurrent] = useState(0);
|
|
25
|
+
const frameRef = useRef<number | null>(null);
|
|
26
|
+
|
|
27
|
+
useEffect(() => {
|
|
28
|
+
if (!enabled || typeof target !== "number" || isNaN(target)) {
|
|
29
|
+
setCurrent(target);
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const startTime = performance.now();
|
|
34
|
+
const startValue = 0;
|
|
35
|
+
|
|
36
|
+
function tick(now: number) {
|
|
37
|
+
const elapsed = now - startTime;
|
|
38
|
+
const progress = Math.min(elapsed / duration, 1);
|
|
39
|
+
// Ease-out cubic
|
|
40
|
+
const eased = 1 - Math.pow(1 - progress, 3);
|
|
41
|
+
setCurrent(startValue + (target - startValue) * eased);
|
|
42
|
+
|
|
43
|
+
if (progress < 1) {
|
|
44
|
+
frameRef.current = requestAnimationFrame(tick);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
frameRef.current = requestAnimationFrame(tick);
|
|
49
|
+
return () => {
|
|
50
|
+
if (frameRef.current) cancelAnimationFrame(frameRef.current);
|
|
51
|
+
};
|
|
52
|
+
}, [target, duration, enabled]);
|
|
53
|
+
|
|
54
|
+
return current;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function TrendIndicator({ trend, trendValue }: { trend: "up" | "down" | "neutral"; trendValue?: string }) {
|
|
58
|
+
const config = {
|
|
59
|
+
up: {
|
|
60
|
+
Icon: TrendingUp,
|
|
61
|
+
color: SEMANTIC_COLORS.success,
|
|
62
|
+
bg: "rgba(34,197,94,0.1)",
|
|
63
|
+
},
|
|
64
|
+
down: {
|
|
65
|
+
Icon: TrendingDown,
|
|
66
|
+
color: SEMANTIC_COLORS.danger,
|
|
67
|
+
bg: "rgba(239,68,68,0.1)",
|
|
68
|
+
},
|
|
69
|
+
neutral: {
|
|
70
|
+
Icon: Minus,
|
|
71
|
+
color: SEMANTIC_COLORS.neutral,
|
|
72
|
+
bg: "rgba(100,116,139,0.1)",
|
|
73
|
+
},
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
const { Icon, color, bg } = config[trend];
|
|
77
|
+
|
|
78
|
+
return (
|
|
79
|
+
<div
|
|
80
|
+
className="inline-flex items-center gap-1 px-2 py-0.5 rounded-full text-[11px] font-bold"
|
|
81
|
+
style={{ backgroundColor: bg, color }}
|
|
82
|
+
>
|
|
83
|
+
<Icon size={12} strokeWidth={2.5} />
|
|
84
|
+
{trendValue && <span>{trendValue}</span>}
|
|
85
|
+
</div>
|
|
86
|
+
);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export function RStatCard({
|
|
90
|
+
label,
|
|
91
|
+
value,
|
|
92
|
+
prefix = "",
|
|
93
|
+
suffix = "",
|
|
94
|
+
decimals = 0,
|
|
95
|
+
trend,
|
|
96
|
+
trendValue,
|
|
97
|
+
subtitle,
|
|
98
|
+
icon,
|
|
99
|
+
color,
|
|
100
|
+
animate = true,
|
|
101
|
+
className = "",
|
|
102
|
+
}: RStatCardProps) {
|
|
103
|
+
const isNumeric = typeof value === "number" && !isNaN(value);
|
|
104
|
+
const animatedValue = useCountUp(isNumeric ? value : 0, 1200, animate && isNumeric);
|
|
105
|
+
|
|
106
|
+
const displayValue = isNumeric
|
|
107
|
+
? `${prefix}${animatedValue.toLocaleString(undefined, { minimumFractionDigits: decimals, maximumFractionDigits: decimals })}${suffix}`
|
|
108
|
+
: `${prefix}${value}${suffix}`;
|
|
109
|
+
|
|
110
|
+
return (
|
|
111
|
+
<div className={`flex flex-col gap-2 p-4 rounded-xl bg-white border border-gray-100 hover:border-gray-200 transition-all duration-200 hover:shadow-sm ${className}`}>
|
|
112
|
+
<div className="flex items-center justify-between">
|
|
113
|
+
<span className="text-xs font-semibold text-gray-400 uppercase tracking-wider">{label}</span>
|
|
114
|
+
{icon && (
|
|
115
|
+
<div
|
|
116
|
+
className="w-8 h-8 rounded-lg flex items-center justify-center"
|
|
117
|
+
style={{ backgroundColor: color ? `${color}15` : "rgba(99,102,241,0.08)" }}
|
|
118
|
+
>
|
|
119
|
+
{React.isValidElement(icon)
|
|
120
|
+
? React.cloneElement(icon as React.ReactElement, { size: 16, style: { color: color || "#6366f1" } } as any)
|
|
121
|
+
: icon}
|
|
122
|
+
</div>
|
|
123
|
+
)}
|
|
124
|
+
</div>
|
|
125
|
+
|
|
126
|
+
<div className="flex items-end gap-2">
|
|
127
|
+
<span className="text-2xl font-bold text-[#2D3142] tracking-tight leading-none">
|
|
128
|
+
{displayValue}
|
|
129
|
+
</span>
|
|
130
|
+
{trend && <TrendIndicator trend={trend} trendValue={trendValue} />}
|
|
131
|
+
</div>
|
|
132
|
+
|
|
133
|
+
{subtitle && (
|
|
134
|
+
<span className="text-[11px] font-medium text-gray-400 leading-tight">{subtitle}</span>
|
|
135
|
+
)}
|
|
136
|
+
</div>
|
|
137
|
+
);
|
|
138
|
+
}
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
// ── Chart Color Palette ──────────────────────────────────────────────────────
|
|
2
|
+
// 16 curated colors with gradient pairs for premium chart aesthetics.
|
|
3
|
+
// Organized into primary rotation + semantic sets.
|
|
4
|
+
|
|
5
|
+
export const CHART_COLORS = [
|
|
6
|
+
'#6366f1', // Indigo
|
|
7
|
+
'#22c55e', // Green
|
|
8
|
+
'#f59e0b', // Amber
|
|
9
|
+
'#ef4444', // Red
|
|
10
|
+
'#06b6d4', // Cyan
|
|
11
|
+
'#8b5cf6', // Violet
|
|
12
|
+
'#ec4899', // Pink
|
|
13
|
+
'#14b8a6', // Teal
|
|
14
|
+
'#f97316', // Orange
|
|
15
|
+
'#3b82f6', // Blue
|
|
16
|
+
'#a855f7', // Purple
|
|
17
|
+
'#10b981', // Emerald
|
|
18
|
+
'#e11d48', // Rose
|
|
19
|
+
'#0ea5e9', // Sky
|
|
20
|
+
'#84cc16', // Lime
|
|
21
|
+
'#d946ef', // Fuchsia
|
|
22
|
+
] as const;
|
|
23
|
+
|
|
24
|
+
// Gradient pairs: [from, to] for gradient fills on charts
|
|
25
|
+
export const CHART_GRADIENTS: [string, string][] = [
|
|
26
|
+
['#6366f1', '#818cf8'], // Indigo
|
|
27
|
+
['#22c55e', '#4ade80'], // Green
|
|
28
|
+
['#f59e0b', '#fbbf24'], // Amber
|
|
29
|
+
['#ef4444', '#f87171'], // Red
|
|
30
|
+
['#06b6d4', '#22d3ee'], // Cyan
|
|
31
|
+
['#8b5cf6', '#a78bfa'], // Violet
|
|
32
|
+
['#ec4899', '#f472b6'], // Pink
|
|
33
|
+
['#14b8a6', '#2dd4bf'], // Teal
|
|
34
|
+
['#f97316', '#fb923c'], // Orange
|
|
35
|
+
['#3b82f6', '#60a5fa'], // Blue
|
|
36
|
+
['#a855f7', '#c084fc'], // Purple
|
|
37
|
+
['#10b981', '#34d399'], // Emerald
|
|
38
|
+
['#e11d48', '#fb7185'], // Rose
|
|
39
|
+
['#0ea5e9', '#38bdf8'], // Sky
|
|
40
|
+
['#84cc16', '#a3e635'], // Lime
|
|
41
|
+
['#d946ef', '#e879f9'], // Fuchsia
|
|
42
|
+
];
|
|
43
|
+
|
|
44
|
+
// Semantic color sets for specific use-cases
|
|
45
|
+
export const SEMANTIC_COLORS = {
|
|
46
|
+
success: '#22c55e',
|
|
47
|
+
successLight: '#4ade80',
|
|
48
|
+
warning: '#f59e0b',
|
|
49
|
+
warningLight: '#fbbf24',
|
|
50
|
+
danger: '#ef4444',
|
|
51
|
+
dangerLight: '#f87171',
|
|
52
|
+
info: '#3b82f6',
|
|
53
|
+
infoLight: '#60a5fa',
|
|
54
|
+
neutral: '#64748b',
|
|
55
|
+
neutralLight: '#94a3b8',
|
|
56
|
+
} as const;
|
|
57
|
+
|
|
58
|
+
// Tooltip styling constants (shared across all chart components)
|
|
59
|
+
export const TOOLTIP_STYLE = {
|
|
60
|
+
backgroundColor: '#1e293b',
|
|
61
|
+
color: '#fff',
|
|
62
|
+
borderRadius: '10px',
|
|
63
|
+
border: 'none',
|
|
64
|
+
boxShadow: '0 10px 30px rgba(0,0,0,0.2)',
|
|
65
|
+
padding: '10px 14px',
|
|
66
|
+
fontSize: '12px',
|
|
67
|
+
fontWeight: 600,
|
|
68
|
+
} as const;
|
|
69
|
+
|
|
70
|
+
export const TOOLTIP_ITEM_STYLE = {
|
|
71
|
+
color: '#cbd5e1',
|
|
72
|
+
fontSize: '12px',
|
|
73
|
+
fontWeight: 500,
|
|
74
|
+
} as const;
|
|
75
|
+
|
|
76
|
+
export const AXIS_TICK_STYLE = {
|
|
77
|
+
fontSize: 11,
|
|
78
|
+
fontWeight: 600,
|
|
79
|
+
fill: '#94a3b8',
|
|
80
|
+
} as const;
|
|
81
|
+
|
|
82
|
+
export const GRID_STROKE = '#f1f5f9';
|
|
83
|
+
export const ANIMATION_DURATION = 1000;
|
|
84
|
+
export const ANIMATION_EASING = 'ease-out' as const;
|
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import React, { useState, useEffect, useCallback, useRef } from "react";
|
|
3
|
+
import { Modal, Button, Input, Pagination, EmptyState, ModalFooter } from "@apptimate/ui";
|
|
4
|
+
import { Search, User, Phone, Mail, MapPin, X } from "lucide-react";
|
|
5
|
+
import toast from "react-hot-toast";
|
|
6
|
+
import { sendRequest, IApiResponse } from "@apptimate/core-lib";
|
|
7
|
+
|
|
8
|
+
export interface SelectedCustomer {
|
|
9
|
+
id: number;
|
|
10
|
+
customer_name: string;
|
|
11
|
+
customer_code?: string;
|
|
12
|
+
phone?: string;
|
|
13
|
+
email?: string;
|
|
14
|
+
address?: string;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export interface CustomerSelectorProps {
|
|
18
|
+
/** Currently selected customer ID */
|
|
19
|
+
value?: number | null;
|
|
20
|
+
/** Callback with full customer object on select */
|
|
21
|
+
onChange: (customer: SelectedCustomer | null) => void;
|
|
22
|
+
/** API base URL */
|
|
23
|
+
apiBase?: string;
|
|
24
|
+
/** API endpoint for listing customers. Default: /api/sales/customers */
|
|
25
|
+
apiPath?: string;
|
|
26
|
+
/** Whether the selector is disabled */
|
|
27
|
+
disabled?: boolean;
|
|
28
|
+
/** Label text */
|
|
29
|
+
label?: string;
|
|
30
|
+
/** Placeholder text */
|
|
31
|
+
placeholder?: string;
|
|
32
|
+
/** Required */
|
|
33
|
+
required?: boolean;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export default function CustomerSelectorComponent({
|
|
37
|
+
value,
|
|
38
|
+
onChange,
|
|
39
|
+
apiBase = process.env.NEXT_PUBLIC_API_URL || "",
|
|
40
|
+
apiPath = "/api/sales/customers",
|
|
41
|
+
disabled = false,
|
|
42
|
+
label = "Customer",
|
|
43
|
+
placeholder = "Search customers...",
|
|
44
|
+
required = false,
|
|
45
|
+
}: CustomerSelectorProps) {
|
|
46
|
+
const [isOpen, setIsOpen] = useState(false);
|
|
47
|
+
const [search, setSearch] = useState("");
|
|
48
|
+
const [dSearch, setDSearch] = useState("");
|
|
49
|
+
const [customers, setCustomers] = useState<any[]>([]);
|
|
50
|
+
const [loading, setLoading] = useState(false);
|
|
51
|
+
const [page, setPage] = useState(1);
|
|
52
|
+
const [total, setTotal] = useState(0);
|
|
53
|
+
const [selected, setSelected] = useState<SelectedCustomer | null>(null);
|
|
54
|
+
const searchRef = useRef<HTMLInputElement>(null);
|
|
55
|
+
|
|
56
|
+
useEffect(() => { const t = setTimeout(() => { setDSearch(search); setPage(1); }, 350); return () => clearTimeout(t); }, [search]);
|
|
57
|
+
|
|
58
|
+
const fetchCustomers = useCallback(async () => {
|
|
59
|
+
setLoading(true);
|
|
60
|
+
try {
|
|
61
|
+
const qs = new URLSearchParams({ page: String(page), per_page: "15", ...(dSearch ? { search: dSearch } : {}) }).toString();
|
|
62
|
+
const r = await sendRequest({ url: `${apiBase}${apiPath}?${qs}`, method: "GET" });
|
|
63
|
+
const data = r.responseData as IApiResponse;
|
|
64
|
+
if (data.is_success) { setCustomers(data.result?.data || []); setTotal(data.result?.total ?? 0); }
|
|
65
|
+
} catch (e: any) { toast.error(e.message); } finally { setLoading(false); }
|
|
66
|
+
}, [page, dSearch, apiBase, apiPath]);
|
|
67
|
+
|
|
68
|
+
useEffect(() => { if (isOpen) fetchCustomers(); }, [isOpen, fetchCustomers]);
|
|
69
|
+
useEffect(() => { if (isOpen) setTimeout(() => searchRef.current?.focus(), 100); }, [isOpen]);
|
|
70
|
+
|
|
71
|
+
const handleSelect = (c: any) => {
|
|
72
|
+
const sel: SelectedCustomer = {
|
|
73
|
+
id: c.id,
|
|
74
|
+
customer_name: c.customer_name || c.name,
|
|
75
|
+
customer_code: c.customer_code || c.code,
|
|
76
|
+
phone: c.phone,
|
|
77
|
+
email: c.email,
|
|
78
|
+
address: c.address,
|
|
79
|
+
};
|
|
80
|
+
setSelected(sel);
|
|
81
|
+
onChange(sel);
|
|
82
|
+
setIsOpen(false);
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
const handleClear = () => {
|
|
86
|
+
setSelected(null);
|
|
87
|
+
onChange(null);
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
return (
|
|
91
|
+
<div>
|
|
92
|
+
<label className="text-xs font-bold text-gray-500 uppercase mb-1 block">{label}{required && <span className="text-red-500 ml-0.5">*</span>}</label>
|
|
93
|
+
|
|
94
|
+
{/* Display */}
|
|
95
|
+
{selected ? (
|
|
96
|
+
<div className="flex items-center gap-2 border border-gray-200 rounded-lg px-3 py-2 bg-white">
|
|
97
|
+
<div className="flex-1 min-w-0">
|
|
98
|
+
<p className="text-sm font-bold text-gray-800 truncate">{selected.customer_name}</p>
|
|
99
|
+
<p className="text-xs text-gray-500">{[selected.customer_code, selected.phone].filter(Boolean).join(" • ")}</p>
|
|
100
|
+
</div>
|
|
101
|
+
{!disabled && <button onClick={handleClear} className="p-1 rounded hover:bg-gray-100"><X size={14} className="text-gray-400" /></button>}
|
|
102
|
+
</div>
|
|
103
|
+
) : (
|
|
104
|
+
<button
|
|
105
|
+
onClick={() => !disabled && setIsOpen(true)}
|
|
106
|
+
className={`w-full text-left border border-gray-200 rounded-lg px-3 py-2 text-sm text-gray-400 hover:border-blue-300 transition-colors ${disabled ? "bg-gray-50 cursor-not-allowed" : "bg-white cursor-pointer"}`}
|
|
107
|
+
>
|
|
108
|
+
<span className="flex items-center gap-2"><Search size={14} />{placeholder}</span>
|
|
109
|
+
</button>
|
|
110
|
+
)}
|
|
111
|
+
|
|
112
|
+
{/* Selector Modal */}
|
|
113
|
+
<Modal isOpen={isOpen} onClose={() => setIsOpen(false)} title={`Select ${label}`} size="lg">
|
|
114
|
+
<div className="p-1">
|
|
115
|
+
<div className="relative mb-4">
|
|
116
|
+
<Search size={16} className="absolute left-3 top-1/2 -translate-y-1/2 text-gray-400" />
|
|
117
|
+
<input
|
|
118
|
+
ref={searchRef}
|
|
119
|
+
type="text"
|
|
120
|
+
value={search}
|
|
121
|
+
onChange={e => setSearch(e.target.value)}
|
|
122
|
+
placeholder="Search by name, code, phone, or email..."
|
|
123
|
+
className="w-full pl-9 pr-4 py-2.5 border border-gray-200 rounded-xl text-sm focus:outline-none focus:ring-2 focus:ring-blue-200 focus:border-blue-300"
|
|
124
|
+
/>
|
|
125
|
+
</div>
|
|
126
|
+
|
|
127
|
+
{loading ? (
|
|
128
|
+
<div className="flex items-center justify-center py-16"><div className="w-6 h-6 border-2 border-blue-400 border-t-transparent rounded-full animate-spin" /></div>
|
|
129
|
+
) : customers.length === 0 ? (
|
|
130
|
+
<div className="py-12 text-center"><EmptyState message="No customers found." /></div>
|
|
131
|
+
) : (
|
|
132
|
+
<div className="space-y-1.5 max-h-[50vh] overflow-y-auto">
|
|
133
|
+
{customers.map((c: any) => (
|
|
134
|
+
<button
|
|
135
|
+
key={c.id}
|
|
136
|
+
onClick={() => handleSelect(c)}
|
|
137
|
+
className="w-full text-left p-3 rounded-xl hover:bg-blue-50 border border-transparent hover:border-blue-200 transition-all flex items-start gap-3"
|
|
138
|
+
>
|
|
139
|
+
<div className="p-2 rounded-xl bg-gray-100 text-gray-500"><User size={16} /></div>
|
|
140
|
+
<div className="flex-1 min-w-0">
|
|
141
|
+
<p className="text-sm font-bold text-gray-800">{c.customer_name || c.name}</p>
|
|
142
|
+
<div className="flex items-center gap-3 mt-1 text-xs text-gray-500">
|
|
143
|
+
{c.customer_code && <span className="bg-gray-100 px-1.5 py-0.5 rounded font-mono">{c.customer_code}</span>}
|
|
144
|
+
{c.phone && <span className="flex items-center gap-1"><Phone size={10} />{c.phone}</span>}
|
|
145
|
+
{c.email && <span className="flex items-center gap-1"><Mail size={10} />{c.email}</span>}
|
|
146
|
+
</div>
|
|
147
|
+
</div>
|
|
148
|
+
</button>
|
|
149
|
+
))}
|
|
150
|
+
</div>
|
|
151
|
+
)}
|
|
152
|
+
|
|
153
|
+
{!loading && customers.length > 0 && total > 15 && (
|
|
154
|
+
<div className="mt-3 pt-3 border-t border-gray-100">
|
|
155
|
+
<Pagination currentPage={page} totalItems={total} itemsPerPage={15} onPageChange={setPage} />
|
|
156
|
+
</div>
|
|
157
|
+
)}
|
|
158
|
+
</div>
|
|
159
|
+
<ModalFooter><Button variant="soft" color="default" onClick={() => setIsOpen(false)}>Cancel</Button></ModalFooter>
|
|
160
|
+
</Modal>
|
|
161
|
+
</div>
|
|
162
|
+
);
|
|
163
|
+
}
|