|
1 | 1 | <script> |
2 | | - import { createEventDispatcher, tick } from 'svelte'; |
| 2 | + import { tick } from 'svelte'; |
3 | 3 | import Modal from './Modal.svelte'; |
4 | 4 | import Button from '../components/Button.svelte'; |
5 | 5 | import Input from '../components/Input.svelte'; |
6 | 6 | import Textarea from '../components/Textarea.svelte'; |
7 | 7 | import CustomFieldRenderer from '../features/items/CustomFieldRenderer.svelte'; |
8 | 8 | import Label from '../components/Label.svelte'; |
9 | 9 | import { Camera, Trash2 } from 'lucide-svelte'; |
10 | | -
|
11 | | - const dispatch = createEventDispatcher(); |
| 10 | + import { api } from '../api.js'; |
12 | 11 |
|
13 | 12 | // Props |
14 | 13 | export let isOpen = false; |
|
23 | 22 | export let customerOrgFields = []; |
24 | 23 | export let attachmentsEnabled = false; |
25 | 24 | export let isEditing = false; |
| 25 | + export let onsave = () => {}; |
| 26 | + export let oncancel = () => {}; |
26 | 27 |
|
27 | 28 | let uploadingAvatar = false; |
28 | 29 | let showAvatarUpload = false; |
|
34 | 35 | if (!files || files.length === 0) return; |
35 | 36 |
|
36 | 37 | if (!attachmentsEnabled) { |
37 | | - alert('Attachments must be enabled to upload customer avatars'); |
| 38 | + alert('Attachments must be enabled to upload organization avatars'); |
38 | 39 | return; |
39 | 40 | } |
40 | 41 |
|
|
51 | 52 | uploadFormData.append('item_id', '0'); |
52 | 53 | uploadFormData.append('category', 'customer_avatar'); |
53 | 54 |
|
54 | | - const response = await fetch('/api/attachments/upload', { |
55 | | - method: 'POST', |
56 | | - body: uploadFormData, |
57 | | - }); |
58 | | -
|
59 | | - if (!response.ok) { |
60 | | - throw new Error(`Upload failed: ${response.statusText}`); |
61 | | - } |
62 | | -
|
63 | | - const uploadResult = await response.json(); |
| 55 | + const uploadResult = await api.attachments.upload(uploadFormData); |
64 | 56 |
|
65 | 57 | if (uploadResult && uploadResult.success && uploadResult.avatar_url) { |
66 | | - formData.avatar_url = uploadResult.avatar_url; |
| 58 | + formData = { ...formData, avatar_url: uploadResult.avatar_url }; |
67 | 59 | showAvatarUpload = false; |
68 | | - alert('Avatar uploaded successfully'); |
69 | 60 | } |
70 | 61 | } catch (err) { |
71 | 62 | alert('Failed to upload avatar: ' + (err.message || err)); |
|
75 | 66 | } |
76 | 67 |
|
77 | 68 | function removeAvatar() { |
78 | | - formData.avatar_url = null; |
| 69 | + formData = { ...formData, avatar_url: null }; |
79 | 70 | } |
80 | 71 |
|
81 | 72 | function getInitials(name) { |
|
89 | 80 |
|
90 | 81 | function handleSubmit() { |
91 | 82 | if (formData.name.trim()) { |
92 | | - dispatch('save'); |
| 83 | + onsave(); |
93 | 84 | } |
94 | 85 | } |
95 | 86 |
|
96 | 87 | function handleCancel() { |
97 | | - dispatch('cancel'); |
| 88 | + oncancel(); |
98 | 89 | } |
99 | 90 |
|
100 | 91 | async function focusNameInput() { |
|
125 | 116 | > |
126 | 117 | <div class="p-6"> |
127 | 118 | <h3 class="text-xl font-semibold mb-6" style="color: var(--ds-text);"> |
128 | | - {isEditing ? 'Edit Customer' : 'New Customer'} |
| 119 | + {isEditing ? 'Edit Organization' : 'New Organization'} |
129 | 120 | </h3> |
130 | 121 |
|
131 | 122 | <div class="grid grid-cols-1 md:grid-cols-2 gap-6"> |
132 | 123 | <div> |
133 | | - <Label required class="mb-2">Customer Name</Label> |
| 124 | + <Label required class="mb-2">Organization Name</Label> |
134 | 125 | <Input id={nameInputId} bind:inputRef={nameInputRef} bind:value={formData.name} required /> |
135 | 126 | </div> |
136 | 127 |
|
|
142 | 133 |
|
143 | 134 | <!-- Avatar Upload Section --> |
144 | 135 | <div class="mt-6"> |
145 | | - <Label class="mb-2">Customer Avatar</Label> |
| 136 | + <Label class="mb-2">Organization Avatar</Label> |
146 | 137 |
|
147 | 138 | <!-- Avatar Preview --> |
148 | 139 | {#if formData.avatar_url} |
149 | 140 | <div class="flex items-center gap-4 mb-3"> |
150 | 141 | <img |
151 | 142 | src={formData.avatar_url} |
152 | | - alt="Customer avatar" |
| 143 | + alt="Organization avatar" |
153 | 144 | class="w-16 h-16 rounded object-cover" |
154 | 145 | /> |
155 | 146 | <div class="flex-1"> |
|
190 | 181 | </Button> |
191 | 182 | {#if !attachmentsEnabled} |
192 | 183 | <p class="text-xs mt-1" style="color: var(--ds-text-warning);"> |
193 | | - Attachments must be enabled to upload customer avatars |
| 184 | + Attachments must be enabled to upload organization avatars |
194 | 185 | </p> |
195 | 186 | {/if} |
196 | 187 | </div> |
|
227 | 218 | id="active" |
228 | 219 | class="mr-3 w-4 h-4 text-blue-600 rounded focus:ring-2 focus:ring-blue-500" |
229 | 220 | /> |
230 | | - <label for="active" class="text-sm font-medium" style="color: var(--ds-text);">Active Customer</label> |
| 221 | + <label for="active" class="text-sm font-medium" style="color: var(--ds-text);">Active Organization</label> |
231 | 222 | </div> |
232 | 223 |
|
233 | 224 | <!-- Custom Fields --> |
|
257 | 248 | size="medium" |
258 | 249 | keyboardHint={submitHint} |
259 | 250 | > |
260 | | - {isEditing ? 'Update' : 'Create'} Customer |
| 251 | + {isEditing ? 'Update' : 'Create'} Organization |
261 | 252 | </Button> |
262 | 253 | <Button |
263 | 254 | variant="default" |
|
0 commit comments