Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adds autosubmit feature and renames component
- Loading branch information
1 parent
8678f3e
commit 790a89b
Showing
15 changed files
with
203 additions
and
105 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
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,13 +1,15 @@ | ||
import { DefaultPlanSelectorWrapper } from 'Applications' | ||
import { DefaultPlanSelectWrapper } from 'Applications' | ||
import { safeFromJsonString } from 'utilities/json-utils' | ||
|
||
document.addEventListener('DOMContentLoaded', () => { | ||
const { dataset } = document.getElementById('default_plan') | ||
const currentService = safeFromJsonString(dataset.currentService) | ||
const plans = safeFromJsonString(dataset.applicationPlans) | ||
const currentPlanId = Number(dataset.currentPlanId) | ||
const currentPlan = safeFromJsonString(dataset.currentPlan) ?? undefined | ||
|
||
DefaultPlanSelectorWrapper({ | ||
DefaultPlanSelectWrapper({ | ||
currentService, | ||
plans, | ||
currentPlanId | ||
currentPlan | ||
}, 'default_plan') | ||
}) |
119 changes: 119 additions & 0 deletions
119
app/javascript/src/Applications/components/DefaultPlanSelect.jsx
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,119 @@ | ||
// @flow | ||
|
||
import React, { useState, useEffect } from 'react' | ||
import * as ajax from 'utilities/ajax' | ||
|
||
import { | ||
Form, | ||
FormGroup, | ||
PageSection, | ||
PageSectionVariants, | ||
Select, | ||
SelectOption, | ||
SelectVariant | ||
} from '@patternfly/react-core' | ||
import { PlanSelectOptionObject } from 'Applications' | ||
|
||
import './DefaultPlanSelect.scss' | ||
|
||
type Service = { | ||
id: number, | ||
name: string, | ||
} | ||
|
||
type ApplicationPlan = { | ||
id: number, | ||
name: string | ||
} | ||
|
||
type Props = { | ||
currentService: Service, | ||
currentPlan?: ApplicationPlan, | ||
plans: ApplicationPlan[] | ||
} | ||
|
||
const NO_DEFAULT_PLAN: ApplicationPlan = { id: -1, name: '(No default plan)' } | ||
|
||
// TODO: prevent reload screen when pressing Enter | ||
|
||
const DefaultPlanSelect = ({ currentService, currentPlan = NO_DEFAULT_PLAN, plans }: Props) => { | ||
const [selection, setSelection] = useState<PlanSelectOptionObject | null>(new PlanSelectOptionObject(currentPlan)) | ||
const [isExpanded, setIsExpanded] = useState(false) | ||
const [isLoading, setIsLoading] = useState(false) | ||
|
||
const onSelect = (_e, newPlan: PlanSelectOptionObject) => { | ||
setSelection(newPlan) | ||
setIsExpanded(false) | ||
setIsLoading(true) | ||
|
||
const body = newPlan.id >= 0 ? new URLSearchParams({ id: newPlan.id.toString() }) : undefined | ||
|
||
// making request... | ||
ajax.post(`/apiconfig/services/${currentService.id}/application_plans/masterize`, body) | ||
.then(data => { | ||
console.log(data) | ||
if (data.ok) { | ||
$.flash('Default plan was updated') | ||
} else { | ||
$.flash.error('Plan could not be updated') | ||
} | ||
}) | ||
.catch(err => { | ||
console.error(err) | ||
$.flash.error('An error ocurred. Please try again later.') | ||
}) | ||
.finally(() => setIsLoading(false)) | ||
} | ||
|
||
const onClear = () => { | ||
setSelection(null) | ||
} | ||
|
||
useEffect(() => { | ||
console.log(setIsLoading) | ||
// console.log('Setting new default: ' + String(selection)) | ||
// setIsLoading(true) | ||
|
||
// setTimeout(() => { | ||
// setIsLoading(false) | ||
// }, 500) | ||
}, [selection]) | ||
|
||
const options = [NO_DEFAULT_PLAN, ...plans] | ||
.filter(p => p.id !== currentPlan.id) // Don't show the current default plan | ||
.map(p => new PlanSelectOptionObject(p)) | ||
|
||
return ( | ||
<PageSection variant={PageSectionVariants.light} className="pf-u-mb-md" id="default-plan-selector"> | ||
<Form onSubmit={e => e.preventDefault()}> | ||
<FormGroup | ||
label="Default plan" | ||
fieldId="application_plan_id" | ||
helperText="Default application plan (if any) is selected automatically upon service subscription." | ||
> | ||
<Select | ||
variant={SelectVariant.typeahead} | ||
aria-label="Select application plan" | ||
placeholderText="Select application plan" | ||
onToggle={() => setIsExpanded(!isExpanded)} | ||
onSelect={onSelect} | ||
onClear={onClear} | ||
onKeyDown={e => { | ||
console.log('pressed') | ||
e.preventDefault() | ||
}} | ||
onBlur={() => console.log('blur')} | ||
isExpanded={isExpanded} | ||
isDisabled={isLoading} | ||
selections={selection} | ||
isCreatable={false} | ||
> | ||
{options.map(p => <SelectOption key={p.id} value={p} />)} | ||
</Select> | ||
</FormGroup> | ||
</Form> | ||
</PageSection> | ||
) | ||
} | ||
|
||
export { DefaultPlanSelect } |
5 changes: 5 additions & 0 deletions
5
app/javascript/src/Applications/components/DefaultPlanSelect.scss
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,5 @@ | ||
#default-plan-selector { | ||
.pf-c-select__toggle.pf-m-disabled input { | ||
background-color: var(--pf-global--disabled-color--300); | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
app/javascript/src/Applications/components/DefaultPlanSelectWrapper.jsx
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,8 @@ | ||
import React from 'react' | ||
|
||
import { DefaultPlanSelect } from 'Applications' | ||
import { createReactWrapper } from 'utilities/createReactWrapper' | ||
|
||
const DefaultPlanSelectWrapper = (props, containerId) => createReactWrapper(<DefaultPlanSelect {...props} />, containerId) | ||
|
||
export { DefaultPlanSelectWrapper } |
80 changes: 0 additions & 80 deletions
80
app/javascript/src/Applications/components/DefaultPlanSelector.jsx
This file was deleted.
Oops, something went wrong.
8 changes: 0 additions & 8 deletions
8
app/javascript/src/Applications/components/DefaultPlanSelectorWrapper.jsx
This file was deleted.
Oops, something went wrong.
7 changes: 6 additions & 1 deletion
7
app/javascript/src/Applications/components/PlanSelectOptionObject.jsx
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,5 +1,5 @@ | ||
// @flow | ||
|
||
export * from 'Applications/components/PlanSelectOptionObject' | ||
export * from 'Applications/components/DefaultPlanSelector' | ||
export * from 'Applications/components/DefaultPlanSelectorWrapper' | ||
export * from 'Applications/components/DefaultPlanSelect' | ||
export * from 'Applications/components/DefaultPlanSelectWrapper' |
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,12 @@ | ||
// @flow | ||
|
||
const post = (url: string, body?: URLSearchParams) => fetch(url, { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8', | ||
'X-CSRF-Token': document.querySelector('meta[name="csrf-token"]').content | ||
}, | ||
body | ||
}) | ||
|
||
export { post } |
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,2 +1,3 @@ | ||
div id='default_plan' data-application-plans=(application_plans_data()) data-current-plan-id=(current_plan_id_data()) | ||
h1 New | ||
div id='default_plan' data=default_plan_data(@service, @plans) | ||
= javascript_pack_tag 'default_plan_selector' |
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
17 changes: 17 additions & 0 deletions
17
spec/javascripts/Applications/components/DefaultPlanSelect.spec.jsx
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,17 @@ | ||
// @flow | ||
|
||
import React from 'react' | ||
|
||
import { DefaultPlanSelect } from 'Applications' | ||
import { mount } from 'enzyme' | ||
|
||
const props = { | ||
currentService: { id: 0, name: 'Le Service' }, | ||
currentPlan: undefined, | ||
plans: [] | ||
} | ||
|
||
it('should render', () => { | ||
const wrapper = mount(<DefaultPlanSelect {...props} />) | ||
expect(wrapper.exists()).toBe(true) | ||
}) |