Skip to content

Commit be3ec6f

Browse files
grv-saini-20sosweetham
authored andcommitted
fix: role based editing
1 parent d3d2485 commit be3ec6f

File tree

1 file changed

+53
-38
lines changed
  • platforms/pictique/src/routes/(protected)/group/[id]

1 file changed

+53
-38
lines changed

platforms/pictique/src/routes/(protected)/group/[id]/+page.svelte

Lines changed: 53 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
<script lang="ts">
22
import { onMount } from 'svelte';
3-
import { ChatMessage, MessageInput } from '$lib/fragments';
3+
import { ChatMessage, MessageInput, InputFile } from '$lib/fragments';
44
import { Avatar, Button, Input, Label } from '$lib/ui';
5-
import { InputFile } from '$lib/fragments';
65
import { goto } from '$app/navigation';
76
import { page } from '$app/state';
87
import Settings from '$lib/icons/Settings.svelte';
98
import { clickOutside } from '$lib/utils';
109
1110
let messagesContainer: HTMLDivElement;
1211
let messageValue = $state('');
13-
1412
let userId = 'user-1';
1513
let id = page.params.id;
1614
@@ -75,28 +73,28 @@
7573
let groupImageDataUrl = $state(group.avatar);
7674
let groupImageFiles = $state<FileList | undefined>();
7775
78-
function handleGroupImageChange() {
79-
if (groupImageFiles && groupImageFiles[0]) {
76+
$effect(()=> {
77+
if (groupImageFiles?.[0]) {
78+
const file = groupImageFiles[0];
8079
const reader = new FileReader();
8180
reader.onload = (e) => {
8281
if (e.target?.result) {
8382
groupImageDataUrl = e.target.result as string;
8483
}
8584
};
86-
reader.readAsDataURL(groupImageFiles[0]);
85+
reader.readAsDataURL(file);
8786
}
88-
}
89-
90-
$effect(() => {
91-
if (groupImageFiles) handleGroupImageChange();
92-
});
93-
87+
})
88+
9489
function saveGroupInfo() {
9590
group.name = groupName;
9691
group.description = groupDescription;
9792
group.avatar = groupImageDataUrl;
9893
openEditDialog = false;
9994
}
95+
96+
const currentUser = group.members.find((m) => m.id === userId);
97+
const canEdit = currentUser?.role === 'admin' || currentUser?.role === 'owner';
10098
</script>
10199

102100
<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,12 +116,14 @@
118116
>
119117
View Members
120118
</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}
127127
</div>
128128
</section>
129129

@@ -148,40 +148,55 @@
148148
/>
149149
</section>
150150

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+
>
152157
<div class="flex flex-col gap-6">
153158
<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+
}}
170169
/>
171170
{/if}
171+
{#if groupImageDataUrl}
172+
<Avatar class="mt-4" src={groupImageDataUrl} alt="Group preview" size="lg" />
173+
{/if}
172174
</div>
173175

174176
<div>
175177
<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}
177183
</div>
178184

179185
<div>
180186
<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}
182192
</div>
183193

184194
<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>
186201
</div>
187202
</dialog>

0 commit comments

Comments
 (0)