<script>
  import { changePassword } from '$lib/apiServer.js';

  let currentPassword = $state(''),
    newPassword = $state(''),
    confirmPassword = $state(''),
    pending = $state(false),
    form;
  let valid = $derived(newPassword === confirmPassword && newPassword !== currentPassword);
  let disabled = $derived(pending || !valid);

  async function onsubmit(event) {
    event.preventDefault();
    pending = true;
    let response = await changePassword(currentPassword, newPassword);
    switch (response.status) {
      case 200:
        form.reset();
        break;
    }
    pending = false;
  }
</script>

<form {onsubmit} bind:this={form}>
  <label
    >current password
    <input
      name="currentPassword"
      type="password"
      placeholder="password"
      bind:value={currentPassword}
    />
  </label>

  <label
    >new password
    <input
      name="newPassword"
      type="password"
      placeholder="password"
      bind:value={newPassword}
    />
  </label>

  <label
    >confirm new password
    <input
      name="confirmPassword"
      type="password"
      placeholder="password"
      bind:value={confirmPassword}
    />
  </label>

  <button type="submit" {disabled}>change password</button>
</form>
