Simplify createSyncJob: calculate requiresMigration in caller
- Remove syncTarget and migrateResources parameters from useCreateSyncJob hook - Calculate requiresMigration in SynchronizeStep based on sync target and checkbox value - Pass requiresMigration as parameter to createSyncJob function
This commit is contained in:
@@ -24,11 +24,10 @@ export const SynchronizeStep = memo(function SynchronizeStep({ onCancel, isCance
|
||||
const { setStepStatusInfo } = useStepStatus();
|
||||
const repoName = watch('repositoryName') ?? '';
|
||||
const syncTarget = watch('repository.sync.target');
|
||||
const { requiresMigration } = useResourceStats(repoName, syncTarget);
|
||||
const { requiresMigration: baseRequiresMigration } = useResourceStats(repoName, syncTarget);
|
||||
|
||||
const { createSyncJob } = useCreateSyncJob({
|
||||
repoName,
|
||||
requiresMigration,
|
||||
syncTarget,
|
||||
setStepStatusInfo,
|
||||
});
|
||||
const [job, setJob] = useState<Job>();
|
||||
@@ -65,8 +64,15 @@ export const SynchronizeStep = memo(function SynchronizeStep({ onCancel, isCance
|
||||
const isButtonDisabled = hasError || (checked !== undefined && isRepositoryHealthy === false) || healthStatusNotReady;
|
||||
|
||||
const startSynchronization = async () => {
|
||||
const migrateResources = getValues('migrate.migrateResources');
|
||||
const response = await createSyncJob({ migrateResources });
|
||||
// Calculate final requiresMigration based on sync target and user selection
|
||||
// For instance sync: use the base requiresMigration
|
||||
// For folder sync: only migrate if user explicitly opts in via checkbox
|
||||
let finalRequiresMigration = baseRequiresMigration;
|
||||
if (syncTarget === 'folder') {
|
||||
finalRequiresMigration = getValues('migrate.migrateResources') ?? false;
|
||||
}
|
||||
|
||||
const response = await createSyncJob(finalRequiresMigration);
|
||||
if (response) {
|
||||
setJob(response);
|
||||
}
|
||||
|
||||
@@ -1,28 +1,17 @@
|
||||
import { t } from '@grafana/i18n';
|
||||
import { useCreateRepositoryJobsMutation } from 'app/api/clients/provisioning/v0alpha1';
|
||||
|
||||
import { StepStatusInfo, Target } from '../types';
|
||||
import { StepStatusInfo } from '../types';
|
||||
|
||||
export interface UseCreateSyncJobParams {
|
||||
repoName: string;
|
||||
requiresMigration: boolean;
|
||||
syncTarget?: Target;
|
||||
setStepStatusInfo?: (info: StepStatusInfo) => void;
|
||||
}
|
||||
|
||||
export interface CreateSyncJobOptions {
|
||||
migrateResources?: boolean;
|
||||
}
|
||||
|
||||
export function useCreateSyncJob({
|
||||
repoName,
|
||||
requiresMigration,
|
||||
syncTarget,
|
||||
setStepStatusInfo,
|
||||
}: UseCreateSyncJobParams) {
|
||||
export function useCreateSyncJob({ repoName, setStepStatusInfo }: UseCreateSyncJobParams) {
|
||||
const [createJob, { isLoading }] = useCreateRepositoryJobsMutation();
|
||||
|
||||
const createSyncJob = async (options?: CreateSyncJobOptions) => {
|
||||
const createSyncJob = async (requiresMigration: boolean) => {
|
||||
if (!repoName) {
|
||||
setStepStatusInfo?.({
|
||||
status: 'error',
|
||||
@@ -34,13 +23,7 @@ export function useCreateSyncJob({
|
||||
try {
|
||||
setStepStatusInfo?.({ status: 'running' });
|
||||
|
||||
// Determine if we should run a migration job:
|
||||
// - For instance sync: always migrate if there are resources
|
||||
// - For folder sync: migrate only if user explicitly opted in via checkbox
|
||||
const shouldMigrate =
|
||||
syncTarget === 'instance' ? requiresMigration : syncTarget === 'folder' && options?.migrateResources;
|
||||
|
||||
const jobSpec = shouldMigrate
|
||||
const jobSpec = requiresMigration
|
||||
? {
|
||||
migrate: {},
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user