Skip to content

Commit 37582b8

Browse files
committed
add edit flow to edit consumers
1 parent 66be81e commit 37582b8

3 files changed

Lines changed: 120 additions & 29 deletions

File tree

otoroshi/app/next/models/Api.scala

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ object ApiRoute {
109109

110110
case class ApiFlows(id: String,
111111
name: String,
112-
consumers: Seq[ApiConsumer] = Seq.empty,
112+
consumers: Seq[String] = Seq.empty,
113113
plugins: NgPlugins)
114114

115115
object ApiFlows {
@@ -134,8 +134,7 @@ object ApiFlows {
134134
name = json.select("name").asString,
135135
plugins = NgPlugins.readFrom(json.select("plugins")),
136136
consumers = (json \ "consumers")
137-
.asOpt[Seq[JsValue]]
138-
.map(_.flatMap(v => ApiConsumer._fmt.reads(v).asOpt))
137+
.asOpt[Seq[String]]
139138
.getOrElse(Seq.empty)
140139
)
141140
} match {
@@ -148,7 +147,8 @@ object ApiFlows {
148147
override def writes(o: ApiFlows): JsValue = Json.obj(
149148
"id" -> o.id,
150149
"name" -> o.name,
151-
"plugins" -> o.plugins.json
150+
"plugins" -> o.plugins.json,
151+
"consumers" -> o.consumers,
152152
)
153153
}
154154
}

otoroshi/javascript/src/pages/ApiEditor/Sidebar.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ export default (props) => {
125125
{!isOnApisHome && <>
126126
{openedSidebar && <p className="sidebar-title mt-3">General</p>}
127127
<div className='me-1 my-2'>
128-
{openedSidebar && version !== 'staging' && <Select
128+
{openedSidebar && version && version !== 'staging' && <Select
129129
value={{ value: version, label: version }}
130130
onChange={item => {
131131
const queryParams = new URLSearchParams(window.location.search);

otoroshi/javascript/src/pages/ApiEditor/index.js

Lines changed: 115 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1644,7 +1644,107 @@ function SidebarComponent(props) {
16441644
}
16451645

16461646
function EditFlow(props) {
1647+
const history = useHistory()
1648+
const params = useParams()
1649+
const location = useLocation()
1650+
1651+
const { item, updateItem, isLoading } = useDraftOfAPI()
1652+
1653+
const [flow, setFlow] = useState()
1654+
1655+
useEffect(() => {
1656+
if (item && !flow) {
1657+
const currentFlow = item.flows.find(flow => flow.id === params.flowId)
1658+
1659+
if (currentFlow) {
1660+
props.setTitle({
1661+
value: `Update ${currentFlow.name}`,
1662+
noThumbtack: true,
1663+
children: <VersionBadge />
1664+
})
1665+
1666+
setFlow({
1667+
...currentFlow,
1668+
consumers: item.consumers.reduce((acc, item) => ({
1669+
...acc,
1670+
[item.id]: currentFlow.consumers.includes(item.id)
1671+
}), {})
1672+
})
1673+
}
1674+
}
1675+
return () => props.setTitle(undefined)
1676+
}, [item])
1677+
1678+
const updateFlow = () => {
1679+
return updateItem({
1680+
...item,
1681+
flows: item.flows.map(ite => {
1682+
if (ite.id === params.flowId) {
1683+
return {
1684+
...flow,
1685+
consumers: Object.entries(flow.consumers).filter(f => f[1]).map(f => f[0])
1686+
}
1687+
} else {
1688+
return ite
1689+
}
1690+
})
1691+
})
1692+
.then(() => historyPush(history, location, `/apis/${params.apiId}/flows`));
1693+
}
1694+
1695+
if (isLoading || !item)
1696+
return <SimpleLoader />
1697+
1698+
return <div style={{
1699+
maxWidth: MAX_WIDTH,
1700+
margin: 'auto'
1701+
}}>
1702+
<NgForm
1703+
schema={FLOW_FORM_SETTINGS.schema(item)}
1704+
flow={FLOW_FORM_SETTINGS.flow}
1705+
value={flow}
1706+
onChange={setFlow}
1707+
/>
1708+
<Button
1709+
type="success"
1710+
className="btn-sm ms-auto d-flex align-items-center"
1711+
onClick={updateFlow}
1712+
>
1713+
Update <VersionBadge size="xs" className="ms-2" />
1714+
</Button>
1715+
</div>
1716+
}
16471717

1718+
const FLOW_FORM_SETTINGS = {
1719+
schema: item => ({
1720+
name: {
1721+
type: 'string',
1722+
props: { label: 'Name' },
1723+
},
1724+
consumers: {
1725+
type: 'form',
1726+
label: 'Enabled consumers',
1727+
schema: item.consumers.reduce((acc, item) => ({
1728+
...acc,
1729+
[item.id]: {
1730+
type: 'box-bool',
1731+
label: item.name,
1732+
props: {
1733+
description: item.description || item.consumer_kind
1734+
}
1735+
}
1736+
}), {}),
1737+
}
1738+
}),
1739+
flow: [
1740+
{
1741+
type: 'group',
1742+
name: 'Informations',
1743+
collapsable: false,
1744+
fields: ["name"]
1745+
},
1746+
'consumers'
1747+
]
16481748
}
16491749

16501750
function NewFlow(props) {
@@ -1665,42 +1765,33 @@ function NewFlow(props) {
16651765
const [flow, setFlow] = useState({
16661766
id: v4(),
16671767
name: 'New flow name',
1668-
plugins: []
1768+
plugins: [],
1769+
consumers: []
16691770
})
16701771

16711772
const { item, updateItem, isLoading } = useDraftOfAPI()
16721773

16731774
const createFlow = () => {
16741775
return updateItem({
16751776
...item,
1676-
flows: [...item.flows, flow]
1777+
flows: [...item.flows, {
1778+
...flow,
1779+
consumers: Object.entries(flow.consumers).filter(f => f[1]).map(f => f[0])
1780+
}]
16771781
})
16781782
.then(() => historyPush(history, location, `/apis/${params.apiId}/flows/${flow.id}`));
16791783
}
16801784

1681-
if (isLoading)
1785+
if (isLoading || !item)
16821786
return <SimpleLoader />
16831787

1684-
return <>
1685-
<Form
1686-
schema={{
1687-
name: {
1688-
type: 'string',
1689-
props: { label: 'Name' },
1690-
},
1691-
consumers: {
1692-
type: 'select',
1693-
props: {
1694-
label: 'Consumers',
1695-
},
1696-
options: item.consumers,
1697-
optionsTransformer: {
1698-
label: 'name',
1699-
value: 'id'
1700-
}
1701-
}
1702-
}}
1703-
flow={["name", "consumers"]}
1788+
return <div style={{
1789+
maxWidth: MAX_WIDTH,
1790+
margin: 'auto'
1791+
}}>
1792+
<NgForm
1793+
schema={FLOW_FORM_SETTINGS.schema(item)}
1794+
flow={FLOW_FORM_SETTINGS.flow}
17041795
value={flow}
17051796
onChange={setFlow}
17061797
/>
@@ -1711,7 +1802,7 @@ function NewFlow(props) {
17111802
>
17121803
Create <VersionBadge size="xs" className="ms-2" />
17131804
</Button>
1714-
</>
1805+
</div>
17151806
}
17161807

17171808
function NewAPI(props) {

0 commit comments

Comments
 (0)