Skip to content

Commit 5b0bb45

Browse files
committed
Add helper function for creating the CustomFieldEditorView.
1 parent 255d9ac commit 5b0bb45

File tree

1 file changed

+26
-16
lines changed

1 file changed

+26
-16
lines changed

WooCommerce/Classes/ViewRelated/Custom Fields/CustomFieldsListView.swift

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -93,23 +93,11 @@ struct CustomFieldsListView: View {
9393
}
9494
}
9595
.listStyle(.plain)
96-
.sheet(item: $viewModel.selectedCustomField, content: { customField in
97-
NavigationView {
98-
CustomFieldEditorView(key: customField.key,
99-
value: customField.value,
100-
onSave: { updatedKey, updatedValue in
101-
viewModel.saveField(key: updatedKey, value: updatedValue, fieldId: customField.fieldId)
102-
})
103-
}
104-
})
96+
.sheet(item: $viewModel.selectedCustomField) { customField in
97+
buildCustomFieldEditorView(customField: customField)
98+
}
10599
.sheet(isPresented: $viewModel.isAddingNewField) {
106-
NavigationView {
107-
CustomFieldEditorView(key: "",
108-
value: "",
109-
onSave: { updatedKey, updatedValue in
110-
viewModel.saveField(key: updatedKey, value: updatedValue, fieldId: nil)
111-
})
112-
}
100+
buildCustomFieldEditorView(customField: nil)
113101
}
114102
}
115103
}
@@ -170,6 +158,28 @@ private struct CustomFieldRow: View {
170158
}
171159
}
172160

161+
// MARK: - Helpers
162+
//
163+
private extension CustomFieldsListView {
164+
/// Builds the Custom Field Editor View.
165+
/// - When `customField` is provided, it configures the editor for editing an existing field
166+
/// - When `customField` is nil, it configures the editor for creating a new field
167+
func buildCustomFieldEditorView(customField: CustomFieldsListViewModel.CustomFieldUI?) -> some View {
168+
NavigationView {
169+
CustomFieldEditorView(
170+
key: customField?.key ?? "",
171+
value: customField?.value ?? "",
172+
onSave: { updatedKey, updatedValue in
173+
viewModel.saveField(
174+
key: updatedKey,
175+
value: updatedValue,
176+
fieldId: customField?.fieldId
177+
)
178+
}
179+
)
180+
}
181+
}
182+
}
173183

174184
// MARK: - Constants
175185
//

0 commit comments

Comments
 (0)