diff --git a/src/api/tenant/tenant.mutations.ts b/src/api/tenant/tenant.mutations.ts index c6e1f467a..e603a7dfe 100644 --- a/src/api/tenant/tenant.mutations.ts +++ b/src/api/tenant/tenant.mutations.ts @@ -16,7 +16,7 @@ function useUpdateMail() { function useAssignTenantDelegatedProducerFeature() { const { t } = useTranslation('mutations-feedback', { - keyPrefix: 'party.updateProducerDelegationAvailability', + keyPrefix: 'party.updateDelegationAvailability', }) return useMutation({ mutationFn: TenantServices.assignTenantDelegatedProducerFeature, @@ -30,7 +30,7 @@ function useAssignTenantDelegatedProducerFeature() { function useDeleteTenantDelegatedProducerFeature() { const { t } = useTranslation('mutations-feedback', { - keyPrefix: 'party.updateProducerDelegationAvailability', + keyPrefix: 'party.updateDelegationAvailability', }) return useMutation({ mutationFn: TenantServices.deleteTenantDelegatedProducerFeature, @@ -42,8 +42,38 @@ function useDeleteTenantDelegatedProducerFeature() { }) } +function useAssignTenantDelegatedConsumerFeature() { + const { t } = useTranslation('mutations-feedback', { + keyPrefix: 'party.updateDelegationAvailability', + }) + return useMutation({ + mutationFn: TenantServices.assignTenantDelegatedConsumerFeature, + meta: { + successToastLabel: t('outcome.success'), + errorToastLabel: t('outcome.error'), + loadingLabel: t('loading'), + }, + }) +} + +function useDeleteTenantDelegatedConsumerFeature() { + const { t } = useTranslation('mutations-feedback', { + keyPrefix: 'party.updateDelegationAvailability', + }) + return useMutation({ + mutationFn: TenantServices.deleteTenantDelegatedConsumerFeature, + meta: { + successToastLabel: t('outcome.success'), + errorToastLabel: t('outcome.error'), + loadingLabel: t('loading'), + }, + }) +} + export const TenantMutations = { useUpdateMail, useAssignTenantDelegatedProducerFeature, useDeleteTenantDelegatedProducerFeature, + useAssignTenantDelegatedConsumerFeature, + useDeleteTenantDelegatedConsumerFeature, } diff --git a/src/api/tenant/tenant.services.ts b/src/api/tenant/tenant.services.ts index e39635803..8bac09ae9 100644 --- a/src/api/tenant/tenant.services.ts +++ b/src/api/tenant/tenant.services.ts @@ -1,7 +1,6 @@ import { BACKEND_FOR_FRONTEND_URL } from '@/config/env' import axiosInstance from '@/config/axios' import type { - DelegatedProducer, GetInstitutionUsersParams, GetTenantsParams, MailSeed, @@ -50,6 +49,14 @@ function deleteTenantDelegatedProducerFeature() { return axiosInstance.delete(`${BACKEND_FOR_FRONTEND_URL}/tenants/delegatedProducer`) } +function assignTenantDelegatedConsumerFeature() { + return axiosInstance.post(`${BACKEND_FOR_FRONTEND_URL}/tenants/delegatedConsumer`) +} + +function deleteTenantDelegatedConsumerFeature() { + return axiosInstance.delete(`${BACKEND_FOR_FRONTEND_URL}/tenants/delegatedConsumer`) +} + export const TenantServices = { getParty, getPartyUsersList, @@ -57,4 +64,6 @@ export const TenantServices = { updateMail, assignTenantDelegatedProducerFeature, deleteTenantDelegatedProducerFeature, + assignTenantDelegatedConsumerFeature, + deleteTenantDelegatedConsumerFeature, } diff --git a/src/pages/DelegationsPage/DelegationsAvailabilityTab/DelegationAvailabilityDrawer.tsx b/src/pages/DelegationsPage/DelegationsAvailabilityTab/DelegationAvailabilityDrawer.tsx index 1a7e56209..01d7c48f6 100644 --- a/src/pages/DelegationsPage/DelegationsAvailabilityTab/DelegationAvailabilityDrawer.tsx +++ b/src/pages/DelegationsPage/DelegationsAvailabilityTab/DelegationAvailabilityDrawer.tsx @@ -9,12 +9,14 @@ type DelegationAvailabilityDrawerProps = { isOpen: boolean onClose: VoidFunction isAvailableProducerDelegations: boolean + isAvailableConsumerDelegations: boolean } export const DelegationAvailabilityDrawer: React.FC = ({ isOpen, onClose, isAvailableProducerDelegations, + isAvailableConsumerDelegations, }) => { const { t } = useTranslation('party', { keyPrefix: 'delegations.availabilityTab' }) const { t: tCommon } = useTranslation('shared-components') @@ -22,21 +24,54 @@ export const DelegationAvailabilityDrawer: React.FC { + if (!isOpen) { + if (checkedProducerDelegations != isAvailableProducerDelegations) { + setCheckedProducerDelegations(isAvailableProducerDelegations) + } + + if (checkedConsumerDelegations != isAvailableConsumerDelegations) { + setCheckedConsumerDelegations(isAvailableConsumerDelegations) + } + } + }, [ + isAvailableProducerDelegations, + isAvailableConsumerDelegations, + isOpen, + checkedProducerDelegations, + checkedConsumerDelegations, + ]) function handleClick() { if (checkedProducerDelegations != isAvailableProducerDelegations) { - if (checkedProducerDelegations === true) { + if (checkedProducerDelegations) { assignProducerDelegationAvailabilty() } else { deleteTenantDelegatedProducerFeature() } } + + if (checkedConsumerDelegations != isAvailableConsumerDelegations) { + if (checkedConsumerDelegations) { + assignConsumerDelegationAvailabilty() + } else { + deleteTenantDelegatedConsumerFeature() + } + } + onClose() } @@ -50,7 +85,7 @@ export const DelegationAvailabilityDrawer: React.FC - + @@ -60,9 +95,9 @@ export const DelegationAvailabilityDrawer: React.FC { //TODO - setCheckedConsumerDelegations(!checkedConsumerDelegations) - }}*/ + onChange={() => { + setCheckedConsumerDelegations((prev) => !prev) + }} /> } label={t('consumeDelegation.value.true')} @@ -80,7 +115,7 @@ export const DelegationAvailabilityDrawer: React.FC { - setCheckedProducerDelegations(!checkedProducerDelegations) + setCheckedProducerDelegations((prev) => !prev) }} /> } diff --git a/src/pages/DelegationsPage/DelegationsAvailabilityTab/DelegationAvailabilityTab.tsx b/src/pages/DelegationsPage/DelegationsAvailabilityTab/DelegationAvailabilityTab.tsx index 9372d98c2..e5eebdebf 100644 --- a/src/pages/DelegationsPage/DelegationsAvailabilityTab/DelegationAvailabilityTab.tsx +++ b/src/pages/DelegationsPage/DelegationsAvailabilityTab/DelegationAvailabilityTab.tsx @@ -7,7 +7,10 @@ import { AuthHooks } from '@/api/auth' import EditIcon from '@mui/icons-material/Edit' import { DelegationAvailabilityDrawer } from './DelegationAvailabilityDrawer' import { TenantHooks } from '@/api/tenant' -import { hasTenantGivenProducerDelegationAvailability } from '@/utils/tenant.utils' +import { + hasTenantGivenConsumerDelegationAvailability, + hasTenantGivenProducerDelegationAvailability, +} from '@/utils/tenant.utils' export const DelegationsAvailabilityTab: React.FC = () => { const { t } = useTranslation('party', { keyPrefix: 'delegations.availabilityTab' }) @@ -15,11 +18,12 @@ export const DelegationsAvailabilityTab: React.FC = () => { const { isAdmin } = AuthHooks.useJwt() const { data: activeTenant } = TenantHooks.useGetActiveUserParty() - const producerDelegationsAvailability = hasTenantGivenProducerDelegationAvailability(activeTenant) + const producerDelegationsAvailability = hasTenantGivenProducerDelegationAvailability(activeTenant) const isAvailableProducerDelegations = Boolean(producerDelegationsAvailability) - const [isAvailableConsumerDelegations, setIsAvailableConsumerDelegations] = React.useState(false) //TODO integrare con BE + const consumerDelegationsAvailability = hasTenantGivenConsumerDelegationAvailability(activeTenant) + const isAvailableConsumerDelegations = Boolean(consumerDelegationsAvailability) const [isDrawerOpen, setIsDrawerOpen] = React.useState(false) @@ -70,7 +74,7 @@ export const DelegationsAvailabilityTab: React.FC = () => { diff --git a/src/static/locales/en/mutations-feedback.json b/src/static/locales/en/mutations-feedback.json index 3e4976efb..7d27448bd 100644 --- a/src/static/locales/en/mutations-feedback.json +++ b/src/static/locales/en/mutations-feedback.json @@ -607,7 +607,7 @@ "error": "Contact Infos could not be updated. Please, try again!" } }, - "updateProducerDelegationAvailability": { + "updateDelegationAvailability": { "loading": "Updating delegation availability", "outcome": { "success": "Delegation availability updated succesfully", diff --git a/src/static/locales/it/mutations-feedback.json b/src/static/locales/it/mutations-feedback.json index c7af67e3d..bf2482fb8 100644 --- a/src/static/locales/it/mutations-feedback.json +++ b/src/static/locales/it/mutations-feedback.json @@ -607,7 +607,7 @@ "error": "Non è stato possibile aggiornare i contatti. Per favore, riprova!" } }, - "updateProducerDelegationAvailability": { + "updateDelegationAvailability": { "loading": "Stiamo aggiornando la disponibilità", "outcome": { "success": "La disponibilità è stata aggiornata correttamente", diff --git a/src/utils/tenant.utils.ts b/src/utils/tenant.utils.ts index 8e25ee0c1..66cb3e639 100644 --- a/src/utils/tenant.utils.ts +++ b/src/utils/tenant.utils.ts @@ -1,4 +1,4 @@ -import { Tenant, TenantFeature } from '@/api/api.generatedTypes' +import type { Tenant, TenantFeature } from '@/api/api.generatedTypes' export function isTenantCertifier(tenant: Tenant) { return tenant.features.some((feature) => 'certifier' in feature && feature.certifier?.certifierId) @@ -10,3 +10,10 @@ export function hasTenantGivenProducerDelegationAvailability(tenant: Tenant) { Boolean('delegatedProducer' in feature && feature.delegatedProducer?.availabilityTimestamp) )?.delegatedProducer?.availabilityTimestamp } + +export function hasTenantGivenConsumerDelegationAvailability(tenant: Tenant) { + return tenant.features.find( + (feature): feature is Extract => + Boolean('delegatedConsumer' in feature && feature.delegatedConsumer?.availabilityTimestamp) + )?.delegatedConsumer?.availabilityTimestamp +}