Skip to content

📈 Open source, privacy-first web analytics — a cookie-less Google Analytics alternative with built-in error tracking and performance monitoring. Self-host it or use our cloud. Get started for free!

project emails | subscribers to ts

kruzhambusdb99aef25487
diff --git a/src/api/index.ts b/src/api/index.ts
index 8d5371377..9163cea59 100644
--- a/src/api/index.ts
+++ b/src/api/index.ts
@@ -687,7 +687,7 @@ export const reGenerateCaptchaSecretKey = (pid: string) =>
export const addSubscriber = (id: string, data: {
email: string
- reportFrequency: keyof typeof reportFrequencies
+ reportFrequency: string
}) =>
api
.post(`project/${id}/subscribers`, data)
@@ -714,7 +714,7 @@ export const getSubscribers = (id: string, offset: number, limit: number) =>
})
export const updateSubscriber = (id: string, subscriberId: string, data: {
- reportFrequency: keyof typeof reportFrequencies
+ reportFrequency: string
}) =>
api
.patch(`project/${id}/subscribers/${subscriberId}`, data)
diff --git a/src/pages/Project/Settings/Emails/ConfirmReportsShare/index.jsx b/src/pages/Project/Settings/Emails/ConfirmReportsShare/index.tsx
similarity index 92%
rename from src/pages/Project/Settings/Emails/ConfirmReportsShare/index.jsx
rename to src/pages/Project/Settings/Emails/ConfirmReportsShare/index.tsx
index a06cf674e..032acd41f 100644
--- a/src/pages/Project/Settings/Emails/ConfirmReportsShare/index.jsx
+++ b/src/pages/Project/Settings/Emails/ConfirmReportsShare/index.tsx
@@ -9,13 +9,17 @@ import Loader from 'ui/Loader'
import { confirmSubscriberInvite } from 'api'
import routes from 'routes'
-const ConfirmReportsShare = () => {
- const { t } = useTranslation('common')
- const { id } = useParams()
- const [loading, setLoading] = useState(true)
- const [error, setError] = useState('')
+const ConfirmReportsShare = (): JSX.Element => {
+ const { t }: {
+ t: (key: string) => string
+ } = useTranslation('common')
+ const { id }: {
+ id: string
+ } = useParams()
+ const [loading, setLoading] = useState<boolean>(true)
+ const [error, setError] = useState<string>('')
- const handleConfirm = async (token) => {
+ const handleConfirm = async (token: string) => {
try {
await confirmSubscriberInvite(id, token)
} catch (e) {
diff --git a/src/pages/Project/Settings/Emails/Emails.container.jsx b/src/pages/Project/Settings/Emails/Emails.container.jsx
deleted file mode 100644
index b09cd7e72..000000000
--- a/src/pages/Project/Settings/Emails/Emails.container.jsx
+++ /dev/null
@@ -1,26 +0,0 @@
-import { connect } from 'react-redux'
-import { errorsActions } from 'redux/reducers/errors'
-import { alertsActions } from 'redux/reducers/alerts'
-
-import Emails from './Emails'
-
-const mapStateToProps = (state) => ({
- user: state.auth.user,
-})
-
-const mapDispatchToProps = (dispatch) => ({
- emailFailed: (message) => {
- dispatch(errorsActions.generateErrors(message))
- },
- addEmail: (message, type = 'success') => {
- dispatch(alertsActions.generateAlerts(message, type))
- },
- removeEmail: (message, type = 'success') => {
- dispatch(alertsActions.generateAlerts(message, type))
- },
- reportTypeNotifiction: (message, type = 'success') => {
- dispatch(alertsActions.generateAlerts(message, type))
- },
-})
-
-export default connect(mapStateToProps, mapDispatchToProps)(Emails)
diff --git a/src/pages/Project/Settings/Emails/Emails.container.tsx b/src/pages/Project/Settings/Emails/Emails.container.tsx
new file mode 100644
index 000000000..0eb84826b
--- /dev/null
+++ b/src/pages/Project/Settings/Emails/Emails.container.tsx
@@ -0,0 +1,37 @@
+import { connect } from 'react-redux'
+import { errorsActions } from 'redux/reducers/errors'
+import { alertsActions } from 'redux/reducers/alerts'
+import { StateType, AppDispatch } from 'redux/store'
+import Emails from './Emails'
+
+const mapStateToProps = (state: StateType) => ({
+ user: state.auth.user,
+})
+
+const mapDispatchToProps = (dispatch: AppDispatch) => ({
+ emailFailed: (message: string) => {
+ dispatch(errorsActions.genericError({
+ message,
+ }))
+ },
+ addEmail: (message: string, type = 'success') => {
+ dispatch(alertsActions.generateAlerts({
+ message,
+ type,
+ }))
+ },
+ removeEmail: (message: string, type = 'success') => {
+ dispatch(alertsActions.generateAlerts({
+ message,
+ type,
+ }))
+ },
+ reportTypeNotifiction: (message: string, type = 'success') => {
+ dispatch(alertsActions.generateAlerts({
+ message,
+ type,
+ }))
+ },
+})
+
+export default connect(mapStateToProps, mapDispatchToProps)(Emails)
diff --git a/src/pages/Project/Settings/Emails/Emails.jsx b/src/pages/Project/Settings/Emails/Emails.tsx
similarity index 83%
rename from src/pages/Project/Settings/Emails/Emails.jsx
rename to src/pages/Project/Settings/Emails/Emails.tsx
index badd89362..cbc3152e4 100644
--- a/src/pages/Project/Settings/Emails/Emails.jsx
+++ b/src/pages/Project/Settings/Emails/Emails.tsx
@@ -1,4 +1,6 @@
-import React, { useState, useEffect, useRef } from 'react'
+import React, {
+ useState, useEffect, useRef,
+} from 'react'
import {
TrashIcon, InboxStackIcon, ChevronDownIcon, CheckIcon,
} from '@heroicons/react/24/outline'
@@ -26,10 +28,26 @@ import Loader from 'ui/Loader'
import Beta from 'ui/Beta'
import cx from 'clsx'
import { WarningPin } from 'ui/Pin'
+import { ISubscribers } from 'redux/models/ISubscribers'
const ModalMessage = ({
project, handleInput, beenSubmitted, errors, form, t,
-}) => (
+}: {
+ project: string
+ handleInput: (e: React.ChangeEvent<HTMLInputElement>) => void
+ beenSubmitted: boolean
+ errors: {
+ email: string
+ reportFrequency: string
+ }
+ form: {
+ email: string
+ reportFrequency: string
+ }
+ t: (key: string, options?: {
+ [key: string]: string | number | boolean | undefined
+ }) => string
+}): JSX.Element => (
<div>
<h2 className='text-xl font-bold text-gray-700 dark:text-gray-200'>
{t('project.settings.inviteTo', { project })}
@@ -86,16 +104,36 @@ const ModalMessage = ({
const EmailList = ({
data, onRemove, t, setEmails, emailFailed, language, reportTypeNotifiction,
+}: {
+ data: {
+ id: string
+ addedAt: string
+ isConfirmed: boolean
+ projectId: string
+ email: string
+ reportFrequency: string
+ }
+ onRemove: (id: string) => void
+ t: (key: string, options?: {
+ [key: string]: string | number | boolean | undefined
+ }) => string
+ setEmails: (value: ISubscribers[] | ((prevVar: ISubscribers[]) => ISubscribers[])) => void;
+ emailFailed: (message: string) => void
+ language: string
+ reportTypeNotifiction: (message: string) => void
}) => {
- const [open, setOpen] = useState(false)
- const [showDeleteModal, setShowDeleteModal] = useState(false)
- const openRef = useRef()
+ const [open, setOpen] = useState<boolean>(false)
+ const [showDeleteModal, setShowDeleteModal] = useState<boolean>(false)
+ const openRef = useRef<HTMLDivElement>(null)
useOnClickOutside(openRef, () => setOpen(false))
const {
id, addedAt, isConfirmed, projectId, email, reportFrequency,
} = data
- const changeRole = async (reportType) => {
+ const changeRole = async (reportType: {
+ value: string
+ label: string
+ }) => {
try {
const results = await updateSubscriber(projectId, id, { reportFrequency: reportType.value })
setEmails((prev) => {
@@ -218,7 +256,9 @@ EmailList.defaultProps = {
data: {},
}
-const NoSubscribers = ({ t }) => (
+const NoSubscribers = ({ t }: {
+ t: (string: string) => string,
+}): JSX.Element => (
<div className='flex flex-col py-6 sm:px-6 lg:px-8'>
<div className='max-w-7xl w-full mx-auto text-gray-900 dark:text-gray-50'>
<h2 className='text-2xl mb-8 text-center leading-snug'>
@@ -230,17 +270,38 @@ const NoSubscribers = ({ t }) => (
const Emails = ({
emailFailed, addEmail, removeEmail, projectId, projectName, reportTypeNotifiction,
-}) => {
- const [showModal, setShowModal] = useState(false)
- const { t, i18n: { language } } = useTranslation('common')
- const [form, setForm] = useState({
+}: {
+ emailFailed: (message: string, type?: string) => void
+ addEmail: (message: string, type?: string) => void
+ removeEmail: (message: string) => void
+ projectId: string
+ projectName: string
+ reportTypeNotifiction: (message: string, type?: string) => void
+}): JSX.Element => {
+ const [showModal, setShowModal] = useState<boolean>(false)
+ const { t, i18n: { language } }: {
+ t: (string: string, options?: {
+ [key: string]: string | number | boolean | undefined | null
+ }) => string,
+ i18n: { language: string },
+ } = useTranslation('common')
+ const [form, setForm] = useState<{
+ email: string,
+ reportFrequency: string,
+ }>({
email: '',
reportFrequency: reportFrequencyForEmailsOptions[3].value,
})
- const [beenSubmitted, setBeenSubmitted] = useState(false)
- const [errors, setErrors] = useState({})
- const [validated, setValidated] = useState(false)
- const [emails, setEmails] = useState([])
+ const [beenSubmitted, setBeenSubmitted] = useState<boolean>(false)
+ const [errors, setErrors] = useState<{
+ email: string,
+ reportFrequency: string,
+ }>({
+ email: '',
+ reportFrequency: '',
+ })
+ const [validated, setValidated] = useState<boolean>(false)
+ const [emails, setEmails] = useState<ISubscribers[]>([])
const [loading, setLoading] = useState(true)
const [paggination, setPaggination] = useState({
page: 1,
@@ -268,7 +329,13 @@ const Emails = ({
}, []) // eslint-disable-line
const validate = () => {
- const allErrors = {}
+ const allErrors: {
+ email: string,
+ reportFrequency: string,
+ } = {
+ email: '',
+ reportFrequency: '',
+ }
if (!isValidEmail(form.email)) {
allErrors.email = t('auth.common.badEmailError')
@@ -286,7 +353,7 @@ const Emails = ({
}
}, [form]) // eslint-disable-line
- const handleInput = ({ target }) => {
+ const handleInput = ({ target }: React.ChangeEvent<HTMLInputElement>) => {
const value = target.type === 'checkbox' ? target.checked : target.value
setForm(oldForm => ({
@@ -297,7 +364,10 @@ const Emails = ({
const onSubmit = async () => {
setShowModal(false)
- setErrors({})
+ setErrors({
+ email: '',
+ reportFrequency: '',
+ })
setValidated(false)
try {
@@ -310,10 +380,10 @@ const Emails = ({
}
// a timeout is needed to prevent the flicker of data fields in the modal when closing
- setTimeout(() => setForm({ email: '' }), 300)
+ setTimeout(() => setForm({ email: '', reportFrequency: '' }), 300)
}
- const handleSubmit = e => {
+ const handleSubmit = (e: React.MouseEvent<HTMLButtonElement, MouseEvent>) => {
e.preventDefault()
e.stopPropagation()
@@ -328,11 +398,14 @@ const Emails = ({
const closeModal = () => {
setShowModal(false)
// a timeout is needed to prevent the flicker of data fields in the modal when closing
- setTimeout(() => setForm({ email: '' }), 300)
- setErrors({})
+ setTimeout(() => setForm({ email: '', reportFrequency: '' }), 300)
+ setErrors({
+ email: '',
+ reportFrequency: '',
+ })
}
- const onRemove = async (email) => {
+ const onRemove = async (email: string) => {
try {
await removeSubscriber(projectId, email)
const results = _filter(emails, s => s.id !== email)
@@ -365,8 +438,10 @@ const Emails = ({
type='button'
onClick={() => setShowModal(true)}
>
- <InboxStackIcon className='w-5 h-5 mr-1' />
- {t('project.emails.add')}
+ <>
+ <InboxStackIcon className='w-5 h-5 mr-1' />
+ {t('project.emails.add')}
+ </>
</Button>
</div>
<div>
@@ -393,12 +468,10 @@ const Emails = ({
<EmailList
data={email}
key={email.id}
- pid={projectId}
onRemove={onRemove}
t={t}
language={language}
setEmails={setEmails}
- removeEmail={removeEmail}
emailFailed={emailFailed}
reportTypeNotifiction={reportTypeNotifiction}
/>
@@ -437,7 +510,6 @@ const Emails = ({
handleInput={handleInput}
errors={errors}
beenSubmitted={beenSubmitted}
- validated={validated}
/>
)}
isOpened={showModal}
diff --git a/src/pages/Project/Settings/Emails/index.jsx b/src/pages/Project/Settings/Emails/index.tsx
similarity index 100%
rename from src/pages/Project/Settings/Emails/index.jsx
rename to src/pages/Project/Settings/Emails/index.tsx