-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1007 from yaacov/refactor-simple-hook-tab
🧼 Refactor simple plan hook form
- Loading branch information
Showing
8 changed files
with
167 additions
and
104 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
3 changes: 3 additions & 0 deletions
3
...s/forklift-console-plugin/src/modules/Plans/views/details/tabs/SimpleHooks/hooks/index.ts
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,3 @@ | ||
// @index(['./*', /style/g], f => `export * from '${f.path}';`) | ||
export * from './usePlanHooks'; | ||
// @endindex |
32 changes: 32 additions & 0 deletions
32
...ift-console-plugin/src/modules/Plans/views/details/tabs/SimpleHooks/hooks/usePlanHooks.ts
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,32 @@ | ||
import { | ||
HookModelGroupVersionKind, | ||
PlanModelGroupVersionKind, | ||
V1beta1Hook, | ||
V1beta1Plan, | ||
V1beta1PlanSpecVmsHooks, | ||
} from '@kubev2v/types'; | ||
import { useK8sWatchResource } from '@openshift-console/dynamic-plugin-sdk'; | ||
|
||
export const usePlanHooks = (name: string, namespace: string) => { | ||
const [plan, loaded, loadError] = useK8sWatchResource<V1beta1Plan>({ | ||
groupVersionKind: PlanModelGroupVersionKind, | ||
namespaced: true, | ||
name, | ||
namespace, | ||
}); | ||
const hooks: V1beta1PlanSpecVmsHooks[] = plan?.spec?.vms?.[0]?.hooks || []; | ||
const postHook = hooks.find((h) => h.step === 'PostHook'); | ||
const preHook = hooks.find((h) => h.step === 'PreHook'); | ||
|
||
const [hookRecourses] = useK8sWatchResource<V1beta1Hook[]>({ | ||
groupVersionKind: HookModelGroupVersionKind, | ||
namespaced: true, | ||
isList: true, | ||
namespace: plan?.metadata?.namespace, | ||
}); | ||
|
||
const postHookResource = hookRecourses.find((h) => h.metadata.name === postHook?.hook?.name); | ||
const preHookResource = hookRecourses.find((h) => h.metadata.name === preHook?.hook?.name); | ||
|
||
return [plan, preHookResource, postHookResource, loaded, loadError]; | ||
}; |
1 change: 1 addition & 0 deletions
1
...s/forklift-console-plugin/src/modules/Plans/views/details/tabs/SimpleHooks/utils/index.ts
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,6 @@ | ||
// @index(['./*', /style/g], f => `export * from '${f.path}';`) | ||
export * from './createHook'; | ||
export * from './deleteHook'; | ||
export * from './onUpdatePlanHooks'; | ||
export * from './updateHook'; | ||
// @endindex |
69 changes: 69 additions & 0 deletions
69
...nsole-plugin/src/modules/Plans/views/details/tabs/SimpleHooks/utils/onUpdatePlanHooks.tsx
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,69 @@ | ||
import React from 'react'; | ||
import { deepCopy } from 'src/modules/Plans/utils'; | ||
import { AlertMessageForModals } from 'src/modules/Providers/modals'; | ||
|
||
import { V1beta1Hook, V1beta1Plan } from '@kubev2v/types'; | ||
|
||
import { FormAction, FormState } from '../state'; | ||
|
||
import { createHook } from './createHook'; | ||
import { deleteHook } from './deleteHook'; | ||
import { updateHook } from './updateHook'; | ||
|
||
type onUpdatePlanHooksProps = { | ||
plan: V1beta1Plan; | ||
preHookResource: V1beta1Hook; | ||
postHookResource: V1beta1Hook; | ||
dispatch: (value: FormAction) => void; | ||
state: FormState; | ||
}; | ||
|
||
// Handle user clicking "save" | ||
export async function onUpdatePlanHooks(props: onUpdatePlanHooksProps) { | ||
const { plan, preHookResource, postHookResource, dispatch, state } = props; | ||
|
||
dispatch({ type: 'SET_LOADING', payload: true }); | ||
|
||
let newPlan = deepCopy(plan); | ||
|
||
try { | ||
if (state.preHookSet) { | ||
if (preHookResource) { | ||
// Patch new hook | ||
await updateHook(state.preHook); | ||
} else { | ||
// Create hook | ||
newPlan = await createHook(newPlan, state.preHook, 'PreHook'); | ||
} | ||
} else { | ||
if (preHookResource) { | ||
// Delete hook | ||
newPlan = await deleteHook(newPlan, preHookResource, 'PreHook'); | ||
} | ||
} | ||
|
||
if (state.postHookSet) { | ||
if (postHookResource) { | ||
// Patch new hook | ||
await updateHook(state.postHook); | ||
} else { | ||
// Create hook | ||
await createHook(newPlan, state.postHook, 'PostHook'); | ||
} | ||
} else { | ||
if (postHookResource) { | ||
// Delete hook | ||
await deleteHook(newPlan, postHookResource, 'PostHook'); | ||
} | ||
} | ||
|
||
dispatch({ type: 'SET_LOADING', payload: false }); | ||
} catch (err) { | ||
dispatch({ | ||
type: 'SET_ALERT_MESSAGE', | ||
payload: <AlertMessageForModals title={'Error'} message={err.message || err.toString()} />, | ||
}); | ||
|
||
dispatch({ type: 'SET_LOADING', payload: false }); | ||
} | ||
} |
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