Skip to content

Commit

Permalink
feat: validate parent space
Browse files Browse the repository at this point in the history
  • Loading branch information
Sekhmet committed Sep 19, 2024
1 parent 6500e7e commit 0a4c0c1
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 2 deletions.
39 changes: 38 additions & 1 deletion apps/ui/src/components/FormSpaceAdvanced.vue
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,15 @@ const props = defineProps<{
}>();
const emit = defineEmits<{
(e: 'updateValidity', valid: boolean): void;
(e: 'deleteSpace');
}>();
const { addNotification } = useUiStore();
const parentSpaceValidationResult = ref(
null as { value: string; valid: boolean } | null
);
const childInput = ref('');
const isAddingChild = ref(false);
const isDeleteSpaceModalOpen = ref(false);
Expand Down Expand Up @@ -84,6 +88,15 @@ const formErrors = computed(() => {
}
);
});
const parentSpaceError = computed(() => {
if (formErrors.value.parent) return formErrors.value.parent as string;
if (parentSpaceValidationResult.value === null) return null;
if (parentSpaceValidationResult.value.value !== parent.value) return null;
if (parentSpaceValidationResult.value.valid) return null;
return `Space ${parentSpaceValidationResult.value.value} not found`;
});
async function addChild() {
try {
Expand All @@ -107,6 +120,30 @@ async function addChild() {
function deleteChild(i: number) {
children.value = children.value.filter((_, index) => index !== i);
}
watchDebounced(
parent,
async parent => {
if (!parent) return;
const space = await network.value.api.loadSpace(parent);
parentSpaceValidationResult.value = {
value: parent,
valid: !!space
};
},
{ debounce: 500 }
);
watchEffect(() => {
const valid =
Object.keys(formErrors.value).length === 0 &&
parentSpaceValidationResult.value?.valid &&
parentSpaceValidationResult.value?.value === parent.value;
emit('updateValidity', !!valid);
});
</script>

<template>
Expand All @@ -127,7 +164,7 @@ function deleteChild(i: number) {
'cursor-not-allowed': !canAddParentSpace
}"
:definition="PARENT_SPACE_DEFINITION"
:error="formErrors.parent"
:error="parentSpaceError ?? undefined"
/>
<UiInputString
v-model="childInput"
Expand Down
5 changes: 4 additions & 1 deletion apps/ui/src/views/Space/Settings.vue
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ const { setTitle } = useTitle();
const uiStore = useUiStore();
const { getDurationFromCurrent, getCurrentFromDuration } = useMetaStore();
const hasAdvancedErrors = ref(false);
const changeControllerModalOpen = ref(false);
const executeFn = ref(save);
const saving = ref(false);
Expand Down Expand Up @@ -530,11 +531,13 @@ watchEffect(() => setTitle(`Edit settings - ${props.space.name}`));
:space-id="space.id"
:is-controller="isController"
@delete-space="handleSpaceDelete"
@update-validity="v => (hasAdvancedErrors = !v)"
/>
</UiContainerSettings>
<div
v-if="
!uiStore.sidebarOpen && ((isModified && canModifySettings) || error)
!uiStore.sidebarOpen &&
((isModified && canModifySettings && !hasAdvancedErrors) || error)
"
class="fixed bg-skin-bg bottom-0 left-0 right-0 lg:left-[312px] xl:right-[240px] border-y px-4 py-3 flex flex-col xs:flex-row justify-between items-center"
>
Expand Down

0 comments on commit 0a4c0c1

Please sign in to comment.