-
Notifications
You must be signed in to change notification settings - Fork 137
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
Use async actions for token status page #526
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
bd176d0
Use asyncactionsfor token status page
daltonfury42 ae2b981
Prediodic refresh for getToken
daltonfury42 8f688aa
Avoid clearning on pending request
daltonfury42 f5a6718
Merge branch 'master' of github.com-df42:SimplQ/simplQ-frontend into …
daltonfury42 31188e9
deleteToken
daltonfury42 5ecd48b
loaded flag in store, refactor (#527)
daltonfury42 0d2e91e
use <LoadingStatus />
daltonfury42 43eabc4
Merge branch 'async-actions-token-status' of github.com-df42:SimplQ/s…
daltonfury42 d301368
Merge branch 'master' of github.com-df42:SimplQ/simplQ-frontend into …
daltonfury42 8dc91f0
use string action names
daltonfury42 65e2c1b
remove console.log
daltonfury42 bb8f29a
use useCallback when needed, from components
daltonfury42 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 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,46 @@ | ||
import React from 'react'; | ||
import LoadingIndicator from 'components/common/LoadingIndicator'; | ||
import Button from 'components/common/Button'; | ||
import { useDispatch, useSelector } from 'react-redux'; | ||
import { useGetToken } from 'store/asyncActions'; | ||
import { selectToken } from 'store/token'; | ||
import LoadingStatus from 'components/common/Loading/LoadingStatus'; | ||
import Button from '../../common/Button'; | ||
import styles from './status.module.scss'; | ||
|
||
export default (props) => { | ||
export default () => { | ||
const token = useSelector(selectToken); | ||
const dispatch = useDispatch(); | ||
const getToken = useGetToken(); | ||
|
||
const onRefreshClick = () => { | ||
dispatch(getToken({ tokenId: token.tokenId })); | ||
}; | ||
|
||
let status = null; | ||
if (props.updateInProgress) { | ||
status = null; | ||
} else if (props.tokenStatus === 'REMOVED') { | ||
if (token.tokenStatus === 'REMOVED') { | ||
status = <p>You have been removed from the queue, have a nice day</p>; | ||
} else if (props.tokenStatus === 'NOTIFIED') { | ||
} else if (token.tokenStatus === 'NOTIFIED') { | ||
status = <p>Your turn is up, please proceed to the counter</p>; | ||
} else if (props.aheadCount === 0) { | ||
} else if (token.aheadCount === 0) { | ||
status = <p>There is no one ahead of you. Please wait to be notified by the queue manager.</p>; | ||
} else { | ||
/* eslint-disable react/jsx-one-expression-per-line */ | ||
status = ( | ||
<> | ||
<p>Hello {props.name}, </p> | ||
<p>Hello {token.name}, </p> | ||
<br /> | ||
<p>People waiting in front of you:</p> | ||
<p className={styles['count']}>{props.aheadCount}</p> | ||
<p className={styles['count']}>{token.aheadCount}</p> | ||
<p>Please wait for your turn. You will be notified here.</p> | ||
<div className={styles['refresh-button']}> | ||
<Button onClick={props.update}>Refresh status</Button> | ||
<Button onClick={onRefreshClick}>Refresh status</Button> | ||
</div> | ||
</> | ||
); | ||
} | ||
|
||
if (!status) return <LoadingIndicator />; | ||
|
||
return <div className={styles['status-box']}>{status}</div>; | ||
return ( | ||
<div className={styles['status-box']}> | ||
<LoadingStatus dependsOn="getToken">{status}</LoadingStatus> | ||
</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
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 |
---|---|---|
@@ -1,15 +1,24 @@ | ||
import LoadingStatus from 'components/common/Loading'; | ||
import React from 'react'; | ||
import { useSelector } from 'react-redux'; | ||
import { selectToken } from 'store/token'; | ||
import styles from './status.module.scss'; | ||
|
||
export default (props) => ( | ||
<div className={styles['token-number']}> | ||
<div className={styles['hanging-threads']} /> | ||
<div className={styles['token-container']}> | ||
<span className={styles['token']}>Token No</span> | ||
</div> | ||
<div className={styles['separator']} /> | ||
<div className={styles['count-container']}> | ||
<span className={styles['count']}>{props.tokenNumber}</span> | ||
export default () => { | ||
const token = useSelector(selectToken); | ||
|
||
return ( | ||
<div className={styles['token-number']}> | ||
<div className={styles['hanging-threads']} /> | ||
<div className={styles['token-container']}> | ||
<span className={styles['token']}>Token No</span> | ||
</div> | ||
<div className={styles['separator']} /> | ||
<div className={styles['count-container']}> | ||
<span className={styles['count']}> | ||
<LoadingStatus dependsOn="getToken">{token.tokenNumber}</LoadingStatus> | ||
</span> | ||
</div> | ||
</div> | ||
</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 |
---|---|---|
@@ -1,76 +1,35 @@ | ||
import React, { useState, useEffect, useCallback } from 'react'; | ||
import HeaderSection from 'components/common/HeaderSection'; | ||
import LoadingIndicator from 'components/common/LoadingIndicator'; | ||
import { TokenRequestFactory } from 'api/requestFactory'; | ||
import useRequest from 'api/useRequest'; | ||
import { notify } from 'services/notification'; | ||
import React, { useEffect, useCallback } from 'react'; | ||
|
||
import { useDispatch, useSelector } from 'react-redux'; | ||
import { useGetToken } from 'store/asyncActions'; | ||
import { selectToken } from 'store/token'; | ||
import styles from './status.module.scss'; | ||
import StatusContainer from './StatusContainer'; | ||
import StatusSidePanel from './StatusSidePanel'; | ||
import TokenNumber from './TokenNumber'; | ||
|
||
const TIMEOUT = 10000; | ||
let timeoutId; | ||
|
||
// TODO Rename component to token status, component and folder/page name | ||
function QueueStatus(props) { | ||
const tokenId = props.match.params.tokenId; | ||
const [tokenStatusResponse, setTokenStatusResponse] = useState(); | ||
const [updateInProgress, setUpdateInProgress] = useState(false); | ||
const { requestMaker } = useRequest(); | ||
|
||
const showNotification = useCallback(() => { | ||
notify(`${tokenStatusResponse.queueName}: You've been notified by the queue manager.`); | ||
}, [tokenStatusResponse]); | ||
|
||
const oldTokenStatus = tokenStatusResponse ? tokenStatusResponse.tokenStatus : undefined; | ||
const update = useCallback(() => { | ||
clearTimeout(timeoutId); | ||
requestMaker(TokenRequestFactory.get(tokenId)).then((response) => { | ||
if (response) { | ||
setTokenStatusResponse(response); | ||
if (response.tokenStatus === 'NOTIFIED' && oldTokenStatus === 'WAITING') { | ||
showNotification(); | ||
} | ||
} | ||
timeoutId = setTimeout(update, TIMEOUT); | ||
}); | ||
// eslint-disable-next-line | ||
}, [tokenId, oldTokenStatus]); // don't add showNotification, will result in infinite loop | ||
const dispatch = useDispatch(); | ||
const token = useSelector(selectToken); | ||
const getToken = useCallback(useGetToken(), []); | ||
|
||
useEffect(() => { | ||
update(); | ||
return () => clearTimeout(timeoutId); | ||
}, [update]); | ||
|
||
const onDeleteClick = async () => { | ||
setUpdateInProgress(true); | ||
await requestMaker(TokenRequestFactory.remove(tokenId)).then((response) => { | ||
if (response) { | ||
setTokenStatusResponse({ ...tokenStatusResponse, tokenStatus: response.tokenStatus }); | ||
} | ||
setUpdateInProgress(false); | ||
}); | ||
}; | ||
|
||
if (!tokenStatusResponse) { | ||
return <LoadingIndicator />; | ||
} | ||
dispatch(getToken({ tokenId, refresh: true })); | ||
}, [tokenId, dispatch, getToken]); | ||
|
||
return ( | ||
<> | ||
<HeaderSection queueName={tokenStatusResponse.queueName} /> | ||
<HeaderSection queueName={token ? token.queueName : 'Loading...'} /> | ||
<div className={styles['main-body']}> | ||
<TokenNumber tokenNumber={tokenStatusResponse.tokenNumber} /> | ||
<StatusContainer | ||
name={tokenStatusResponse.name} | ||
updateInProgress={updateInProgress} | ||
tokenStatus={tokenStatusResponse.tokenStatus} | ||
aheadCount={tokenStatusResponse.aheadCount} | ||
update={update} | ||
/> | ||
<StatusSidePanel leaveQueueHandler={onDeleteClick} queueId={tokenStatusResponse.queueId} /> | ||
<TokenNumber /> | ||
<StatusContainer /> | ||
<StatusSidePanel /> | ||
</div> | ||
</> | ||
); | ||
} | ||
|
||
export default QueueStatus; |
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,36 @@ | ||
import { createAsyncThunk } from '@reduxjs/toolkit'; | ||
import { useMakeAuthedRequest } from 'api/auth'; | ||
import * as RequestFactory from 'api/requestFactory'; | ||
import { useDispatch } from 'react-redux'; | ||
import { useHistory } from 'react-router'; | ||
import { setInfoPopupMessage } from 'store/appSlice'; | ||
|
||
const typePrefix = 'deleteToken/action'; | ||
|
||
/** | ||
* A hook to access the deleteToken async action creator. | ||
* | ||
* @returns — deleteToken async action creator | ||
*/ | ||
const useDeleteToken = () => { | ||
const makeAuthedRequest = useMakeAuthedRequest(); | ||
const history = useHistory(); | ||
const dispatch = useDispatch(); | ||
|
||
const deleteToken = createAsyncThunk(typePrefix, async (arg) => { | ||
const { tokenId } = arg; | ||
const authedRequest = makeAuthedRequest(RequestFactory.deleteToken(tokenId)); | ||
const response = await authedRequest; | ||
dispatch(setInfoPopupMessage('Successfully left queue')); | ||
if (arg.goHome) { | ||
history.push('/'); | ||
} | ||
return response; | ||
}); | ||
|
||
return deleteToken; | ||
}; | ||
|
||
const deleteToken = createAsyncThunk(typePrefix); | ||
|
||
export { deleteToken, useDeleteToken }; |
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,36 @@ | ||
import { createAsyncThunk } from '@reduxjs/toolkit'; | ||
import useAuth, { makeAuthedRequest } from 'api/auth'; | ||
import * as RequestFactory from 'api/requestFactory'; | ||
|
||
const typePrefix = 'getToken/action'; | ||
let timer = null; | ||
const REFRESH_INTERVAL = 3000; | ||
|
||
/** | ||
* A hook to access the getToken async action creator. | ||
* | ||
* @returns — getToken async action creator | ||
*/ | ||
const useGetToken = () => { | ||
const auth = useAuth(); | ||
|
||
const getToken = createAsyncThunk(typePrefix, async ({ tokenId, refresh }, { dispatch }) => { | ||
if (timer) { | ||
clearTimeout(timer); | ||
} | ||
const authedRequest = makeAuthedRequest(auth, RequestFactory.getToken(tokenId)); | ||
const response = await authedRequest.then((resp) => { | ||
if (refresh === true) { | ||
timer = setTimeout(() => dispatch(getToken({ tokenId, refresh })), REFRESH_INTERVAL); | ||
} | ||
return resp; | ||
}); | ||
return response; | ||
}); | ||
|
||
return getToken; | ||
daltonfury42 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}; | ||
|
||
const getToken = createAsyncThunk(typePrefix); | ||
|
||
export { getToken, useGetToken }; |
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
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.
getToken()
here might cause fetch request on every render. Not 100% sure, please check, network tab, or just put someconosle.log()
statement to see what's going on.UPDATE: Just noticed it was don in action. I think it should be done here.
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.
Why do you think we should do here? I thought it's better to do it once inside the action than do it everytime the action than every time it is used?
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.
Let's merge this as is. It will make maintenance easier, and if we can polish it 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.
I've standardised for now: bb8f29a
The PR will be merged on your approval.