@dcrackel/hematournamentui 1.0.708 → 1.0.709

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,109 @@
1
+ import { afterEach, describe, expect, it } from 'vitest';
2
+ import { enableAutoUnmount, mount } from '@vue/test-utils';
3
+ import ImageCropper from './ImageCropper.vue';
4
+
5
+ enableAutoUnmount(afterEach);
6
+
7
+ const mountCropper = props =>
8
+ mount(ImageCropper, {
9
+ props: {
10
+ urlToImage: 'https://images.example/profile.png',
11
+ imageServer: 'https://images.example/',
12
+ uploadImageName: 'profile',
13
+ isProfile: true,
14
+ ...props
15
+ },
16
+ global: { stubs: { AvatarCropper: true } }
17
+ });
18
+
19
+ describe('ImageCropper upload draft events', () => {
20
+ it('emits a successful upload URL and previews it with a fresh cache key', async () => {
21
+ const wrapper = mountCropper({ uploadImageName: 'new-profile' });
22
+ wrapper.vm.handleUploading();
23
+ wrapper.vm.handleUploaded({ response: { status: 201 } });
24
+ await wrapper.vm.$nextTick();
25
+
26
+ expect(wrapper.emitted('update:url')).toEqual([['https://images.example/new-profile.png']]);
27
+ expect(wrapper.emitted('update:uploading')).toEqual([[true], [false]]);
28
+ const preview = new URL(wrapper.find('img').attributes('src'));
29
+ expect(preview.pathname).toBe('/new-profile.png');
30
+ expect(preview.searchParams.has('preview')).toBe(true);
31
+ await wrapper.setProps({ urlToImage: 'https://images.example/new-profile.png' });
32
+ expect(wrapper.find('img').attributes('src')).toBe(preview.href);
33
+ });
34
+
35
+ it('refreshes the preview even when an upload overwrites the existing URL', async () => {
36
+ const wrapper = mountCropper();
37
+ wrapper.vm.handleUploaded({ response: { status: 200 } });
38
+ await wrapper.vm.$nextTick();
39
+ expect(wrapper.find('img').attributes('src')).toContain('/profile.png?preview=');
40
+ expect(wrapper.emitted('update:url')).toEqual([['https://images.example/profile.png']]);
41
+ });
42
+
43
+ it('keeps the previous image and reports unsuccessful responses separately', () => {
44
+ const wrapper = mountCropper();
45
+ wrapper.vm.handleUploading();
46
+ wrapper.vm.handleUploaded({ response: { status: 500 } });
47
+ expect(wrapper.vm.uploading).toBe(false);
48
+ expect(wrapper.vm.localImageUrl).toBe('https://images.example/profile.png');
49
+ expect(wrapper.emitted('update:url')).toBeUndefined();
50
+ expect(wrapper.emitted('error:upload')[0][0]).toMatchObject({ message: 'Image upload failed.', status: 500 });
51
+ });
52
+
53
+ it('reports malformed upload responses without crashing or emitting an edit', () => {
54
+ const wrapper = mountCropper();
55
+ wrapper.vm.handleUploaded(undefined);
56
+ expect(wrapper.emitted('update:url')).toBeUndefined();
57
+ expect(wrapper.emitted('error:upload')).toHaveLength(1);
58
+ });
59
+
60
+ it('rejects unsafe uploaded URLs', () => {
61
+ const wrapper = mountCropper({ imageServer: 'javascript:' });
62
+ wrapper.vm.handleUploaded({ response: { status: 200 } });
63
+ expect(wrapper.emitted('update:url')).toBeUndefined();
64
+ expect(wrapper.emitted('error:upload')[0][0].message).toContain('valid image URL');
65
+ });
66
+
67
+ it('shows a placeholder on image-load failure and leaves the draft URL intact', async () => {
68
+ const wrapper = mountCropper();
69
+ await wrapper.find('img').trigger('error');
70
+ expect(wrapper.emitted('update:url')).toBeUndefined();
71
+ expect(wrapper.emitted('error:load')[0][0]).toMatchObject({ url: 'https://images.example/profile.png' });
72
+ expect(wrapper.find('img').attributes('src')).not.toBe('https://images.example/profile.png');
73
+ expect(wrapper.vm.localImageUrl).toBe('https://images.example/profile.png');
74
+ expect(wrapper.vm.showCropper).toBe(false);
75
+ });
76
+
77
+ it('reconciles authoritative URL changes after a failed preview', async () => {
78
+ const wrapper = mountCropper();
79
+ await wrapper.find('img').trigger('error');
80
+ await wrapper.setProps({ urlToImage: 'https://images.example/confirmed.png' });
81
+ expect(wrapper.find('img').attributes('src')).toBe('https://images.example/confirmed.png');
82
+ });
83
+
84
+ it('reports the cropper upload error and releases the busy state', () => {
85
+ const wrapper = mountCropper();
86
+ wrapper.vm.handleUploading();
87
+ wrapper.vm.handlerError({ type: 'upload', message: 'Upload rejected', context: { response: { status: 413 } } });
88
+ expect(wrapper.emitted('error:upload')).toEqual([[{ message: 'Upload rejected', status: 413 }]]);
89
+ expect(wrapper.vm.uploading).toBe(false);
90
+ expect(wrapper.emitted('update:url')).toBeUndefined();
91
+ });
92
+
93
+ it('reports rejected upload requests and releases the busy state', async () => {
94
+ const wrapper = mountCropper();
95
+ const response = Promise.reject(new Error('Network unavailable'));
96
+ wrapper.vm.handleUploading({ response });
97
+ await wrapper.vm.$nextTick();
98
+ expect(wrapper.vm.uploading).toBe(false);
99
+ expect(wrapper.emitted('error:upload')[0][0].message).toBe('Network unavailable');
100
+ expect(wrapper.emitted('update:url')).toBeUndefined();
101
+ });
102
+
103
+ it('disables the native image selector and guards programmatic opening', () => {
104
+ const wrapper = mountCropper({ disabled: true });
105
+ expect(wrapper.find('button').attributes('disabled')).toBeDefined();
106
+ wrapper.vm.setShowCropper();
107
+ expect(wrapper.vm.showCropper).toBe(false);
108
+ });
109
+ });
@@ -1,133 +1,192 @@
1
- <template>
2
- <section class="flex w-full justify-center">
3
- <img
4
- v-if="!uploading"
5
- :src="displayedUrl"
6
- :class="getClasses"
7
- :style="customStyle"
8
- :alt="displayedUrl"
9
- @error="onImageError"
10
- @click="setShowCropper()"
11
- />
12
- <div v-if="uploading" :class="getSkeletonClass" @error="onImageError" @click="setShowCropper()" />
13
- <avatar-cropper
14
- v-model="showCropper"
15
- :upload-url="`${uploadServer}${uploadImageName}`"
16
- :cropper-options="cropperOptions"
17
- @uploading="handleUploading"
18
- @uploaded="handleUploaded"
19
- @completed="handleCompleted"
20
- @error="handlerError"
21
- />
22
- </section>
23
- </template>
24
-
25
- <script>
26
- import AvatarCropper from 'vue-avatar-cropper';
27
- import SelectAnProfile from '../../../../assets/select-profile.png';
28
- import SelectAnImage from '../../../../assets/select-tournament.png';
29
- import loading from '../../../../assets/loading-spinner.gif';
30
-
31
- export default {
32
- name: 'GenericImageCropper',
33
- emits: ['update:url'],
34
- props: {
35
- urlToImage: {
36
- type: String,
37
- default: SelectAnImage
38
- },
39
- customClass: {
40
- type: String,
41
- default: ''
42
- },
43
- customSkeletonClass: {
44
- type: String,
45
- default: ''
46
- },
47
- customStyle: {
48
- type: Object,
49
- default: () => ({})
50
- },
51
- uploadImageName: {
52
- type: String,
53
- default: 'default-image'
54
- },
55
- uploadServer: {
56
- type: String,
57
- default: ''
58
- },
59
- imageServer: {
60
- type: String,
61
- default: ''
62
- },
63
- isProfile: {
64
- type: Boolean,
65
- default: false
66
- },
67
- cropperOptions: {
68
- type: Object,
69
- default: () => ({
70
- aspectRatio: 3 / 2,
71
- autoCropArea: 1,
72
- viewMode: 1,
73
- movable: true,
74
- zoomable: true
75
- })
76
- }
77
- },
78
- components: {
79
- AvatarCropper
80
- },
81
- data() {
82
- return {
83
- loading,
84
- showCropper: false,
85
- uploading: false,
86
- localImageUrl: this.urlToImage
87
- };
88
- },
89
- computed: {
90
- displayedUrl() {
91
- return this.localImageUrl || (this.isProfile ? SelectAnProfile : SelectAnImage);
92
- },
93
- getClasses() {
94
- return this.customClass
95
- ? this.customClass
96
- : 'h-52 w-80 border-2 border-dashed rounded-lg bg-contain bg-contain bg-center bg-no-repeat';
97
- },
98
- getSkeletonClass() {
99
- return this.customSkeletonClass
100
- ? this.customSkeletonClass
101
- : 'border-2 h-52 w-80 border-dashed rounded-lg bg-quaternaryHighlight animate-pulse';
102
- }
103
- },
104
- methods: {
105
- setShowCropper() {
106
- this.showCropper = true;
107
- },
108
- handleUploading() {
109
- this.uploading = true;
110
- },
111
- handleUploaded(response) {
112
- this.uploading = false;
113
- if (response.response.status === 200) {
114
- this.localImageUrl = `${this.imageServer}${this.uploadImageName}.png`;
115
- this.$emit('update:url', `${this.imageServer}${this.uploadImageName}.png`);
116
- }
117
- },
118
- handleCompleted() {
119
- this.message = 'Upload completed.';
120
- },
121
- handlerError(message, type, xhr) {
122
- console.error(message);
123
- console.error(type);
124
- console.error(xhr);
125
- this.message = 'Oops! Something went wrong...';
126
- },
127
- onImageError() {
128
- this.$emit('update:url', null);
129
- this.showCropper = true;
130
- }
131
- }
132
- };
133
- </script>
1
+ <template>
2
+ <section class="flex w-full justify-center" :aria-busy="uploading" :inert="disabled">
3
+ <button
4
+ v-if="!uploading"
5
+ type="button"
6
+ :disabled="disabled"
7
+ :aria-label="isProfile ? 'Change profile image' : 'Change image'"
8
+ class="flex items-center justify-center"
9
+ @click="setShowCropper"
10
+ >
11
+ <img
12
+ :src="displayedUrl"
13
+ :class="getClasses"
14
+ :style="customStyle"
15
+ :alt="isProfile ? 'Profile image' : 'Uploaded image'"
16
+ @error="onImageError"
17
+ />
18
+ </button>
19
+ <div v-if="uploading" :class="getSkeletonClass" role="status" aria-label="Uploading image" />
20
+ <avatar-cropper
21
+ v-model="showCropper"
22
+ :upload-url="`${uploadServer}${uploadImageName}`"
23
+ :cropper-options="cropperOptions"
24
+ @uploading="handleUploading"
25
+ @uploaded="handleUploaded"
26
+ @completed="handleCompleted"
27
+ @error="handlerError"
28
+ />
29
+ </section>
30
+ </template>
31
+
32
+ <script>
33
+ import AvatarCropper from 'vue-avatar-cropper';
34
+ import SelectAnProfile from '../../../../assets/select-profile.png';
35
+ import SelectAnImage from '../../../../assets/select-tournament.png';
36
+ import loading from '../../../../assets/loading-spinner.gif';
37
+
38
+ function getImageErrorStatus(error, xhr) {
39
+ return error?.context?.response?.status ?? xhr?.status;
40
+ }
41
+
42
+ export default {
43
+ name: 'GenericImageCropper',
44
+ emits: ['update:url', 'update:uploading', 'error:load', 'error:upload'],
45
+ props: {
46
+ urlToImage: {
47
+ type: String,
48
+ default: SelectAnImage
49
+ },
50
+ customClass: {
51
+ type: String,
52
+ default: ''
53
+ },
54
+ customSkeletonClass: {
55
+ type: String,
56
+ default: ''
57
+ },
58
+ customStyle: {
59
+ type: Object,
60
+ default: () => ({})
61
+ },
62
+ uploadImageName: {
63
+ type: String,
64
+ default: 'default-image'
65
+ },
66
+ uploadServer: {
67
+ type: String,
68
+ default: ''
69
+ },
70
+ imageServer: {
71
+ type: String,
72
+ default: ''
73
+ },
74
+ isProfile: {
75
+ type: Boolean,
76
+ default: false
77
+ },
78
+ disabled: {
79
+ type: Boolean,
80
+ default: false
81
+ },
82
+ cropperOptions: {
83
+ type: Object,
84
+ default: () => ({
85
+ aspectRatio: 3 / 2,
86
+ autoCropArea: 1,
87
+ viewMode: 1,
88
+ movable: true,
89
+ zoomable: true
90
+ })
91
+ }
92
+ },
93
+ components: {
94
+ AvatarCropper
95
+ },
96
+ data() {
97
+ return {
98
+ loading,
99
+ showCropper: false,
100
+ uploading: false,
101
+ localImageUrl: this.urlToImage,
102
+ imageLoadFailed: false,
103
+ previewVersion: 0,
104
+ message: ''
105
+ };
106
+ },
107
+ computed: {
108
+ displayedUrl() {
109
+ const fallback = this.isProfile ? SelectAnProfile : SelectAnImage;
110
+ if (this.imageLoadFailed || !this.localImageUrl) return fallback;
111
+ if (!this.previewVersion) return this.localImageUrl;
112
+ const preview = new URL(this.localImageUrl, document.baseURI);
113
+ preview.searchParams.set('preview', this.previewVersion);
114
+ return preview.href;
115
+ },
116
+ getClasses() {
117
+ return this.customClass
118
+ ? this.customClass
119
+ : 'h-52 w-80 border-2 border-dashed rounded-lg bg-contain bg-contain bg-center bg-no-repeat';
120
+ },
121
+ getSkeletonClass() {
122
+ return this.customSkeletonClass
123
+ ? this.customSkeletonClass
124
+ : 'border-2 h-52 w-80 border-dashed rounded-lg bg-quaternaryHighlight animate-pulse';
125
+ }
126
+ },
127
+ watch: {
128
+ urlToImage(url) {
129
+ if (url === this.localImageUrl) return;
130
+ this.localImageUrl = url;
131
+ this.imageLoadFailed = false;
132
+ this.previewVersion = 0;
133
+ }
134
+ },
135
+ methods: {
136
+ setShowCropper() {
137
+ if (this.disabled || this.uploading) return;
138
+ this.showCropper = true;
139
+ },
140
+ setUploading(uploading) {
141
+ this.uploading = uploading;
142
+ this.$emit('update:uploading', uploading);
143
+ },
144
+ handleUploading(event) {
145
+ this.setUploading(true);
146
+ // The cropper exposes the request promise but does not emit rejected fetches.
147
+ event?.response?.catch?.(error => {
148
+ this.handlerError({ type: 'upload', message: error?.message || 'Image upload failed.' });
149
+ });
150
+ },
151
+ handleUploaded(response) {
152
+ const status = response?.response?.status;
153
+ if (!(status >= 200 && status < 300)) {
154
+ this.handlerError({ message: 'Image upload failed.', context: { response: response?.response } });
155
+ return;
156
+ }
157
+ const url = `${this.imageServer}${this.uploadImageName}.png`;
158
+ if (!this.isValidImageUrl(url)) {
159
+ this.handlerError({ message: 'The upload did not provide a valid image URL.' });
160
+ return;
161
+ }
162
+ this.localImageUrl = url;
163
+ this.imageLoadFailed = false;
164
+ this.previewVersion = Date.now();
165
+ this.$emit('update:url', url);
166
+ this.setUploading(false);
167
+ },
168
+ handleCompleted() {
169
+ this.message = 'Upload completed.';
170
+ },
171
+ handlerError(message, type, xhr) {
172
+ this.setUploading(false);
173
+ this.message = typeof message === 'string' ? message : message?.message || 'Image upload failed.';
174
+ const eventName = (message?.type ?? type) === 'load' ? 'error:load' : 'error:upload';
175
+ this.$emit(eventName, { message: this.message, status: getImageErrorStatus(message, xhr) });
176
+ },
177
+ isValidImageUrl(url) {
178
+ if (typeof url !== 'string' || !url.trim()) return false;
179
+ try {
180
+ return ['http:', 'https:'].includes(new URL(url, document.baseURI).protocol);
181
+ } catch {
182
+ return false;
183
+ }
184
+ },
185
+ onImageError() {
186
+ if (this.imageLoadFailed) return;
187
+ this.imageLoadFailed = true;
188
+ this.$emit('error:load', { message: 'Could not load the image.', url: this.localImageUrl });
189
+ }
190
+ }
191
+ };
192
+ </script>
@@ -65,3 +65,17 @@ export const EmptyAccessLevel = {
65
65
  userLevel: 'Club'
66
66
  }
