@vendure-platform/create 0.3.0 → 0.5.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.
Files changed (59) hide show
  1. package/CHANGELOG.md +8 -0
  2. package/README.md +7 -7
  3. package/dist/add-to-existing.js +1 -583
  4. package/dist/assets/storefront/messages/de.json +2 -0
  5. package/dist/assets/storefront/messages/en.json +2 -0
  6. package/dist/assets/storefront/package.json +1 -1
  7. package/dist/assets/storefront/src/app/[locale]/(catalog)/product/[slug]/page.tsx +7 -2
  8. package/dist/assets/storefront/src/app/[locale]/account/shared-carts/[id]/activity-feed.tsx +152 -0
  9. package/dist/assets/storefront/src/app/[locale]/account/shared-carts/[id]/add-item-search.tsx +130 -0
  10. package/dist/assets/storefront/src/app/[locale]/account/shared-carts/[id]/add-member-form.tsx +124 -0
  11. package/dist/assets/storefront/src/app/[locale]/account/shared-carts/[id]/members-panel.tsx +240 -0
  12. package/dist/assets/storefront/src/app/[locale]/account/shared-carts/[id]/page.tsx +47 -0
  13. package/dist/assets/storefront/src/app/[locale]/account/shared-carts/[id]/rename-dialog.tsx +96 -0
  14. package/dist/assets/storefront/src/app/[locale]/account/shared-carts/[id]/shared-cart-detail-client.tsx +555 -0
  15. package/dist/assets/storefront/src/app/[locale]/account/shared-carts/actions.ts +260 -0
  16. package/dist/assets/storefront/src/app/[locale]/account/shared-carts/new/new-shared-cart-form.tsx +77 -0
  17. package/dist/assets/storefront/src/app/[locale]/account/shared-carts/new/page.tsx +47 -0
  18. package/dist/assets/storefront/src/app/[locale]/account/shared-carts/page.tsx +263 -0
  19. package/dist/assets/storefront/src/app/[locale]/cart/cart.tsx +12 -1
  20. package/dist/assets/storefront/src/app/[locale]/cart/order-summary.tsx +23 -0
  21. package/dist/assets/storefront/src/components/commerce/account-nav-items.ts +2 -0
  22. package/dist/assets/storefront/src/components/commerce/add-to-shared-cart.tsx +225 -0
  23. package/dist/assets/storefront/src/components/commerce/product-info.tsx +14 -1
  24. package/dist/assets/storefront/src/components/commerce/shared-cart-role.ts +39 -0
  25. package/dist/assets/storefront/src/components/commerce/shared-cart-status-badge.tsx +24 -0
  26. package/dist/assets/storefront/src/graphql-env.d.ts +28 -15
  27. package/dist/assets/storefront/src/lib/vendure/api.ts +8 -4
  28. package/dist/assets/storefront/src/lib/vendure/mutations.ts +96 -0
  29. package/dist/assets/storefront/src/lib/vendure/queries.ts +142 -0
  30. package/dist/assets/storefront/src/schema.graphql +321 -61
  31. package/dist/bin/run.js +1 -119
  32. package/dist/cli.js +1 -309
  33. package/dist/git-init.js +1 -60
  34. package/dist/index.js +1 -19
  35. package/dist/plugin-registry.js +1 -203
  36. package/dist/registry.js +1 -29
  37. package/dist/scaffold-shared.js +1 -124
  38. package/dist/scaffold-storefront-only.js +1 -101
  39. package/dist/scaffolder.js +1 -203
  40. package/dist/sort-package-json.js +1 -26
  41. package/dist/storefront-copier.js +1 -149
  42. package/dist/template-renderer.js +1 -221
  43. package/dist/types.js +1 -3
  44. package/package.json +5 -4
  45. package/dist/add-to-existing.js.map +0 -1
  46. package/dist/assets/storefront/src/lib/quantity.test.js +0 -20
  47. package/dist/bin/run.js.map +0 -1
  48. package/dist/cli.js.map +0 -1
  49. package/dist/git-init.js.map +0 -1
  50. package/dist/index.js.map +0 -1
  51. package/dist/plugin-registry.js.map +0 -1
  52. package/dist/registry.js.map +0 -1
  53. package/dist/scaffold-shared.js.map +0 -1
  54. package/dist/scaffold-storefront-only.js.map +0 -1
  55. package/dist/scaffolder.js.map +0 -1
  56. package/dist/sort-package-json.js.map +0 -1
  57. package/dist/storefront-copier.js.map +0 -1
  58. package/dist/template-renderer.js.map +0 -1
  59. package/dist/types.js.map +0 -1
