-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadd.js
More file actions
123 lines (112 loc) · 3 KB
/
add.js
File metadata and controls
123 lines (112 loc) · 3 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
var html = require('choo/html')
var Content = require('./index')
/**
* Class representing Add component.
* Use it to produce content forms.
* @exports Add
* @example
class UserAdd extends Add {
static identity (props) {
return 'user-new'
}
constructor(arg) {
super(arg)
this.user = {}
}
save() {
return client.mutate({
mutation: gql`
mutation($name: String!) {
createUser(name: $name) {
id name
}
}
`,
variables: this.user
}).then(res => {
this.submitting = false
this.emit('pushState', '/users')
})
}
form (props) {
if (!this.user) return ''
return html `
<div>
${inputContainer('Name', input({
name: 'user-name',
placeholder: 'Full name',
value: this.user.name,
onChange: (e) => this.user.name = e.target.value
}))}
</div>
`
}
}
*/
class Add extends Content {
/**
* @param {String} id - Id of component
* @param {Object} state
* @param {function} emit
*/
constructor () {
super(...arguments)
this.submitting = false
}
/**
* Implement it in extended class to create the form.
* @return {HTMLElement}
*/
form () {
throw new Error('content:Add form() should be implemented.')
}
/**
* Presave hook method. Called when form is submitted.
* If you really don't want to use is use save() instead.
* @param {Event} e - Submit event.
* @return {Object} - Returns what's returned from save()
*/
presave (e) {
e.preventDefault()
this.submitting = true
this.rerender()
return this.save(e)
}
/**
* Should be implemented in extended class to handle form submission.
* @param {Event} e - Submit event.
*/
save (e) {
throw new Error('content:Add save() should be implemented.')
}
/**
* Base render method. Shouldn't be extended in most cases.
* @param {Object} props - Props send with render()
* @return {HTMLElement}
*/
createElement (props) {
function handleCancel (e) {
e.preventDefault()
window.history.back()
}
return html`
<div class="content add">
${this.header(props)}
<div class="bg-white shadow-md rounded px-8 pt-6 pb-8 mb-4">
<form onsubmit=${this.presave.bind(this)} class="md:w-1/2">
${this.form(props)}
<div>
<a href=${document.referrer} onclick=${handleCancel} class="no-underline hover:bg-red bg-transparent text-red hover:text-white py-2 px-4 rounded">İptal</a>
<button class="bg-indigo hover:bg-indigo-dark text-white font-bold py-2 px-4 rounded ${this.submitting ? 'opacity-50 cursor-not-allowed' : ''}"
type="submit" disabled=${this.submitting}>
Kaydet
</button>
</div>
</form>
</div>
${this.footer(props)}
</div>
`
}
}
module.exports = Add