67
67
  };
68
+
69
+ export const ClearedHemaRatingId = {
70
+ args: {
71
+ person: { ...personsMock[1], HemaRatingsId: null, HEMARatings: [{ HemaRatingId: 123 }] },
72
+ userLevel: 'Admin'
73
+ }
74
+ };
75
+
76
+ export const InvalidHemaRatingId = {
77
+ args: {
78
+ person: { ...personsMock[1], HemaRatingsId: '1.5' },
79
+ userLevel: 'Admin'
80
+ }
81
+ };
@@ -0,0 +1,39 @@
1
+ import { afterEach, describe, expect, it } from 'vitest';
2
+ import { enableAutoUnmount, mount } from '@vue/test-utils';
3
+ import Administration from './Administration.vue';
4
+ import TitledInput from '../../../../Molecules/CombinationInputs/TitledInput/TitledInput.vue';
5
+
6
+ enableAutoUnmount(afterEach);
7
+
8
+ function mountAdministration(id) {
9
+ return mount(Administration, {
10
+ props: {
11
+ person: { PersonId: 1, HemaRatingsId: id, HEMARatings: [{ HemaRatingId: 123 }], AccessLevel: 'User' },
12
+ userLevel: 'Admin'
13
+ },
14
+ global: { stubs: { DropDownMenu: true } }
15
+ });
16
+ }
17
+
18
+ const findHemaInput = wrapper => wrapper.findAllComponents(TitledInput)[1].find('input');
19
+
20
+ describe('Administration HR ID draft', () => {
21
+ it.each([null, ''])('keeps the explicitly cleared ID %j blank', id => {
22
+ const wrapper = mountAdministration(id);
23
+ expect(findHemaInput(wrapper).element.value).toBe('');
24
+ expect(findHemaInput(wrapper).attributes('aria-invalid')).toBe('false');
25
+ });
26
+
27
+ it('falls back to legacy ratings only when the canonical ID is undefined', () => {
28
+ const wrapper = mountAdministration(undefined);
29
+ expect(findHemaInput(wrapper).element.value).toBe('123');
30
+ });
31
+
32
+ it('shows invalid-ID feedback and preserves invalid input for correction', async () => {
33
+ const wrapper = mountAdministration(0);
34
+ expect(wrapper.text()).toContain('positive whole-number ID');
35
+ expect(findHemaInput(wrapper).attributes('aria-invalid')).toBe('true');
36
+ await findHemaInput(wrapper).setValue('-5');
37
+ expect(wrapper.emitted('update:person')).toEqual([[{ field: 'HemaRatingsId', value: '-5' }]]);
38
+ });
39
+ });
@@ -14,9 +14,10 @@
14
14
  <div class="flex flex-col mb-4">
