Skip to content

Commit 1c2fd0c

Browse files
committed
refactor rule to form values logic
1 parent b291892 commit 1c2fd0c

File tree

1 file changed

+31
-17
lines changed

1 file changed

+31
-17
lines changed

app/forms/firewall-rules-create.tsx

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,34 @@ export const valuesToRuleUpdate = (values: FirewallRuleValues): VpcFirewallRuleU
7676
targets: values.targets,
7777
})
7878

79+
/** convert in the opposite direction for when we're creating from existing rule */
80+
const ruleToValues = (rule: VpcFirewallRule): FirewallRuleValues => ({
81+
...rule,
82+
enabled: rule.status === 'enabled',
83+
protocols: rule.filters.protocols || [],
84+
ports: rule.filters.ports || [],
85+
hosts: rule.filters.hosts || [],
86+
})
87+
88+
/** Empty form for when we're not creating from an existing rule */
89+
const defaultValuesEmpty: FirewallRuleValues = {
90+
enabled: true,
91+
name: '',
92+
description: '',
93+
94+
priority: 0,
95+
action: 'allow',
96+
direction: 'inbound',
97+
98+
// in the request body, these go in a `filters` object. we probably don't
99+
// need such nesting here though. not even sure how to do it
100+
protocols: [],
101+
102+
ports: [],
103+
hosts: [],
104+
targets: [],
105+
}
106+
79107
type PortRangeFormValues = {
80108
portRange: string
81109
}
@@ -565,23 +593,9 @@ export function CreateFirewallRuleForm() {
565593
const existingRules = useMemo(() => sortBy(data.rules, (r) => r.priority), [data])
566594
const originalRule = existingRules.find((rule) => rule.name === firewallRule)
567595

568-
const defaultValues: FirewallRuleValues = {
569-
enabled: originalRule ? originalRule.status === 'enabled' : true,
570-
name: originalRule ? incrementName(originalRule.name) : '',
571-
description: originalRule ? originalRule.description : '',
572-
573-
priority: originalRule ? originalRule.priority : 0,
574-
action: originalRule ? originalRule.action : 'allow',
575-
direction: originalRule ? originalRule.direction : 'inbound',
576-
577-
// in the request body, these go in a `filters` object. we probably don't
578-
// need such nesting here though. not even sure how to do it
579-
protocols: originalRule ? originalRule.filters.protocols || [] : [],
580-
581-
ports: originalRule ? originalRule.filters.ports || [] : [],
582-
hosts: originalRule ? originalRule.filters.hosts || [] : [],
583-
targets: originalRule ? originalRule.targets : [],
584-
}
596+
const defaultValues: FirewallRuleValues = originalRule
597+
? ruleToValues({ ...originalRule, name: incrementName(originalRule.name) })
598+
: defaultValuesEmpty
585599

586600
const form = useForm({ defaultValues })
587601

0 commit comments

Comments
 (0)