11
11
</div >
12
12
13
13
<div class =" field mt-5" >
14
- <!-- Label/toggle -->
15
- <div class =" flex justify-content-between" >
16
- <div class =" flex justify-content-start" >
17
- <label for =" credentialValuesEdit" >
18
- <strong >{{ $t('issue.credentialFieldValues') }}</strong >
19
- </label >
20
- </div >
21
- <div class =" flex justify-content-end" >
22
- {{ $t('common.json') }}
23
- <InputSwitch v-model =" showRawJson" class =" ml-1" @input =" toggleJson" />
24
- </div >
25
- </div >
26
-
27
- <!-- Raw JSON input mode -->
28
- <div v-show =" showRawJson" >
29
- <Textarea
30
- id =" credentialValuesEdit"
31
- v-model =" credentialValuesJson"
32
- :auto-resize =" true"
33
- rows =" 20"
34
- cols =" 60"
35
- class =" w-full mt-1"
36
- />
37
-
38
- <div class =" flex justify-content-end" >
39
- <small >
40
- <!-- eslint-disable-next-line @intlify/vue-i18n/no-raw-text -->
41
- {{ $t('issue.schema') }}: {{ schemaForSelectedCred.schema.name }}
42
- <!-- eslint-disable-next-line @intlify/vue-i18n/no-raw-text -->
43
- {{ $t('issue.version') }}:
44
- {{ schemaForSelectedCred.schema.version }}
45
- </small >
46
- </div >
47
- </div >
48
-
49
- <!-- Dynamic Attribute field list -->
50
- <div v-show =" !showRawJson" >
14
+ <ToggleJson
15
+ ref =" jsonVal"
16
+ :to-json =" credentialValueToJson"
17
+ :from-json =" jsonToCredentialValue"
18
+ generic =" CredentialValue[]"
19
+ >
20
+ <!-- Dynamic Attribute field list -->
51
21
<div
52
22
v-for =" (item, index) in credentialValuesRaw"
53
23
:key =" item.name"
62
32
class =" w-full"
63
33
/>
64
34
</div >
35
+ </ToggleJson >
36
+ <div class =" flex justify-content-end" >
37
+ <small >
38
+ <!-- eslint-disable-next-line @intlify/vue-i18n/no-raw-text -->
39
+ {{ $t('issue.schema') }}: {{ schemaForSelectedCred.schema.name }}
40
+ <!-- eslint-disable-next-line @intlify/vue-i18n/no-raw-text -->
41
+ {{ $t('issue.version') }}:
42
+ {{ schemaForSelectedCred.schema.version }}
43
+ </small >
65
44
</div >
66
-
67
45
<Button label =" Save" class =" mt-5 w-full" @click =" saveCredValues" />
68
46
</div >
69
47
</template >
73
51
import { onMounted , ref } from ' vue' ;
74
52
// PrimeVue / Validation
75
53
import Button from ' primevue/button' ;
76
- import InputSwitch from ' primevue/inputswitch' ;
77
54
import InputText from ' primevue/inputtext' ;
78
- import Textarea from ' primevue/textarea' ;
79
55
import { useToast } from ' vue-toastification' ;
56
+ import { tryParseJson } from ' @/helpers/jsonParsing' ;
57
+ import ToggleJson from ' @/components/common/ToggleJson.vue' ;
80
58
81
59
const toast = useToast ();
82
60
83
61
// Props
62
+ interface CredentialValue {
63
+ name: string ;
64
+ value: string ;
65
+ }
84
66
interface Props {
85
- existingCredentialValues? : { name : string ; value : string } [];
67
+ existingCredentialValues? : CredentialValue [];
86
68
header: String ;
87
69
schemaForSelectedCred: any ;
88
70
}
@@ -91,58 +73,43 @@ const props = defineProps<Props>();
91
73
const emit = defineEmits ([' back' , ' save' ]);
92
74
93
75
// Fields
94
- const credentialValuesJson = ref (' ' );
95
- const credentialValuesRaw = ref ([] as { name: string ; value: string }[]);
96
-
97
- const showRawJson = ref (false );
76
+ const credentialValuesRaw = ref <CredentialValue []>([]);
98
77
99
- // TODO: util function file
100
- function _tryParseJson(jsonString : string ) {
101
- try {
102
- const o = JSON .parse (jsonString );
103
- if (o && typeof o === ' object' ) {
104
- return o ;
105
- }
106
- return false ;
107
- } catch (e ) {
108
- return false ;
109
- }
110
- }
78
+ const jsonVal = ref <{ showRawJson: boolean ; valuesJson: string }>({
79
+ showRawJson: false ,
80
+ valuesJson: ' ' ,
81
+ });
111
82
112
- function _jsonToCredRaw() {
113
- const parsed = _tryParseJson (credentialValuesJson .value );
83
+ function jsonToCredentialValue(
84
+ jsonString : string
85
+ ): CredentialValue [] | undefined {
86
+ const parsed = tryParseJson <CredentialValue []>(jsonString );
114
87
if (parsed ) {
115
- credentialValuesRaw .value = JSON .parse (credentialValuesJson .value );
88
+ credentialValuesRaw .value = parsed ;
89
+ return parsed ;
116
90
} else {
117
91
toast .warning (' The JSON you inputted has invalid syntax' );
92
+ return undefined ;
118
93
}
119
94
}
120
95
121
- const toggleJson = () => {
122
- if (showRawJson .value ) {
123
- // Convert over to the json from what was entered on the fields
124
- credentialValuesJson .value = JSON .stringify (
125
- credentialValuesRaw .value ,
126
- undefined ,
127
- 2
128
- );
129
- } else {
130
- // Parse the entered JSON into fields, or ignore and warn if invalid syntax
131
- _jsonToCredRaw ();
132
- }
133
- };
96
+ function credentialValueToJson(): string | undefined {
97
+ // Convert over to the json from what was entered on the fields
98
+ const j = JSON .stringify (credentialValuesRaw .value , undefined , 2 );
99
+ return j ;
100
+ }
134
101
135
102
const saveCredValues = () => {
136
- if (showRawJson .value ) {
137
- _jsonToCredRaw ( );
103
+ if (jsonVal .value . showRawJson ) {
104
+ jsonToCredentialValue ( jsonVal . value . valuesJson );
138
105
}
139
106
emit (' save' , credentialValuesRaw .value );
140
107
};
141
108
142
- // Whnen the component is intialized set up the fields and raw JSON based
109
+ // When the component is initialized set up the fields and raw JSON based
143
110
// on the supplied schema and if there is existing values already
144
111
onMounted (() => {
145
- // Popuplate cred editor if it's not already been edited
112
+ // Populate cred editor if it's not already been edited
146
113
if (! props .existingCredentialValues ?.length ) {
147
114
const schemaFillIn = props .schemaForSelectedCred .schema .attrNames .map (
148
115
(a : string ) => {
@@ -158,10 +125,5 @@ onMounted(() => {
158
125
} else {
159
126
credentialValuesRaw .value = props .existingCredentialValues ;
160
127
}
161
- credentialValuesJson .value = JSON .stringify (
162
- credentialValuesRaw .value ,
163
- undefined ,
164
- 2
165
- );
166
128
});
167
129
</script >
0 commit comments