-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
505b12c
commit fc65f70
Showing
10 changed files
with
165 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
app/src/client/components/MarketingEmailPreferenceSwitcher.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import React, { useState } from 'react'; | ||
|
||
import { updateCurrentUser } from 'wasp/client/operations'; | ||
import NotificationBox from './NotificationBox'; | ||
|
||
export function MarketingEmailPreferenceSwitcher({ | ||
hasSubscribedToMarketingEmails, | ||
}: { | ||
hasSubscribedToMarketingEmails: boolean; | ||
}) { | ||
const [status, setStatus] = useState(hasSubscribedToMarketingEmails); | ||
const [hasChanged, setHasChanged] = useState(false); | ||
const [notificationType, setNotificationType] = useState<string | null>(null); | ||
|
||
const handleChange = (e: React.ChangeEvent<HTMLInputElement>) => { | ||
setStatus(e.target.value === 'Yes'); | ||
setHasChanged(true); | ||
}; | ||
|
||
const onClick = () => { | ||
setNotificationType(null); | ||
}; | ||
|
||
const handleClick = async (status: boolean) => { | ||
try { | ||
await updateCurrentUser({ hasSubscribedToMarketingEmails: status }); | ||
setNotificationType('success'); | ||
} catch (e) { | ||
setNotificationType('error'); | ||
} | ||
setHasChanged(false); | ||
}; | ||
|
||
const notificationMsg = | ||
notificationType === 'success' | ||
? 'Your changes are saved successfully.' | ||
: 'Something went wrong. Please try again later.'; | ||
|
||
return ( | ||
<> | ||
{notificationType && ( | ||
<NotificationBox type={notificationType as 'success' | 'error'} onClick={onClick} message={notificationMsg} /> | ||
)} | ||
<div className='mt-1 text-sm text-captn-dark-blue sm:col-span-1 sm:mt-0'> | ||
<label className='mr-4'> | ||
<input | ||
type='radio' | ||
value='Yes' | ||
checked={status} | ||
onChange={handleChange} | ||
className='form-radio text-captn-light-blue mr-2 focus:ring-1 outline-none' | ||
/> | ||
Yes | ||
</label> | ||
<label> | ||
<input | ||
type='radio' | ||
value='No' | ||
checked={!status} | ||
onChange={handleChange} | ||
className='form-radio text-captn-light-blue mr-2 focus:ring-1 outline-none' | ||
/> | ||
No | ||
</label> | ||
</div> | ||
|
||
<div className='ml-0 md:ml-4 flex-shrink-0 sm:col-span-1 sm:mt-0' style={{ height: '24px' }}> | ||
<button | ||
onClick={() => handleClick(status)} | ||
disabled={!hasChanged} | ||
className={`mt-4 md:-mt-10 no-underline rounded-md px-3.5 py-2.5 text-sm text-airt-font-base ring-1 ring-inset ring-gray-200 shadow-sm focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-indigo-600 dark:text-captn-light-cream bg-airt-primary hover:bg-opacity-85 ${ | ||
!hasChanged ? 'opacity-40 cursor-not-allowed' : '' | ||
}`} | ||
> | ||
Save | ||
</button> | ||
</div> | ||
</> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import React from 'react'; | ||
|
||
interface NotificationBoxProps { | ||
type: 'success' | 'error'; | ||
message: string; | ||
onClick: () => void; | ||
} | ||
|
||
const NotificationBox: React.FC<NotificationBoxProps> = ({ type, message, onClick }) => { | ||
const isSuccess = type === 'success'; | ||
|
||
return ( | ||
<div className='fixed inset-0 flex items-center justify-center z-50 p-16 backdrop-blur-sm bg-airt-font-base/30'> | ||
<div className='bg-airt-primary rounded-lg shadow-lg p-8 m-4 max-w-sm mx-auto'> | ||
<h2 className='text-xl font-bold mb-4 text-airt-font-base'>{isSuccess ? 'Success' : 'Error'}</h2> | ||
<p className='text-airt-font-base'>{message}</p> | ||
<div className='mt-4 text-right'> | ||
<button | ||
onClick={onClick} | ||
className={`py-2 px-4 rounded text-airt-font-base focus:outline-none ${ | ||
isSuccess ? 'bg-airt-secondary' : 'bg-airt-secondary' | ||
}`} | ||
> | ||
OK | ||
</button> | ||
</div> | ||
</div> | ||
</div> | ||
); | ||
}; | ||
|
||
export default NotificationBox; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.