<commit_before><template>
  <ModalWindow
    :title="$t('WALLET_RENAME.TITLE')"
    @close="emitCancel"
  >
    <div class="flex flex-col justify-center">
      <p>{{ $t('WALLET_RENAME.ADDRESS_INFO', { wallet: wallet.address }) }}</p>
      <InputText
        v-model="schema.name"
        :is-invalid="$v.schema.name.$invalid"
        :helper-text="nameError"
        :label="$t('WALLET_RENAME.NEW')"
        class="mt-5"
        name="name"
      />

      <button
        :disabled="$v.schema.name.$invalid"
        class="blue-button mt-5"
        type="button"
        @click="renameWallet"
      >
        {{ $t('WALLET_RENAME.RENAME') }}
      </button>
    </div>
  </ModalWindow>
</template>

<script>
import { InputText } from '@/components/Input'
import { ModalWindow } from '@/components/Modal'
import Wallet from '@/models/wallet'

export default {
  name: 'WalletRenameModal',

  schema: Wallet.schema,

  components: {
    InputText,
    ModalWindow
  },

  props: {
    wallet: {
      type: Object,
      required: true
    }
  },

  computed: {
    nameError () {
      if (this.$v.schema.name.$invalid) {
        if (!this.$v.schema.name.doesNotExists) {
          return this.$t('VALIDATION.NAME.DUPLICATED', [this.schema.name])
        } else if (!this.$v.schema.name.schemaMaxLength) {
          return this.$t('VALIDATION.NAME.MAX_LENGTH', [Wallet.schema.properties.name.maxLength])
        // NOTE: not used, unless the minimum length is changed
        } else if (!this.$v.schema.name.schemaMinLength) {
          return this.$tc('VALIDATION.NAME.MIN_LENGTH', Wallet.schema.properties.name.minLength)
        }
      }
      return null
    }
  },

  mounted () {
    this.schema.name = this.wallet.name
  },

  methods: {
    renameWallet () {
      if (this.wallet.isLedger) {
        try {
          this.$store.dispatch('wallet/setLedgerName', {
            address: this.wallet.address,
            name: this.wallet.name
          })
        } catch (error) {
          this.$error(this.$t('WALLET_RENAME.ERROR_LEDGER', { error }))
        }
        this.wallet.name = this.schema.name
      } else {
        this.wallet.name = this.schema.name
        this.$store.dispatch('wallet/update', this.wallet)
      }
      this.emitRenamed()
    },

    emitCancel () {<commit_msg>fix: ledger wallet rename saving old name (#698)<commit_after><template>
  <ModalWindow
    :title="$t('WALLET_RENAME.TITLE')"
    @close="emitCancel"
  >
    <div class="flex flex-col justify-center">
      <p>{{ $t('WALLET_RENAME.ADDRESS_INFO', { wallet: wallet.address }) }}</p>
      <InputText
        v-model="schema.name"
        :is-invalid="$v.schema.name.$invalid"
        :helper-text="nameError"
        :label="$t('WALLET_RENAME.NEW')"
        class="mt-5"
        name="name"
      />

      <button
        :disabled="$v.schema.name.$invalid"
        class="blue-button mt-5"
        type="button"
        @click="renameWallet"
      >
        {{ $t('WALLET_RENAME.RENAME') }}
      </button>
    </div>
  </ModalWindow>
</template>

<script>
import { InputText } from '@/components/Input'
import { ModalWindow } from '@/components/Modal'
import Wallet from '@/models/wallet'

export default {
  name: 'WalletRenameModal',

  schema: Wallet.schema,

  components: {
    InputText,
    ModalWindow
  },

  props: {
    wallet: {
      type: Object,
      required: true
    }
  },

  computed: {
    nameError () {
      if (this.$v.schema.name.$invalid) {
        if (!this.$v.schema.name.doesNotExists) {
          return this.$t('VALIDATION.NAME.DUPLICATED', [this.schema.name])
        } else if (!this.$v.schema.name.schemaMaxLength) {
          return this.$t('VALIDATION.NAME.MAX_LENGTH', [Wallet.schema.properties.name.maxLength])
        // NOTE: not used, unless the minimum length is changed
        } else if (!this.$v.schema.name.schemaMinLength) {
          return this.$tc('VALIDATION.NAME.MIN_LENGTH', Wallet.schema.properties.name.minLength)
        }
      }
      return null
    }
  },

  mounted () {
    this.schema.name = this.wallet.name
  },

  methods: {
    renameWallet () {
      const newName = this.schema.name
      if (this.wallet.isLedger) {
        try {
          this.$store.dispatch('wallet/setLedgerName', {
            address: this.wallet.address,
            name: newName
          })
          this.wallet.name = newName
        } catch (error) {
          this.$error(this.$t('WALLET_RENAME.ERROR_LEDGER', { error }))
        }
      } else {
        this.wallet.name = newName
        this.$store.dispatch('wallet/update', this.wallet)
      }
      this.emitRenamed()
    },

    emitCancel () {
{'fraction_non_alphanumeric': 0.1541925798841947, 'fraction_numerical': 0.0015011794981771391, 'mean_word_length': 2.2254495159059475, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6434893', 'n_tokens_mistral': 1522, 'n_tokens_neox': 1525, 'n_words': 244}