-
Notifications
You must be signed in to change notification settings - Fork 429
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
displays info message with link to latest change request
- Loading branch information
1 parent
ca5ed73
commit c3d246f
Showing
2 changed files
with
77 additions
and
32 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,54 +1,97 @@ | ||
import { FC, useRef } from 'react' | ||
import { useGetChangeRequestsQuery } from 'common/services/useChangeRequest' | ||
import WarningMessage from './WarningMessage' | ||
import moment from 'moment' | ||
import InfoMessage from './InfoMessage' | ||
import { RouterChildContext } from 'react-router-dom' | ||
import Button from './base/forms/Button' | ||
|
||
type ExistingChangeRequestAlertType = { | ||
projectId: number | string | ||
environmentId: string | ||
featureId: number | ||
className?: string | ||
history: RouterChildContext['router']['history'] | ||
} | ||
|
||
const ExistingChangeRequestAlert: FC<ExistingChangeRequestAlertType> = ({ | ||
className, | ||
environmentId, | ||
featureId, | ||
history, | ||
projectId, | ||
}) => { | ||
const { data } = useGetChangeRequestsQuery({ | ||
committed: false, | ||
environmentId, | ||
feature_id: featureId, | ||
}) | ||
const date = useRef(moment().toISOString()) | ||
const { data: scheduledChangeRequests } = useGetChangeRequestsQuery({ | ||
environmentId, | ||
feature_id: featureId, | ||
live_from_after: date.current, | ||
}) | ||
|
||
if (scheduledChangeRequests?.results?.length) { | ||
return ( | ||
<div className={className}> | ||
<WarningMessage | ||
warningMessage={ | ||
'You have scheduled changes upcoming for this feature, please check the Scheduling page.' | ||
} | ||
/> | ||
</div> | ||
|
||
const { data: changeRequests } = useGetChangeRequestsQuery( | ||
{ | ||
committed: false, | ||
environmentId, | ||
feature_id: featureId, | ||
}, | ||
{ refetchOnMountOrArgChange: true }, | ||
) | ||
|
||
const { data: scheduledChangeRequests } = useGetChangeRequestsQuery( | ||
{ | ||
environmentId, | ||
feature_id: featureId, | ||
live_from_after: date.current, | ||
}, | ||
{ refetchOnMountOrArgChange: true }, | ||
) | ||
|
||
const handleNavigate = () => { | ||
const changes = scheduledChangeRequests?.results?.length | ||
? scheduledChangeRequests?.results | ||
: changeRequests?.results | ||
const latestChangeRequest = changes?.at(-1)?.id | ||
closeModal() | ||
history.push( | ||
`/project/${projectId}/environment/${environmentId}/change-requests/${latestChangeRequest}`, | ||
) | ||
} | ||
if (data?.results?.length) { | ||
return ( | ||
<div className={className}> | ||
<WarningMessage | ||
warningMessage={ | ||
'You have open change requests for this feature, please check the Change Requests page.' | ||
} | ||
/> | ||
</div> | ||
) | ||
|
||
const getRequestChangeInfoText = ( | ||
hasScheduledChangeRequests: boolean, | ||
hasChangeRequests: boolean, | ||
) => { | ||
if (hasScheduledChangeRequests) { | ||
return [ | ||
'You have scheduled changes upcoming for this feature.', | ||
'to view your scheduled changes.', | ||
] | ||
} | ||
|
||
if (hasChangeRequests) { | ||
return [ | ||
'You have open change requests for this feature.', | ||
'to view your requested changes.', | ||
] | ||
} | ||
} | ||
return null | ||
|
||
const requestChangeInfoText = getRequestChangeInfoText( | ||
!!scheduledChangeRequests?.results?.length, | ||
!!changeRequests?.results?.length, | ||
) | ||
|
||
if (!requestChangeInfoText?.length) { | ||
return null | ||
} | ||
|
||
return ( | ||
<div className={className}> | ||
<InfoMessage> | ||
<span> | ||
{requestChangeInfoText[0]} Click{' '} | ||
<Button onClick={handleNavigate} theme='text'> | ||
here | ||
</Button>{' '} | ||
{requestChangeInfoText[1]} | ||
</span> | ||
</InfoMessage> | ||
</div> | ||
) | ||
} | ||
|
||
export default ExistingChangeRequestAlert |
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