@@ -3,6 +3,8 @@ import {Button} from '@/components/ui/button';
3
3
  import {Price} from '@/components/commerce/price';
4
4
  import {maskCode} from '@/lib/format';
5
5
  import {RequestQuoteButton} from './request-quote-button';
6
+ import {AddToSharedCart} from '@/components/commerce/add-to-shared-cart';
7
+ import {canCreateSharedCarts} from '@/components/commerce/shared-cart-role';
6
8
  import {getAuthToken} from '@/lib/auth';
7
9
  import {getActiveCustomer, getCompanyAccount} from '@/lib/vendure/actions';
8
10
  import {getTranslations} from 'next-intl/server';
@@ -71,6 +73,13 @@ export async function OrderSummary({activeOrder}: { activeOrder: ActiveOrder })
71
73
  }
72
74
  }
73
75
 
76
+ const canShareCart = canCreateSharedCarts(companyAccount?.permissions);
77
+ let shareDefaultName = 'Shared cart';
78
+ if (canShareCart) {
79
+ const customer = await getActiveCustomer();
80
+ if (customer?.firstName) shareDefaultName = `${customer.firstName}'s shared cart`;
81
+ }
82
+
74
83
  return (
75
84
  <div className="border rounded-lg p-6 bg-card">
76
85
  <h2 className="text-xl font-bold mb-4">{t('orderSummary')}</h2>
@@ -170,6 +179,20 @@ export async function OrderSummary({activeOrder}: { activeOrder: ActiveOrder })
170
179
  />
171
180
  )}
172
181
 
182
+ {canShareCart && activeOrder.lines.length > 0 && (
183
+ <AddToSharedCart
184
+ lines={activeOrder.lines.map(line => ({
185
+ productVariantId: line.productVariant.id,
186
+ quantity: line.quantity,
187
+ }))}
188
+ canCreate
189
+ defaultName={shareDefaultName}
190
+ label="Share this cart"
191
+ size="default"
192
+ className="w-full mt-2 gap-2"
193
+ />
194
+ )}
195
+
173
196
  <Button variant="outline" className="w-full mt-2" asChild>
174
197
  <Link href="/">{t('continueShopping')}</Link>
175
198
  </Button>
@@ -14,6 +14,7 @@ import {
14
14
  Receipt,
15
15
  Heart,
16
16
  Zap,
17
+ ShoppingCart,
17
18
  } from 'lucide-react';
18
19
 
