frontend/src/components/PersistentVolumeClaimsCreateYaml.vue
<!-- PersistentVolumeClaimsCreateYaml.vue -->
<template>
<div v-if="show" class="modal-overlay" @click="closeModal">
<div class="modal-content yaml-editor" @click.stop>
<div class="modal-header">
<h3>Create Persistent Volume Claim - YAML</h3>
<button class="close-button" @click="closeModal">×</button>
</div>
<div class="modal-body">
<div v-if="error" class="error-message">{{ error }}</div>
<div class="yaml-container">
<textarea
v-model="yamlContent"
:disabled="isSubmitting"
class="yaml-textarea"
placeholder="Enter your PersistentVolumeClaim YAML here..."
></textarea>
</div>
<div class="template-section">
<button
class="template-button"
@click="loadTemplate"
:disabled="isSubmitting"
>
Load Template
</button>
<span class="template-hint">Click to load a basic PVC template</span>
</div>
</div>
<div class="modal-footer">
<button
class="cancel-button"
@click="closeModal"
:disabled="isSubmitting"
>
Cancel
</button>
<button
class="create-button"
@click="createPvc"
:disabled="isSubmitting || !yamlContent.trim()"
>
{{ isSubmitting ? 'Creating...' : 'Create PVC' }}
</button>
</div>
</div>
</div>
</template>
<script lang="ts">
import { defineComponent, ref } from 'vue';
import { PersistentVolumeClaimCreateOptions } from '../types/custom';
export default defineComponent({
name: 'PersistentVolumeClaimsCreateYaml',
props: {
show: {
type: Boolean,
required: true
}
},
emits: ['close', 'create-pvc'],
setup(props, { emit }) {
// State
const yamlContent = ref('');
const isSubmitting = ref(false);
const error = ref('');
// Template YAML for PVC
const pvcTemplate = `apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: my-pvc
namespace: default
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1Gi
storageClassName: standard`;
// Methods
const closeModal = () => {
if (!isSubmitting.value) {
resetForm();
emit('close');
}
};
const resetForm = () => {
yamlContent.value = '';
error.value = '';
isSubmitting.value = false;
};
const loadTemplate = () => {
yamlContent.value = pvcTemplate;
error.value = '';
};
const validateYaml = (yaml: string): boolean => {
try {
// Basic validation - check if it looks like a PVC
const trimmed = yaml.trim();
if (!trimmed) {
error.value = 'YAML content cannot be empty';
return false;
}
// Check for required fields
if (!trimmed.includes('kind:') || !trimmed.includes('PersistentVolumeClaim')) {
error.value = 'YAML must contain "kind: PersistentVolumeClaim"';
return false;
}
if (!trimmed.includes('apiVersion:')) {
error.value = 'YAML must contain "apiVersion"';
return false;
}
if (!trimmed.includes('metadata:')) {
error.value = 'YAML must contain "metadata" section';
return false;
}
if (!trimmed.includes('spec:')) {
error.value = 'YAML must contain "spec" section';
return false;
}
return true;
} catch (err) {
error.value = 'Invalid YAML format';
return false;
}
};
const createPvc = () => {
error.value = '';
if (!yamlContent.value.trim()) {
error.value = 'YAML content is required';
return;
}
if (!validateYaml(yamlContent.value)) {
return;
}
isSubmitting.value = true;
try {
const opts: PersistentVolumeClaimCreateOptions = {
context: '', // This will be set by the parent component
opts: {
yamlContent: yamlContent.value.trim(),
isYaml: true
}
};
emit('create-pvc', opts);
resetForm();
closeModal();
} catch (err: any) {
error.value = `Failed to create PVC: ${err.message || err}`;
isSubmitting.value = false;
}
};
return {
yamlContent,
isSubmitting,
error,
closeModal,
loadTemplate,
createPvc
};
}
});
</script>
<style src="@/assets/css/MenuDialog.css" scoped></style>
<style scoped>
.yaml-container {
margin-bottom: 1rem;
}
.yaml-textarea {
width: 100%;
height: 400px;
padding: 1rem;
border: 1px solid #444;
border-radius: 4px;
background-color: #1e1e1e;
color: #ffffff;
font-family: 'Courier New', Consolas, monospace;
font-size: 14px;
line-height: 1.4;
resize: vertical;
min-height: 300px;
max-height: 600px;
}
.yaml-textarea:focus {
outline: none;
border-color: #007acc;
box-shadow: 0 0 0 2px rgba(0, 122, 204, 0.2);
}
.yaml-textarea:disabled {
opacity: 0.6;
cursor: not-allowed;
}
.yaml-textarea::placeholder {
color: #666;
font-style: italic;
}
.template-section {
display: flex;
align-items: center;
gap: 0.75rem;
margin-bottom: 1rem;
padding: 0.75rem;
background-color: #333;
border-radius: 4px;
border: 1px solid #444;
}
.template-button {
padding: 0.5rem 1rem;
border: 1px solid #007acc;
background-color: #007acc;
color: white;
border-radius: 4px;
cursor: pointer;
font-size: 0.9rem;
transition: all 0.2s;
}
.template-button:hover:not(:disabled) {
background-color: #005a9e;
border-color: #005a9e;
}
.template-button:disabled {
opacity: 0.6;
cursor: not-allowed;
}
.template-hint {
color: #ccc;
font-size: 0.85rem;
font-style: italic;
}
.error-message {
background-color: #4a1a1a;
border: 1px solid #cc4444;
color: #ff6b6b;
padding: 0.75rem;
border-radius: 4px;
margin-bottom: 1rem;
font-size: 0.9rem;
}
.modal-footer {
display: flex;
justify-content: flex-end;
gap: 0.5rem;
padding: 1rem 1.5rem;
border-top: 1px solid #444;
}
.cancel-button {
padding: 0.5rem 1rem;
border: 1px solid #666;
background-color: transparent;
color: #ccc;
border-radius: 4px;
cursor: pointer;
transition: all 0.2s;
}
.cancel-button:hover:not(:disabled) {
background-color: #444;
border-color: #777;
}
.create-button {
padding: 0.5rem 1rem;
border: 1px solid #28a745;
background-color: #28a745;
color: white;
border-radius: 4px;
cursor: pointer;
transition: all 0.2s;
}
.create-button:hover:not(:disabled) {
background-color: #218838;
border-color: #1e7e34;
}
.create-button:disabled {
opacity: 0.6;
cursor: not-allowed;
}
/* Responsive design */
@media (max-width: 768px) {
.yaml-textarea {
height: 300px;
font-size: 12px;
}
.template-section {
flex-direction: column;
align-items: flex-start;
gap: 0.5rem;
}
.template-button {
font-size: 0.8rem;
padding: 0.4rem 0.8rem;
}
}
</style>