Login
Saat ini login masih dihardcode di component Navigation, seharusnya halaman login diakses melalui form login. Sebagai contoh, kita akan membuat form login di halaman home. Selain form login kita juga akan membutuhkan toast notification, dan fetch api.
Toast
buat file src/helper/toast.js
import { writable, derived } from "svelte/store"
const TIMEOUT = 2000
function createNotificationStore (timeout) {
const _notifications = writable([])
function send (message, type = "default", timeout) {
_notifications.update(state => {
return [...state, { id: id(), type, message, timeout }]
})
}
let timers = []
const notifications = derived(_notifications, ($_notifications, set) => {
set($_notifications)
if ($_notifications.length > 0) {
const timer = setTimeout(() => {
_notifications.update(state => {
state.shift()
return state
})
}, $_notifications[0].timeout)
return () => {
clearTimeout(timer)
}
}
})
const { subscribe } = notifications
return {
subscribe,
send,
default: (msg, timeout = TIMEOUT) => send(msg, "default", timeout),
danger: (msg, timeout = TIMEOUT) => send(msg, "danger", timeout),
warning: (msg, timeout = TIMEOUT) => send(msg, "warning", timeout),
info: (msg, timeout = TIMEOUT) => send(msg, "info", timeout),
success: (msg, timeout = TIMEOUT) => send(msg, "success", timeout),
}
}
function id() {
return '_' + Math.random().toString(36).substr(2, 9);
};
export const notifications = createNotificationStore()Buat file src/components/Toast.svelte
Pasang component Toast di file src/App.svelte
Form Login
Buat komponen Input pada file src/components/Input.svelte
Buat komponen Button pada file src/components/Button.svelte
Pada button dibuat suatu loader, ide-nya ketika button diklik, maka akan muncul loader selama sekian detik (sekitar 1,5 detik). Untuk buat file src/components/Loader/svelte
Update halaman src/routes/Home.svelte untuk menambahkan form login
Karena fungsi login sdh diambil alih di halaman Home, maka komponen Navigation kita update untuk menghapus kode tentang login.
Call API
Saat ini, fungsi loginCall() pada halaman Home masih dihardcode. Pada aplikasi sesungguhnya fungsi ini memanggil api dari backend melalui Ajax.
Ubah file src/routes/Home.svelte
Last updated
Was this helpful?