2023-02-14 05:21:25 -05:00
|
|
|
import { useCallback, useState } from 'react'
|
|
|
|
import { Trans, useTranslation } from 'react-i18next'
|
|
|
|
import { postJSON } from '../../../../infrastructure/fetch-json'
|
|
|
|
import { useSubscriptionDashboardContext } from '../../context/subscription-dashboard-context'
|
2024-06-18 06:01:37 -04:00
|
|
|
import { ManagedInstitution as Institution } from '../../../../../../types/subscription/dashboard/managed-institution'
|
2023-06-08 09:19:32 -04:00
|
|
|
import { RowLink } from './row-link'
|
2023-09-27 05:45:49 -04:00
|
|
|
import { debugConsole } from '@/utils/debugging'
|
2024-06-18 06:01:37 -04:00
|
|
|
import getMeta from '@/utils/meta'
|
2024-09-30 05:49:18 -04:00
|
|
|
import OLButton from '@/features/ui/components/ol/ol-button'
|
2023-02-14 05:21:25 -05:00
|
|
|
|
|
|
|
type ManagedInstitutionProps = {
|
|
|
|
institution: Institution
|
|
|
|
}
|
|
|
|
|
|
|
|
export default function ManagedInstitution({
|
|
|
|
institution,
|
|
|
|
}: ManagedInstitutionProps) {
|
|
|
|
const { t } = useTranslation()
|
|
|
|
const [subscriptionChanging, setSubscriptionChanging] = useState(false)
|
|
|
|
const { updateManagedInstitution } = useSubscriptionDashboardContext()
|
|
|
|
|
|
|
|
const changeInstitutionalEmailSubscription = useCallback(
|
|
|
|
(e, institutionId: Institution['v1Id']) => {
|
|
|
|
const updateSubscription = async (institutionId: Institution['v1Id']) => {
|
|
|
|
setSubscriptionChanging(true)
|
|
|
|
try {
|
|
|
|
const data = await postJSON<string[]>(
|
|
|
|
`/institutions/${institutionId}/emailSubscription`
|
|
|
|
)
|
|
|
|
institution.metricsEmail.optedOutUserIds = data
|
|
|
|
updateManagedInstitution(institution)
|
|
|
|
} catch (error) {
|
2023-09-27 05:45:49 -04:00
|
|
|
debugConsole.error(error)
|
2023-02-14 05:21:25 -05:00
|
|
|
}
|
|
|
|
setSubscriptionChanging(false)
|
|
|
|
}
|
|
|
|
|
|
|
|
e.preventDefault()
|
|
|
|
updateSubscription(institutionId)
|
|
|
|
},
|
|
|
|
[institution, updateManagedInstitution]
|
|
|
|
)
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div>
|
|
|
|
<p>
|
|
|
|
<Trans
|
|
|
|
i18nKey="you_are_a_manager_of_commons_at_institution_x"
|
|
|
|
components={[<strong />]} // eslint-disable-line react/jsx-key
|
|
|
|
values={{
|
|
|
|
institutionName: institution.name || '',
|
|
|
|
}}
|
2023-10-19 04:27:45 -04:00
|
|
|
shouldUnescape
|
|
|
|
tOptions={{ interpolation: { escapeValue: true } }}
|
2023-02-14 05:21:25 -05:00
|
|
|
/>
|
|
|
|
</p>
|
2024-09-30 05:49:18 -04:00
|
|
|
<ul className="list-group p-0">
|
|
|
|
<RowLink
|
|
|
|
href={`/metrics/institutions/${institution.v1Id}`}
|
|
|
|
heading={t('view_metrics')}
|
|
|
|
subtext={t('view_metrics_commons_subtext')}
|
|
|
|
icon="insights"
|
|
|
|
/>
|
|
|
|
<RowLink
|
|
|
|
href={`/institutions/${institution.v1Id}/hub`}
|
|
|
|
heading={t('view_hub')}
|
|
|
|
subtext={t('view_hub_subtext')}
|
|
|
|
icon="account_circle"
|
|
|
|
/>
|
|
|
|
<RowLink
|
|
|
|
href={`/manage/institutions/${institution.v1Id}/managers`}
|
|
|
|
heading={t('manage_institution_managers')}
|
|
|
|
subtext={t('manage_managers_subtext')}
|
|
|
|
icon="manage_accounts"
|
|
|
|
/>
|
|
|
|
</ul>
|
2023-02-14 05:21:25 -05:00
|
|
|
<div>
|
|
|
|
<p>
|
|
|
|
<span>Monthly metrics emails: </span>
|
|
|
|
{subscriptionChanging ? (
|
|
|
|
<i className="fa fa-spin fa-refresh" />
|
|
|
|
) : (
|
2024-09-30 05:49:18 -04:00
|
|
|
<OLButton
|
|
|
|
variant="link"
|
2023-02-14 05:21:25 -05:00
|
|
|
className="btn-inline-link"
|
|
|
|
onClick={e =>
|
|
|
|
changeInstitutionalEmailSubscription(e, institution.v1Id)
|
|
|
|
}
|
|
|
|
>
|
|
|
|
{institution.metricsEmail.optedOutUserIds.includes(
|
2024-06-18 06:01:37 -04:00
|
|
|
getMeta('ol-user_id')!
|
2023-02-14 05:21:25 -05:00
|
|
|
)
|
|
|
|
? t('subscribe')
|
|
|
|
: t('unsubscribe')}
|
2024-09-30 05:49:18 -04:00
|
|
|
</OLButton>
|
2023-02-14 05:21:25 -05:00
|
|
|
)}
|
|
|
|
</p>
|
|
|
|
</div>
|
|
|
|
<hr />
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|