summaryrefslogtreecommitdiff
path: root/Rune/Views/Domains/RecordAddView.swift
blob: 8dfa81dbd96a1b5a94b978ae9bbeabfce346a6a7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import SwiftUI

struct RecordAddView: View {
    let domainName: String
    @ObservedObject var viewModel: DomainViewModel
    let client: NjallaClient

    @Environment(\.dismiss) private var dismiss

    @State private var draft = DNSRecordDraft()
    @State private var localErrorMessage: String?
    var body: some View {
        Form {
            DNSRecordFormSections(draft: $draft)

            Section {
                Button("Save") {
                    Task {
                        await save()
                    }
                }
                .disabled(viewModel.isSaving || !draft.canSubmit)
            }
        }
        .navigationTitle("Add Record")
        .navigationBarTitleDisplayMode(.inline)
        .toolbar {
            ToolbarItem(placement: .cancellationAction) {
                Button("Cancel", role: .cancel) {
                    dismiss()
                }
            }
        }
        .onChange(of: draft.type) { oldValue, newValue in
            guard oldValue != newValue else { return }
            draft.resetTypeSpecificFields()
        }
        .alert("API Error", isPresented: localErrorBinding) {
            Button("OK", role: .cancel) {}
        } message: {
            Text(localErrorMessage ?? "")
        }
    }

    private func save() async {
        do {
            try await viewModel.addRecord(for: domainName, draft: draft, client: client)
            dismiss()
        } catch is CancellationError {
            return
        } catch {
            if (error as? URLError)?.code == .cancelled {
                return
            }
            localErrorMessage = error.localizedDescription
        }
    }

    private var localErrorBinding: Binding<Bool> {
        Binding(
            get: { localErrorMessage != nil },
            set: { newValue in
                if !newValue {
                    localErrorMessage = nil
                }
            }
        )
    }
}

struct DNSRecordFormSections: View {
    @Binding var draft: DNSRecordDraft

    var body: some View {
        Section("Record") {
            Picker("Type", selection: $draft.type) {
                ForEach(DNSRecordType.allCases) { type in
                    Text(type.rawValue).tag(type)
                }
            }

            TextField("Name", text: $draft.name)
                .textInputAutocapitalization(.never)
                .autocorrectionDisabled()
        }

        if draft.type.usesContent {
            Section("Content") {
                TextField("Content", text: $draft.content, axis: .vertical)
                    .textInputAutocapitalization(.never)
                    .autocorrectionDisabled()
            }
        }

        if draft.type.usesTTL {
            Section("TTL") {
                TextField("TTL", text: $draft.ttl)
                    .keyboardType(.numberPad)
            }
        }

        if draft.type.usesPriority {
            Section("Priority") {
                TextField("Priority", text: $draft.prio)
                    .keyboardType(.numberPad)
            }
        }

        if draft.type.usesWeight {
            Section("Weight") {
                TextField("Weight", text: $draft.weight)
                    .keyboardType(.numberPad)
            }
        }

        if draft.type.usesPort {
            Section("Port") {
                TextField("Port", text: $draft.port)
                    .keyboardType(.numberPad)
            }
        }

        if draft.type.usesTarget {
            Section("Target") {
                TextField("Target", text: $draft.target)
                    .textInputAutocapitalization(.never)
                    .autocorrectionDisabled()
            }
        }

        if draft.type.usesSSHFields {
            Section {
                TextField("SSH Algorithm", text: $draft.sshAlgorithm)
                    .keyboardType(.numberPad)
                TextField("SSH Type", text: $draft.sshType)
                    .keyboardType(.numberPad)
            } header: {
                Text("SSHFP")
            } footer: {
                Text("Algorithm values: 1-5. Type values: 1-2.")
            }
        }
    }
}