<script>
  let { changePassword = async (currentPassword, newPassword) => {} } = $props();

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

  async function onsubmit(event) {
    event.preventDefault();
    pending = true;
    try {
      await changePassword(currentPassword, newPassword);
      event.target.reset();
    } finally {
      pending = false;
    }
  }
</script>

<form class="form" {onsubmit}>
  <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>
