feat: convert api-config to script setup and ts (#1535)

Co-authored-by: Dominik Pschenitschni <mail@celement.de>
Reviewed-on: vikunja/frontend#1535
Reviewed-by: konrad <k@knt.li>
Co-authored-by: Dominik Pschenitschni <dpschen@noreply.kolaente.de>
Co-committed-by: Dominik Pschenitschni <dpschen@noreply.kolaente.de>
This commit is contained in:
Dominik Pschenitschni 2022-02-13 22:06:26 +00:00 committed by konrad
parent d57c9af332
commit b84fe4c88b
1 changed files with 54 additions and 67 deletions

View File

@ -39,79 +39,66 @@
</div> </div>
</template> </template>
<script> <script setup lang="ts">
import Message from '@/components/misc/message' import {ref, computed, watch} from 'vue'
import { useI18n } from 'vue-i18n'
import {parseURL} from 'ufo' import {parseURL} from 'ufo'
import {checkAndSetApiUrl} from '@/helpers/checkAndSetApiUrl' import {checkAndSetApiUrl} from '@/helpers/checkAndSetApiUrl'
import {success} from '@/message'
export default { import Message from '@/components/misc/message.vue'
name: 'apiConfig',
components: { const props = defineProps({
Message, configureOpen: {
type: Boolean,
required: false,
default: false,
}, },
data() { })
return { const emit = defineEmits(['foundApi'])
configureApi: false,
apiUrl: window.API_URL, const apiUrl = ref(window.API_URL)
errorMsg: '', const configureApi = ref(apiUrl.value === '')
successMsg: '',
const apiDomain = computed(() => parseURL(apiUrl.value).host || parseURL(window.location.href).host)
watch(() => props.configureOpen, (value) => {
configureApi.value = value
}, { immediate: true })
const {t} = useI18n()
const errorMsg = ref('')
const successMsg = ref('')
async function setApiUrl() {
if (apiUrl.value === '') {
// Don't try to check and set an empty url
errorMsg.value = t('apiConfig.urlRequired')
return
}
try {
const url = await checkAndSetApiUrl(apiUrl.value)
if (url === '') {
// If the config setter function could not figure out a url
throw new Error('URL cannot be empty.')
} }
},
emits: ['foundApi'],
created() {
if (this.apiUrl === '') {
this.configureApi = true
}
},
computed: {
apiDomain() {
return parseURL(this.apiUrl).host || parseURL(window.location.href).host
},
},
props: {
configureOpen: {
type: Boolean,
required: false,
default: false,
},
},
watch: {
configureOpen: {
handler(value) {
this.configureApi = value
},
immediate: true,
},
},
methods: {
async setApiUrl() {
if (this.apiUrl === '') {
// Don't try to check and set an empty url
this.errorMsg = this.$t('apiConfig.urlRequired')
return
}
try { // Set it + save it to local storage to save us the hoops
const url = await checkAndSetApiUrl(this.apiUrl) errorMsg.value = ''
apiUrl.value = url
if (url === '') { success({message: t('apiConfig.success', {domain: apiDomain.value})})
// If the config setter function could not figure out a url configureApi.value = false
throw new Error('URL cannot be empty.') emit('foundApi', apiUrl.value)
} } catch (e) {
// Still not found, url is still invalid
// Set it + save it to local storage to save us the hoops successMsg.value = ''
this.errorMsg = '' errorMsg.value = t('apiConfig.error', {domain: apiDomain.value})
this.$message.success({message: this.$t('apiConfig.success', {domain: this.apiDomain})}) }
this.configureApi = false
this.apiUrl = url
this.$emit('foundApi', this.apiUrl)
} catch (e) {
// Still not found, url is still invalid
this.successMsg = ''
this.errorMsg = this.$t('apiConfig.error', {domain: this.apiDomain})
}
},
},
} }
</script> </script>