Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: clear identity #7361

Merged
merged 9 commits into from
Sep 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 67 additions & 23 deletions components/common/IdentityForm.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<Loader v-model="isLoading" :status="status" />
<form @submit.prevent>
<h1 class="title is-size-3">
{{ $i18n.t('identity.set') }}
{{ $i18n.t('identity.set', [getChainName(identityPrefix)]) }}
<NeoTooltip
:label="$i18n.t('identity.fundsReserve')"
position="bottom"
Expand All @@ -12,11 +12,31 @@
</NeoTooltip>
</h1>

<p v-if="accountId" class="subtitle is-size-6">
<Auth />
<span>{{ $i18n.t('general.balance') }}: </span>
<Money :value="balance" inline />
</p>
<div v-if="hasIdentity" class="is-size-6">
<hr class="my-7" />
<div class="mb-4">
{{ $t('identity.establishedIdentity') }}
</div>
<div
class="is-flex is-justify-content-space-between is-align-items-flex-end">
<div
class="is-flex is-justify-content-space-between is-align-items-center">
<Avatar :value="accountId" :size="34" />
<div class="ml-4">
<div class="has-text-grey">{{ $t('identity.existing') }}</div>
<div>{{ identityData.display }}</div>
</div>
</div>
<NeoButton
:label="$i18n.t('identity.clear')"
class="mb-1"
no-shadow
rounded
size="small"
@click.native="deleteIdentity" />
</div>
<hr class="my-7" />
</div>

<NeoField label="Handle">
<NeoInput
Expand Down Expand Up @@ -73,16 +93,15 @@
:disabled="disabled"
:loading="isLoading"
expanded
@click="submit" />
@click="setIdentity" />
</form>
</section>
</template>

<script lang="ts" setup>
import { notificationTypes, showNotification } from '@/utils/notification'
import { NeoField, NeoIcon, NeoInput, NeoTooltip } from '@kodadot1/brick'
import { NeoButton, NeoField, NeoInput } from '@kodadot1/brick'
import type { IdentityFields } from '@/composables/useIdentity'
const Auth = defineAsyncComponent(() => import('@/components/shared/Auth.vue'))
const BasicInput = defineAsyncComponent(
() => import('@/components/shared/form/BasicInput.vue')
)
Expand All @@ -98,9 +117,9 @@ const SubmitButton = defineAsyncComponent(

const { $i18n } = useNuxtApp()
import { useIdentityStore } from '@/stores/identity'
import { getChainName } from '@/utils/chain'

const { apiInstance } = useApi()
const { accountId, balance } = useAuth()
const { accountId } = useAuth()
const { urlPrefix } = usePrefix()
const identityStore = useIdentityStore()
const { howAboutToExecute, isLoading, initTransactionLoader, status } =
Expand All @@ -109,7 +128,12 @@ const identity = ref<IdentityFields>({})
const deposit = ref('0')
const inputLengthLimit = ref(32)

const { identity: identityData } = useIdentity({
const {
identity: identityData,
identityApi,
identityPrefix,
refetchIdentity,
} = useIdentity({
address: accountId,
})

Expand All @@ -125,6 +149,14 @@ watch(identityData, () => {
}
})

const hasIdentity = computed(() => {
const { display, legal, web, twitter, riot, email } = identityData.value
return (
accountId.value &&
Boolean(display || legal || web || twitter || riot || email)
)
})

const handleUrlPrefixChange = async () => {
deposit.value = await fetchDeposit()

Expand All @@ -147,23 +179,35 @@ const enhanceIdentityData = (): Record<string, any> => {
}

const fetchDeposit = async () => {
const api = await apiInstance.value
const api = await identityApi.value
return api.consts.identity?.basicDeposit?.toString()
}

const submit = async (): Promise<void> => {
const api = await apiInstance.value
const deleteIdentity = async (): Promise<void> => {
const api = await identityApi.value
initTransactionLoader()
const cb = api.tx.identity.clearIdentity
howAboutToExecute(accountId.value, cb, [], (block: string) => {
identity.value = {}
refetchIdentity()

showNotification(
`[Identity] You have cleared your account's identity since block ${block}`,
notificationTypes.success
)
})
}
const setIdentity = async (): Promise<void> => {
const api = await identityApi.value
initTransactionLoader()
const cb = api.tx.identity.setIdentity
const args = [enhanceIdentityData()]
howAboutToExecute(accountId.value, cb, args, onSuccess)
}

const onSuccess = (block: string) => {
showNotification(
`[Identity] You are known as ${identity.value.display} since block ${block}`,
notificationTypes.success
)
howAboutToExecute(accountId.value, cb, args, (block: string) => {
showNotification(
`[Identity] You are known as ${identity.value.display} since block ${block}`,
notificationTypes.success
)
})
}

const disabled = computed(
Expand Down
10 changes: 9 additions & 1 deletion composables/useIdentity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,20 @@ export default function useIdentity({
customNameOption?: string
}) {
const { urlPrefix } = usePrefix()
const { apiInstanceByPrefix } = useApi()
const isDotAddress = computed(() => ['dot', 'ahp'].includes(urlPrefix.value))
const id = computed(
() =>
address.value &&
(isDotAddress.value
? accountToPublicKey(address.value)
: getss58AddressByPrefix(address.value, 'ksm'))
: getss58AddressByPrefix(address.value, 'rmrk'))
)

const identityPrefix = computed(() => (isDotAddress.value ? 'dot' : 'rmrk'))

const identityApi = computed(() => apiInstanceByPrefix(identityPrefix.value))

const identity = computed<IdentityFields>(() => data.value?.identity || {})

const { data, refetch, loading } = useGraphql({
Expand Down Expand Up @@ -49,6 +54,9 @@ export default function useIdentity({
twitter,
display,
name,
identityApi,
identityPrefix,
refetchIdentity: refetch,
}
}

Expand Down
5 changes: 4 additions & 1 deletion locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -924,8 +924,11 @@
},
"identity": {
"page": "Identity",
"set": "Create on-chain identity",
"set": "Create On-Chain identity on {0}",
"click": "Click to create identity",
"clear": "Clear (burn)",
"existing" : "Existing Onchain Identity",
"establishedIdentity": "It appears you already established an onchain identity. Update it as needed (no new deposit needed) or delete to reclaim your deposit.",
"handleRequired": "Your handle is required",
"deposit": "Deposit",
"fundsReserve": "0.0333 KSM will be reserved. These funds are returned when the identity is cleared.",
Expand Down
3 changes: 2 additions & 1 deletion styles/global.scss
Original file line number Diff line number Diff line change
Expand Up @@ -306,9 +306,10 @@ a.has-text-grey {
}
.border {
@include ktheme() {
border: 1px solid theme('border-color');
border: 1px solid theme('border-color') !important;
}
}

.no-border {
border: none !important;
}
Expand Down