Skip to content

Commit

Permalink
Error is now handled by parent component of ToggleJson
Browse files Browse the repository at this point in the history
  • Loading branch information
Gavinok committed Dec 4, 2023
1 parent 4779f86 commit 16d633c
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 37 deletions.
28 changes: 15 additions & 13 deletions services/tenant-ui/frontend/src/components/common/ToggleJson.vue
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,22 @@
</div>
</template>

<script setup lang="ts">
<script setup lang="ts" generic="T">
// Libraries
import { ref, defineExpose } from 'vue';
import InputText from 'primevue/inputtext';
// Source
import InputSwitch from 'primevue/inputswitch';
import Textarea from 'primevue/textarea';
import { useI18n } from 'vue-i18n';
const showRawJson = ref<boolean>(false);
const valuesJson = ref<string>('');
// TODO expose a way to get the final results as the object
// representation for submitting schemas and issuing credentials
// Undefined indicates that the conversion was a failure
// note that indication of the error should be handled in the
// toJson and fromJson using libraries like vue-toastification
const props = defineProps<{
toJson: () => string;
fromJson: (jsonRepresentation: string) => void;
toJson: () => string | undefined;
fromJson: (jsonRepresentation: string) => T | undefined;
}>();
defineExpose({
Expand All @@ -45,14 +44,17 @@ defineExpose({
});
const toggleJson = () => {
try {
if (showRawJson.value) {
valuesJson.value = props.toJson();
if (showRawJson.value) {
const res = props.toJson();
if (res) {
valuesJson.value = res;
} else {
props.fromJson(valuesJson.value);
showRawJson.value = !showRawJson.value;
}
} else {
if (!props.fromJson(valuesJson.value)) {
showRawJson.value = !showRawJson.value;
}
} catch (e) {
showRawJson.value = !showRawJson.value;
}
};
</script>
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
ref="jsonVal"
:to-json="credentialValueToJson"
:from-json="jsonToCredentialValue"
generic="CredentialValue[]"
>
<!-- Dynamic Attribute field list -->
<div
Expand Down Expand Up @@ -74,27 +75,29 @@ const emit = defineEmits(['back', 'save']);
// Fields
const credentialValuesRaw = ref<CredentialValue[]>([]);
const showRawJson = ref(false);
const jsonVal = ref<{ showRawJson: boolean; valuesJson: string }>({
showRawJson: false,
valuesJson: '',
});
function jsonToCredentialValue(jsonString: string) {
function jsonToCredentialValue(
jsonString: string
): CredentialValue[] | undefined {
const parsed = tryParseJson<CredentialValue[]>(jsonString);
if (parsed) {
credentialValuesRaw.value = parsed;
return parsed;
} else {
toast.warning('The JSON you inputted has invalid syntax');
return undefined;
}
}
const credentialValueToJson = () => {
function credentialValueToJson(): string | undefined {
// Convert over to the json from what was entered on the fields
const j = JSON.stringify(credentialValuesRaw.value, undefined, 2);
return j;
};
}
const saveCredValues = () => {
if (jsonVal.value.showRawJson) {
Expand All @@ -103,10 +106,10 @@ const saveCredValues = () => {
emit('save', credentialValuesRaw.value);
};
// Whnen the component is intialized set up the fields and raw JSON based
// When the component is initialized set up the fields and raw JSON based
// on the supplied schema and if there is existing values already
onMounted(() => {
// Popuplate cred editor if it's not already been edited
// Populate cred editor if it's not already been edited
if (!props.existingCredentialValues?.length) {
const schemaFillIn = props.schemaForSelectedCred.schema.attrNames.map(
(a: string) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
ref="jsonVal"
:to-json="schemaToJson"
:from-json="jsonToSchema"
generic="SchemaSendRequest"
>
<!-- schema name -->
<ValidatedField
Expand Down Expand Up @@ -57,8 +58,6 @@ import { useVuelidate } from '@vuelidate/core';
import { helpers, required } from '@vuelidate/validators';
import { storeToRefs } from 'pinia';
import Button from 'primevue/button';
import InputSwitch from 'primevue/inputswitch';
import Textarea from 'primevue/textarea';
import { reactive, ref } from 'vue';
import { useI18n } from 'vue-i18n';
import { useToast } from 'vue-toastification';
Expand Down Expand Up @@ -171,9 +170,6 @@ if (props.isCopy) {
const v$ = useVuelidate(rules, formFields);
const schemaValuesJson = ref<string>('');
const showRawJson = ref<boolean>(false);
function convertToJson(): SchemaSendRequest | undefined {
const attributeNames = attributes.value?.attributes
.filter((x: Attribute) => x.name !== '')
Expand All @@ -186,17 +182,17 @@ function convertToJson(): SchemaSendRequest | undefined {
};
}
const schemaToJson = () => {
function schemaToJson(): string | undefined {
const rawJson: SchemaSendRequest | undefined = convertToJson();
if (rawJson) {
return JSON.stringify(rawJson, undefined, 2);
} else {
toast.error('Failed to convert Schema to Json');
throw new Error('Failed to convert to Json');
toast.error('Failed to convert to Json');
return undefined;
}
};
}
function jsonToSchema(jsonString: string) {
function jsonToSchema(jsonString: string): SchemaSendRequest | undefined {
const parsed = tryParseJson<SchemaSendRequest>(jsonString);
if (parsed) {
const newAt: Array<Attribute> = [
Expand All @@ -206,27 +202,29 @@ function jsonToSchema(jsonString: string) {
attributes.value.attributes = newAt;
formFields.name = parsed.schema_name;
formFields.version = parsed.schema_version;
return parsed;
} else {
toast.error('The JSON you inputted has invalid syntax');
throw new Error('Failed to parse Schema');
return undefined;
}
}
// Form submission
const submitted = ref(false);
const handleSubmit = async (isFormValid: boolean) => {
submitted.value = true;
if (!isFormValid) {
return;
}
try {
if (!isFormValid) return;
const payload: SchemaSendRequest | undefined = jsonVal.value.showRawJson
? tryParseJson<SchemaSendRequest>(jsonVal.value.valuesJson ?? '')
? jsonToSchema(jsonVal.value.valuesJson)
: convertToJson();
if (!payload?.attributes.length) {
if (!payload) return;
if (!payload.attributes.length) {
toast.error(t('configuration.schemas.emptyAttributes'));
return undefined;
return;
}
if (payload) {
Expand Down

0 comments on commit 16d633c

Please sign in to comment.