Skip to content

Commit 1a36bc1

Browse files
committed
Integrate DataSource into view model
1 parent 2a62402 commit 1a36bc1

File tree

3 files changed

+40
-18
lines changed

3 files changed

+40
-18
lines changed

WooCommerce/Classes/Tools/Zendesk/ZendeskManager.swift

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@ protocol ZendeskManagerProtocol: SupportManagerAdapter {
6464
func generalTags() -> [String]
6565
func wcPayTags() -> [String]
6666

67-
func generalCustomFields() -> [[Int64: String]]
68-
func wcPayCustomFields() -> [[Int64: String]]
67+
func generalCustomFields() -> [Int64: String]
68+
func wcPayCustomFields() -> [Int64: String]
6969
}
7070

7171
struct NoZendeskManager: ZendeskManagerProtocol {
@@ -157,12 +157,12 @@ extension NoZendeskManager {
157157
[]
158158
}
159159

160-
func generalCustomFields() -> [[Int64: String]] {
161-
[]
160+
func generalCustomFields() -> [Int64: String] {
161+
[:]
162162
}
163163

164-
func wcPayCustomFields() -> [[Int64: String]] {
165-
[]
164+
func wcPayCustomFields() -> [Int64: String] {
165+
[:]
166166
}
167167
}
168168

@@ -555,19 +555,23 @@ extension ZendeskManager {
555555
getWCPayTags(supportSourceTag: nil)
556556
}
557557

558-
func generalCustomFields() -> [[Int64: String]] {
558+
func generalCustomFields() -> [Int64: String] {
559559
// Extracts the custom fields from the `createRequest` method
560-
createRequest(supportSourceTag: nil).customFields.compactMap { field in
561-
guard let value = field.value as? String else { return nil } // Guards that all values are string
562-
return [field.fieldId: value]
560+
createRequest(supportSourceTag: nil).customFields.reduce([:]) { dict, field in
561+
guard let value = field.value as? String else { return dict } // Guards that all values are string
562+
var mutableDict = dict
563+
mutableDict[field.fieldId] = value
564+
return mutableDict
563565
}
564566
}
565567

566-
func wcPayCustomFields() -> [[Int64: String]] {
568+
func wcPayCustomFields() -> [Int64: String] {
567569
// Extracts the custom fields from the `createWCPayRequest` method.
568-
createWCPayRequest(supportSourceTag: nil).customFields.compactMap { field in
569-
guard let value = field.value as? String else { return nil } // Guards that all values are string
570-
return [field.fieldId: value]
570+
createWCPayRequest(supportSourceTag: nil).customFields.reduce([:]) { dict, field in
571+
guard let value = field.value as? String else { return dict } // Guards that all values are string
572+
var mutableDict = dict
573+
mutableDict[field.fieldId] = value
574+
return mutableDict
571575
}
572576
}
573577
}

WooCommerce/Classes/ViewRelated/Dashboard/Settings/Help/SupportForm/SupportFormViewModel.swift

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,30 @@ public protocol SupportFormMetaDataSource {
1313

1414
/// Zendesk Custom Fields
1515
///
16-
var customFields: [[Int64: String]] { get }
16+
var customFields: [Int64: String] { get }
1717
}
1818

1919

20-
/// View Model for the support form
20+
/// View Model for the support form.
2121
///
2222
public final class SupportFormViewModel {
2323

24+
/// Zendesk metadata provider.
25+
///
26+
private let dataSource: SupportFormMetaDataSource
27+
28+
init(dataSource: SupportFormMetaDataSource) {
29+
self.dataSource = dataSource
30+
}
31+
32+
/// Submits the support request using the Zendesk Provider.
33+
///
34+
func submitSupportRequest(onCompletion: @escaping (Result<Void, Error>) -> Void) {
35+
ZendeskProvider.shared.createSupportRequest(formID: dataSource.formID,
36+
customFields: dataSource.customFields,
37+
tags: dataSource.tags,
38+
subject: "Temporary Subject",
39+
description: "Temporary Description",
40+
onCompletion: onCompletion)
41+
}
2442
}

WooCommerce/Classes/ViewRelated/Dashboard/Settings/Help/SupportForm/SupportFormsDataSources.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ struct GeneralSupportDataSource: SupportFormMetaDataSource {
1111
ZendeskProvider.shared.generalTags()
1212
}
1313

14-
var customFields: [[Int64: String]] {
14+
var customFields: [Int64: String] {
1515
ZendeskProvider.shared.generalCustomFields()
1616
}
1717
}
@@ -27,7 +27,7 @@ struct WCPaySupportDataSource: SupportFormMetaDataSource {
2727
ZendeskProvider.shared.wcPayTags()
2828
}
2929

30-
var customFields: [[Int64: String]] {
30+
var customFields: [Int64: String] {
3131
ZendeskProvider.shared.wcPayCustomFields()
3232
}
3333
}

0 commit comments

Comments
 (0)