15
15
  <TitledInput
16
16
  :inputValue="hemaRatingId"
17
- :inputType="'number'"
17
+ inputType="text"
18
18
  placeholder="HEMA Rating ID"
19
- title="HEMA Rating ID"
19
+ :invalid="hemaRatingValidation.invalid"
20
+ :title="hemaRatingLabel"
20
21
  @update:value="emitUpdate('HemaRatingsId', $event)"
21
22
  />
22
23
  </div>
@@ -41,6 +42,8 @@
41
42
  import TitledInput from '../../../../Molecules/CombinationInputs/TitledInput/TitledInput.vue';
42
43
  import BaseText from '../../../../Atoms/Text/BaseText.vue';
43
44
  import DropDownMenu from '../../../../Organisms/ComplexInputs/DropDown/DropDownMenu.vue';
45
+ import { getDraftHemaRatingsId } from '../../PersonManagementDraft.js';
46
+ import { validateField } from '../../../../Util/Validations/PersonManagementValidations.js';
44
47
 
45
48
  const STANDARD_ACCESS_LEVELS = [
46
49
  { text: 'User', link: 'User' },
@@ -63,7 +66,14 @@ export default {
63
66
  return this.person?.PersonId ?? '';
64
67
  },
65
68
  hemaRatingId() {
66
- return this.person?.HemaRatingsId ?? this.person?.HEMARatings?.[0]?.HemaRatingId ?? '';
69
+ return getDraftHemaRatingsId(this.person);
70
+ },
71
+ hemaRatingValidation() {
72
+ return validateField('HemaRatingsId', this.hemaRatingId);
73
+ },
74
+ hemaRatingLabel() {
75
+ if (this.hemaRatingValidation.invalid) return `HEMA Rating ID - ${this.hemaRatingValidation.message}`;
76
+ return 'HEMA Rating ID';
67
77
  },
68
78
  accessLevels() {
69
79
  if (this.userLevel === 'Admin') {