Files

64 lines
2.2 KiB
HTML
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
{% extends "base.html" %}
{% block title %}编辑帖子 - {{ site_name }}{% endblock %}
{% block content %}
<div class="page-title">
<h1>编辑帖子</h1>
<p><a href="/my/topics/">← 返回我的帖子</a></p>
</div>
<div class="card">
<div class="card-body">
<div id="error-box" style="display:none;margin-bottom:12px;" class="form-error"></div>
<form id="edit-form">
<div class="form-group">
<label>标题</label>
<input type="text" name="title" id="edit-title" class="form-control" required>
</div>
<div class="form-group">
<label>分类</label>
<select name="category" id="edit-category" class="form-control"></select>
</div>
<div class="form-group">
<label>内容(支持 Markdown)</label>
<textarea name="content" id="edit-content" class="form-control" required></textarea>
</div>
<button type="submit" class="btn btn-primary">保存</button>
<a href="/my/topics/" class="btn">取消</a>
</form>
</div>
</div>
<script>
(async function() {
var id = window.location.pathname.match(/\/my\/topics\/(\d+)\/edit\//)[1];
var res = await fetch('/api/my/topics/' + id + '/');
var data = await res.json();
if (!data.success) return;
document.getElementById('edit-title').value = data.topic.title;
document.getElementById('edit-content').value = data.topic.content;
var sel = document.getElementById('edit-category');
sel.innerHTML = data.categories.map(function(c) {
var selected = c.value === data.topic.category ? ' selected' : '';
return '<option value="' + c.value + '"' + selected + '>' + c.label + '</option>';
}).join('');
document.getElementById('edit-form').addEventListener('submit', async function(e) {
e.preventDefault();
var fd = new FormData(this);
fd.append('csrf_token', CSRF_TOKEN);
var r = await fetch('/my/topics/' + id + '/edit/', { method: 'POST', body: fd });
var j = await r.json();
if (j.success) {
window.location.href = '/my/topics/';
} else {
var box = document.getElementById('error-box');
box.textContent = j.error || '保存失败';
box.style.display = 'block';
}
});
})();
</script>
{% endblock %}