-
Notifications
You must be signed in to change notification settings - Fork 0
feat(ui): require organization selection before entering pages #63
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
Open
Thorben-D
wants to merge
1
commit into
main
Choose a base branch
from
force_taxonomy
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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
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 | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,242 @@ | ||||||
| import React, { ReactElement, useEffect } from 'react'; | ||||||
| import { | ||||||
| useForemanOrganization, | ||||||
| useForemanLocation, | ||||||
| } from 'foremanReact/Root/Context/ForemanContext'; | ||||||
| import { translate as _ } from 'foremanReact/common/I18n'; | ||||||
|
|
||||||
| import { | ||||||
| Card, | ||||||
| CardTitle, | ||||||
| CardBody, | ||||||
| CardFooter, | ||||||
| Bullseye, | ||||||
| ActionList, | ||||||
| ActionListItem, | ||||||
| Button, | ||||||
| Form, | ||||||
| TextVariants, | ||||||
| TextContent, | ||||||
| Text, | ||||||
| } from '@patternfly/react-core'; | ||||||
|
|
||||||
| import axios, { AxiosResponse } from 'axios'; | ||||||
| import { foremanUrl } from 'foremanReact/common/helpers'; | ||||||
| import { addToast } from 'foremanReact/components/ToastsList'; | ||||||
| import { useDispatch } from 'react-redux'; | ||||||
|
|
||||||
| import { TaxonSelector } from './components/TaxonSelector'; | ||||||
|
|
||||||
| interface ForceTaxonomyProps { | ||||||
| organization?: boolean; | ||||||
| location?: boolean; | ||||||
| children: React.ReactNode; | ||||||
| } | ||||||
|
|
||||||
| export const ForceTaxonomy = ({ | ||||||
| organization, | ||||||
| location = false, | ||||||
| children = false, | ||||||
| }: ForceTaxonomyProps): ReactElement => { | ||||||
| const [selectedOrganization, setSelectedOrganization] = React.useState< | ||||||
| string | ||||||
| >(''); | ||||||
| const [selectedLocation, setSelectedLocation] = React.useState<string>(''); | ||||||
|
|
||||||
| const currentOrganization = useForemanOrganization(); | ||||||
| const currentLocation = useForemanLocation(); | ||||||
|
|
||||||
| const organizationUpdateRequired = | ||||||
| organization && currentOrganization === undefined; | ||||||
| const locationUpdateRequired = location && currentLocation === undefined; | ||||||
|
|
||||||
| useEffect(() => { | ||||||
| if (organizationUpdateRequired && locationUpdateRequired) { | ||||||
| document.title = _('Organization/Location required!'); | ||||||
| } else if (locationUpdateRequired) { | ||||||
| document.title = _('Location required!'); | ||||||
| } else if (organizationUpdateRequired) { | ||||||
| document.title = _('Organization required!'); | ||||||
| } | ||||||
| }, [organizationUpdateRequired, locationUpdateRequired]); | ||||||
|
|
||||||
| const dispatch = useDispatch(); | ||||||
|
|
||||||
| const card = (content: ReactElement): ReactElement => ( | ||||||
| <Bullseye> | ||||||
| <Card ouiaId="BasicCard" style={{ minHeight: '30vh', minWidth: '50vh' }}> | ||||||
| {content} | ||||||
| </Card> | ||||||
| </Bullseye> | ||||||
| ); | ||||||
|
|
||||||
| const updateTaxonomy = async (): Promise<void> => { | ||||||
| const updateTaxon = async ({ | ||||||
| type, | ||||||
| endpoint, | ||||||
| }: { | ||||||
| type: 'organization' | 'location'; | ||||||
| endpoint: string; | ||||||
| }): Promise<void> => { | ||||||
| try { | ||||||
| await axios.get(foremanUrl(endpoint)); | ||||||
| dispatch( | ||||||
| addToast({ | ||||||
| type: 'success', | ||||||
| key: `UPDATE_CURRENT_${type.toUpperCase()}_SUCC`, | ||||||
| message: `Successfully updated the current ${type}!`, | ||||||
| sticky: false, | ||||||
| }) | ||||||
| ); | ||||||
| } catch (e) { | ||||||
| dispatch( | ||||||
| addToast({ | ||||||
| type: 'danger', | ||||||
| key: `UPDATE_CURRENT_${type.toUpperCase()}_ERR`, | ||||||
| message: `Updating the current ${type} failed with error code "${ | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The message needs a translation. |
||||||
| (e as { | ||||||
| response: AxiosResponse; | ||||||
| }).response.status | ||||||
| }".`, | ||||||
| sticky: false, | ||||||
| }) | ||||||
| ); | ||||||
| } | ||||||
| }; | ||||||
|
|
||||||
| await Promise.all([ | ||||||
| organizationUpdateRequired && | ||||||
| updateTaxon({ | ||||||
| type: 'organization', | ||||||
| endpoint: `/organizations/${selectedOrganization}/select`, | ||||||
| }), | ||||||
| locationUpdateRequired && | ||||||
| updateTaxon({ | ||||||
| type: 'location', | ||||||
| endpoint: `/locations/${selectedLocation}/select`, | ||||||
| }), | ||||||
| ]); | ||||||
|
|
||||||
| // Unfortunately, the context is hydrated during the initial mounting of the root React App. | ||||||
| // There is currently no mechanism to refresh it out of tree. Therefore, a reload is needed here. | ||||||
| window.location.reload(); | ||||||
| }; | ||||||
|
|
||||||
| if (organizationUpdateRequired && locationUpdateRequired) { | ||||||
| return card( | ||||||
| <> | ||||||
| <CardTitle> | ||||||
| <TextContent> | ||||||
| <Text component={TextVariants.h2}> | ||||||
| {_('An organization and a location are required to proceed.')} | ||||||
| </Text> | ||||||
| </TextContent> | ||||||
| </CardTitle> | ||||||
| <CardBody> | ||||||
| <Form> | ||||||
| <TaxonSelector | ||||||
| type="organization" | ||||||
| selected={selectedOrganization} | ||||||
| onSelect={taxonId => setSelectedOrganization(taxonId)} | ||||||
| /> | ||||||
| <TaxonSelector | ||||||
| type="location" | ||||||
| selected={selectedLocation} | ||||||
| onSelect={taxonId => setSelectedLocation(taxonId)} | ||||||
| /> | ||||||
| </Form> | ||||||
| </CardBody> | ||||||
| <CardFooter> | ||||||
| <ActionList> | ||||||
| <ActionListItem> | ||||||
| <Button | ||||||
| variant="primary" | ||||||
| id="single-group-next-button" | ||||||
| isDisabled={ | ||||||
| selectedOrganization === '' || selectedLocation === '' | ||||||
| } | ||||||
| onClick={() => updateTaxonomy()} | ||||||
| > | ||||||
| {_('Save')} | ||||||
| </Button> | ||||||
| </ActionListItem> | ||||||
| </ActionList> | ||||||
| </CardFooter> | ||||||
| </> | ||||||
| ); | ||||||
| } else if (locationUpdateRequired) { | ||||||
| return card( | ||||||
| <> | ||||||
| <CardTitle> | ||||||
| <TextContent> | ||||||
| <Text component={TextVariants.h2}> | ||||||
| {_('A location is required to proceed.')} | ||||||
| </Text> | ||||||
| </TextContent> | ||||||
| </CardTitle> | ||||||
| <CardBody> | ||||||
| <Form> | ||||||
| <TaxonSelector | ||||||
| type="location" | ||||||
| selected={selectedLocation} | ||||||
| onSelect={taxonId => { | ||||||
| setSelectedLocation(taxonId); | ||||||
| }} | ||||||
| /> | ||||||
| </Form> | ||||||
| </CardBody> | ||||||
| <CardFooter> | ||||||
| <ActionList> | ||||||
| <ActionListItem> | ||||||
| <Button | ||||||
| variant="primary" | ||||||
| id="single-group-next-button" | ||||||
| isDisabled={selectedLocation === ''} | ||||||
| onClick={() => updateTaxonomy()} | ||||||
| > | ||||||
| {_('Save')} | ||||||
| </Button> | ||||||
| </ActionListItem> | ||||||
| </ActionList> | ||||||
| </CardFooter> | ||||||
| </> | ||||||
| ); | ||||||
| } else if (organizationUpdateRequired) { | ||||||
| return card( | ||||||
| <> | ||||||
| <CardTitle> | ||||||
| <TextContent> | ||||||
| <Text component={TextVariants.h2}> | ||||||
| {_('An organization is required to proceed.')} | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Please check if all of those texts end with a dot or not. Same suggestion applies to line 173 and 131. |
||||||
| </Text> | ||||||
| </TextContent> | ||||||
| </CardTitle> | ||||||
| <CardBody> | ||||||
| <Form> | ||||||
| <TaxonSelector | ||||||
| type="organization" | ||||||
| selected={selectedOrganization} | ||||||
| onSelect={taxonId => setSelectedOrganization(taxonId)} | ||||||
| /> | ||||||
| </Form> | ||||||
| </CardBody> | ||||||
| <CardFooter> | ||||||
| <ActionList> | ||||||
| <ActionListItem> | ||||||
| <Button | ||||||
| variant="primary" | ||||||
| id="single-group-next-button" | ||||||
| isDisabled={selectedOrganization === ''} | ||||||
| onClick={() => updateTaxonomy()} | ||||||
| > | ||||||
| {_('Save')} | ||||||
| </Button> | ||||||
| </ActionListItem> | ||||||
| </ActionList> | ||||||
| </CardFooter> | ||||||
| </> | ||||||
| ); | ||||||
| } | ||||||
|
|
||||||
| return <>{children}</>; | ||||||
| }; | ||||||
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,81 @@ | ||
| import React, { ReactElement } from 'react'; | ||
|
|
||
| import { useAPI } from 'foremanReact/common/hooks/API/APIHooks'; | ||
| import { foremanUrl } from 'foremanReact/common/helpers'; | ||
| import { translate as _, sprintf as __ } from 'foremanReact/common/I18n'; | ||
| import { | ||
| EmptyState, | ||
| EmptyStateHeader, | ||
| EmptyStateIcon, | ||
| FormGroup, | ||
| FormSelect, | ||
| FormSelectOption, | ||
| Spinner, | ||
| } from '@patternfly/react-core'; | ||
|
|
||
| import { Taxon } from '../../../types/common'; | ||
|
|
||
| interface TaxonSelectorProps { | ||
| type: 'organization' | 'location'; | ||
| selected: string; | ||
| onSelect: (taxonId: string) => void; | ||
| } | ||
|
|
||
| export const TaxonSelector = ({ | ||
| type, | ||
| selected, | ||
| onSelect, | ||
| }: TaxonSelectorProps): ReactElement => { | ||
| const taxonResponse = useAPI<{ results: Taxon[] }>( | ||
| 'get', | ||
| foremanUrl(`/api/v2/${type}s`) | ||
| ); | ||
|
|
||
| if (taxonResponse.status === 'ERROR') { | ||
| // TODO: Handle error | ||
| } else if (taxonResponse.status === 'RESOLVED') { | ||
| return ( | ||
| <FormGroup | ||
| label={type === 'organization' ? _('Organization') : _('Location')} | ||
| isRequired | ||
| > | ||
| <FormSelect | ||
| value={selected} | ||
| onChange={(_event, value) => { | ||
| onSelect(value); | ||
| }} | ||
| > | ||
| {[ | ||
| <FormSelectOption | ||
| key="placeholder" | ||
| value="" | ||
| label={ | ||
| type === 'organization' | ||
| ? _('Select an organization') | ||
| : _('Select a location') | ||
| } | ||
| isPlaceholder | ||
| />, | ||
| ...taxonResponse.response.results.map(taxon => ( | ||
| <FormSelectOption | ||
| key={taxon.id} | ||
| value={taxon.id} | ||
| label={taxon.title} | ||
| /> | ||
| )), | ||
| ]} | ||
| </FormSelect> | ||
| </FormGroup> | ||
| ); | ||
| } | ||
|
|
||
| return ( | ||
| <EmptyState> | ||
| <EmptyStateHeader | ||
| titleText={__(_('Loading %(taxon)s information...'), { taxon: type })} | ||
| headingLevel="h4" | ||
| icon={<EmptyStateIcon icon={Spinner} />} | ||
| /> | ||
| </EmptyState> | ||
| ); | ||
| }; |
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,11 @@ | ||
| export interface Organization { | ||
| id: number; | ||
| title: string; | ||
| } | ||
|
|
||
| export interface Location { | ||
| id: number; | ||
| title: string; | ||
| } | ||
|
|
||
| export interface Taxon extends Organization, Location {} |
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.
The message needs a translation.