Skip to content
Extraits de code Groupes Projets
Valider de2ac08c rédigé par hamza.bouslama's avatar hamza.bouslama
Parcourir les fichiers

Adding functionality to change all variable product price

parent d93ee7aa
Branches
1 requête de fusion!459Resolve MS-25
Pipeline #12051 réussi avec l'étape
in 7 minutes et 39 secondes
......@@ -90,6 +90,7 @@ export default function ProductVariationsCard({
const [snackbarSeverity, setSnackbarSeverity] = useState<
"success" | "error"
>("success");
const [openBulkPriceDialog, setOpenBulkPriceDialog] = useState(false);
useEffect(() => {
......@@ -99,8 +100,6 @@ export default function ProductVariationsCard({
setSelectedQuantity(0);
}, [product]);
const handleSavePrice = async () => {
try {
if (productData.productType === ProductType.SIMPLE) {
......@@ -144,6 +143,61 @@ export default function ProductVariationsCard({
setSnackbarOpen(true);
}
};
const handleSaveBulkPrice = async () => {
// Type guard to ensure we only proceed with variable products
if (product.productType !== ProductType.VARIABLE) {
console.error("Bulk price update is only for variable products.");
return;
}
try {
const newPriceValue = parseFloat(newPrice);
if (isNaN(newPriceValue)) {
throw new Error("Le prix doit être un nombre valide.");
}
// dataFiltered is now guaranteed to be IVariationItem[]
const updatedVariations = dataFiltered.map((variation) => ({
...variation,
regularPrice: newPriceValue,
}));
// Update variations via API (if applicable)
await Promise.all(
updatedVariations.map((variation) =>
updateVariationPrice(variation.id.toString(), newPriceValue)
)
);
// Update local state
setProductData((prevProduct) => {
const newVariations = prevProduct.variations.map((variation) => {
const updatedVariation = updatedVariations.find(
(uv) => uv.id === variation.id
);
return updatedVariation
? { ...variation, regularPrice: newPriceValue }
: variation;
});
const updatedProduct = { ...prevProduct, variations: newVariations };
onProductUpdate(updatedProduct); // Notify parent of the update
return updatedProduct;
});
// Reset UI state
setOpenBulkPriceDialog(false);
setNewPrice("");
setSnackbarMessage("Tous les prix ont été mis à jour avec succès.");
setSnackbarSeverity("success");
setSnackbarOpen(true);
} catch (error) {
console.error("Failed to update bulk prices:", error);
setSnackbarMessage("Échec de la mise à jour des prix.");
setSnackbarSeverity("error");
setSnackbarOpen(true);
}
};
const attributeNames = useMemo(() => {
......@@ -510,6 +564,7 @@ export default function ProductVariationsCard({
.filter((value) => value.trim() !== "")
: []
}
onBulkPriceClick={() => setOpenBulkPriceDialog(true)}
/>
<VariationTableFiltersResult
......@@ -755,21 +810,63 @@ export default function ProductVariationsCard({
/>
</DialogContent>
<DialogActions>
<Button
onClick={handleClosePriceDialog}
color="secondary"
variant="outlined"
>
Annuler
</Button>
<Button
onClick={handleSavePrice}
color="primary"
variant="contained"
>
Enregistrer
</Button>
</DialogActions>
<Button
onClick={handleClosePriceDialog}
color="secondary"
variant="outlined"
>
Annuler
</Button>
<Button
onClick={handleSavePrice}
color="primary"
variant="contained"
>
Enregistrer
</Button>
</DialogActions>
</Dialog>
<Dialog
open={openBulkPriceDialog}
onClose={() => setOpenBulkPriceDialog(false)}
fullWidth
maxWidth="xs"
>
<DialogTitle>Modifier tous les prix</DialogTitle>
<DialogContent>
<TextField
autoFocus
margin="normal"
id="newBulkPrice"
label="Nouveau Prix pour toutes les variations (€)"
type="number"
fullWidth
variant="outlined"
value={newPrice}
onChange={handlePriceChange}
InputProps={{
startAdornment: (
<InputAdornment position="start"></InputAdornment>
),
}}
/>
</DialogContent>
<DialogActions>
<Button
onClick={() => setOpenBulkPriceDialog(false)}
color="secondary"
variant="outlined"
>
Annuler
</Button>
<Button
onClick={handleSaveBulkPrice} // Define this function next
color="primary"
variant="contained"
>
Enregistrer
</Button>
</DialogActions>
</Dialog>
</Card>
<Snackbar
......
......@@ -11,6 +11,7 @@ import InputAdornment from "@mui/material/InputAdornment";
import Iconify from "@/shared/components/iconify";
import { Filters } from "./Product-Variation";
import { AttributeType, IVariationAttribute } from "@/shared/types/product";
import { Button } from "@mui/material";
type VariationTableToolbarProps = {
filters: Filters;
......@@ -24,6 +25,7 @@ type VariationTableToolbarProps = {
label: string; // Nom pour l'attribut de label
choice: string; // Nom pour l'attribut de choix
};
onBulkPriceClick: () => void; // New prop to handle bulk price button click
};
export default function VariationTableToolbar({
......@@ -34,6 +36,7 @@ export default function VariationTableToolbar({
termLabelOptions,
termChoiceOptions,
attributeNames, // Noms dynamiques des attributs
onBulkPriceClick, // New prop to handle bulk price button click
}: VariationTableToolbarProps) {
const handleFilterNameOrUGS = useCallback(
(event: React.ChangeEvent<HTMLInputElement>) => {
......@@ -92,22 +95,28 @@ export default function VariationTableToolbar({
);
return (
<Stack spacing={2} direction={{ xs: "column", md: "row" }} sx={{ p: 2.5 }}>
{/* Recherche par nom ou UGS */}
<TextField
fullWidth
value={filters.nameOrUGS}
onChange={handleFilterNameOrUGS}
placeholder="Rechercher par nom ou UGS..."
InputProps={{
startAdornment: (
<InputAdornment position="start">
<Iconify icon="eva:search-fill" sx={{ color: "text.disabled" }} />
</InputAdornment>
),
}}
/>
<Stack spacing={2} direction={{ xs: "column", md: "row" }} sx={{ p: 2.5 , pl: 5 }}>
{/* Recherche par nom ou UGS */}
<TextField
sx={{ width: "90%" }}
value={filters.nameOrUGS}
onChange={handleFilterNameOrUGS}
placeholder="Rechercher par nom ou UGS..."
InputProps={{
startAdornment: (
<InputAdornment position="start">
<Iconify icon="eva:search-fill" sx={{ color: "text.disabled" }} />
</InputAdornment>
),
}}
/>
<Button
variant="outlined"
onClick={onBulkPriceClick}
sx={{ width: "10%" }}
>
Modifier toutes les prix
</Button>
{/* Sélection de couleur - afficher uniquement si des options valides existent */}
{validTermColorOptions.length > 0 && (
<FormControl sx={{ width: { xs: 1, md: 200 } }}>
......
0% ou .
You are about to add 0 people to the discussion. Proceed with caution.
Terminez d'abord l'édition de ce message.
Veuillez vous inscrire ou vous pour commenter