Skip to content

Commit 3cf971f

Browse files
refactor(vue3): migrate CertificateCustonOptions to script setup ts
Signed-off-by: Vitor Mattos <1079143+vitormattos@users.noreply.github.com>
1 parent 027d100 commit 3cf971f

1 file changed

Lines changed: 124 additions & 125 deletions

File tree

src/views/Settings/CertificateCustonOptions.vue

Lines changed: 124 additions & 125 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
<div class="array-field-header">
3333
<strong>{{ getOptionProperty(certificate.id, 'label') }}</strong>
3434
<span v-if="isMaxItemsReached(certificate)" class="max-items-warning">
35-
({{ t('libresign', 'Maximum {max} items', {max: $options.MAX_ARRAY_ITEMS}) }})
35+
({{ t('libresign', 'Maximum {max} items', {max: MAX_ARRAY_ITEMS}) }})
3636
</span>
3737
<NcButton :aria-label="t('libresign', 'Add new')"
3838
:disabled="isMaxItemsReached(certificate)"
@@ -86,9 +86,11 @@
8686
</div>
8787
</template>
8888

89-
<script>
89+
<script setup lang="ts">
9090
import { t } from '@nextcloud/l10n'
9191
92+
import { computed, ref, watch } from 'vue'
93+
9294
import { emit } from '@nextcloud/event-bus'
9395
9496
import {
@@ -106,131 +108,128 @@ import { options, selectCustonOption } from '../../helpers/certification'
106108
107109
const MAX_ARRAY_ITEMS = 10
108110
109-
export default {
111+
defineOptions({
110112
name: 'CertificateCustonOptions',
111-
MAX_ARRAY_ITEMS,
112-
components: {
113-
NcButton,
114-
NcIconSvgWrapper,
115-
NcListItem,
116-
NcPopover,
117-
NcTextField,
118-
},
119-
props: {
120-
names: {
121-
type: Array,
122-
required: true,
123-
},
124-
},
125-
setup() {
126-
return {
127-
t,
128-
mdiDelete,
129-
mdiPlus,
130-
}
131-
},
132-
data() {
133-
return {
134-
certificateList: [],
135-
}
136-
},
137-
computed: {
138-
customNamesOptions() {
139-
return this.options.filter(itemA =>
140-
!this.certificateList.some(itemB => itemB.id === itemA.id),
141-
)
142-
},
143-
options() {
144-
return options.filter(option => option.id !== 'CN')
145-
},
146-
},
147-
watch: {
148-
names: {
149-
handler(values) {
150-
this.certificateList = values
151-
},
152-
immediate: true,
153-
},
154-
},
155-
methods: {
156-
getOptionProperty(id, property) {
157-
return this.options.find(option => option.id === id)[property]
158-
},
159-
isMaxItemsReached(certificate) {
160-
if (!Array.isArray(certificate.value)) return false
161-
return certificate.value.length >= MAX_ARRAY_ITEMS
162-
},
163-
validateMin(item) {
164-
return item.value.length >= item.min
165-
},
166-
validateMax(item) {
167-
if (Object.hasOwn(item, 'max')) {
168-
return item.value.length <= item.max
169-
}
170-
return true
171-
},
172-
validate(id) {
173-
const custonOption = selectCustonOption(id)
174-
if (custonOption.isSome()) {
175-
const item = custonOption.unwrap()
176-
if (this.validateMin(item) && this.validateMax(item)) {
177-
item.error = false
178-
} else {
179-
item.error = true
180-
}
181-
const listToSave = this.certificateList.map(certificate => ({
182-
id: certificate.id,
183-
value: certificate.value,
184-
}))
185-
emit('libresign:update:certificateToSave', listToSave)
186-
}
187-
},
188-
validateArray(id) {
189-
const listToSave = this.certificateList.map(certificate => ({
190-
id: certificate.id,
191-
value: certificate.value,
192-
}))
193-
emit('libresign:update:certificateToSave', listToSave)
194-
},
195-
addArrayEntry(id) {
196-
const certificate = this.certificateList.find(cert => cert.id === id)
197-
if (certificate && Array.isArray(certificate.value)) {
198-
if (certificate.value.length < MAX_ARRAY_ITEMS) {
199-
certificate.value.push('')
200-
this.validateArray(id)
201-
}
202-
}
203-
},
204-
removeArrayEntry(id, index) {
205-
const certificate = this.certificateList.find(cert => cert.id === id)
206-
if (certificate && Array.isArray(certificate.value) && certificate.value.length > 1) {
207-
certificate.value.splice(index, 1)
208-
this.validateArray(id)
209-
}
210-
},
211-
async removeOptionalAttribute(id) {
212-
const custonOption = selectCustonOption(id)
213-
if (custonOption.isSome()) {
214-
const itemSelected = {
215-
...custonOption.unwrap(),
216-
value: '',
217-
}
218-
const list = this.certificateList.filter(item => item.id !== itemSelected.id)
219-
this.certificateList = list
220-
}
221-
},
222-
async onOptionalAttributeSelect(selected) {
223-
const custonOption = selectCustonOption(selected.id)
224-
if (custonOption.isSome()) {
225-
const option = custonOption.unwrap()
226-
if (option.id === 'OU') {
227-
option.value = ['']
228-
}
229-
this.certificateList = [option, ...this.certificateList]
230-
}
231-
},
232-
},
113+
})
114+
115+
interface CertificateOption {
116+
id: string
117+
label?: string
118+
helperText?: string
119+
min?: number
120+
max?: number
121+
value: string | string[]
122+
error?: boolean
123+
}
124+
125+
const props = defineProps<{
126+
names: CertificateOption[]
127+
}>()
128+
129+
const certificateList = ref<CertificateOption[]>([])
130+
131+
const availableOptions = computed(() => options.filter(option => option.id !== 'CN'))
132+
133+
const customNamesOptions = computed(() => availableOptions.value.filter(itemA =>
134+
!certificateList.value.some(itemB => itemB.id === itemA.id),
135+
))
136+
137+
watch(() => props.names, (values) => {
138+
certificateList.value = values as CertificateOption[]
139+
}, { immediate: true })
140+
141+
function getOptionProperty(id: string, property: 'label' | 'helperText' | 'max') {
142+
return availableOptions.value.find(option => option.id === id)?.[property]
143+
}
144+
145+
function isMaxItemsReached(certificate: CertificateOption) {
146+
if (!Array.isArray(certificate.value)) {
147+
return false
148+
}
149+
return certificate.value.length >= MAX_ARRAY_ITEMS
150+
}
151+
152+
function validateMin(item: CertificateOption) {
153+
return item.min === undefined || String(item.value).length >= item.min
154+
}
155+
156+
function validateMax(item: CertificateOption) {
157+
return item.max === undefined || String(item.value).length <= item.max
158+
}
159+
160+
function emitCertificateList() {
161+
const listToSave = certificateList.value.map(certificate => ({
162+
id: certificate.id,
163+
value: certificate.value,
164+
}))
165+
emit('libresign:update:certificateToSave', listToSave)
166+
}
167+
168+
function validate(id: string) {
169+
const metadata = selectCustonOption(id)
170+
const certificate = certificateList.value.find(item => item.id === id)
171+
if (metadata.isSome() && certificate) {
172+
const option = metadata.unwrap()
173+
certificate.min = option.min
174+
certificate.max = option.max
175+
certificate.error = !(validateMin(certificate) && validateMax(certificate))
176+
emitCertificateList()
177+
}
178+
}
179+
180+
function validateArray(_id: string) {
181+
emitCertificateList()
233182
}
183+
184+
function addArrayEntry(id: string) {
185+
const certificate = certificateList.value.find(cert => cert.id === id)
186+
if (certificate && Array.isArray(certificate.value) && certificate.value.length < MAX_ARRAY_ITEMS) {
187+
certificate.value.push('')
188+
validateArray(id)
189+
}
190+
}
191+
192+
function removeArrayEntry(id: string, index: number) {
193+
const certificate = certificateList.value.find(cert => cert.id === id)
194+
if (certificate && Array.isArray(certificate.value) && certificate.value.length > 1) {
195+
certificate.value.splice(index, 1)
196+
validateArray(id)
197+
}
198+
}
199+
200+
async function removeOptionalAttribute(id: string) {
201+
const custonOption = selectCustonOption(id)
202+
if (custonOption.isSome()) {
203+
certificateList.value = certificateList.value.filter(item => item.id !== custonOption.unwrap().id)
204+
}
205+
}
206+
207+
async function onOptionalAttributeSelect(selected: { id: string }) {
208+
const custonOption = selectCustonOption(selected.id)
209+
if (custonOption.isSome()) {
210+
const option = custonOption.unwrap()
211+
certificateList.value = [{
212+
...option,
213+
value: option.id === 'OU' ? [''] : option.value,
214+
}, ...certificateList.value]
215+
}
216+
}
217+
218+
defineExpose({
219+
certificateList,
220+
customNamesOptions,
221+
availableOptions,
222+
getOptionProperty,
223+
isMaxItemsReached,
224+
validateMin,
225+
validateMax,
226+
validate,
227+
validateArray,
228+
addArrayEntry,
229+
removeArrayEntry,
230+
removeOptionalAttribute,
231+
onOptionalAttributeSelect,
232+
})
234233
</script>
235234

236235
<style lang="scss" scoped>

0 commit comments

Comments
 (0)