-
Notifications
You must be signed in to change notification settings - Fork 4
Feat/user timezone #908
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
Merged
Merged
Feat/user timezone #908
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
c9adbe6
chore: add timezone lib
lissavxo 3c97062
feat: timezone selector
lissavxo 5e83b2b
feat: update timezone endpoint
lissavxo ed6b76c
feat: implement timezone option
lissavxo 05289ae
fix: timezone clear cache
lissavxo 564dff8
fix: use moment timezone
lissavxo 5525100
Merge branch 'master' into feat/user-timezone
chedieck e660b2c
fix: timezone count payments
lissavxo 69644a3
feat: timezone update feedback
lissavxo d604690
refactor: clean up
lissavxo 1a96fdd
refactor: fix timezone error
lissavxo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,67 @@ | ||
| import React, { useState } from 'react' | ||
| import Select from 'react-timezone-select' | ||
| import style from './timezone-selector.module.css' | ||
|
|
||
| interface TimezoneSelectorProps { | ||
| value: string | ||
| onChange: (value: string) => void | ||
| label?: string | ||
| isEditable?: boolean | ||
| } | ||
|
|
||
| const TimezoneSelector: React.FC<TimezoneSelectorProps> = ({ value }) => { | ||
| const [selectedTimezone, setSelectedTimezone] = useState(value !== '' ? value : '') | ||
| const [error, setError] = useState('') | ||
| const [success, setSuccess] = useState('') | ||
|
|
||
| const handleChange = (selectedOption: any): void => { | ||
| const updateTimezone = async (): Promise<void> => { | ||
| const oldTimezone = selectedTimezone | ||
| try { | ||
| const res = await fetch('/api/user/timezone', { | ||
| method: 'PUT', | ||
| headers: { | ||
| 'Content-Type': 'application/json' | ||
| }, | ||
| body: JSON.stringify({ timezone: selectedOption.value }) | ||
| }) | ||
| if (res.status === 200) { | ||
| setSelectedTimezone(selectedOption.value) | ||
| setError('') | ||
| setSuccess('Timezone updated successfully.') | ||
| } else { | ||
| setSuccess('') | ||
| setError('Failed to update timezone.') | ||
| setSelectedTimezone(oldTimezone) | ||
| } | ||
| } catch (err: any) { | ||
| setSuccess('') | ||
| setError('Failed to update timezone.') | ||
| setSelectedTimezone(oldTimezone) | ||
| } finally { | ||
| setTimeout(() => { | ||
| setSuccess('') | ||
| setError('') | ||
| }, 3000) | ||
| } | ||
| } | ||
|
|
||
| void updateTimezone() | ||
| } | ||
|
|
||
| return ( | ||
| <div className={style.timezone_selector}> | ||
| <Select | ||
| value={selectedTimezone} | ||
| onChange={handleChange} | ||
| disabled={false} | ||
| className={style.select_timezone} | ||
| displayValue="UTC" | ||
| /> | ||
| {error !== '' && <span className={style.error_message}> {error} </span>} | ||
| {success !== '' && <span className={style.success_message}> {success} </span>} | ||
| </div> | ||
| ) | ||
| } | ||
|
|
||
| export default TimezoneSelector | ||
This file contains hidden or 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,40 @@ | ||
| .timezone_selector { | ||
| display: flex; | ||
| flex-direction: column; | ||
| gap: 10px; | ||
| margin: 15px 0; | ||
| padding: 10px; | ||
| border: 1px solid var(--secondary-bg-color); | ||
| border-radius: 5px; | ||
| background-color: var(--secondary-bg-color); | ||
| } | ||
|
|
||
| .select_timezone { | ||
| background-color: var(--secondary-bg-color) !important; | ||
| color: black; | ||
| font-family: 'Poppins', sans-serif; | ||
| } | ||
|
|
||
| .success_message { | ||
| color: green; | ||
| font-size: 16px; | ||
| width: 100%; | ||
| height: 100%; | ||
| background-color: var(--secondary-bg-color); | ||
| top: 0; | ||
| left: 0; | ||
| display: flex; | ||
| align-items: center; | ||
| justify-content: center; | ||
| border-radius: 10px; | ||
| } | ||
|
|
||
| .error_message { | ||
| color: red; | ||
| font-size: 14px; | ||
| top: -20px; | ||
| left: 0; | ||
| background-color: rgba(255, 0, 0, 0.1); | ||
| padding: 1px 10px; | ||
| border-radius: 5px; | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -1,11 +1,17 @@ | ||
| import { CacheGet } from 'redis/index' | ||
| import { fetchUserProfileFromId } from 'services/userService' | ||
| import { setSession } from 'utils/setSession' | ||
|
|
||
| export default async (req: any, res: any): Promise<void> => { | ||
| if (req.method === 'GET') { | ||
| await setSession(req, res) | ||
| const userId = req.session.userId | ||
| const resJSON = await CacheGet.dashboardData(userId) | ||
| const userReqTimezone = req.headers.timezone as string | ||
| const userProfile = await fetchUserProfileFromId(userId) | ||
| const userPreferredTimezone = userProfile?.preferredTimezone | ||
| const timezone = userPreferredTimezone !== '' ? userPreferredTimezone : userReqTimezone | ||
|
|
||
| const resJSON = await CacheGet.dashboardData(userId, timezone) | ||
| res.status(200).json(resJSON) | ||
| } | ||
| } |
This file contains hidden or 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 |
|---|---|---|
| @@ -1,12 +1,16 @@ | ||
| import { CacheGet } from 'redis/index' | ||
| import { fetchUserProfileFromId } from 'services/userService' | ||
| import { setSession } from 'utils/setSession' | ||
|
|
||
| export default async (req: any, res: any): Promise<void> => { | ||
| if (req.method === 'GET') { | ||
| await setSession(req, res) | ||
| const userId = req.session.userId | ||
|
|
||
| const resJSON = await CacheGet.paymentsCount(userId) | ||
| const userReqTimezone = req.headers.timezone as string | ||
| const userProfile = await fetchUserProfileFromId(userId) | ||
| const userPreferredTimezone = userProfile?.preferredTimezone | ||
| const timezone = userPreferredTimezone !== '' ? userPreferredTimezone : userReqTimezone | ||
| const resJSON = await CacheGet.paymentsCount(userId, timezone) | ||
| res.status(200).json(resJSON) | ||
| } | ||
| } |
This file contains hidden or 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,19 @@ | ||
| import { setSession } from 'utils/setSession' | ||
| import * as userService from 'services/userService' | ||
| import { parseUpdateUserTimezonePUTRequest } from 'utils/validators' | ||
| import { clearDashboardCache } from 'redis/dashboardCache' | ||
|
|
||
| export default async ( | ||
| req: any, | ||
| res: any | ||
| ): Promise<void> => { | ||
| await setSession(req, res, true) | ||
|
|
||
| if (req.method === 'PUT') { | ||
| const session = req.session | ||
| const preferredTimezone = parseUpdateUserTimezonePUTRequest(req.body) | ||
| const user = await userService.updatePreferredTimezone(session.userId, preferredTimezone.timezone) | ||
| await clearDashboardCache(session.userId) | ||
| res.status(200).json(user) | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this should actually be at the else of the
ifabove. If the api runs into an error, it will return a status code 500 response message, not throw here.