Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export enum CamapIconId {
// youtube = 'youtube',

absent = 'vacation',
contract = 'contract',
contract = 'book',
constOrders = "refresh",
varOrders = "basket",
delivery = 'clock',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,11 @@ const ProductModal = ({ product, onClose }: ProductModalProps) => {
const { t } = useTranslation(['shop/default']);
const { t: tUnit } = useTranslation(['unit']);

// eslint-disable-next-line no-empty-pattern
const [] = React.useState();

if (!product) return null;

return (
<Dialog open onClose={onClose} maxWidth="md" scroll="body">
<Box p={3}>
<Box pt={8} pb={3} px={3}>
<Tooltip title={`${t('close')}`}>
<IconButton
onClick={onClose}
Expand Down Expand Up @@ -89,7 +86,7 @@ const ProductModal = ({ product, onClose }: ProductModalProps) => {
<Typography
component="h3"
sx={{
fontSize: '1.4rem',
fontSize: '1.2rem',
textTransform: 'uppercase',
mb: 1,
mr: 4,
Expand All @@ -100,7 +97,7 @@ const ProductModal = ({ product, onClose }: ProductModalProps) => {
<Typography
component="span"
sx={{
fontSize: '1.4rem',
fontSize: '1.2rem',
textTransform: 'initial',
}}
>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Alert, Box, Button, Modal } from '@mui/material';
import React, { useEffect, useState } from 'react';
import { useCamapTranslation } from '../../utils/hooks/use-camap-translation';
import CsaCatalogDefaultOrder from '../csaCatalog/containers/CsaCatalogDefaultOrder';
import { CsaCatalogContext } from '../csaCatalog/CsaCatalog.context';
import { useRestSubscriptionPost } from '../csaCatalog/requests';
import CsaCatalogOrdersMobile from 'modules/csaCatalog/containers/CsaCatalogOrdersMobile';

interface BatchOrderCreateSubProps {
selectedMember: number;
Expand Down Expand Up @@ -36,7 +36,7 @@ const BatchOrderCreateSub = ({
};

const handleDefaultOrder = async () => {
createSub(
return await createSub(
Object.keys(defaultOrder).map((productId) => ({
productId: parseInt(productId, 10),
quantity: defaultOrder[parseInt(productId, 10)],
Expand All @@ -45,17 +45,22 @@ const BatchOrderCreateSub = ({
};

const createSub = async (dOrder: any) => {
await createSubscription({
const succeeded = await createSubscription({
userId: selectedMember!,
catalogId,
defaultOrder: dOrder,
absentDistribIds: [],
});

setShowDefaultOrderModal(false);

// // we have to recall /api/subscription to refresh orders displaying
refetchSubscriptions();
if(succeeded) {
setShowDefaultOrderModal(false);

// // we have to recall /api/subscription to refresh orders displaying
refetchSubscriptions();
}

return succeeded;
};

return (
Expand Down Expand Up @@ -91,7 +96,7 @@ const BatchOrderCreateSub = ({
transform: 'translate(-50%, -50%)',
}}
>
<CsaCatalogDefaultOrder onNext={() => handleDefaultOrder()} />
<CsaCatalogOrdersMobile onNext={() => handleDefaultOrder()} mode="defaultOrder" />
</Box>
</Modal>
</>
Expand Down
41 changes: 30 additions & 11 deletions packages/front-core/src/modules/csaCatalog/CsaCatalog.context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ interface CsaCatalogContextProps {
adminMode?: boolean | undefined;
initialOrders: Orders;
remainingDistributions: number;
minSubscriptionOrder: number;
cancelOrder: (distributionId: number) => void;
}

Expand Down Expand Up @@ -76,6 +77,7 @@ export const CsaCatalogContext = React.createContext<CsaCatalogContextProps>({
initialOrders: {},
remainingDistributions: 0,
cancelOrder: () => { },
minSubscriptionOrder: 0,
});

const CsaCatalogContextProvider = ({
Expand Down Expand Up @@ -208,23 +210,33 @@ const CsaCatalogContextProvider = ({
}, [catalog]);

React.useEffect(() => {
if (!subscription) return;
if (isConstOrders) {
if (!subscription) {
if(!catalog) return;
// if no subscription, initialize the default order with 0 for all products
setDefaultOrder(
subscription.distributions[0].orders.reduce((acc, d) => {
acc[d.productId] = d.qty;
catalog.products.reduce((acc, p) => {
acc[p.id] = 0;
return acc;
}, {} as Record<number, number>),
);
} else {
setDefaultOrder(
subscription.defaultOrder.reduce((acc, d) => {
acc[d.productId] = d.quantity;
return acc;
}, {} as Record<number, number>),
);
if (isConstOrders) {
setDefaultOrder(
subscription.distributions[0].orders.reduce((acc, d) => {
acc[d.productId] = d.qty;
return acc;
}, {} as Record<number, number>),
);
} else {
setDefaultOrder(
subscription.defaultOrder.reduce((acc, d) => {
acc[d.productId] = d.quantity;
return acc;
}, {} as Record<number, number>),
);
}
}
}, [isConstOrders, setDefaultOrder, subscription]);
}, [isConstOrders, setDefaultOrder, subscription, catalog]);

const cancelOrder = useCallback((distributionId: number) => {
const upd = { ...updatedOrders };
Expand Down Expand Up @@ -266,6 +278,12 @@ const CsaCatalogContextProvider = ({
}).length;
}, [subscription, distributions]);

const minSubscriptionOrder = React.useMemo(() => {
return !!subscription
? subscription?.minSubscriptionOrder
: (catalog?.catalogMinOrdersTotal ?? 0) / distributions.length * remainingDistributions;
}, [subscription, catalog, distributions, remainingDistributions]);

/** */
return (
<CsaCatalogContext.Provider
Expand Down Expand Up @@ -300,6 +318,7 @@ const CsaCatalogContextProvider = ({
initialOrders,
remainingDistributions,
cancelOrder,
minSubscriptionOrder,
}}
>
{children}
Expand Down
39 changes: 21 additions & 18 deletions packages/front-core/src/modules/csaCatalog/CsaCatalogRouter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import React from 'react';
import { useCamapTranslation } from '../../utils/hooks/use-camap-translation';
import { CsaCatalogContext } from './CsaCatalog.context';
import CsaCatalogAbsences from './containers/CsaCatalogAbsences';
import CsaCatalogDefaultOrder from './containers/CsaCatalogDefaultOrder';
import CsaCatalogOrdersMobile from './containers/CsaCatalogOrdersMobile';
import CsaCatalogPresentation from './containers/CsaCatalogPresentation';
import CsaCatalogSubscription from './containers/CsaCatalogSubscription';
Expand Down Expand Up @@ -41,7 +40,7 @@ const CsaCatalogRouter = ({ userId }: CsaCatalogRouterProps) => {
isConstOrders
} = React.useContext(CsaCatalogContext);

const [step, setStep] = React.useState<'presentation' | 'absences' | 'defaultOrder' | 'review'>(
const [step, setStep] = React.useState<'presentation' | 'absences' | 'requiredOrders' | 'review'>(
!initialSubscriptionId ? 'presentation' : 'review'
);
const [
Expand Down Expand Up @@ -84,29 +83,26 @@ const CsaCatalogRouter = ({ userId }: CsaCatalogRouterProps) => {
setSubscription(updatedDefaultOrderData);
}, [updatedDefaultOrderData, setSubscription]);

const onPresentationNext = () => {
if (step !== 'presentation') return;
setStep('defaultOrder');
if (!isConstOrders && catalog?.distribMinOrdersTotal === 0) {
onDefaultOrderNext();
const gotoDefaultOrder = () => {
if (!isConstOrders && (catalog?.distribMinOrdersTotal ?? 0) === 0 && (catalog?.catalogMinOrdersTotal ?? 0) === 0) {
gotoAbsences();
} else {
setStep('requiredOrders');
window.scrollTo({ top: 0, behavior: 'smooth' });
}
};

const onDefaultOrderNext = async () => {
console.log(step);
if (step !== 'defaultOrder') return;
setStep('absences');
const gotoAbsences = async () => {
console.log(catalog?.absentDistribsMaxNb);
if ((catalog?.absentDistribsMaxNb ?? 0) <= 0) {
onAbsencesNext();
} else {
setStep('absences');
window.scrollTo({ top: 0, behavior: 'smooth' });
}
};

const checkDefaultOrderAndContinue = async () => {
const checkInitialOrdersAndContinue = async () => {
const checkDefaultOrderData = await checkSubscriptionDefaultOrder(
Object.keys(defaultOrder).map((productId) => {
const productIdNumber = parseInt(productId, 10);
Expand All @@ -118,10 +114,9 @@ const CsaCatalogRouter = ({ userId }: CsaCatalogRouterProps) => {
};
}),
);

if (!checkDefaultOrderData) return false;

onDefaultOrderNext();
gotoAbsences();
return true;
};

Expand All @@ -133,10 +128,18 @@ const CsaCatalogRouter = ({ userId }: CsaCatalogRouterProps) => {
productId: parseInt(productId, 10),
quantity: defaultOrder[parseInt(productId, 10)],
})),
absentDistribIds: absenceDistributionsIds
absentDistribIds: absenceDistributionsIds,
initialOrders: Object.keys(updatedOrders).map((distributionId) => ({
id: parseInt(distributionId, 10),
orders: Object.keys(updatedOrders[parseInt(distributionId, 10)]).map((productId) => ({
productId: parseInt(productId, 10),
qty: updatedOrders[parseInt(distributionId, 10)][parseInt(productId, 10)],
})),
}))
});

if (!subscriptionSucceeded) return;

window.scrollTo({ top: 0, behavior: 'smooth' });

setStep('review');
Expand Down Expand Up @@ -217,13 +220,13 @@ const CsaCatalogRouter = ({ userId }: CsaCatalogRouterProps) => {

{/* This is the flow when user is not subscribed */}
{step === 'presentation' && (
<CsaCatalogPresentation onNext={onPresentationNext} />
<CsaCatalogPresentation onNext={gotoDefaultOrder} />
)}
{step === 'defaultOrder' && (
{step === 'requiredOrders' && (
<Box
width={'100%'}
>
<CsaCatalogDefaultOrder onNext={checkDefaultOrderAndContinue} />
<CsaCatalogOrdersMobile onNext={checkInitialOrdersAndContinue} mode={catalog?.catalogMinOrdersTotal > 0 ? 'initialOrders' : 'defaultOrder'}/>
</Box>
)}
{step === 'absences' && <CsaCatalogAbsences onNext={onAbsencesNext} adminMode={adminMode} />}
Expand Down
Loading
Loading