diff --git a/app/frontend/src/javascript/components/dashboard/reservations/prepaid-packs-panel.tsx b/app/frontend/src/javascript/components/dashboard/reservations/prepaid-packs-panel.tsx index 247e57f37..e67a74c91 100644 --- a/app/frontend/src/javascript/components/dashboard/reservations/prepaid-packs-panel.tsx +++ b/app/frontend/src/javascript/components/dashboard/reservations/prepaid-packs-panel.tsx @@ -15,6 +15,8 @@ import { SelectOption } from '../../../models/select'; import { ProposePacksModal } from '../../prepaid-packs/propose-packs-modal'; import * as React from 'react'; import { User } from '../../../models/user'; +import PrepaidPackAPI from '../../../api/prepaid-pack'; +import { PrepaidPack } from '../../../models/prepaid-pack'; interface PrepaidPacksPanelProps { user: User, @@ -28,16 +30,18 @@ const PrepaidPacksPanel: React.FC = ({ user, onError }) const { t } = useTranslation('logged'); const [machines, setMachines] = useState>([]); - const [packs, setPacks] = useState>([]); + const [packs, setPacks] = useState>([]); + const [userPacks, setUserPacks] = useState>([]); const [threshold, setThreshold] = useState(null); const [selectedMachine, setSelectedMachine] = useState(null); const [packsModal, setPacksModal] = useState(false); + const [packsForSubscribers, setPacksForSubscribers] = useState(false); const { handleSubmit, control, formState } = useForm<{ machine_id: number }>(); useEffect(() => { UserPackAPI.index({ user_id: user.id }) - .then(setPacks) + .then(setUserPacks) .catch(onError); SettingAPI.get('renew_pack_threshold') .then(data => setThreshold(parseFloat(data.value))) @@ -45,6 +49,12 @@ const PrepaidPacksPanel: React.FC = ({ user, onError }) MachineAPI.index({ disabled: false }) .then(setMachines) .catch(onError); + PrepaidPackAPI.index({ disabled: false, group_id: user.group_id, priceable_type: 'Machine' }) + .then(setPacks) + .catch(onError); + SettingAPI.get('pack_only_for_subscription') + .then(data => setPacksForSubscribers(data.value === 'true')) + .catch(onError); }, []); /** @@ -77,18 +87,26 @@ const PrepaidPacksPanel: React.FC = ({ user, onError }) * Build the options for the select dropdown, for the given list of machines */ const buildMachinesOptions = (machines: Array): Array> => { - return machines.map(m => { + const packMachinesId = packs.map(p => p.priceable_id); + return machines.filter(m => packMachinesId.includes(m.id)).map(m => { return { label: m.name, value: m.id }; }); }; + /** + * Check if the user can buy a pack + */ + const canBuyPacks = (): boolean => { + return (packs.length > 0 && (!packsForSubscribers || (packsForSubscribers && user.subscribed_plan != null))); + }; + /** * Callback triggered when a prepaid pack was successfully bought: refresh the list of packs for the user */ const onPackBoughtSuccess = () => { togglePacksModal(); UserPackAPI.index({ user_id: user.id }) - .then(setPacks) + .then(setUserPacks) .catch(onError); }; @@ -96,7 +114,7 @@ const PrepaidPacksPanel: React.FC = ({ user, onError })

{t('app.logged.dashboard.reservations_dashboard.prepaid_packs_panel.title')}

- {packs.map(pack => ( + {userPacks.map(pack => (
{t('app.logged.dashboard.reservations_dashboard.prepaid_packs_panel.name')} @@ -122,7 +140,7 @@ const PrepaidPacksPanel: React.FC = ({ user, onError })
))} -
+ {canBuyPacks() &&

{t('app.logged.dashboard.reservations_dashboard.prepaid_packs_panel.cta_info')}

@@ -140,7 +158,7 @@ const PrepaidPacksPanel: React.FC = ({ user, onError }) onError={onError} onDecline={togglePacksModal} onSuccess={onPackBoughtSuccess} />} -
+
} ); diff --git a/config/locales/app.logged.en.yml b/config/locales/app.logged.en.yml index 33587a88d..1edafbeaf 100644 --- a/config/locales/app.logged.en.yml +++ b/config/locales/app.logged.en.yml @@ -156,7 +156,7 @@ en: cancelled_slot: "Cancelled" credits_panel: title: "My credits" - info: "Your subscription comes with free credits you can use on reservations" + info: "Your subscription comes with free credits you can use when reservating" remaining_credits_html: "You can book {REMAINING} {REMAINING, plural, one{slot} other{slots}} for free." used_credits_html: "You have already used {USED} {USED, plural, =0{credit} one{credit} other{credits}}." no_credits: "You don't have any credits yet. Some subscriptions may allow you to book some slots for free."