Newer
Older
const { images_url } = require("../constants");
const { fetch_post_article, fetch_article, fetch_update_article } = require("../xhr");
constructor(params) {
this.params = params || {};
output: this.params.data || {
title: "",
subtitle: "",
category: "",
details: [],
images: [],
body: "",
reset() {
this.state.output = {
title: "",
subtitle: "",
category: "",
details: [],
images: [],
body: "",
};
this.state.article_sent = {};
this.refresh();
}
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
handle_text_input(field, e) {
this.state.output[field] = e.target.value;
}
handle_del_detail(index) {
this.state.output.details.splice(index, 1);
this.refresh_details();
}
handle_add_detail() {
this.state.output.details.push({ label: "", value: "" });
this.refresh_details();
}
handle_del_image(index) {
this.state.output.images.splice(index, 1);
this.refresh_images();
}
handle_add_image() {
this.state.output.images.push("")
this.refresh_images();
}
refresh_details() {
obj2htm.subRender(
this.render_details_inputs(),
document.getElementById("create-article-form-details"),
{ mode: "replace" }
);
}
render_details_inputs() {
return {
tag: "ul",
style_rules: {
gridColumn: "1 / span 2",
display: "flex",
flexDirection: "column",
gap: "10px",
listStyleType: "none",
padding: 0,
},
id: "create-article-form-details",
contents: this.state.output.details.map((detail, i) => {
return {
tag: "li",
style_rules: {
display: "grid",
gridTemplateColumns: "200px auto 60px",
gap: "10px",
},
contents: [
{
tag: "input",
type: "text",
placeholder: "Label",
value: detail.label,
oninput: e => {
this.state.output.details[i].label = e.target.value;
}
},
{
tag: "input",
type: "text",
placeholder: "Value",
value: detail.value,
oninput: e => {
this.state.output.details[i].value = e.target.value;
}
},
{
tag: "button", contents: "DEL",
onclick: this.handle_del_detail.bind(this, i)
}
]
}
}).concat([
{
tag: "li", contents: [{
tag: "button", contents: "ADD DETAIL",
onclick: this.handle_add_detail.bind(this)
}]
}
])
}
}
refresh_images() {
obj2htm.subRender(
this.render_images_inputs(),
document.getElementById("create-article-form-images"),
{ mode: "replace" }
);
}
render_images_inputs() {
return {
tag: "ul",
style_rules: {
gridColumn: "1 / span 2",
display: "flex",
flexDirection: "column",
gap: "10px",
listStyleType: "none",
padding: 0,
},
id: "create-article-form-images",
contents: this.state.output.images.map((img, i) => {
return {
tag: "li",
style_rules: {
display: "flex",
alignItems: "center",
{
tag: "div",
style_rules: {
display: "flex",
flexDirection: "center",
alignItems: "center",
justifyContent: "center",
width: "150px",
height: "150px",
overflow: "hidden",
},
contents: [
{
tag: "img",
style_rules: { minWidth: "100%", minHeight: "100%" },
src: img ? `${images_url}/${img}` : "",
}
],
},
{
tag: "input",
type: "text",
placeholder: "image file name",
value: img,
oninput: e => {
this.state.output.images[i] = e.target.value;
}
},
{
tag: "button", contents: "OK",
onclick: this.refresh_images.bind(this)
},
{
tag: "button", contents: "DEL",
onclick: this.handle_del_image.bind(this, i)
}
]
}
}).concat([
{
tag: "li", contents: [{
tag: "button", contents: "ADD IMAGE",
onclick: this.handle_add_image.bind(this)
}]
}
])
}
}
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
render_article_sent() {
const article = this.state.article_sent;
return {
tag: "div",
style_rules: {
maxWidth: "800px",
},
contents: [
{ tag: "button", contents: "RESET", onclick: this.reset.bind(this) },
{ tag: "h2", contents: article.title },
{ tag: "h4", contents: article.subtitle },
{ tag: "p", contents: article.body.replace(/\n/g, "<br>") },
{
tag: "ul", contents: article.details.map(det => {
return {
tag: "li",
style_rules: {
display: "flex",
gap: "20px",
justifyContent: "space-between",
},
contents: [
{ tag: "span", contents: det.label },
{ tag: "span", contents: det.value }
]
};
})
},
{
tag: "div", style_rules: { display: "flex", gap: "10px" },
contents: article.images.map(img => {
return {
tag: "img",
style_rules: { height: "100px", width: "auto" },
src: `${images_url}/${img}`
}
})
}
]
}
}
refresh() {
obj2htm.subRender(
this.render(),
document.getElementById("create-article-form"),
{ mode: "replace" }
);
}
style_rules: {
display: "grid",
maxWidth: "800px",
gridTemplateColumns: "1fr 1fr",
gap: "20px",
},
onsubmit: e => {
e.preventDefault();
const __fetch = this.params.data
? fetch_update_article
: fetch_post_article;
__fetch(this.state.output)
const id = res.insertedId ? res.insertedId.$oid : res._id ? res._id.$oid : undefined;
if (!id) {
alert("error")
} else {
fetch_article(id)
.then(article => {
this.state.article_sent = article;
this.refresh();
})
.catch(er => console.log(er));
}
})
.catch(err => console.log(err))
contents: this.state.article_sent._id ? [this.render_article_sent()] : [
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
{
tag: "input", type: "text", placeholder: "category",
value: this.state.output.category,
oninput: this.handle_text_input.bind(this, "category")
},
{
tag: "input", type: "text",
placeholder: "Article title",
value: this.state.output.title,
oninput: this.handle_text_input.bind(this, "title")
},
{
tag: "input", type: "text",
style_rules: {
gridColumn: "1 / span 2"
},
placeholder: "Article subtitle",
value: this.state.output.subtitle,
oninput: this.handle_text_input.bind(this, "subtitle")
},
{
tag: "textarea",
style_rules: {
gridColumn: "1 / span 2",
height: "300px",
},
value: this.state.output.body,
placeholder: "Article body",
oninput: this.handle_text_input.bind(this, "body")
},
this.render_details_inputs(),
this.render_images_inputs(),
{ tag: "input", type: "submit" }
]
}
}
}
module.exports = CreateArticleForm;