Skip to content

Commit 2f35ad4

Browse files
committed
Better variable naming and code readability
1 parent a02603a commit 2f35ad4

File tree

2 files changed

+37
-42
lines changed

2 files changed

+37
-42
lines changed

web-server/pages/api/internal/team/[team_id]/incident_prs_filter.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
import * as yup from 'yup';
22

33
import { handleRequest } from '@/api-helpers/axios';
4-
import { Endpoint } from '@/api-helpers/global';
4+
import { Endpoint, nullSchema } from '@/api-helpers/global';
55
import {
66
TeamIncidentPRsSettingApiResponse,
77
TeamIncidentPRsSettingsResponse
88
} from '@/types/resources';
99

10-
const getSchema = yup.object();
1110
const pathSchema = yup.object().shape({
1211
team_id: yup.string().uuid().required()
1312
});
@@ -26,7 +25,7 @@ const putSchema = yup.object().shape({
2625

2726
const endpoint = new Endpoint(pathSchema);
2827

29-
endpoint.handle.GET(getSchema, async (req, res) => {
28+
endpoint.handle.GET(nullSchema, async (req, res) => {
3029
const { team_id } = req.payload;
3130
const { setting } = await handleRequest<TeamIncidentPRsSettingApiResponse>(
3231
`/teams/${team_id}/settings`,

web-server/src/components/TeamIncidentPRsFilter.tsx

Lines changed: 35 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -34,34 +34,31 @@ import { FlexBox } from './FlexBox';
3434
import { LightTooltip, IOSSwitch } from './Shared';
3535
import { Line } from './Text';
3636

37-
const incidentPRFilterFormSchema = yup
38-
.object({
39-
setting: yup
40-
.object({
41-
include_revert_prs: yup.boolean(),
42-
filters: yup.array(
43-
yup.object({
44-
field: yup.string().required(),
45-
value: yup
46-
.string()
47-
.required()
48-
.test(
49-
'regex-group',
50-
'Regex must contain exactly one (\\d+) match group',
51-
(val) => {
52-
if (!val) return false;
53-
const match = val.match(/\(\\d\+\)/g);
54-
return match && match.length === 1;
55-
}
56-
)
57-
})
58-
)
59-
})
60-
.required()
61-
})
62-
.required();
37+
const individualFilterSchema = yup.object({
38+
field: yup.string().required(),
39+
value: yup
40+
.string()
41+
.test(
42+
'regex-group',
43+
'Regex must contain exactly one (\\d+) match group',
44+
(val) => {
45+
if (!val) return false;
46+
const match = val.match(/\(\\d\+\)/g);
47+
return match && match.length === 1;
48+
}
49+
)
50+
});
51+
52+
const settingsSchema = yup.object({
53+
include_revert_prs: yup.boolean().required(),
54+
filters: yup.array(individualFilterSchema).required()
55+
});
56+
57+
const incidentPRFilterFormSchema = yup.object({
58+
setting: settingsSchema.required()
59+
});
6360

64-
type incidentPRFilterFormSchema = yup.InferType<
61+
type IncidentPRFilterFormSchema = yup.InferType<
6562
typeof incidentPRFilterFormSchema
6663
>;
6764

@@ -103,7 +100,7 @@ export const TeamIncidentPRsFilter: FC<{
103100
(s) => s.team.teamIncidentPRsFilters
104101
)?.setting;
105102

106-
const addUserMethods = useForm<incidentPRFilterFormSchema>({
103+
const formMethods = useForm<IncidentPRFilterFormSchema>({
107104
resolver: yupResolver(incidentPRFilterFormSchema),
108105
mode: 'onChange',
109106
defaultValues: {
@@ -115,7 +112,7 @@ export const TeamIncidentPRsFilter: FC<{
115112
watch,
116113
formState: { isDirty, isValid, errors },
117114
setValue
118-
} = addUserMethods;
115+
} = formMethods;
119116

120117
const settings = watch('setting');
121118

@@ -173,6 +170,13 @@ export const TeamIncidentPRsFilter: FC<{
173170
}
174171
}, [settings.filters, setValue]);
175172

173+
const isSingleEmptyFilter =
174+
settings.filters.length === 1 &&
175+
!settings.filters[0].field &&
176+
!settings.filters[0].value;
177+
178+
const isSaveEnabled = isDirty && (isValid || isSingleEmptyFilter);
179+
176180
return (
177181
<FlexBox gap={2} col maxWidth={'560px'}>
178182
<Line white small mt={-1} textAlign={'start'}>
@@ -182,7 +186,7 @@ export const TeamIncidentPRsFilter: FC<{
182186

183187
<Divider />
184188

185-
<FormProvider {...addUserMethods}>
189+
<FormProvider {...formMethods}>
186190
<FlexBox col gap2>
187191
<FlexBox>
188192
<Line big flexGrow={1}>
@@ -364,15 +368,7 @@ export const TeamIncidentPRsFilter: FC<{
364368
type="submit"
365369
variant="outlined"
366370
color="primary"
367-
disabled={
368-
!isDirty ||
369-
!(
370-
isValid ||
371-
(settings.filters.length === 1 &&
372-
!settings.filters[0].field &&
373-
!settings.filters[0].value)
374-
)
375-
}
371+
disabled={!isSaveEnabled}
376372
loading={isSaving.value}
377373
sx={{
378374
'&.Mui-disabled': {

0 commit comments

Comments
 (0)