Repository navigation
Expand file tree
/
Copy pathapp.js
More file actions
138 lines (117 loc) · 4.25 KB
/
Copy pathapp.js
File metadata and controls
138 lines (117 loc) · 4.25 KB
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
import * as couch from '/couch.js'
const RepositoriesListEntry = (properties, doc) => {
const template = properties.templates.repositoriesListEntry
const clone = document.importNode(template.content, true)
const li = clone.querySelector('li')
li.innerText = doc.text
li.setAttribute('data-id', doc.id)
li.className = doc.provisionedAt ? 'provisioned' : 'requested'
return clone
}
const ActivitiesListEntry = (properties, doc) => {
const template = properties.templates.activitiesListEntry
const clone = document.importNode(template.content, true)
const li = clone.querySelector('li')
li.innerText = doc.text
return clone
}
const Dashboard = async (properties, username) => {
const template = properties.templates.dashboard
const clone = document.importNode(template.content, true)
clone.querySelector('h2').innerText = `Hi ${username},`
const form = clone.querySelector('form')
const reponameInput = clone.querySelector('input[name=reponame]')
form.onsubmit = async e => {
e.preventDefault()
const name = reponameInput.value
const response = await couch.createRepoRequest(username, name)
if (response.ok) {
reponameInput.value = ''
}
}
const repositoriesList = clone.querySelector('#repositories-list')
const activitiesList = clone.querySelector('#activities-list')
const repositoriesListElements = {}
couch.getChanges(username, change => {
const parts = change.id.split(':')
const [type, name, subtype, subname, subsubtype, subsubname] = parts
if (type === 'repo' && parts.length === 2) {
const entry = RepositoriesListEntry(properties, {
id: change.id,
text: `http://localhost:8080/${username}/${name}.git`,
provisionedAt: change.doc.provisionedAt
})
const existing = repositoriesList.querySelector(`[data-id="${change.id}"]`)
if (existing) {
repositoriesList.replaceChild(entry, existing)
} else {
repositoriesList.prepend(entry)
}
}
if (type === 'repo' && subtype === 'branch' && subsubtype === 'ref' && parts.length === 6) {
const entry = ActivitiesListEntry(properties, {
text: `pushed to repo ${name} on branch ${subname} ref ${subsubname.slice(0, 7)}`
})
activitiesList.prepend(entry)
}
})
const logoutLink = clone.querySelector('a')
logoutLink.onclick = async e => {
e.preventDefault()
const response = await couch.deleteSession()
if (response.ok) {
LoginForm(properties)
}
}
properties.elements.article.innerHTML = ''
properties.elements.article.appendChild(clone)
}
const SignupForm = properties => {
const template = properties.templates.signupForm
const clone = document.importNode(template.content, true)
const form = clone.querySelector('form')
const usernameInput = clone.querySelector('input[name=username]')
const passwordInput = clone.querySelector('input[name=password]')
form.onsubmit = async e => {
e.preventDefault()
const name = usernameInput.value
const password = passwordInput.value
const response = await couch.createUser(name, password)
if (response.ok) {
await LoginForm(properties)
}
}
properties.elements.article.innerHTML = ''
properties.elements.article.appendChild(clone)
}
const LoginForm = properties => {
const template = properties.templates.loginForm
const clone = document.importNode(template.content, true)
const form = clone.querySelector('form')
const usernameInput = clone.querySelector('input[name=username]')
const passwordInput = clone.querySelector('input[name=password]')
form.onsubmit = async e => {
e.preventDefault()
const name = usernameInput.value
const password = passwordInput.value
const response = await couch.createSession(name, password)
if (response.ok) {
await Dashboard(properties, name)
}
}
const signupLink = clone.querySelector('a')
signupLink.onclick = e => {
e.preventDefault()
SignupForm(properties)
}
properties.elements.article.innerHTML = ''
properties.elements.article.appendChild(clone)
}
export const App = async properties => {
const response = await couch.getSession()
if (response.ok && response.userCtx.name) {
Dashboard(properties, response.userCtx.name)
} else {
LoginForm(properties)
}
}