@@ -2,8 +2,7 @@ import type { Actions } from '@sveltejs/kit';
22import { fail , redirect } from '@sveltejs/kit' ;
33import { superValidate } from 'sveltekit-superforms' ;
44import { zod } from 'sveltekit-superforms/adapters' ;
5- import { productSchema , productVariantSchema } from '$lib/schemas/product.schema' ;
6- import type { CreateProductVariantInput } from '$lib/types/product.js' ;
5+ import { productSchema } from '$lib/schemas/product.schema' ;
76
87export const load = async ( { params, locals } ) => {
98 const productId = params . id ;
@@ -110,65 +109,9 @@ export const actions: Actions = {
110109 }
111110
112111 console . log ( 'Product updated successfully:' , result . data ) ;
112+
113+ // Invalidate all cached data to ensure fresh product data is loaded
114+ // This will refresh the product list, product details, and any other cached data
113115 redirect ( 303 , '/admin/products' ) ;
114- } ,
115- removeVariant : async ( { request, params, locals } ) => {
116- const productId = params . id ;
117- if ( ! productId ) {
118- return fail ( 400 , { error : 'Product ID is required' } ) ;
119- }
120- const { commercify } = locals ;
121- const formData = await request . formData ( ) ;
122- const variantId = formData . get ( 'variantId' ) as string ;
123- if ( ! variantId ) {
124- return fail ( 400 , { error : 'Variant ID is required' } ) ;
125- }
126- try {
127- const result = await commercify . deleteProductVariant ( productId , variantId ) ;
128- if ( ! result . success ) {
129- console . error ( 'Error removing product variant:' , result . error ) ;
130- return fail ( 400 , {
131- error : result . error || 'Failed to remove product variant'
132- } ) ;
133- }
134- console . log ( 'Product variant removed successfully:' , result . data ) ;
135- redirect ( 303 , `/admin/products/${ productId } /edit` ) ;
136- } catch ( error ) {
137- console . error ( 'Error removing product variant:' , error ) ;
138- return fail ( 500 , { error : 'Failed to remove product variant' } ) ;
139- }
140- } ,
141- addVariant : async ( { request, params, locals } ) => {
142- const productId = params . id ;
143- if ( ! productId ) {
144- return fail ( 400 , { error : 'Product ID is required' } ) ;
145- }
146- const { commercify } = locals ;
147- const form = await superValidate ( request , zod ( productVariantSchema ) ) ;
148- if ( ! form . valid ) {
149- return fail ( 400 , { form } ) ;
150- }
151- console . log ( 'Adding product variant with data:' , form . data ) ;
152-
153- const input : CreateProductVariantInput = {
154- sku : form . data . sku ,
155- price : form . data . price ,
156- stock : form . data . stock || 0 ,
157- weight : form . data . weight || 0 ,
158- attributes : form . data . attributes || { } ,
159- images : form . data . images || [ ] ,
160- isDefault : form . data . isDefault || false
161- } ;
162-
163- const result = await commercify . addProductVariant ( productId , input ) ;
164- if ( ! result . success ) {
165- console . error ( 'Error adding product variant:' , result . error ) ;
166- return fail ( 400 , {
167- form,
168- error : result . error || 'Failed to add product variant'
169- } ) ;
170- }
171- console . log ( 'Product variant added successfully:' , result . data ) ;
172- redirect ( 303 , `/admin/products/${ productId } /edit` ) ;
173116 }
174117} ;
0 commit comments