Perbaikan menu Operator
This commit is contained in:
+60
-357
@@ -684,6 +684,10 @@ Alpine.data('satuRemoteTable', (config) => ({
|
||||
destroyUrl: config.destroyUrl ?? null,
|
||||
token: config.token ?? '',
|
||||
pending: [],
|
||||
// Opsional: formulir tambah/ubah lewat modal ({storeUrl, updateUrl, fields:[{name, default}]}); baris memberi row.form
|
||||
form: config.form ?? null,
|
||||
editing: null,
|
||||
formData: {},
|
||||
|
||||
get window() {
|
||||
const start = Math.max(1, Math.min(this.page - 2, this.pages - 4));
|
||||
@@ -699,6 +703,62 @@ Alpine.data('satuRemoteTable', (config) => ({
|
||||
this.load(1);
|
||||
},
|
||||
|
||||
startInsert() {
|
||||
if (!this.form) return this.demo('Tambah');
|
||||
this.editing = 'new';
|
||||
this.formData = Object.fromEntries(this.form.fields.map((f) => [f.name, Array.isArray(f.default) ? [...f.default] : f.default]));
|
||||
this.openForm();
|
||||
},
|
||||
|
||||
startEdit(row) {
|
||||
if (!this.form || !row.form) return this.demo('Ubah');
|
||||
this.editing = row.id;
|
||||
this.formData = Object.fromEntries(this.form.fields.map((f) => [f.name, Array.isArray(row.form[f.name]) ? [...row.form[f.name]] : (row.form[f.name] ?? (Array.isArray(f.default) ? [] : ''))]));
|
||||
this.openForm();
|
||||
},
|
||||
|
||||
openForm() {
|
||||
const dialog = this.$refs.form;
|
||||
if (dialog && !dialog.open) dialog.showModal();
|
||||
this.$nextTick(() => dialog?.querySelector('input:not([type=hidden]), select, textarea')?.focus());
|
||||
},
|
||||
|
||||
closeForm() {
|
||||
this.$refs.form?.close();
|
||||
},
|
||||
|
||||
formBackdrop(event) {
|
||||
if (event.target === this.$refs.form) this.closeForm();
|
||||
},
|
||||
|
||||
async save() {
|
||||
if (!this.form || this.loading || this.editing === null) return;
|
||||
const isNew = this.editing === 'new';
|
||||
const url = isNew ? this.form.storeUrl : this.form.updateUrl.replace('__ID__', encodeURIComponent(this.editing));
|
||||
const body = Object.fromEntries(Object.entries(this.formData).map(([k, v]) => [k, v === '' ? null : v]));
|
||||
this.loading = true;
|
||||
try {
|
||||
const response = await fetch(url, {
|
||||
method: isNew ? 'POST' : 'PUT',
|
||||
headers: { 'Content-Type': 'application/json', Accept: 'application/json', 'X-CSRF-TOKEN': this.token, 'X-Requested-With': 'XMLHttpRequest' },
|
||||
body: JSON.stringify(body),
|
||||
});
|
||||
const data = await response.json().catch(() => null);
|
||||
if (!response.ok) {
|
||||
const first = data?.errors ? Object.values(data.errors).flat()[0] : null;
|
||||
throw new Error(first || data?.message || 'Data tidak dapat disimpan. Muat ulang halaman lalu coba lagi.');
|
||||
}
|
||||
console.info(`[SIAKAD] ${isNew ? 'POST' : 'PUT'} ${url}`, { hasil: data?.message });
|
||||
window.toast('success', data?.message || 'Data disimpan.');
|
||||
this.closeForm();
|
||||
this.loading = false;
|
||||
this.load(isNew ? 1 : this.page);
|
||||
} catch (error) {
|
||||
window.toast('danger', error.message);
|
||||
this.loading = false;
|
||||
}
|
||||
},
|
||||
|
||||
go(page) {
|
||||
const next = Math.min(Math.max(1, page), this.pages);
|
||||
if (next !== this.page) this.load(next);
|
||||
@@ -1242,61 +1302,6 @@ document.addEventListener('alpine:initialized', () => {
|
||||
}).observe(document.body, { childList: true, subtree: true });
|
||||
});
|
||||
|
||||
// Setting > Setting Prodi rincian (akun DB lama): tab bagian dan modal Edit (PUT JSON, lalu halaman dimuat ulang)
|
||||
Alpine.data('satuSettingProdi', (config) => ({
|
||||
tab: 'krs',
|
||||
form: JSON.parse(JSON.stringify(config.form)),
|
||||
saving: false,
|
||||
|
||||
openForm() {
|
||||
this.form = JSON.parse(JSON.stringify(config.form));
|
||||
const dialog = this.$refs.form;
|
||||
if (dialog && !dialog.open) dialog.showModal();
|
||||
},
|
||||
|
||||
closeForm() {
|
||||
this.$refs.form?.close();
|
||||
},
|
||||
|
||||
backdrop(event) {
|
||||
if (event.target === this.$refs.form) this.closeForm();
|
||||
},
|
||||
|
||||
async save() {
|
||||
if (this.saving) return;
|
||||
const section = (s) => ({ buka: Boolean(s.buka), awal: s.buka ? s.awal || null : null, akhir: s.buka ? s.akhir || null : null });
|
||||
const body = {
|
||||
periode: config.periode,
|
||||
kurikulum: this.form.kurikulum || null,
|
||||
krs: section(this.form.krs),
|
||||
nilai: section(this.form.nilai),
|
||||
registrasi: section(this.form.registrasi),
|
||||
};
|
||||
this.saving = true;
|
||||
try {
|
||||
const response = await fetch(config.url, {
|
||||
method: 'PUT',
|
||||
headers: { 'Content-Type': 'application/json', Accept: 'application/json', 'X-CSRF-TOKEN': config.token, 'X-Requested-With': 'XMLHttpRequest' },
|
||||
body: JSON.stringify(body),
|
||||
});
|
||||
const data = await response.json().catch(() => null);
|
||||
if (!response.ok) {
|
||||
const first = data?.errors ? Object.values(data.errors).flat()[0] : null;
|
||||
throw new Error(first || data?.message || 'Permintaan gagal. Muat ulang halaman lalu coba lagi.');
|
||||
}
|
||||
console.info(`[SIAKAD] PUT ${config.url}`, { hasil: data?.message });
|
||||
window.toast('success', data.message);
|
||||
this.closeForm();
|
||||
window.location.reload();
|
||||
} catch (error) {
|
||||
window.toast('danger', error.message);
|
||||
} finally {
|
||||
this.saving = false;
|
||||
}
|
||||
},
|
||||
}));
|
||||
|
||||
// Setting > Periode Akademik (akun DB lama): daftar di sisi klien, tambah/ubah lewat modal formulir, hapus lewat modal konfirmasi
|
||||
Alpine.data('satuPeriode', (config) => ({
|
||||
rows: config.rows,
|
||||
urls: config.urls,
|
||||
@@ -1430,306 +1435,4 @@ Alpine.data('satuPeriode', (config) => ({
|
||||
},
|
||||
}));
|
||||
|
||||
/* Setting > Kategori Kuesioner Layanan: daftar di sisi klien, tambah/ubah lewat modal satu kolom, hapus lewat konfirmasi. */
|
||||
Alpine.data('satuKategoriKuesioner', (config) => ({
|
||||
rows: config.rows,
|
||||
urls: config.urls,
|
||||
token: config.token,
|
||||
ready: config.ready,
|
||||
q: '',
|
||||
per: config.per,
|
||||
page: 1,
|
||||
editing: null,
|
||||
form: { nama: '' },
|
||||
saving: false,
|
||||
pending: null,
|
||||
|
||||
get filtered() {
|
||||
const q = this.q.trim().toLowerCase();
|
||||
if (q === '') return this.rows;
|
||||
return this.rows.filter((row) => row.nama.toLowerCase().includes(q));
|
||||
},
|
||||
|
||||
get pages() {
|
||||
return Math.max(1, Math.ceil(this.filtered.length / this.per));
|
||||
},
|
||||
|
||||
get view() {
|
||||
const start = (this.page - 1) * this.per;
|
||||
return this.filtered.slice(start, start + this.per);
|
||||
},
|
||||
|
||||
get window() {
|
||||
const start = Math.max(1, Math.min(this.page - 2, this.pages - 4));
|
||||
return Array.from({ length: Math.min(5, this.pages) }, (_, i) => start + i);
|
||||
},
|
||||
|
||||
reset() {
|
||||
this.page = 1;
|
||||
},
|
||||
|
||||
go(page) {
|
||||
this.page = Math.min(Math.max(1, page), this.pages);
|
||||
},
|
||||
|
||||
startInsert() {
|
||||
this.editing = 'new';
|
||||
this.form = { nama: '' };
|
||||
this.openForm();
|
||||
},
|
||||
|
||||
startEdit(row) {
|
||||
this.editing = row.id;
|
||||
this.form = { nama: row.nama };
|
||||
this.openForm();
|
||||
},
|
||||
|
||||
openForm() {
|
||||
const dialog = this.$refs.form;
|
||||
if (dialog && !dialog.open) dialog.showModal();
|
||||
this.$nextTick(() => this.$refs.nama?.focus());
|
||||
},
|
||||
|
||||
closeDialog(ref) {
|
||||
this.$refs[ref]?.close();
|
||||
},
|
||||
|
||||
backdrop(event, ref) {
|
||||
if (event.target === this.$refs[ref]) this.closeDialog(ref);
|
||||
},
|
||||
|
||||
async request(url, method, body) {
|
||||
const response = await fetch(url, {
|
||||
method,
|
||||
headers: { 'Content-Type': 'application/json', Accept: 'application/json', 'X-CSRF-TOKEN': this.token, 'X-Requested-With': 'XMLHttpRequest' },
|
||||
body: body === undefined ? undefined : JSON.stringify(body),
|
||||
});
|
||||
const data = await response.json().catch(() => null);
|
||||
if (!response.ok) {
|
||||
const first = data?.errors ? Object.values(data.errors).flat()[0] : null;
|
||||
throw new Error(first || data?.message || 'Permintaan gagal. Muat ulang halaman lalu coba lagi.');
|
||||
}
|
||||
console.info(`[SIAKAD] ${method} ${url}`, { hasil: data?.message });
|
||||
return data;
|
||||
},
|
||||
|
||||
async save() {
|
||||
if (this.saving || this.editing === null) return;
|
||||
if (this.form.nama.trim() === '') {
|
||||
window.toast('warning', 'Nama kategori wajib diisi.');
|
||||
this.$refs.nama?.focus();
|
||||
return;
|
||||
}
|
||||
const isNew = this.editing === 'new';
|
||||
const url = isNew ? this.urls.store : this.urls.update.replace('__ID__', this.editing);
|
||||
this.saving = true;
|
||||
try {
|
||||
const data = await this.request(url, isNew ? 'POST' : 'PUT', { nama: this.form.nama.trim() });
|
||||
this.rows = data.rows;
|
||||
if (this.page > this.pages) this.page = this.pages;
|
||||
window.toast('success', data.message);
|
||||
this.closeDialog('form');
|
||||
} catch (error) {
|
||||
window.toast('danger', error.message);
|
||||
} finally {
|
||||
this.saving = false;
|
||||
}
|
||||
},
|
||||
|
||||
askRemove(row) {
|
||||
this.pending = row;
|
||||
const dialog = this.$refs.dialog;
|
||||
if (dialog && !dialog.open) dialog.showModal();
|
||||
},
|
||||
|
||||
async confirmRemove() {
|
||||
if (!this.pending || this.saving) return;
|
||||
this.saving = true;
|
||||
try {
|
||||
const data = await this.request(this.urls.destroy.replace('__ID__', this.pending.id), 'DELETE');
|
||||
this.rows = data.rows;
|
||||
if (this.page > this.pages) this.page = this.pages;
|
||||
window.toast('success', data.message);
|
||||
this.closeDialog('dialog');
|
||||
} catch (error) {
|
||||
window.toast('danger', error.message);
|
||||
} finally {
|
||||
this.saving = false;
|
||||
}
|
||||
},
|
||||
}));
|
||||
|
||||
/* Setting > Jenis Jawaban: daftar di sisi klien dengan centang baris, modal nama + daftar jawaban, hapus massal. */
|
||||
Alpine.data('satuJenisJawaban', (config) => ({
|
||||
rows: config.rows,
|
||||
urls: config.urls,
|
||||
token: config.token,
|
||||
ready: config.ready,
|
||||
q: '',
|
||||
per: config.per,
|
||||
page: 1,
|
||||
picked: [],
|
||||
editing: null,
|
||||
form: { nama: '', jawaban: [] },
|
||||
keySeq: 0,
|
||||
saving: false,
|
||||
pending: [],
|
||||
|
||||
get filtered() {
|
||||
const q = this.q.trim().toLowerCase();
|
||||
if (q === '') return this.rows;
|
||||
return this.rows.filter((row) => row.nama.toLowerCase().includes(q) || row.jawaban.some((a) => a.teks.toLowerCase().includes(q)));
|
||||
},
|
||||
|
||||
get pages() {
|
||||
return Math.max(1, Math.ceil(this.filtered.length / this.per));
|
||||
},
|
||||
|
||||
get view() {
|
||||
const start = (this.page - 1) * this.per;
|
||||
return this.filtered.slice(start, start + this.per);
|
||||
},
|
||||
|
||||
get window() {
|
||||
const start = Math.max(1, Math.min(this.page - 2, this.pages - 4));
|
||||
return Array.from({ length: Math.min(5, this.pages) }, (_, i) => start + i);
|
||||
},
|
||||
|
||||
get allPicked() {
|
||||
return this.view.length > 0 && this.view.every((row) => this.picked.includes(row.id));
|
||||
},
|
||||
|
||||
get pendingLabel() {
|
||||
const names = this.rows.filter((row) => this.pending.includes(row.id)).map((row) => row.nama);
|
||||
return names.length > 3 ? `${names.length} jenis jawaban` : names.join(', ');
|
||||
},
|
||||
|
||||
reset() {
|
||||
this.page = 1;
|
||||
},
|
||||
|
||||
go(page) {
|
||||
this.page = Math.min(Math.max(1, page), this.pages);
|
||||
},
|
||||
|
||||
toggleAll() {
|
||||
const ids = this.view.map((row) => row.id);
|
||||
this.picked = this.allPicked ? this.picked.filter((id) => !ids.includes(id)) : [...new Set([...this.picked, ...ids])];
|
||||
},
|
||||
|
||||
answer(nilai = '', teks = '') {
|
||||
return { key: ++this.keySeq, nilai, teks };
|
||||
},
|
||||
|
||||
addAnswer() {
|
||||
const last = this.form.jawaban[this.form.jawaban.length - 1];
|
||||
this.form.jawaban.push(this.answer(Number.isInteger(last?.nilai) ? last.nilai + 1 : 1));
|
||||
},
|
||||
|
||||
removeAnswer(i) {
|
||||
if (this.form.jawaban.length > 1) this.form.jawaban.splice(i, 1);
|
||||
},
|
||||
|
||||
startInsert() {
|
||||
this.editing = 'new';
|
||||
this.form = { nama: '', jawaban: [this.answer(1)] };
|
||||
this.openForm();
|
||||
},
|
||||
|
||||
startEdit(row) {
|
||||
this.editing = row.id;
|
||||
this.form = { nama: row.nama, jawaban: row.jawaban.map((a) => this.answer(a.nilai, a.teks)) };
|
||||
this.openForm();
|
||||
},
|
||||
|
||||
openForm() {
|
||||
const dialog = this.$refs.form;
|
||||
if (dialog && !dialog.open) dialog.showModal();
|
||||
this.$nextTick(() => this.$refs.nama?.focus());
|
||||
},
|
||||
|
||||
closeDialog(ref) {
|
||||
this.$refs[ref]?.close();
|
||||
},
|
||||
|
||||
backdrop(event, ref) {
|
||||
if (event.target === this.$refs[ref]) this.closeDialog(ref);
|
||||
},
|
||||
|
||||
async request(url, method, body) {
|
||||
const response = await fetch(url, {
|
||||
method,
|
||||
headers: { 'Content-Type': 'application/json', Accept: 'application/json', 'X-CSRF-TOKEN': this.token, 'X-Requested-With': 'XMLHttpRequest' },
|
||||
body: body === undefined ? undefined : JSON.stringify(body),
|
||||
});
|
||||
const data = await response.json().catch(() => null);
|
||||
if (!response.ok) {
|
||||
const first = data?.errors ? Object.values(data.errors).flat()[0] : null;
|
||||
throw new Error(first || data?.message || 'Permintaan gagal. Muat ulang halaman lalu coba lagi.');
|
||||
}
|
||||
console.info(`[SIAKAD] ${method} ${url}`, { hasil: data?.message });
|
||||
return data;
|
||||
},
|
||||
|
||||
async save() {
|
||||
if (this.saving || this.editing === null) return;
|
||||
if (this.form.nama.trim() === '') {
|
||||
window.toast('warning', 'Nama jenis jawaban wajib diisi.');
|
||||
this.$refs.nama?.focus();
|
||||
return;
|
||||
}
|
||||
const jawaban = this.form.jawaban.filter((a) => a.teks.trim() !== '').map((a) => ({ nilai: Number(a.nilai), teks: a.teks.trim() }));
|
||||
if (jawaban.length === 0) {
|
||||
window.toast('warning', 'Isi minimal satu jawaban.');
|
||||
return;
|
||||
}
|
||||
const isNew = this.editing === 'new';
|
||||
const url = isNew ? this.urls.store : this.urls.update.replace('__ID__', this.editing);
|
||||
this.saving = true;
|
||||
try {
|
||||
const data = await this.request(url, isNew ? 'POST' : 'PUT', { nama: this.form.nama.trim(), jawaban });
|
||||
this.rows = data.rows;
|
||||
if (this.page > this.pages) this.page = this.pages;
|
||||
window.toast('success', data.message);
|
||||
this.closeDialog('form');
|
||||
} catch (error) {
|
||||
window.toast('danger', error.message);
|
||||
} finally {
|
||||
this.saving = false;
|
||||
}
|
||||
},
|
||||
|
||||
remove() {
|
||||
if (!this.picked.length) {
|
||||
window.toast('warning', 'Pilih data yang akan dihapus terlebih dahulu.');
|
||||
return;
|
||||
}
|
||||
this.askRemove(this.picked);
|
||||
},
|
||||
|
||||
askRemove(ids) {
|
||||
this.pending = [...ids];
|
||||
const dialog = this.$refs.dialog;
|
||||
if (dialog && !dialog.open) dialog.showModal();
|
||||
},
|
||||
|
||||
async confirmRemove() {
|
||||
const ids = [...this.pending];
|
||||
if (!ids.length || this.saving) return;
|
||||
this.saving = true;
|
||||
try {
|
||||
const data = await this.request(this.urls.destroy, 'POST', { ids });
|
||||
this.rows = data.rows;
|
||||
this.picked = this.picked.filter((id) => !ids.includes(id));
|
||||
if (this.page > this.pages) this.page = this.pages;
|
||||
window.toast('success', data.message);
|
||||
this.closeDialog('dialog');
|
||||
} catch (error) {
|
||||
window.toast('danger', error.message);
|
||||
} finally {
|
||||
this.saving = false;
|
||||
}
|
||||
},
|
||||
}));
|
||||
|
||||
Alpine.start();
|
||||
|
||||
Reference in New Issue
Block a user