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!

Created & integrated common Modal component

Blue Mouse38fbf5fc9312
diff --git a/src/pages/Dashboard/Dashboard.jsx b/src/pages/Dashboard/Dashboard.jsx
index 331a790ed..8f9eac7a3 100644
--- a/src/pages/Dashboard/Dashboard.jsx
+++ b/src/pages/Dashboard/Dashboard.jsx
@@ -60,7 +60,7 @@ const ProjectCart = ({ name, url, created, active, overall }) => {
<ArrowSmDownIcon className='self-center flex-shrink-0 h-4 w-4 text-red-500' />
<span className='sr-only'>
Descreased by
- </span>
+ </span>
</>
)}
{overall.percChange}%
diff --git a/src/pages/UserSettings/UserSettings.jsx b/src/pages/UserSettings/UserSettings.jsx
index 1cea7020b..826fbb25a 100644
--- a/src/pages/UserSettings/UserSettings.jsx
+++ b/src/pages/UserSettings/UserSettings.jsx
@@ -1,10 +1,12 @@
import React, { useState, useEffect, memo } from 'react'
import { useSelector } from 'react-redux'
+import _size from 'lodash/size'
+import _isEmpty from 'lodash/isEmpty'
import { MailIcon } from '@heroicons/react/outline'
import Input from 'ui/Input'
import Button from 'ui/Button'
-import Modal from 'components/Modal'
+import Modal from 'ui/Modal'
import { isValidEmail, isValidPassword } from 'utils/validator'
const UserSettings = ({ onDelete, onExport, onSubmit, onEmailConfirm }) => {
@@ -20,7 +22,7 @@ const UserSettings = ({ onDelete, onExport, onSubmit, onEmailConfirm }) => {
const [beenSubmitted, setBeenSubmitted] = useState(false)
const [showModal, setShowModal] = useState(false)
const [showExportModal, setShowExportModal] = useState(false)
- const [error, setError] = useState('')
+ const [error, setError] = useState(null)
useEffect(() => {
validate()
@@ -53,7 +55,7 @@ const UserSettings = ({ onDelete, onExport, onSubmit, onEmailConfirm }) => {
allErrors.email = 'Please provide a valid email.'
}
- if (form.password.length > 0 && !isValidPassword(form.password)) {
+ if (_size(form.password) > 0 && !isValidPassword(form.password)) {
allErrors.password = 'The entered password is incorrect.'
}
@@ -61,7 +63,7 @@ const UserSettings = ({ onDelete, onExport, onSubmit, onEmailConfirm }) => {
allErrors.repeat = 'Passwords have to match.'
}
- const valid = Object.keys(allErrors).length === 0
+ const valid = _isEmpty(Object.keys(allErrors))
setErrors(allErrors)
setValidated(valid)
@@ -116,7 +118,7 @@ const UserSettings = ({ onDelete, onExport, onSubmit, onEmailConfirm }) => {
{!user?.isActive && (
<div href='#' className='flex cursor-pointer mt-4 pl-0 underline text-blue-600 hover:text-indigo-800' onClick={() => onEmailConfirm(setError)}>
<MailIcon className='mt-0.5 mr-2 w-6 h-6 text-blue-500' />
- Didn't receive a link to confirm the email address? Request a new one!
+ Didn't receive a link to confirm the email address? Request a new one!
</div>
)}
<div className='flex justify-between mt-4'>
@@ -138,35 +140,35 @@ const UserSettings = ({ onDelete, onExport, onSubmit, onEmailConfirm }) => {
</div>
</form>
- {showExportModal && (
- <Modal
- onCancel={() => setShowExportModal(false)}
- onSubmit={() => { setShowExportModal(false); onExport() }}
- submitText='Continue'
- cancelText='Close'
- title='Data export'
- text={'As requested by Art. 20 of General Data Protection Regulation (GDPR) the you have the right to receive your personal data that we store.\nThe data export will be ready within 24 hours and sent to your email account.\nNote: you can request the data export only once per two weeks.'}
- />
- )}
- {showModal && (
- <Modal
- onCancel={() => setShowModal(false)}
- onSubmit={() => { setShowModal(false); onDelete() }}
- submitText='Delete my account'
- cancelText='Close'
- title='Delete your account?'
- text={'By pressing \'Delete my account\' you understand, that this action is irreversible.\nAll your data will be deleted from our servers.'}
- />
- )}
- {error && (
- <Modal
- onSubmit={() => { setError('') }}
- onCancel={() => { setError('') }}
- submitText='Got it'
- title='Error'
- text={error}
- />
- )}
+ <Modal
+ onClose={() => setShowExportModal(false)}
+ onSubmit={() => { setShowExportModal(false); onExport() }}
+ submitText='Continue'
+ closeText='Close'
+ title='Data export'
+ type='info'
+ message={'As requested by Art. 20 of General Data Protection Regulation (GDPR) the you have the right to receive your personal data that we store.\nThe data export will be ready within 24 hours and sent to your email account.\nNote: you can request the data export only once per two weeks.'}
+ isOpened={showExportModal}
+ />
+ <Modal
+ onClose={() => setShowModal(false)}
+ onSubmit={() => { setShowModal(false); onDelete() }}
+ submitText='Delete my account'
+ closeText='Close'
+ title='Delete your account?'
+ submitType='danger'
+ type='error'
+ message={'Are you sure you want to deactivate your account?\nAll of your data will be permanently removed from our servers forever.\nThis action cannot be undone. '}
+ isOpened={showModal}
+ />
+ <Modal
+ onClose={() => { setError('') }}
+ closeText='Got it'
+ type='error'
+ title='Error'
+ message={error}
+ isOpened={Boolean(error)}
+ />
</div>
)
}
diff --git a/src/ui/Modal.jsx b/src/ui/Modal.jsx
index e6cbd5712..28c8ebf18 100644
--- a/src/ui/Modal.jsx
+++ b/src/ui/Modal.jsx
@@ -1,17 +1,17 @@
-import React, { Fragment, useState, memo } from 'react'
+import React, { Fragment, memo } from 'react'
import PropTypes from 'prop-types'
import cx from 'classnames'
import { Dialog, Transition } from '@headlessui/react'
import { CheckIcon } from '@heroicons/react/outline'
import { ExclamationIcon } from '@heroicons/react/outline'
+import { InformationCircleIcon } from '@heroicons/react/outline'
-// todo: onClose, onSubmit, closeText, submitText, create a redux endpoint to operate all the modals
-const Modal = ({ className, type, title, message }) => {
- const [open, setOpen] = useState(true)
-
+const Modal = ({
+ className, type, title, message, isOpened, onClose, onSubmit, closeText, submitText, submitType,
+}) => {
return (
- <Transition.Root show={open} as={Fragment}>
- <Dialog as='div' className={cx('fixed z-10 inset-0 overflow-y-auto', className)} open={open} onClose={setOpen} static>
+ <Transition.Root show={isOpened} as={Fragment}>
+ <Dialog as='div' className={cx('fixed z-10 inset-0 overflow-y-auto', className)} open={isOpened} onClose={onClose} static>
<div className='flex items-end justify-center min-h-screen pt-4 px-4 pb-20 text-center sm:block sm:p-0'>
<Transition.Child
as={Fragment}
@@ -38,37 +38,56 @@ const Modal = ({ className, type, title, message }) => {
leaveFrom='opacity-100 translate-y-0 sm:scale-100'
leaveTo='opacity-0 translate-y-4 sm:translate-y-0 sm:scale-95'
>
- <div className='inline-block align-bottom bg-white rounded-lg px-4 pt-5 pb-4 text-left overflow-hidden shadow-xl transform transition-all sm:my-8 sm:align-middle sm:max-w-sm sm:w-full sm:p-6'>
- <div>
+ <div className='inline-block align-bottom bg-white rounded-lg px-4 pt-5 pb-4 text-left overflow-hidden shadow-xl transform transition-all sm:my-8 sm:align-middle sm:max-w-lg sm:w-full sm:px-5 sm:py-4'>
+ <div className='sm:flex sm:items-start'>
{type === 'success' && (
- <div className='mx-auto flex items-center justify-center h-12 w-12 rounded-full bg-green-100'>
+ <div className='mx-auto flex-shrink-0 flex items-center justify-center h-12 w-12 rounded-full bg-green-100 sm:mx-0 sm:h-10 sm:w-10'>
<CheckIcon className='h-6 w-6 text-green-600' aria-hidden='true' />
</div>
)}
{type === 'error' && (
- <div className="mx-auto flex-shrink-0 flex items-center justify-center h-12 w-12 rounded-full bg-red-100 sm:mx-0 sm:h-10 sm:w-10">
- <ExclamationIcon className="h-6 w-6 text-red-600" aria-hidden="true" />
+ <div className='mx-auto flex-shrink-0 flex items-center justify-center h-12 w-12 rounded-full bg-red-100 sm:mx-0 sm:h-10 sm:w-10'>
+ <ExclamationIcon className='h-6 w-6 text-red-600' aria-hidden='true' />
+ </div>
+ )}
+ {type === 'info' && (
+ <div className='mx-auto flex-shrink-0 flex items-center justify-center h-12 w-12 rounded-full bg-blue-100 sm:mx-0 sm:h-10 sm:w-10'>
+ <InformationCircleIcon className='h-6 w-6 text-blue-600' aria-hidden='true' />
</div>
)}
- <div className='mt-3 text-center sm:mt-5'>
+ <div className='mt-3 text-center sm:mt-0 sm:ml-4 sm:text-left'>
<Dialog.Title as='h3' className='text-lg leading-6 font-medium text-gray-900'>
{title}
</Dialog.Title>
<div className='mt-2'>
- <p className='text-sm text-gray-500'>
+ <p className='text-sm text-gray-500 whitespace-pre-line'>
{message}
</p>
</div>
</div>
</div>
- <div className='mt-5 sm:mt-6'>
- <button
- type='button'
- className='inline-flex justify-center w-full rounded-md border border-transparent shadow-sm px-4 py-2 bg-indigo-600 text-base font-medium text-white hover:bg-indigo-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500 sm:text-sm'
- onClick={() => setOpen(false)}
- >
- Go back to dashboard
- </button>
+ <div className='px-4 py-3 sm:px-0 sm:pb-0 sm:flex sm:flex-row-reverse'>
+ {submitText && (
+ <button
+ type='button'
+ className={cx('w-full inline-flex justify-center rounded-md border border-transparent shadow-sm px-4 py-2 text-base font-medium text-white sm:ml-3 sm:w-auto sm:text-sm', {
+ 'bg-indigo-600 hover:bg-indigo-700': submitType === 'regular',
+ 'bg-red-600 hover:bg-red-700': submitType === 'danger',
+ })}
+ onClick={onSubmit}
+ >
+ {submitText}
+ </button>
+ )}
+ {closeText && (
+ <button
+ type='button'
+ className='mt-3 w-full inline-flex justify-center rounded-md border border-gray-300 shadow-sm px-4 py-2 bg-white text-base font-medium text-gray-700 hover:bg-gray-50 sm:mt-0 sm:ml-3 sm:w-auto sm:text-sm'
+ onClick={onClose}
+ >
+ {closeText}
+ </button>
+ )}
</div>
</div>
</Transition.Child>
@@ -79,15 +98,27 @@ const Modal = ({ className, type, title, message }) => {
}
Modal.propTypes = {
- type: PropTypes.oneOfType(['error', 'success']).isRequired,
+ type: PropTypes.oneOfType(['error', 'success', 'info']),
title: PropTypes.string.isRequired,
+ onClose: PropTypes.func.isRequired,
message: PropTypes.string,
className: PropTypes.string,
+ isOpened: PropTypes.bool,
+ onSubmit: PropTypes.func,
+ closeText: PropTypes.string,
+ submitText: PropTypes.string,
+ submitType: PropTypes.oneOf(['regular', 'danger']),
}
Modal.defaultProps = {
className: '',
message: '',
+ isOpened: false,
+ onSubmit: () => { },
+ closeText: null,
+ submitText: null,
+ submitType: 'regular',
+ type: null,
}
export default memo(Modal)