<script>
    import ErrorAlert from "../common/ErrorAlert.svelte";
    import SpinnerButton from "../common/SpinnerButton.svelte";
    import Avatar from "./Avatar.svelte";
    import {getContext} from "svelte";

    const user = getContext("user");
    const channel = getContext("channel");
    let name = user.name;
    let email = user.email;
    let errorMessage = "";

    function saveName(e) {
        e.preventDefault();
        errorMessage = "";

        axios
            .post("/account/update-name", {
                name: name,
            })
            .then((response) => {
                // window.reload();
            })
            .catch((error) => {
                errorMessage = error.response?.data.error;
                console.log({errorMessage});
            });
    }

    function saveEmail(e) {
        e.preventDefault();
        errorMessage = "";

        axios
            .post("/account/update-email", {
                email: email,
            })
            .then((response) => {
                // window.reload();
            })
            .catch((error) => {
                errorMessage = error.response?.data.error;
                console.log({errorMessage});
            });
    }
</script>


<div class="wrapper-tiny">
    <ErrorAlert message={errorMessage}/>
    <h3 class="header-small mb-5">
        <Avatar name={user.name}/>
    </h3>
    <form on:submit={saveName}>
        <div class="input-group mb-3">
            <input
                type="text"
                bind:value={name}
                class="form-control"
                placeholder="Name"
            />
            <SpinnerButton label="Update"/>
        </div>
    </form>

    <div class="list-group">
        <a class="list-group-item list-group-item-action" href="{ channel.lucentUrl }/logout">Logout from this device</a>
    </div>
</div>
