aboutsummaryrefslogtreecommitdiff
path: root/src/routes/Records.js
blob: 2a21fab5bc8eb1ba48d4ccadebe0f1680fe31556 (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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
// Meow meow meow meow
import RecordModal from '../components/RecordModal'
import RecordTable from '../components/RecordTable'
import { RequireAuth, useAuth } from '../hooks/useAuth'
import { useErrorModal } from '../hooks/useErrorModal'
import './Records.css'
import React, { useEffect } from 'react'
import { useParams } from 'react-router-dom'

export function Records() {
  const { zoneName } = useParams()
  const [zone, setZone] = React.useState()
  const [showModal, setShowModal] = React.useState(false)
  const [openRecord, setOpenRecord] = React.useState({})
  const [originalRecord, setOriginalRecord] = React.useState({})
  const [updateSignal, setUpdateSignal] = React.useState(0)

  const auth = useAuth()
  const errorModal = useErrorModal()

  function setAndOpenRecord(record) {
    setOpenRecord(record)
    setOriginalRecord(record)
    setShowModal(true)
  }

  function saveRecord() {
    let updatedName = openRecord.name
    if (!updatedName.endsWith('.')) {
      updatedName += '.'
    }
    if (!updatedName.endsWith(zoneName)) {
      updatedName += zoneName
    }
    const record = { ...openRecord, name: updatedName }
    if (record.name !== originalRecord.name && originalRecord.name !== '') {
      fetch(`/api/v1/zones/${zoneName}/${record.id}`, {
        method: 'DELETE',
        headers: {
          Authorization: `Bearer ${auth.token}`,
          'Content-Type': 'application/json',
        },
      }).then((res) => {
        if (res.status === 200) {
        } else {
          res
            .json()
            .then((data) => {
              errorModal.show(data.error)
            })
            .catch((err) => {
              errorModal.show('Unknown error updating record')
            })
        }
      })
    }
    if (record.id != null) {
      fetch(`/api/v1/zones/${zoneName}/${record.id}`, {
        method: 'PUT',
        headers: {
          Authorization: `Bearer ${auth.token}`,
          'Content-Type': 'application/json',
        },
        body: JSON.stringify(record),
      }).then((res) => {
        if (res.status === 200) {
          setShowModal(false)
          setUpdateSignal(updateSignal + 1)
          setOpenRecord({})
          setOriginalRecord({})
        } else {
          res
            .json()
            .then((data) => {
              errorModal.show(data.error)
            })
            .catch((err) => {
              errorModal.show('Unknown error updating record')
            })
        }
      })
    } else {
      fetch(`/api/v1/zones/${zoneName}`, {
        method: 'PUT',
        headers: {
          Authorization: `Bearer ${auth.token}`,
          'Content-Type': 'application/json',
        },
        body: JSON.stringify(record),
      }).then((res) => {
        if (res.status === 200) {
          setShowModal(false)
          setUpdateSignal(updateSignal + 1)
          setOpenRecord({})
          setOriginalRecord({})
        } else {
          res
            .json()
            .then((data) => {
              errorModal.show(data.error)
            })
            .catch((err) => {
              errorModal.show('Unknown error creating record')
            })
        }
      })
    }
  }

  function deleteRecord(id) {
    fetch(`/api/v1/zones/${zoneName}/${id}`, {
      method: 'DELETE',
      headers: {
        Authorization: `Bearer ${auth.token}`,
        'Content-Type': 'application/json',
      },
    }).then((res) => {
      if (res.status === 200) {
        setUpdateSignal(updateSignal + 1)
      } else {
        res
          .json()
          .then((data) => {
            errorModal.show(data.error)
          })
          .catch((err) => {
            errorModal.show('Unknown error deleting record')
          })
      }
    })
  }

  // Load zone data on page load
  useEffect(() => {
    fetch(`/api/v1/zones/${zoneName}`, {
      headers: {
        Authorization: `Bearer ${auth.token}`,
      },
    }).then((res) => {
      if (res.status === 200) {
        res.json().then((data) => {
          setZone(data)
        })
      } else {
        res
          .json()
          .then((data) => {
            errorModal.show(data.error)
          })
          .catch((err) => {
            errorModal.show('Unknown error loading zone')
          })
      }
    })
  }, [zoneName, auth.token, updateSignal])

  return (
    <RequireAuth>
      <main className="records-table-container">
        <h1>
          DNS Records for <b>{zoneName}</b>
        </h1>
        {zone && (
          <RecordTable
            records={zone}
            setAndOpenRecord={setAndOpenRecord}
            deleteRecord={deleteRecord}
          ></RecordTable>
        )}
        <RecordModal
          showModal={showModal}
          setShowModal={setShowModal}
          openRecord={openRecord}
          setOpenRecord={setOpenRecord}
          saveRecord={saveRecord}
        />
      </main>
    </RequireAuth>
  )
}

export default Records