-
Notifications
You must be signed in to change notification settings - Fork 71
THREESCALE-2286: Refactor Service Discovery into a React component #763
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
damianpm
merged 7 commits into
master
from
THREESCALE-2286-Refactoring-Service-Discovery
May 29, 2019
Merged
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
a13cab7
Deleting old service discovery files
be69900
Adding new service discovery react components with flow types
526b0b0
Adding js tests
8815ea4
Moving CSRFToken and fetchData to utils
628b9c5
Fixing cucmbers: adding @javascript tag
8801b86
Improving test coverage
53fc8e7
Better tests descriptions
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
Adding new service discovery react components with flow types
- Loading branch information
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| import {NewServiceFormWrapper} from 'NewService' | ||
| import {safeFromJsonString} from 'utilities/json-utils' | ||
|
|
||
| document.addEventListener('DOMContentLoaded', () => { | ||
| const newServiceWrapper = document.getElementById('new_service_wrapper') | ||
| const newServiceFormProps = safeFromJsonString(newServiceWrapper.dataset.newServiceData) | ||
|
|
||
| NewServiceFormWrapper(newServiceFormProps, 'new_service_wrapper') | ||
| }) |
13 changes: 13 additions & 0 deletions
13
app/javascript/src/NewService/components/FormElements/ErrorMessage.jsx
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,13 @@ | ||
| // @flow | ||
|
|
||
| import React from 'react' | ||
|
|
||
| const ErrorMessage = ({fetchErrorMessage}: { | ||
| fetchErrorMessage: string | ||
| }) => <p className='errorMessage'> | ||
| {`Sorry, your request has failed with the error: ${fetchErrorMessage}`} | ||
| </p> | ||
|
|
||
| export { | ||
| ErrorMessage | ||
| } |
38 changes: 38 additions & 0 deletions
38
app/javascript/src/NewService/components/FormElements/FormWrapper.jsx
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,38 @@ | ||
| // @flow | ||
|
|
||
| import React from 'react' | ||
| import type {FormProps} from 'NewService/types' | ||
|
damianpm marked this conversation as resolved.
Outdated
|
||
| import {CSRFToken} from 'utilities/utils' | ||
| import {HiddenServiceDiscoveryInput} from 'NewService/components/FormElements' | ||
|
|
||
| const FormWrapper = (props: FormProps) => { | ||
| const {id, formActionPath, hasHiddenServiceDiscoveryInput, submitText} = props | ||
| return ( | ||
| <form | ||
| className='formtastic service' | ||
| id={id} | ||
| action={formActionPath} | ||
| acceptCharset="UTF-8" | ||
| method="post" | ||
| > | ||
| <input name="utf8" type="hidden" value="✓"/> | ||
| <CSRFToken /> | ||
| {hasHiddenServiceDiscoveryInput && <HiddenServiceDiscoveryInput />} | ||
| <fieldset className="inputs" name="Service"> | ||
| <legend><span>Service</span></legend> | ||
| <ol> | ||
| {props.children} | ||
|
damianpm marked this conversation as resolved.
Outdated
|
||
| </ol> | ||
| </fieldset> | ||
| <fieldset className="buttons"> | ||
| <input | ||
| type="submit" | ||
| name="commit" | ||
| value={submitText} | ||
| className="important-button create"/> | ||
| </fieldset> | ||
| </form> | ||
| ) | ||
| } | ||
|
|
||
| export {FormWrapper} | ||
13 changes: 13 additions & 0 deletions
13
app/javascript/src/NewService/components/FormElements/HiddenServiceDiscoveryInput.jsx
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,13 @@ | ||
| // @flow | ||
|
|
||
| import React from 'react' | ||
|
|
||
| const HiddenServiceDiscoveryInput = () => | ||
| <input | ||
| value='discover' | ||
| type='hidden' | ||
| name='service[source]' | ||
| id='service_source' | ||
| /> | ||
|
|
||
| export {HiddenServiceDiscoveryInput} |
17 changes: 17 additions & 0 deletions
17
app/javascript/src/NewService/components/FormElements/Label.jsx
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,17 @@ | ||
| // @flow | ||
|
|
||
| import React from 'react' | ||
|
|
||
| type Props = { | ||
| htmlFor: string, | ||
| label: string, | ||
| required?: boolean | ||
| } | ||
| const Label = (props: Props) => { | ||
| const {htmlFor, label, required} = props | ||
| return <label htmlFor={htmlFor}>{label} | ||
| {required && <abbr title="required">*</abbr>} | ||
| </label> | ||
| } | ||
|
|
||
| export {Label} |
35 changes: 35 additions & 0 deletions
35
app/javascript/src/NewService/components/FormElements/Select.jsx
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,35 @@ | ||
| // @flow | ||
|
|
||
| import React from 'react' | ||
|
|
||
| type Option = { | ||
| metadata: { | ||
| name: string | ||
| } | ||
| } | ||
|
|
||
| type Props = { | ||
| name: string, | ||
| id: string, | ||
| onChange?: (event: SyntheticEvent<HTMLSelectElement>) => void, | ||
| options: Array<Option> | ||
| } | ||
|
|
||
| const Options = ({options}) => { | ||
| return options.map(option => { | ||
| const { name } = option.metadata | ||
| return <option key={name} value={name}>{name}</option> | ||
| }) | ||
| } | ||
|
|
||
| const Select = ({name, id, onChange, options}: Props) => | ||
| <select | ||
| required="required" | ||
| name={name} | ||
| id={id} | ||
| onChange={onChange} | ||
| > | ||
| {<Options options={options}/>} | ||
| </select> | ||
|
|
||
| export {Select} |
43 changes: 43 additions & 0 deletions
43
app/javascript/src/NewService/components/FormElements/ServiceDiscoveryListItems.jsx
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,43 @@ | ||
| // @flow | ||
|
|
||
| import React from 'react' | ||
| import {Label, Select} from 'NewService/components/FormElements' | ||
|
|
||
| type Props = { | ||
| fetchServices: (namespace: string) => Promise<void>, | ||
| projects: string[], | ||
|
damianpm marked this conversation as resolved.
Outdated
|
||
| services: string[] | ||
| } | ||
|
|
||
| const ServiceDiscoveryListItems = (props: Props) => { | ||
| const {fetchServices, projects, services} = props | ||
| return ( | ||
| <React.Fragment> | ||
| <li id="service_name_input" className="string required"> | ||
| <Label | ||
| htmlFor='namespace' | ||
| label='Namespace' | ||
| /> | ||
| <Select | ||
| name='service[namespace]' | ||
| id='service_namespace' | ||
| onChange={(e) => fetchServices(e.currentTarget.value)} | ||
| options={projects} | ||
| /> | ||
| </li> | ||
| <li> | ||
| <Label | ||
| htmlFor='service_name' | ||
| label='Name' | ||
| /> | ||
| <Select | ||
| name='service[name]' | ||
| id='service_name' | ||
| options={services} | ||
| /> | ||
| </li> | ||
| </React.Fragment> | ||
| ) | ||
| } | ||
|
|
||
| export {ServiceDiscoveryListItems} | ||
36 changes: 36 additions & 0 deletions
36
app/javascript/src/NewService/components/FormElements/ServiceManualListItems.jsx
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,36 @@ | ||
| // @flow | ||
|
|
||
| import React from 'react' | ||
| import {Label} from 'NewService/components/FormElements' | ||
|
|
||
| const ServiceManualListItems = () => | ||
| <React.Fragment> | ||
| <li id="service_name_input" className="string required"> | ||
| <Label | ||
| htmlFor='service_name' | ||
| label='Name' | ||
| required | ||
| /> | ||
| <input maxLength="255" id="service_name" type="text" name="service[name]" autoFocus="autoFocus"/> | ||
| </li> | ||
| <li id="service_system_name_input" className="string required"> | ||
| <Label | ||
| htmlFor='service_system_name' | ||
| label='System name' | ||
| required | ||
| /> | ||
| <input maxLength="255" id="service_system_name" type="text" name="service[system_name]"/> | ||
| <p className="inline-hints"> | ||
| Only ASCII letters, numbers, dashes and underscores are allowed. | ||
| </p> | ||
| </li> | ||
| <li id="service_description_input" className="text optional"> | ||
| <Label | ||
| htmlFor='service_description' | ||
| label='Description' | ||
| /> | ||
| <textarea rows="3" id="service_description" name="service[description]"></textarea> | ||
| </li> | ||
| </React.Fragment> | ||
|
|
||
| export {ServiceManualListItems} |
7 changes: 7 additions & 0 deletions
7
app/javascript/src/NewService/components/FormElements/index.jsx
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,7 @@ | ||
| export {FormWrapper} from 'NewService/components/FormElements/FormWrapper' | ||
| export {HiddenServiceDiscoveryInput} from 'NewService/components/FormElements/HiddenServiceDiscoveryInput' | ||
| export {Label} from 'NewService/components/FormElements/Label' | ||
| export {Select} from 'NewService/components/FormElements/Select' | ||
| export {ErrorMessage} from 'NewService/components/FormElements/ErrorMessage' | ||
| export {ServiceDiscoveryListItems} from 'NewService/components/FormElements/ServiceDiscoveryListItems' | ||
| export {ServiceManualListItems} from 'NewService/components/FormElements/ServiceManualListItems' |
45 changes: 45 additions & 0 deletions
45
app/javascript/src/NewService/components/NewServiceForm.jsx
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,45 @@ | ||
| // @flow | ||
|
|
||
| import React, {useState} from 'react' | ||
|
|
||
| import {ServiceSourceForm, ServiceDiscoveryForm, ServiceManualForm} from 'NewService' | ||
| import {createReactWrapper} from 'utilities/createReactWrapper' | ||
|
|
||
| type Props = { | ||
| isServiceDiscoveryUsable: boolean, | ||
| serviceDiscoveryAuthenticateUrl: string, | ||
| providerAdminServiceDiscoveryServicesPath: string, | ||
| adminServicesPath: string | ||
| } | ||
|
|
||
| const NewServiceForm = (props: Props) => { | ||
|
damianpm marked this conversation as resolved.
Outdated
|
||
| const {isServiceDiscoveryUsable, serviceDiscoveryAuthenticateUrl, | ||
| providerAdminServiceDiscoveryServicesPath, adminServicesPath} = props | ||
|
|
||
| const [formMode, setFormMode] = useState('manual') | ||
|
|
||
| const handleFormsVisibility = (event: SyntheticEvent<HTMLSelectElement>) => { | ||
| setFormMode(event.currentTarget.value) | ||
| } | ||
|
|
||
| const formToRender = () => formMode === 'manual' | ||
| ? <ServiceManualForm formActionPath={adminServicesPath}/> | ||
| : <ServiceDiscoveryForm formActionPath={providerAdminServiceDiscoveryServicesPath}/> | ||
|
|
||
| return ( | ||
| <React.Fragment> | ||
| <h1>New API</h1> | ||
| <ServiceSourceForm | ||
| isServiceDiscoveryUsable={isServiceDiscoveryUsable} | ||
| serviceDiscoveryAuthenticateUrl={serviceDiscoveryAuthenticateUrl} | ||
| handleFormsVisibility={handleFormsVisibility} | ||
| /> | ||
| {formToRender()} | ||
| </React.Fragment> | ||
| ) | ||
| } | ||
|
|
||
| const NewServiceFormWrapper = (props: Props, containerId: string) => | ||
| createReactWrapper(<NewServiceForm {...props} />, containerId) | ||
|
|
||
| export {NewServiceForm, NewServiceFormWrapper} | ||
63 changes: 63 additions & 0 deletions
63
app/javascript/src/NewService/components/ServiceDiscoveryForm.jsx
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,63 @@ | ||
| // @flow | ||
|
|
||
| import React from 'react' | ||
| import {useState, useEffect} from 'react' | ||
|
|
||
| import {FormWrapper, ErrorMessage, | ||
| ServiceDiscoveryListItems} from 'NewService/components/FormElements' | ||
| import {fetchData} from 'utilities/utils' | ||
| import type {FormProps} from 'NewService/types' | ||
|
|
||
| const BASE_PATH = '/p/admin/service_discovery' | ||
| const PROJECTS_PATH = `${BASE_PATH}/projects.json` | ||
|
|
||
| const ServiceDiscoveryForm = ({formActionPath}: {formActionPath: string}) => { | ||
| const [projects, setProjects] = useState([]) | ||
| const [services, setServices] = useState([]) | ||
| const [fetchErrorMessage, setFetchErrorMessage] = useState('') | ||
|
|
||
| const fetchProjects = async () => { | ||
|
damianpm marked this conversation as resolved.
Outdated
|
||
| try { | ||
| const data = await fetchData(PROJECTS_PATH) | ||
| setProjects(data['projects']) | ||
| fetchServices(data.projects[0].metadata.name) | ||
| } catch (error) { | ||
| setFetchErrorMessage(error.message) | ||
| } | ||
| } | ||
|
|
||
| const fetchServices = async (namespace: string) => { | ||
|
damianpm marked this conversation as resolved.
Outdated
|
||
| try { | ||
| const data = await fetchData(`${BASE_PATH}/namespaces/${namespace}/services.json`) | ||
| setServices(data['services']) | ||
| } catch (error) { | ||
| setFetchErrorMessage(error.message) | ||
| } | ||
| } | ||
|
|
||
| const listItemsProps = {fetchServices, projects, services} | ||
|
|
||
| useEffect(() => { | ||
| fetchProjects() | ||
| }, []) | ||
|
|
||
| const formProps: FormProps = { | ||
| id: 'service_source', | ||
| formActionPath, | ||
| hasHiddenServiceDiscoveryInput: true, | ||
| submitText: 'Create Service' | ||
| } | ||
|
|
||
| return ( | ||
| <React.Fragment> | ||
| {fetchErrorMessage && <ErrorMessage fetchErrorMessage={fetchErrorMessage}/>} | ||
| <FormWrapper {...formProps}> | ||
| <ServiceDiscoveryListItems {...listItemsProps}/> | ||
| </FormWrapper> | ||
| </React.Fragment> | ||
| ) | ||
| } | ||
|
|
||
| export { | ||
| ServiceDiscoveryForm | ||
| } | ||
23 changes: 23 additions & 0 deletions
23
app/javascript/src/NewService/components/ServiceManualForm.jsx
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,23 @@ | ||
| // @flow | ||
|
|
||
| import React from 'react' | ||
| import {FormWrapper, ServiceManualListItems} from 'NewService/components/FormElements' | ||
| import type {FormProps} from 'NewService/types' | ||
|
|
||
| const ServiceManualForm = ({formActionPath}: {formActionPath: string}) => { | ||
| const formProps: FormProps = { | ||
|
damianpm marked this conversation as resolved.
Outdated
|
||
| id: 'new_service', | ||
| formActionPath, | ||
| submitText: 'Add API' | ||
| } | ||
|
|
||
| return ( | ||
| <FormWrapper {...formProps}> | ||
| <ServiceManualListItems/> | ||
| </FormWrapper> | ||
| ) | ||
| } | ||
|
|
||
| export { | ||
| ServiceManualForm | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.