feat(products): update event handling for product list refresh on creation and reset actions
This commit is contained in:
@@ -72,7 +72,11 @@ export default function AdminProductList() {
|
|||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const handler = () => refetchProducts();
|
const handler = () => refetchProducts();
|
||||||
window.addEventListener('product-created', handler);
|
window.addEventListener('product-created', handler);
|
||||||
return () => window.removeEventListener('product-created', handler);
|
window.addEventListener('product-list-changed', handler);
|
||||||
|
return () => {
|
||||||
|
window.removeEventListener('product-created', handler);
|
||||||
|
window.removeEventListener('product-list-changed', handler);
|
||||||
|
};
|
||||||
}, [refetchProducts]);
|
}, [refetchProducts]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
'use client';
|
'use client';
|
||||||
|
|
||||||
import { useState, useTransition, useEffect } from 'react';
|
import { useState, useTransition, useEffect } from 'react';
|
||||||
import { useRouter } from 'next/navigation';
|
|
||||||
import type { MergePreview, Product } from '../../../features/inventory/types';
|
import type { MergePreview, Product } from '../../../features/inventory/types';
|
||||||
|
|
||||||
export default function MergePreviewForm() {
|
export default function MergePreviewForm() {
|
||||||
@@ -14,7 +13,6 @@ export default function MergePreviewForm() {
|
|||||||
const [isPending, startTransition] = useTransition();
|
const [isPending, startTransition] = useTransition();
|
||||||
const [isConfirming, setIsConfirming] = useState(false);
|
const [isConfirming, setIsConfirming] = useState(false);
|
||||||
const [isExpanded, setIsExpanded] = useState(false);
|
const [isExpanded, setIsExpanded] = useState(false);
|
||||||
const router = useRouter();
|
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (isExpanded && products.length === 0) {
|
if (isExpanded && products.length === 0) {
|
||||||
@@ -96,7 +94,7 @@ export default function MergePreviewForm() {
|
|||||||
setIsConfirming(false);
|
setIsConfirming(false);
|
||||||
setSourceProductId('');
|
setSourceProductId('');
|
||||||
setTargetProductId('');
|
setTargetProductId('');
|
||||||
router.refresh();
|
window.dispatchEvent(new CustomEvent('product-list-changed'));
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
setError(err instanceof Error ? err.message : 'Okänt fel');
|
setError(err instanceof Error ? err.message : 'Okänt fel');
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,12 +1,10 @@
|
|||||||
'use client';
|
'use client';
|
||||||
|
|
||||||
import { useState } from 'react';
|
import { useState } from 'react';
|
||||||
import { useRouter } from 'next/navigation';
|
|
||||||
|
|
||||||
export default function ResetProductsButton() {
|
export default function ResetProductsButton() {
|
||||||
const [isPending, setIsPending] = useState(false);
|
const [isPending, setIsPending] = useState(false);
|
||||||
const [error, setError] = useState<string | null>(null);
|
const [error, setError] = useState<string | null>(null);
|
||||||
const router = useRouter();
|
|
||||||
|
|
||||||
async function handleClick() {
|
async function handleClick() {
|
||||||
if (
|
if (
|
||||||
@@ -24,7 +22,7 @@ export default function ResetProductsButton() {
|
|||||||
const data = await res.json().catch(() => ({}));
|
const data = await res.json().catch(() => ({}));
|
||||||
throw new Error(data?.error || 'Återställning misslyckades');
|
throw new Error(data?.error || 'Återställning misslyckades');
|
||||||
}
|
}
|
||||||
router.refresh();
|
window.dispatchEvent(new CustomEvent('product-list-changed'));
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
setError(err instanceof Error ? err.message : 'Okänt fel');
|
setError(err instanceof Error ? err.message : 'Okänt fel');
|
||||||
} finally {
|
} finally {
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
'use client';
|
'use client';
|
||||||
|
|
||||||
import { useState, useTransition } from 'react';
|
import { useState, useTransition } from 'react';
|
||||||
import { useRouter } from 'next/navigation';
|
|
||||||
|
|
||||||
type PendingProduct = {
|
type PendingProduct = {
|
||||||
id: number;
|
id: number;
|
||||||
@@ -17,7 +16,6 @@ export default function PendingProductsClient({ products: initial }: { products:
|
|||||||
const [isPending, startTransition] = useTransition();
|
const [isPending, startTransition] = useTransition();
|
||||||
const [error, setError] = useState<string | null>(null);
|
const [error, setError] = useState<string | null>(null);
|
||||||
const [processing, setProcessing] = useState<number | null>(null);
|
const [processing, setProcessing] = useState<number | null>(null);
|
||||||
const router = useRouter();
|
|
||||||
|
|
||||||
function handleAction(id: number, status: 'active' | 'rejected') {
|
function handleAction(id: number, status: 'active' | 'rejected') {
|
||||||
setError(null);
|
setError(null);
|
||||||
@@ -34,7 +32,6 @@ export default function PendingProductsClient({ products: initial }: { products:
|
|||||||
throw new Error(data?.error || 'Fel vid uppdatering');
|
throw new Error(data?.error || 'Fel vid uppdatering');
|
||||||
}
|
}
|
||||||
setProducts((prev) => prev.filter((p) => p.id !== id));
|
setProducts((prev) => prev.filter((p) => p.id !== id));
|
||||||
router.refresh();
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
setError(err instanceof Error ? err.message : 'Fel vid uppdatering');
|
setError(err instanceof Error ? err.message : 'Fel vid uppdatering');
|
||||||
} finally {
|
} finally {
|
||||||
|
|||||||
Reference in New Issue
Block a user