19
20
  export interface NavItem {
@@ -31,6 +32,7 @@ export const personalNavItems: NavItem[] = [
31
32
  { href: '/account/orders', label: 'orders', icon: Package },
32
33
  { href: '/account/store-credits', label: 'storeCredits', icon: CreditCard },
33
34
  { href: '/account/quotes', label: 'quotes', icon: FileText },
35
+ { href: '/account/shared-carts', label: 'sharedCarts', icon: ShoppingCart },
34
36
  { href: '/account/shopping-lists', label: 'shoppingLists', icon: Heart },
35
37
  { href: '/account/quick-order', label: 'quickOrder', icon: Zap },
36
38
  { href: '/account/approvals', label: 'approvals', icon: ShieldCheck },
@@ -0,0 +1,225 @@
1
+ 'use client';
2
+
3
+ import {useState, useTransition} from 'react';
4
+ import {useRouter} from '@/i18n/navigation';
5
+ import {toast} from 'sonner';
6
+ import {Button} from '@/components/ui/button';
7
+ import {Input} from '@/components/ui/input';
8
+ import {Textarea} from '@/components/ui/textarea';
9
+ import {Label} from '@/components/ui/label';
10
+ import {
11
+ Dialog,
12
+ DialogContent,
13
+ DialogDescription,
14
+ DialogFooter,
15
+ DialogHeader,
16
+ DialogTitle,
17
+ } from '@/components/ui/dialog';
18
+ import {
19
+ DropdownMenu,
20
+ DropdownMenuContent,
21
+ DropdownMenuItem,
22
+ DropdownMenuLabel,
23
+ DropdownMenuSeparator,
24
+ DropdownMenuTrigger,
25
+ } from '@/components/ui/dropdown-menu';
26
+ import {Users, Plus, Loader2, ChevronDown} from 'lucide-react';
27
+ import {
28
+ fetchEditableOpenSharedCarts,
29
+ addItemsToSharedCart,
30
+ shareMyCart,
31
+ type EditableSharedCart,
32
+ } from '@/app/[locale]/account/shared-carts/actions';
33
+
34
+ type CartLine = {productVariantId: string; quantity: number};
35
+
36
+ export function AddToSharedCart({
37
+ lines,
38
+ disabled,
39
+ canCreate,
40
+ defaultName,
41
+ label = 'Add to shared cart',
42
+ size = 'lg',
43
+ className,
44
+ }: {
45
+ /** Items to add to (or seed a new) shared cart — one line on a PDP, the whole order on the cart page. */
46
+ lines: CartLine[];
47
+ disabled?: boolean;
48
+ canCreate?: boolean;
49
+ /** Pre-filled name for the create dialog. */
50
+ defaultName?: string;
51
+ label?: string;
52
+ size?: 'default' | 'lg';
53
+ className?: string;
54
+ }) {
55
+ const router = useRouter();
56
+ const [carts, setCarts] = useState<EditableSharedCart[] | null>(null);
57
+ const [isLoading, setIsLoading] = useState(false);
58
+ const [isPending, startTransition] = useTransition();
59
+ const [createOpen, setCreateOpen] = useState(false);
60
+
61
+ function loadCarts(open: boolean) {
62
+ if (!open || carts !== null) return;
63
+ setIsLoading(true);
64
+ fetchEditableOpenSharedCarts().then(result => {
65
+ setCarts(result);
66
+ setIsLoading(false);
67
+ });
68
+ }
69
+
70
+ function add(cart: EditableSharedCart) {
71
+ if (lines.length === 0) return;
72
+ startTransition(async () => {
73
+ const result = await addItemsToSharedCart(cart.id, lines);
74
+ if (!result.success) {
75
+ toast.error(result.message ?? 'Could not add to the shared cart.');
76
+ return;
77
+ }
78
+ toast.success(`Added to “${cart.name}”`, {
79
+ description: 'Personal discounts don’t apply to shared carts — priced at your company rate.',
80
+ action: {label: 'View', onClick: () => router.push(`/account/shared-carts/${cart.id}`)},
81
+ });
82
+ });
83
+ }
84
+
85
+ const hasCarts = carts !== null && carts.length > 0;
86
+
87
+ return (
88
+ <>
89
+ <DropdownMenu onOpenChange={loadCarts}>
90
+ <DropdownMenuTrigger asChild>
91
+ <Button variant="outline" size={size} disabled={disabled || isPending} className={className ?? 'gap-2'}>
92
+ {isPending ? <Loader2 className="h-4 w-4 animate-spin" /> : <Users className="h-4 w-4" />}
93
+ {label}
94
+ <ChevronDown className="h-4 w-4 opacity-60" />
95
+ </Button>
96
+ </DropdownMenuTrigger>
97
+ <DropdownMenuContent align="end" className="w-64">
98
+ <DropdownMenuLabel>Add to a shared cart</DropdownMenuLabel>
99
+ <DropdownMenuSeparator />
100
+ {isLoading || carts === null ? (
101
+ <div className="px-2 py-3 text-sm text-muted-foreground flex items-center gap-2">
102
+ <Loader2 className="h-4 w-4 animate-spin" />
103
+ Loading…
104
+ </div>
105
+ ) : (
106
+ <>
107
+ {hasCarts &&
108
+ carts.map(cart => (
109
+ <DropdownMenuItem key={cart.id} onSelect={() => add(cart)} disabled={isPending}>
110
+ <Users className="h-4 w-4 mr-2 opacity-70" />
111
+ <span className="truncate">{cart.name}</span>
112
+ </DropdownMenuItem>
113
+ ))}
114
+ {canCreate ? (
115
+ <>
116
+ {hasCarts && <DropdownMenuSeparator />}
117
+ <DropdownMenuItem onSelect={() => setCreateOpen(true)} disabled={isPending}>
118
+ <Plus className="h-4 w-4 mr-2" />
119
+ Create new shared cart
120
+ </DropdownMenuItem>
121
+ </>
122
+ ) : (
123
+ !hasCarts && (
124
+ <div className="px-2 py-2 text-sm text-muted-foreground">
125
+ No shared carts you can add to.
126
+ </div>
127
+ )
128
+ )}
129
+ </>
130
+ )}
131
+ </DropdownMenuContent>
132
+ </DropdownMenu>
133
+
134
+ <CreateSharedCartDialog
135
+ open={createOpen}
136
+ onOpenChange={setCreateOpen}
137
+ lines={lines}
138
+ defaultName={defaultName ?? 'Shared cart'}
139
+ />
140
+ </>
141
+ );
142
+ }
143
+
144
+ function CreateSharedCartDialog({
145
+ open,
146
+ onOpenChange,
147
+ lines,
148
+ defaultName,
149
+ }: {
150
+ open: boolean;
151
+ onOpenChange: (open: boolean) => void;
152
+ lines: CartLine[];
153
+ defaultName: string;
154
+ }) {
155
+ const router = useRouter();
156
+ const [name, setName] = useState(defaultName);
157
+ const [description, setDescription] = useState('');
158
+ const [isPending, startTransition] = useTransition();
159
+
160
+ function handleSubmit(e: React.FormEvent) {
161
+ e.preventDefault();
162
+ const trimmed = name.trim();
163
+ if (!trimmed || lines.length === 0) return;
164
+ startTransition(async () => {
165
+ const result = await shareMyCart(trimmed, description.trim() || undefined, lines);
166
+ // The cart may be created even if some items failed to copy; land on
167
+ // it regardless so the user isn't stranded, warning on partial copy.
168
+ if (!result.id) {
169
+ toast.error(result.message ?? 'Could not create the shared cart.');
170
+ return;
171
+ }
172
+ if (!result.success && result.message) toast.warning(result.message);
173
+ router.push(`/account/shared-carts/${result.id}`);
174
+ });
175
+ }
176
+
177
+ return (
178
+ <Dialog open={open} onOpenChange={onOpenChange}>
179
+ <DialogContent>
180
+ <form onSubmit={handleSubmit}>
181
+ <DialogHeader>
182
+ <DialogTitle>Create a shared cart</DialogTitle>
183
+ <DialogDescription>
184
+ Start a new shared cart with {lines.length} {lines.length === 1 ? 'item' : 'items'} so colleagues
185
+ can build the order with you.
186
+ </DialogDescription>
187
+ </DialogHeader>
188
+ <div className="space-y-4 py-4">
189
+ <div className="space-y-2">
190
+ <Label htmlFor="create-shared-cart-name">Name</Label>
191
+ <Input
192
+ id="create-shared-cart-name"
193
+ value={name}
194
+ onChange={e => setName(e.target.value)}
195
+ maxLength={255}
196
+ required
197
+ autoFocus
198
+ />
199
+ </div>
200
+ <div className="space-y-2">
201
+ <Label htmlFor="create-shared-cart-description">Description (optional)</Label>
202
+ <Textarea
203
+ id="create-shared-cart-description"
204
+ value={description}
205
+ onChange={e => setDescription(e.target.value)}
206
+ rows={3}
207
+ maxLength={1000}
208
+ placeholder="What is this cart for?"
209
+ />
210
+ </div>
211
+ </div>
212
+ <DialogFooter>
213
+ <Button type="button" variant="ghost" onClick={() => onOpenChange(false)} disabled={isPending}>
214
+ Cancel
215
+ </Button>
216
+ <Button type="submit" disabled={!name.trim() || isPending}>
217
+ {isPending && <Loader2 className="h-4 w-4 mr-2 animate-spin" />}
218
+ Create shared cart
219
+ </Button>
220
+ </DialogFooter>
221
+ </form>
222
+ </DialogContent>
223
+ </Dialog>
224
+ );
225
+ }
@@ -21,6 +21,7 @@ import {MIN_ORDER_QUANTITY, normalizeQuantity, sanitizeQuantityInput} from '@/li
21
21
  import {useCartCount} from '@/lib/cart-count-context';
22
22
  import {PackagingUnitSelector} from '@/components/commerce/packaging-unit-selector';
23
23
  import {ShoppingListToggle} from '@/components/commerce/shopping-list-toggle';
24
+ import {AddToSharedCart} from '@/components/commerce/add-to-shared-cart';
24
25
  import type {ProductDetail, PackagingUnitOption} from '@/lib/vendure/queries';
25
26
  import type {ShoppingListInfo} from '@/app/[locale]/account/shopping-lists/actions';
26
27
 
@@ -29,9 +30,11 @@ export interface ProductInfoProps {
29
30
  searchParams: { [key: string]: string | string[] | undefined };
30
31
  shoppingLists?: ShoppingListInfo[] | null;
31
32
  currencyCode?: string;
33
+ isCompanyUser?: boolean;
34
+ canCreateSharedCart?: boolean;
32
35
  }
33
36
 
34
- export function ProductInfo({ product, searchParams, shoppingLists, currencyCode }: ProductInfoProps) {
37
+ export function ProductInfo({ product, searchParams, shoppingLists, currencyCode, isCompanyUser, canCreateSharedCart }: ProductInfoProps) {
35
38
  const tProduct = useTranslations('Product');
36
39
  const tCart = useTranslations('Cart');
37
40
  const pathname = usePathname();
@@ -503,6 +506,16 @@ export function ProductInfo({ product, searchParams, shoppingLists, currencyCode
503
506
  />
504
507
  )}
505
508
  </div>
509
+
510
+ {selectedVariant && isCompanyUser && (
511
+ <div className="mt-2">
512
+ <AddToSharedCart
513
+ lines={[{productVariantId: selectedVariant.id, quantity}]}
514
+ disabled={!canAddToCart}
515
+ canCreate={canCreateSharedCart}
516
+ />
517
+ </div>
518
+ )}
506
519
  </div>
507
520
 
508
521
  {/* Volume Pricing — reference material, post-action */}
@@ -0,0 +1,39 @@
1
+ // Shared-cart role helpers. Roles are ordered view < edit < checkout; the
2
+ // server is the source of truth for permissions, these drive UI gating only.
3
+ import type { SharedCartRole } from '@/app/[locale]/account/shared-carts/actions';
4
+
5
+ const ROLE_RANK: Record<SharedCartRole, number> = { view: 0, edit: 1, checkout: 2 };
6
+
7
+ // Company permission to create and own a shared cart. Most buyers hold this; it also lets you
8
+ // govern carts you own, but nothing else.
9
+ export const USE_SHARED_CARTS_PERMISSION = 'UseSharedCarts';
10
+
11
+ // Company permission to govern *any* cart in the company — manage members, transfer, rename,
12
+ // delete — regardless of ownership. Admin-level; holding it also lets you create.
13
+ export const MANAGE_SHARED_CARTS_PERMISSION = 'ManageSharedCarts';
14
+
15
+ export function canCreateSharedCarts(permissions: readonly string[] | null | undefined): boolean {
16
+ return (
17
+ permissions?.some((p) => p === USE_SHARED_CARTS_PERMISSION || p === MANAGE_SHARED_CARTS_PERMISSION) ??
18
+ false
19
+ );
20
+ }
21
+
22
+ export function canManageSharedCarts(permissions: readonly string[] | null | undefined): boolean {
23
+ return permissions?.includes(MANAGE_SHARED_CARTS_PERMISSION) ?? false;
24
+ }
25
+
26
+ export const ROLE_OPTIONS: ReadonlyArray<{ value: SharedCartRole; label: string; hint: string }> = [
27
+ { value: 'view', label: 'Viewer', hint: 'Can view the cart and activity' },
28
+ { value: 'edit', label: 'Editor', hint: 'Can add, change, and remove items' },
29
+ { value: 'checkout', label: 'Checkout', hint: 'Can edit and place the order' },
30
+ ];
31
+
32
+ export function roleLabel(role: string): string {
33
+ return ROLE_OPTIONS.find((o) => o.value === role)?.label ?? role;
34
+ }
35
+
36
+ export function roleAtLeast(role: SharedCartRole | undefined, min: SharedCartRole): boolean {
37
+ if (!role) return false;
38
+ return ROLE_RANK[role] >= ROLE_RANK[min];
39
+ }
@@ -0,0 +1,24 @@
1
+ import {Badge} from '@/components/ui/badge';
2
+ import {CircleDot, Lock, CheckCircle2, type LucideIcon} from 'lucide-react';
3
+
4
+ const STATUS_CONFIG: Record<string, {color: string; label: string; icon: LucideIcon}> = {
5
+ Open: {color: 'bg-emerald-100 text-emerald-800', label: 'Open', icon: CircleDot},
6
+ Locked: {color: 'bg-amber-100 text-amber-800', label: 'Checkout in progress', icon: Lock},
7
+ Closed: {color: 'bg-gray-100 text-gray-600', label: 'Closed', icon: CheckCircle2},
8
+ };
9
+
10
+ interface SharedCartStatusBadgeProps {
11
+ status: string;
12
+ }
13
+
14
+ export function SharedCartStatusBadge({status}: SharedCartStatusBadgeProps) {
15
+ const config = STATUS_CONFIG[status] ?? {color: 'bg-gray-100 text-gray-800', label: status, icon: CircleDot};
16
+ const Icon = config.icon;
17
+
18
+ return (
19
+ <Badge className={config.color} variant="secondary">
20
+ <Icon className="h-3 w-3 mr-1" />
21
+ {config.label}
22
+ </Badge>
23
+ );
24
+ }