-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature:
VariableMenu
component (#1294)
* Add variable menu (broken currently) * Fix access check * Add an update event * Don't use delteItem utility * Fix comment * Don't inconsistnetly use template checks * Fix emit location * Feature: Variable edit modal (#1295) * Update locale strings * Improve validation handler * Add comment for TODO * Add new components to bucket export * Add VariableEditModal to VariableMenu * Add missing export * Emit update and create events * Remove non-null assertion in edit modal - it's not needed * Re-emit the new edit menu emission (Variable)
- Loading branch information
1 parent
0ad62f9
commit 54d7ffc
Showing
6 changed files
with
213 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
<template> | ||
<p-modal v-model:showModal="internalValue" :title="localization.info.newVariable"> | ||
<p-form @submit="submit"> | ||
<p-content> | ||
<p-label :label="localization.info.name" :state="nameState" :message="nameErrorMessage"> | ||
<p-text-input v-model="name" :state="nameState" /> | ||
</p-label> | ||
|
||
<p-label :label="localization.info.value" :state="valueState" :message="valueErrorMessage"> | ||
<p-text-input v-model="value" :state="valueState" /> | ||
</p-label> | ||
|
||
<p-label :label="localization.info.tags"> | ||
<p-tag-input v-model="tags" /> | ||
</p-label> | ||
</p-content> | ||
</p-form> | ||
|
||
<template #actions> | ||
<p-button :loading="pending" @click="submit"> | ||
{{ localization.info.save }} | ||
</p-button> | ||
</template> | ||
<template #cancel> | ||
<p-button inset @click="internalValue = false"> | ||
{{ localization.info.close }} | ||
</p-button> | ||
</template> | ||
</p-modal> | ||
</template> | ||
|
||
<script lang="ts" setup> | ||
import { showToast } from '@prefecthq/prefect-design' | ||
import { useValidation, useValidationObserver, ValidationRule } from '@prefecthq/vue-compositions' | ||
import { isNull } from 'lodash' | ||
import { computed, ref } from 'vue' | ||
import { useWorkspaceApi } from '@/compositions' | ||
import { localization } from '@/localization' | ||
import { Variable, VariableEdit } from '@/models' | ||
import { isRequired, isString } from '@/utilities' | ||
const props = defineProps<{ | ||
variable: Variable, | ||
showModal: boolean, | ||
}>() | ||
const emit = defineEmits<{ | ||
(event: 'update:showModal', value: boolean): void, | ||
(event: 'update', value: Variable): void, | ||
}>() | ||
const internalValue = computed({ | ||
get() { | ||
return props.showModal | ||
}, | ||
set(value: boolean): void { | ||
emit('update:showModal', value) | ||
}, | ||
}) | ||
const api = useWorkspaceApi() | ||
const isUnique: ValidationRule<string | undefined> = async (value, label, { signal, source, previousValue }) => { | ||
if (value === previousValue) { | ||
return | ||
} | ||
if (source === 'validator') { | ||
await new Promise((resolve) => setTimeout(resolve, 1000)) | ||
} | ||
if (signal.aborted) { | ||
return | ||
} | ||
if (isNull(value) || !isString(value)) { | ||
return false | ||
} | ||
try { | ||
const variable = await api.variables.getVariableByName(value) | ||
return variable.id === props.variable.id | ||
} catch { | ||
/* Variable doesn't exist: silence is golden */ | ||
return true | ||
} | ||
} | ||
const { validate, pending } = useValidationObserver() | ||
const name = ref<string>(props.variable.name) | ||
const value = ref<string>(props.variable.value) | ||
const tags = ref<string[]>(props.variable.tags) | ||
const rules: Record<string, ValidationRule<string | undefined>[]> = { | ||
name: [isRequired(localization.info.name), isUnique], | ||
value: [isRequired(localization.info.value)], | ||
} | ||
const { error: nameErrorMessage, state: nameState } = useValidation(name, localization.info.name, rules.name) | ||
const { error: valueErrorMessage, state: valueState } = useValidation(value, localization.info.value, rules.value) | ||
const submit = async (): Promise<void> => { | ||
const valid = await validate() | ||
if (valid) { | ||
try { | ||
const values: VariableEdit = { | ||
name: name.value, | ||
value: value.value, | ||
tags: tags.value, | ||
} | ||
const variable = await api.variables.editVariable(props.variable.id, values) | ||
showToast(localization.success.editVariable, 'success') | ||
internalValue.value = false | ||
emit('update', variable) | ||
} catch (error) { | ||
console.error(error) | ||
showToast(localization.error.editVariable, 'error') | ||
} | ||
} | ||
} | ||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
<template> | ||
<p-icon-button-menu v-bind="$attrs"> | ||
<copy-overflow-menu-item :label="localization.info.copyId" :item="variable.id" /> | ||
<copy-overflow-menu-item :label="localization.info.copyName" :item="variable.name" /> | ||
<copy-overflow-menu-item :label="localization.info.copyValue" :item="variable.value" /> | ||
<p-overflow-menu-item v-if="can.update.variable" :label="localization.info.edit" @click="openEditModal" /> | ||
<p-overflow-menu-item v-if="can.delete.variable" :label="localization.info.delete" @click="openDeleteModal" /> | ||
</p-icon-button-menu> | ||
|
||
<VariableEditModal v-model:showModal="showEditModal" :variable="variable" @update="handleUpdate" /> | ||
|
||
<ConfirmDeleteModal | ||
v-model:showModal="showDeleteModal" | ||
:label="localization.info.delete" | ||
:name="variable.name" | ||
@delete="deleteVariable(variable.id)" | ||
/> | ||
</template> | ||
|
||
<script lang="ts"> | ||
export default { | ||
name: 'VariableMenu', | ||
expose: [], | ||
inheritAttrs: false, | ||
} | ||
</script> | ||
|
||
<script lang="ts" setup> | ||
import { showToast } from '@prefecthq/prefect-design' | ||
import { ConfirmDeleteModal, CopyOverflowMenuItem, VariableEditModal } from '@/components' | ||
import { useWorkspaceApi, useCan, useShowModal } from '@/compositions' | ||
import { localization } from '@/localization' | ||
import { Variable } from '@/models' | ||
defineProps<{ | ||
variable: Variable, | ||
}>() | ||
const emit = defineEmits<{ | ||
(event: 'delete', value: string): void, | ||
(event: 'update', value: Variable): void, | ||
}>() | ||
const can = useCan() | ||
const { showModal: showDeleteModal, open: openDeleteModal, close: closeDeleteModal } = useShowModal() | ||
const { showModal: showEditModal, open: openEditModal } = useShowModal() | ||
const api = useWorkspaceApi() | ||
const deleteVariable = async (id: string): Promise<void> => { | ||
closeDeleteModal() | ||
try { | ||
await api.variables.deleteVariable(id) | ||
showToast(localization.success.delete(localization.info.variable), 'success') | ||
emit('delete', id) | ||
} catch (error) { | ||
console.error(error) | ||
showToast(localization.error.delete(localization.info.variable.toLowerCase()), 'error') | ||
} | ||
} | ||
const handleUpdate = (variable: Variable): void => { | ||
emit('update', variable) | ||
} | ||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters