Skip to content

Commit

Permalink
changed access token editor to use generic model api
Browse files Browse the repository at this point in the history
  • Loading branch information
vabene1111 committed Sep 26, 2024
1 parent 22a3654 commit 5ada8e5
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 15 deletions.
27 changes: 14 additions & 13 deletions vue3/src/components/model_editors/AccessTokenEditor.vue
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<template>
<v-card>
<v-card-title>
{{ $t(OBJ_LOCALIZATION_KEY) }}
{{ $t(modelClass.model.localizationKey) }}
<v-btn class="float-right" icon="$close" variant="plain" @click="emit('close')" v-if="dialog"></v-btn>
</v-card-title>
<v-card-text>
Expand Down Expand Up @@ -31,27 +31,26 @@
<script setup lang="ts">
import {VDateInput} from 'vuetify/labs/VDateInput' //TODO remove once component is out of labs
import {computed, onMounted, ref} from "vue";
import {AccessToken, ApiApi} from "@/openapi";
import {computed, onBeforeMount, onMounted, PropType, ref} from "vue";
import {AccessToken} from "@/openapi";
import DeleteConfirmDialog from "@/components/dialogs/DeleteConfirmDialog.vue";
import {useI18n} from "vue-i18n";
import {ErrorMessageType, PreparedMessage, useMessageStore} from "@/stores/MessageStore";
import {DateTime} from "luxon";
import {useClipboard} from "@vueuse/core";
import BtnCopy from "@/components/buttons/BtnCopy.vue";
import {GenericModel, getGenericModelFromString} from "@/types/Models";
const {t} = useI18n()
const {copy} = useClipboard()
const emit = defineEmits(['create', 'save', 'delete', 'close'])
const props = defineProps({
item: {type: {} as AccessToken, required: false},
item: {type: {} as PropType<AccessToken>, required: false},
dialog: {type: Boolean, default: false}
})
const OBJ_LOCALIZATION_KEY = 'Access_Token'
const editingObj = ref({} as AccessToken)
const modelClass = ref({} as GenericModel)
const loading = ref(false)
/**
Expand All @@ -65,7 +64,11 @@ const isUpdate = computed(() => {
* display name for object in headers/delete dialog/...
*/
const objectName = computed(() => {
return isUpdate ? `${t(OBJ_LOCALIZATION_KEY)} ${editingObj.value.token}` : `${t(OBJ_LOCALIZATION_KEY)} (${t('New')})`
return isUpdate ? `${t(modelClass.value.model.localizationKey)} ${editingObj.value.token}` : `${t(modelClass.value.model.localizationKey)} (${t('New')})`
})
onBeforeMount(() => {
modelClass.value = getGenericModelFromString('AccessToken', t)
})
onMounted(() => {
Expand All @@ -82,17 +85,16 @@ onMounted(() => {
* saves the edited object in the database
*/
async function saveObject() {
let api = new ApiApi()
if (isUpdate.value) {
api.apiAccessTokenUpdate({id: editingObj.value.id, accessToken: editingObj.value}).then(r => {
modelClass.value.update(editingObj.value.id, editingObj.value).then(r => {
editingObj.value = r
emit('save', r)
useMessageStore().addPreparedMessage(PreparedMessage.UPDATE_SUCCESS)
}).catch(err => {
useMessageStore().addError(ErrorMessageType.UPDATE_ERROR, err)
})
} else {
api.apiAccessTokenCreate({accessToken: editingObj.value}).then(r => {
modelClass.value.create(editingObj.value).then(r => {
editingObj.value = r
emit('create', r)
useMessageStore().addPreparedMessage(PreparedMessage.CREATE_SUCCESS)
Expand All @@ -106,8 +108,7 @@ async function saveObject() {
* deletes the editing object from the database
*/
async function deleteObject() {
let api = new ApiApi()
api.apiAccessTokenDestroy({id: editingObj.value.id}).then(r => {
modelClass.value.destroy(editingObj.value.id).then(r => {
editingObj.value = {} as AccessToken
emit('delete', editingObj.value)
}).catch(err => {
Expand Down
19 changes: 17 additions & 2 deletions vue3/src/types/Models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,21 @@ export const TViewLog = {
} as Model
SUPPORTED_MODELS.set(TViewLog.name, TViewLog)

export const TAccessToken = {
name: 'AccessToken',
localizationKey: 'Access_Token',
icon: 'fa-solid fa-key',

isPaginated: true,

tableHeaders: [
{title: 'Access_Token', key: 'token'},
{title: 'Created', key: 'createdAt'},
{title: 'Actions', key: 'action', align: 'end'},
]
} as Model
SUPPORTED_MODELS.set(TAccessToken.name, TAccessToken)

export const TFoodInheritField = {
name: 'FoodInheritField',
localizationKey: 'FoodInherit',
Expand Down Expand Up @@ -352,7 +367,7 @@ export class GenericModel {
throw new Error('Cannot create on this model!')
} else {
let createRequestParams: any = {}
createRequestParams[this.model.name.toLowerCase()] = obj
createRequestParams[this.model.name.charAt(0).toLowerCase() + this.model.name.slice(1)] = obj
return this.api[`api${this.model.name}Create`](createRequestParams)
}
}
Expand All @@ -370,7 +385,7 @@ export class GenericModel {
} else {
let updateRequestParams: any = {}
updateRequestParams['id'] = id
updateRequestParams[this.model.name.toLowerCase()] = obj
updateRequestParams[this.model.name.charAt(0).toLowerCase() + this.model.name.slice(1)] = obj
return this.api[`api${this.model.name}Update`](updateRequestParams)
}
}
Expand Down

0 comments on commit 5ada8e5

Please sign in to comment.