|
1 | 1 | <script lang="ts">
|
2 | 2 | import { onMount } from 'svelte';
|
3 |
| - import { ChatMessage, MessageInput } from '$lib/fragments'; |
| 3 | + import { ChatMessage, MessageInput, InputFile } from '$lib/fragments'; |
4 | 4 | import { Avatar, Button, Input, Label } from '$lib/ui';
|
5 |
| - import { InputFile } from '$lib/fragments'; |
6 | 5 | import { goto } from '$app/navigation';
|
7 | 6 | import { page } from '$app/state';
|
8 | 7 | import Settings from '$lib/icons/Settings.svelte';
|
9 | 8 | import { clickOutside } from '$lib/utils';
|
10 | 9 |
|
11 | 10 | let messagesContainer: HTMLDivElement;
|
12 | 11 | let messageValue = $state('');
|
13 |
| -
|
14 | 12 | let userId = 'user-1';
|
15 | 13 | let id = page.params.id;
|
16 | 14 |
|
|
75 | 73 | let groupImageDataUrl = $state(group.avatar);
|
76 | 74 | let groupImageFiles = $state<FileList | undefined>();
|
77 | 75 |
|
78 |
| - function handleGroupImageChange() { |
79 |
| - if (groupImageFiles && groupImageFiles[0]) { |
| 76 | + $effect(()=> { |
| 77 | + if (groupImageFiles?.[0]) { |
| 78 | + const file = groupImageFiles[0]; |
80 | 79 | const reader = new FileReader();
|
81 | 80 | reader.onload = (e) => {
|
82 | 81 | if (e.target?.result) {
|
83 | 82 | groupImageDataUrl = e.target.result as string;
|
84 | 83 | }
|
85 | 84 | };
|
86 |
| - reader.readAsDataURL(groupImageFiles[0]); |
| 85 | + reader.readAsDataURL(file); |
87 | 86 | }
|
88 |
| - } |
89 |
| -
|
90 |
| - $effect(() => { |
91 |
| - if (groupImageFiles) handleGroupImageChange(); |
92 |
| - }); |
93 |
| -
|
| 87 | + }) |
| 88 | + |
94 | 89 | function saveGroupInfo() {
|
95 | 90 | group.name = groupName;
|
96 | 91 | group.description = groupDescription;
|
97 | 92 | group.avatar = groupImageDataUrl;
|
98 | 93 | openEditDialog = false;
|
99 | 94 | }
|
| 95 | +
|
| 96 | + const currentUser = group.members.find((m) => m.id === userId); |
| 97 | + const canEdit = currentUser?.role === 'admin' || currentUser?.role === 'owner'; |
100 | 98 | </script>
|
101 | 99 |
|
102 | 100 | <section class="flex flex-col md:flex-row items-center justify-between gap-4 px-2 md:px-4 py-3 border-b border-gray-200">
|
|
118 | 116 | >
|
119 | 117 | View Members
|
120 | 118 | </Button>
|
121 |
| - <button |
122 |
| - onclick={() => (openEditDialog = true)} |
123 |
| - class="border border-brand-burnt-orange-900 rounded-full p-2" |
124 |
| - > |
125 |
| - <Settings size="24px" color="var(--color-brand-burnt-orange)" /> |
126 |
| - </button> |
| 119 | + {#if canEdit} |
| 120 | + <button |
| 121 | + onclick={() => (openEditDialog = true)} |
| 122 | + class="border border-brand-burnt-orange-900 rounded-full p-2" |
| 123 | + > |
| 124 | + <Settings size="24px" color="var(--color-brand-burnt-orange)" /> |
| 125 | + </button> |
| 126 | + {/if} |
127 | 127 | </div>
|
128 | 128 | </section>
|
129 | 129 |
|
|
148 | 148 | />
|
149 | 149 | </section>
|
150 | 150 |
|
151 |
| -<dialog open={openEditDialog} use:clickOutside={() => openEditDialog = false} onclose={() => (openEditDialog = false)} class="w-full max-w-[300px] z-50 absolute start-[50%] top-[50%] translate-x-[-50%] translate-y-[-50%] p-2 border border-gray-100 rounded-4xl" > |
| 151 | +<dialog |
| 152 | + open={openEditDialog} |
| 153 | + use:clickOutside={() => (openEditDialog = false)} |
| 154 | + onclose={() => (openEditDialog = false)} |
| 155 | + class="w-[90vw] md:max-w-[30vw] z-50 absolute start-[50%] top-[50%] translate-x-[-50%] translate-y-[-50%] p-4 border border-gray-400 rounded-3xl bg-white shadow-xl" |
| 156 | +> |
152 | 157 | <div class="flex flex-col gap-6">
|
153 | 158 | <div>
|
154 |
| - <InputFile |
155 |
| - bind:files={groupImageFiles} |
156 |
| - accept="image/*" |
157 |
| - label="Upload Group Avatar" |
158 |
| - cancelLabel="Remove" |
159 |
| - oncancel={() => { |
160 |
| - groupImageDataUrl = ''; |
161 |
| - groupImageFiles = undefined; |
162 |
| - }} |
163 |
| - /> |
164 |
| - {#if groupImageDataUrl} |
165 |
| - <Avatar |
166 |
| - class="mt-4" |
167 |
| - src={groupImageDataUrl} |
168 |
| - alt="Group preview" |
169 |
| - size="lg" |
| 159 | + {#if canEdit} |
| 160 | + <InputFile |
| 161 | + bind:files={groupImageFiles} |
| 162 | + accept="image/*" |
| 163 | + label="Upload Group Avatar" |
| 164 | + cancelLabel="Remove" |
| 165 | + oncancel={() => { |
| 166 | + groupImageDataUrl = ''; |
| 167 | + groupImageFiles = undefined; |
| 168 | + }} |
170 | 169 | />
|
171 | 170 | {/if}
|
| 171 | + {#if groupImageDataUrl} |
| 172 | + <Avatar class="mt-4" src={groupImageDataUrl} alt="Group preview" size="lg" /> |
| 173 | + {/if} |
172 | 174 | </div>
|
173 | 175 |
|
174 | 176 | <div>
|
175 | 177 | <Label>Group Name</Label>
|
176 |
| - <Input type="text" placeholder="Edit group name" bind:value={groupName} /> |
| 178 | + {#if canEdit} |
| 179 | + <Input type="text" placeholder="Edit group name" bind:value={groupName} /> |
| 180 | + {:else} |
| 181 | + <p class="text-gray-700">{group.name}</p> |
| 182 | + {/if} |
177 | 183 | </div>
|
178 | 184 |
|
179 | 185 | <div>
|
180 | 186 | <Label>Description</Label>
|
181 |
| - <Input type="text" placeholder="Edit group description" bind:value={groupDescription} /> |
| 187 | + {#if canEdit} |
| 188 | + <Input type="text" placeholder="Edit group description" bind:value={groupDescription} /> |
| 189 | + {:else} |
| 190 | + <p class="text-gray-700">{group.description}</p> |
| 191 | + {/if} |
182 | 192 | </div>
|
183 | 193 |
|
184 | 194 | <hr class="text-grey" />
|
185 |
| - <Button size="sm" variant="secondary" callback={saveGroupInfo}>Save Changes</Button> |
| 195 | + <div class="flex items-center gap-2"> |
| 196 | + <Button size="sm" variant="primary" callback={() => {(openEditDialog = false)}}>Cancel</Button> |
| 197 | + {#if canEdit} |
| 198 | + <Button size="sm" variant="secondary" callback={saveGroupInfo}>Save Changes</Button> |
| 199 | + {/if} |
| 200 | + </div> |
186 | 201 | </div>
|
187 | 202 | </dialog>
|
0 commit comments