Skip to content

armgit / lyftr

Public Imported from GitHub
0

Self-hosted fitness tracker. Log workouts, track nutrition, and monitor progress. Docker deployment, mobile-friendly.

6.2 KB
Raw Blame
import { useState } from 'react'
import { Pressable, ScrollView, View } from 'react-native'
import { router } from 'expo-router'
import { ArrowLeft, ShieldCheck } from 'lucide-react-native'
import { useAsyncAction, useTranslation, differentRuleLabel, lengthRuleLabel, matchRuleLabel, newPasswordRules } from '@lyftr/shared'
import {
  Alert,
  AppText,
  Button,
  Card,
  PageHeader,
  PasswordField,
  PasswordRule,
  Screen,
} from '../../../src/components/ui'
import { client } from '../../../src/lib/lyftr'
import { useTheme } from '../../../src/theme/useTheme'

// Its own screen rather than a panel inside Settings, matching web's /settings/password.
// Web needs the route so /.well-known/change-password has somewhere to redirect; mobile
// follows so the two platforms stay one flow, and so this becomes deep-linkable if app
// links are ever added.
//
// Laid out beat for beat with that page — back link, header, the form on a card, and a
// centred confirmation card on success — so the two do not merely do the same thing, they
// read as the same screen.
export default function ChangePasswordScreen() {
  const { t } = useTranslation()
  const { colors, brand } = useTheme()

  const [current, setCurrent] = useState('')
  const [next, setNext] = useState('')
  const [confirm, setConfirm] = useState('')
  const [done, setDone] = useState(false)

  const rules = newPasswordRules({ password: next, confirm, current })

  const back = () => (router.canGoBack() ? router.back() : router.navigate('/settings'))

  const save = useAsyncAction(async () => {
    // The shared client persists the returned token pair, so this device stays signed
    // in while every other one is evicted.
    await client.userAPI.changePassword({ current_password: current, new_password: next })
    setCurrent(''); setNext(''); setConfirm('')
    setDone(true)
  }, t('settings.account.password.changeFailed'))

  const submit = async () => {
    // The rules render under the fields as the user types and the button stays disabled
    // until they pass, so this is a backstop. The alert below is server failures only —
    // a wrong current password, or a change that landed elsewhere first.
    if (!rules.ready) return

    void save.run()
  }

  const backLink = (
    <Pressable
      onPress={back}
      hitSlop={8}
      className="flex-row items-center gap-1.5 self-start active:opacity-60"
    >
      <ArrowLeft size={16} color={colors.txMuted} />
      <AppText variant="body" color="muted">{t('settings.account.backLink')}</AppText>
    </Pressable>
  )

  // Success replaces the form rather than sitting above it. Three filled-looking boxes
  // under a success banner invite a second submit that could only fail — the current
  // password is no longer current. The card carries its own heading, so the page header
  // is dropped here rather than saying the same words twice.
  if (done) {
    return (
      <Screen>
        <ScrollView showsVerticalScrollIndicator={false}>
          <View className="gap-5 py-4">
            {backLink}
            <Card className="items-center gap-3 p-6">
              <View className="h-12 w-12 items-center justify-center rounded-full border border-success-500/20 bg-success-500/10">
                <ShieldCheck size={24} color={brand.successSoft} strokeWidth={2.2} />
              </View>
              <View className="gap-1">
                <AppText variant="title" className="text-center">{t('settings.account.password.changedTitle')}</AppText>
                <AppText variant="body" color="muted" className="text-center">
                  {t('settings.account.password.changedBody')}
                </AppText>
              </View>
              <Button title={t('settings.account.backToSettings')} variant="secondary" onPress={back} className="mt-1" />
            </Card>
          </View>
        </ScrollView>
      </Screen>
    )
  }

  return (
    <Screen>
      <ScrollView showsVerticalScrollIndicator={false} keyboardShouldPersistTaps="handled">
        <View className="gap-5 py-4">
          {backLink}
          <PageHeader
            title={t('settings.account.password.pageTitle')}
            subtitle={t('settings.account.password.pageSubtitle')}
          />

          <Card className="gap-4">
            <PasswordField
              label={t('settings.account.password.current')}
              value={current}
              onChangeText={setCurrent}
              textContentType="password"
            />
            {/* newPassword lets the OS keychain offer to generate and save one, and stops
                it filing the new value under the old password's entry. */}
            <PasswordField
              label={t('settings.account.password.new')}
              value={next}
              onChangeText={setNext}
              textContentType="newPassword"
            >
              <PasswordRule state={rules.length}>{lengthRuleLabel(next)}</PasswordRule>
              <PasswordRule state={rules.different}>{differentRuleLabel()}</PasswordRule>
            </PasswordField>
            <PasswordField
              label={t('settings.account.password.confirmNew')}
              value={confirm}
              onChangeText={setConfirm}
              textContentType="newPassword"
            >
              <PasswordRule state={rules.match}>{matchRuleLabel(rules.match)}</PasswordRule>
            </PasswordField>

            {/* Form-level, so it belongs beside the action it blocked rather than hanging
                off one field — pinning it to the confirm box pointed the user at the wrong
                input for an error about the current password. */}
            {save.error ? <Alert variant="error">{save.error}</Alert> : null}

            <View className="flex-row gap-2">
              <Button
                title={t('settings.account.password.submit')}
                onPress={submit}
                loading={save.busy}
                disabled={!rules.ready}
                className="flex-1"
              />
              <Button
                title={t('common.cancel')}
                variant="secondary"
                onPress={back}
                disabled={save.busy}
                className="flex-1"
              />
            </View>
          </Card>
        </View>
      </ScrollView>
    </Screen>
  )
}