Skip to content

Commit b533c66

Browse files
refactor(vue3): migrate ReadCertificate to script setup ts
Signed-off-by: Vitor Mattos <1079143+vitormattos@users.noreply.github.com>
1 parent 21ac7ec commit b533c66

1 file changed

Lines changed: 64 additions & 58 deletions

File tree

src/views/ReadCertificate/ReadCertificate.vue

Lines changed: 64 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,9 @@
3636
</NcDialog>
3737
</template>
3838

39-
<script>
39+
<script setup lang="ts">
4040
import { t } from '@nextcloud/l10n'
41+
import { onMounted, ref } from 'vue'
4142
4243
import axios from '@nextcloud/axios'
4344
import { generateOcsUrl } from '@nextcloud/router'
@@ -52,65 +53,70 @@ import CertificateContent from './CertificateContent.vue'
5253
5354
import { useSignMethodsStore } from '../../store/signMethods.js'
5455
55-
export default {
56+
defineOptions({
5657
name: 'ReadCertificate',
57-
components: {
58-
NcDialog,
59-
NcPasswordField,
60-
NcButton,
61-
NcNoteCard,
62-
NcLoadingIcon,
63-
CertificateContent,
64-
},
65-
setup() {
66-
const signMethodsStore = useSignMethodsStore()
67-
return { signMethodsStore }
68-
},
69-
data() {
70-
return {
71-
hasLoading: false,
72-
password: '',
73-
certificate: {},
74-
error: '',
75-
size: 'small',
76-
}
77-
},
78-
mounted() {
79-
this.reset()
80-
},
81-
methods: {
82-
t,
83-
reset() {
84-
this.password = ''
85-
this.certificate = {}
86-
this.error = ''
87-
this.size = 'small'
88-
},
89-
async send() {
90-
this.hasLoading = true
91-
await axios.post(generateOcsUrl('/apps/libresign/api/v1/account/pfx/read'), {
92-
password: this.password,
93-
})
94-
.then(({ data }) => {
95-
this.certificate = data.ocs.data
96-
this.size = 'large'
97-
this.error = ''
98-
})
99-
.catch(({ response }) => {
100-
if (response?.data?.ocs?.data?.message?.length > 0) {
101-
this.error = response.data.ocs.data.message
102-
} else {
103-
this.error = t('libresign', 'Invalid password')
104-
}
105-
})
106-
this.hasLoading = false
107-
},
108-
onClose() {
109-
this.signMethodsStore.closeModal('readCertificate')
110-
this.reset()
111-
},
112-
},
58+
})
59+
60+
type CertificateData = Record<string, unknown>
61+
62+
type SignMethodsStore = {
63+
modal: {
64+
readCertificate: boolean
65+
}
66+
closeModal: (modalName: string) => void
11367
}
68+
69+
const signMethodsStore = useSignMethodsStore() as SignMethodsStore
70+
const hasLoading = ref(false)
71+
const password = ref('')
72+
const certificate = ref<CertificateData>({})
73+
const error = ref('')
74+
const size = ref<'small' | 'large'>('small')
75+
76+
function reset() {
77+
password.value = ''
78+
certificate.value = {}
79+
error.value = ''
80+
size.value = 'small'
81+
}
82+
83+
async function send() {
84+
hasLoading.value = true
85+
try {
86+
const { data } = await axios.post(generateOcsUrl('/apps/libresign/api/v1/account/pfx/read'), {
87+
password: password.value,
88+
})
89+
certificate.value = data.ocs.data
90+
size.value = 'large'
91+
error.value = ''
92+
} catch (caughtError) {
93+
const message = (caughtError as { response?: { data?: { ocs?: { data?: { message?: string } } } } }).response?.data?.ocs?.data?.message
94+
error.value = message && message.length > 0 ? message : t('libresign', 'Invalid password')
95+
}
96+
hasLoading.value = false
97+
}
98+
99+
function onClose() {
100+
signMethodsStore.closeModal('readCertificate')
101+
reset()
102+
}
103+
104+
onMounted(() => {
105+
reset()
106+
})
107+
108+
defineExpose({
109+
signMethodsStore,
110+
hasLoading,
111+
password,
112+
certificate,
113+
error,
114+
size,
115+
reset,
116+
send,
117+
onClose,
118+
t,
119+
})
114120
</script>
115121

116122
<style lang="scss" scoped>

0 commit comments

Comments
 (0)