Skip to content

Commit

Permalink
internal(Thresholds): Validate and save to Generator file (#446)
Browse files Browse the repository at this point in the history
  • Loading branch information
cristianoventura authored Feb 5, 2025
1 parent 704be28 commit fef51d8
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 29 deletions.
4 changes: 3 additions & 1 deletion src/schemas/generator/v1/thresholds.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ export const ThresholdSchema = z.object({
url: z.string().optional(),
statistic: ThresholdStatisticSchema,
condition: ThresholdConditionSchema,
value: z.number({ message: 'Invalid value' }),
value: z
.number({ message: 'Invalid value' })
.min(0, { message: 'Invalid value' }),
stopTest: z.boolean().default(false),
})

Expand Down
3 changes: 2 additions & 1 deletion src/store/generator/selectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export function selectGeneratorData(state: GeneratorStore): GeneratorFileData {
allowlist,
includeStaticAssets,
scriptName,
thresholds,
} = state

return {
Expand All @@ -66,7 +67,7 @@ export function selectGeneratorData(state: GeneratorStore): GeneratorFileData {
allowlist,
includeStaticAssets,
scriptName,
thresholds: [],
thresholds,
}
}

Expand Down
20 changes: 3 additions & 17 deletions src/store/generator/slices/thresholds.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ interface State {
}

interface Actions {
addThreshold: (threshold: Threshold) => void
updateThreshold: (threshold: Threshold) => void
deleteThreshold: (id: string) => void
setThresholds: (thresholds: Threshold[]) => void
}

export type ThresholdSliceStore = State & Actions
Expand All @@ -17,20 +15,8 @@ export const createThresholdSlice: ImmerStateCreator<ThresholdSliceStore> = (
set
) => ({
thresholds: [],
addThreshold: (threshold) =>
setThresholds: (thresholds: Threshold[]) =>
set((state) => {
state.thresholds.push(threshold)
}),
updateThreshold(threshold) {
set((state) => {
const index = state.thresholds.findIndex((t) => t.id === threshold.id)
if (index !== -1) {
state.thresholds[index] = threshold
}
})
},
deleteThreshold: (id) =>
set((state) => {
state.thresholds = state.thresholds.filter((t) => t.id !== id)
state.thresholds = thresholds
}),
})
4 changes: 1 addition & 3 deletions src/test/factories/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,7 @@ export function createGeneratorState(
setVus: vi.fn(),

thresholds: [],
addThreshold: vi.fn(),
updateThreshold: vi.fn(),
deleteThreshold: vi.fn(),
setThresholds: vi.fn(),

...state,
}
Expand Down
2 changes: 2 additions & 0 deletions src/types/thresholds.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ import {
ThresholdDataSchema,
ThresholdMetricSchema,
ThresholdSchema,
ThresholdStatisticSchema,
} from '@/schemas/generator'

export type Threshold = z.infer<typeof ThresholdSchema>
export type ThresholdData = z.infer<typeof ThresholdDataSchema>
export type ThresholdMetric = z.infer<typeof ThresholdMetricSchema>
export type ThresholdStatstic = z.infer<typeof ThresholdStatisticSchema>
20 changes: 18 additions & 2 deletions src/views/Generator/TestOptions/Thresholds/ThresholdRow.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { FieldGroup, ControlledSelect } from '@/components/Form'
import { ThresholdData } from '@/types/thresholds'
import { Threshold, ThresholdData } from '@/types/thresholds'
import { TrashIcon } from '@radix-ui/react-icons'
import { Table, TextField, Checkbox, IconButton, Flex } from '@radix-ui/themes'
import {
Expand All @@ -17,6 +17,7 @@ import {
import { useThresholdURLOptions } from './Thresholds.hooks'
import { css } from '@emotion/react'
import { useTheme } from '@/hooks/useTheme'
import { useEffect } from 'react'

type ThresholdRowProps = {
index: number
Expand All @@ -30,12 +31,26 @@ export function ThresholdRow({ field, index, remove }: ThresholdRowProps) {
formState: { errors },
control,
watch,
setValue,
} = useFormContext<ThresholdData>()

const urlOptions = useThresholdURLOptions()
const threshold = watch('thresholds')[index]
const threshold = watch('thresholds')[index] as Threshold
const theme = useTheme()

// Handle selected statistic when the metric field changes
useEffect(() => {
const availableStatistics = getStatisticOptions(threshold.metric).map(
(option) => option.value
)
if (!availableStatistics.includes(threshold.statistic)) {
const newStatistic = availableStatistics[0]
if (newStatistic) {
setValue(`thresholds.${index}.statistic`, newStatistic)
}
}
}, [threshold.metric, threshold.statistic, index, setValue])

return (
<Table.Row key={field.id}>
<Table.Cell>
Expand Down Expand Up @@ -115,6 +130,7 @@ export function ThresholdRow({ field, index, remove }: ThresholdRowProps) {
name={`thresholds.${index}.stopTest`}
render={({ field }) => (
<Checkbox
checked={field.value}
onCheckedChange={field.onChange}
{...register(`thresholds.${index}.stopTest`)}
/>
Expand Down
10 changes: 7 additions & 3 deletions src/views/Generator/TestOptions/Thresholds/Thresholds.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { Table } from '@/components/Table'

export function Thresholds() {
const thresholds = useGeneratorStore((store) => store.thresholds)
const setThresholds = useGeneratorStore((store) => store.setThresholds)

const formMethods = useForm<{ thresholds: Threshold[] }>({
resolver: zodResolver(ThresholdDataSchema),
Expand Down Expand Up @@ -41,9 +42,12 @@ export function Thresholds() {
})
}

const onSubmit = useCallback((data: ThresholdData) => {
console.log('onSubmit', data)
}, [])
const onSubmit = useCallback(
(data: ThresholdData) => {
setThresholds(data.thresholds)
},
[setThresholds]
)

// Submit onChange
useEffect(() => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ThresholdMetricSchema } from '@/schemas/generator'
import { ThresholdMetric } from '@/types/thresholds'
import { ThresholdMetric, ThresholdStatstic } from '@/types/thresholds'
import { exhaustive } from '@/utils/typescript'

type ThresholdMetricMap = {
Expand Down Expand Up @@ -92,7 +92,10 @@ export const THRESHOLD_CONDITIONS_OPTIONS = [
{ label: '!==', value: '!==' },
]

export const getStatisticOptions = (metricName: ThresholdMetric) => {
type StatisticOption = { label: string; value: ThresholdStatstic }
export const getStatisticOptions = (
metricName: ThresholdMetric
): Array<StatisticOption> => {
const { type } = metricsMap[metricName]

switch (type) {
Expand Down

0 comments on commit fef51d8

Please sign in to comment.