Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: PIN-5823 - delegated availability #1049

Open
wants to merge 4 commits into
base: feature/pin-5258-consumer-delegations
Choose a base branch
from
Open
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
34 changes: 32 additions & 2 deletions src/api/tenant/tenant.mutations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ function useUpdateMail() {

function useAssignTenantDelegatedProducerFeature() {
const { t } = useTranslation('mutations-feedback', {
keyPrefix: 'party.updateProducerDelegationAvailability',
keyPrefix: 'party.updateDelegationAvailability',
})
return useMutation({
mutationFn: TenantServices.assignTenantDelegatedProducerFeature,
Expand All @@ -30,7 +30,7 @@ function useAssignTenantDelegatedProducerFeature() {

function useDeleteTenantDelegatedProducerFeature() {
const { t } = useTranslation('mutations-feedback', {
keyPrefix: 'party.updateProducerDelegationAvailability',
keyPrefix: 'party.updateDelegationAvailability',
})
return useMutation({
mutationFn: TenantServices.deleteTenantDelegatedProducerFeature,
Expand All @@ -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,
}
11 changes: 10 additions & 1 deletion src/api/tenant/tenant.services.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { BACKEND_FOR_FRONTEND_URL } from '@/config/env'
import axiosInstance from '@/config/axios'
import type {
DelegatedProducer,
GetInstitutionUsersParams,
GetTenantsParams,
MailSeed,
Expand Down Expand Up @@ -50,11 +49,21 @@ 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,
getTenants,
updateMail,
assignTenantDelegatedProducerFeature,
deleteTenantDelegatedProducerFeature,
assignTenantDelegatedConsumerFeature,
deleteTenantDelegatedConsumerFeature,
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,34 +9,69 @@ type DelegationAvailabilityDrawerProps = {
isOpen: boolean
onClose: VoidFunction
isAvailableProducerDelegations: boolean
isAvailableConsumerDelegations: boolean
}

export const DelegationAvailabilityDrawer: React.FC<DelegationAvailabilityDrawerProps> = ({
isOpen,
onClose,
isAvailableProducerDelegations,
isAvailableConsumerDelegations,
}) => {
const { t } = useTranslation('party', { keyPrefix: 'delegations.availabilityTab' })
const { t: tCommon } = useTranslation('shared-components')
const { mutate: assignProducerDelegationAvailabilty } =
TenantMutations.useAssignTenantDelegatedProducerFeature()
const { mutate: deleteTenantDelegatedProducerFeature } =
TenantMutations.useDeleteTenantDelegatedProducerFeature()
const { mutate: assignConsumerDelegationAvailabilty } =
TenantMutations.useAssignTenantDelegatedConsumerFeature()
const { mutate: deleteTenantDelegatedConsumerFeature } =
TenantMutations.useDeleteTenantDelegatedConsumerFeature()

const [checkedProducerDelegations, setCheckedProducerDelegations] = React.useState(
isAvailableProducerDelegations
)

const checkedConsumerDelegations = false //TODO disponibilità fruizione
const [checkedConsumerDelegations, setCheckedConsumerDelegations] = React.useState(
isAvailableConsumerDelegations
)

React.useEffect(() => {
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()
}

Expand All @@ -50,7 +85,7 @@ export const DelegationAvailabilityDrawer: React.FC<DelegationAvailabilityDrawer
label: tCommon('drawer.updateLabel'),
}}
>
<Stack spacing={4}>
<Stack spacing={4} mb={3}>
<Box>
<SectionContainer innerSection>
<Stack spacing={2}>
Expand All @@ -60,9 +95,9 @@ export const DelegationAvailabilityDrawer: React.FC<DelegationAvailabilityDrawer
control={
<Switch
checked={checkedConsumerDelegations}
/*onChange={() => { //TODO
setCheckedConsumerDelegations(!checkedConsumerDelegations)
}}*/
onChange={() => {
setCheckedConsumerDelegations((prev) => !prev)
}}
/>
}
label={t('consumeDelegation.value.true')}
Expand All @@ -80,7 +115,7 @@ export const DelegationAvailabilityDrawer: React.FC<DelegationAvailabilityDrawer
<Switch
checked={checkedProducerDelegations}
onChange={() => {
setCheckedProducerDelegations(!checkedProducerDelegations)
setCheckedProducerDelegations((prev) => !prev)
}}
/>
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,23 @@ 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' })
const { t: tCommon } = useTranslation('common')
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)

Expand Down Expand Up @@ -70,7 +74,7 @@ export const DelegationsAvailabilityTab: React.FC = () => {
<DelegationAvailabilityDrawer
isOpen={isDrawerOpen}
onClose={onCloseDrawer}
//isAvailableConsumerDelegations={isAvailableConsumerDelegations}
isAvailableConsumerDelegations={isAvailableConsumerDelegations}
isAvailableProducerDelegations={isAvailableProducerDelegations}
/>
</Grid>
Expand Down
2 changes: 1 addition & 1 deletion src/static/locales/en/mutations-feedback.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion src/static/locales/it/mutations-feedback.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
9 changes: 8 additions & 1 deletion src/utils/tenant.utils.ts
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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<TenantFeature, { delegatedConsumer?: unknown }> =>
Boolean('delegatedConsumer' in feature && feature.delegatedConsumer?.availabilityTimestamp)
)?.delegatedConsumer?.availabilityTimestamp
}
Loading