Skip to content

Commit

Permalink
Merge pull request #33 from VaquitApp/feature/receive-invite
Browse files Browse the repository at this point in the history
Feat: receive invite
  • Loading branch information
gabokatta authored May 29, 2024
2 parents 7c152c8 + 0c5b6f6 commit e3f9f83
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 6 deletions.
5 changes: 4 additions & 1 deletion my-app/src/lib/server/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,5 +93,8 @@ export const categoryService = {
list: (groupId: Id, cookies: Cookies) => get(`category/${groupId}`, getAuthHeader(cookies))
};
export const inviteService = {
send: (data: SendInvite, cookies: Cookies) => post(`invite`, data, getAuthHeader(cookies))
get: (token: string, cookies: Cookies) => get(`invite/${token}`, getAuthHeader(cookies)),
send: (data: SendInvite, cookies: Cookies) => post(`invite`, data, getAuthHeader(cookies)),
accept: (data: AcceptInvite, cookies: Cookies) =>
post(`invite/join/${data.token}`, undefined!, getAuthHeader(cookies))
};
30 changes: 30 additions & 0 deletions my-app/src/routes/invites/accept/[id]/+page.server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { inviteService } from '$lib/server/api';
import { error, redirect } from '@sveltejs/kit';
import type { Actions, PageServerLoad } from './$types';

export const load: PageServerLoad = async ({ params, cookies }) => {
const token = params.id || '';
const invite: Invite = await inviteService.get(token, cookies);
return { invite };
};

export const actions: Actions = {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
default: async ({ cookies, request, params }) => {
const token = params.id || '';

// TODO: El metodo getGroup esta restringido a solo lectura de usuarios que ya forman parte del team
// La idea era en el formulario mostrar el nombre y descripcion del grupo, pero por ahora estamos ok
// con esto.

if (!token || !token.length) {
throw error(400, 'Token is required');
}

const invite: AcceptInvite = {
token: token
};
await inviteService.accept(invite, cookies);
redirect(302, `/groups`);
}
};
36 changes: 36 additions & 0 deletions my-app/src/routes/invites/accept/[id]/+page.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<script lang="ts">
import { title } from '$lib';
import type { PageData } from './$types';
export let data: PageData;
const pageTitle = `Unirse`;
</script>

<svelte:head>
<title>{title} - {pageTitle}</title>
</svelte:head>

<nav aria-label="breadcrumb">
<ul>
<li><a href="/groups">Grupos</a></li>
<li>Invitaciones</li>
<li>Aceptar</li>
</ul>
</nav>

<h2>¡Has sido Invitad@ a un grupo!</h2>
<form method="POST">
<fieldset>
<!--TODO: TRAERNOS DATOS DEL GRUPO..-->
<label>
ID del Grupo
<input type="text" name="group_id" value={data.invite.group_id} disabled />
</label>
<label>
Token de la Invitación
<input type="text" name="token" value={data.invite.token} disabled />
</label>
<button>Unirse</button>
<button type="button" class="outline" on:click={() => history.back()}>Cancelar</button>
</fieldset>
</form>
10 changes: 5 additions & 5 deletions my-app/src/routes/invites/send/[[id=integer]]/+page.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import type { Actions, PageServerLoad } from './$types';
import { getUserId } from '$lib/auth';

export const load: PageServerLoad = async ({ params, cookies }) => {
const id = Number(params.id) || 0;
const group: Group = await groupService.get(id, cookies);
const group_id = Number(params.id) || 0;
const group: Group = await groupService.get(group_id, cookies);
return { group };
};

export const actions: Actions = {
default: async ({ cookies, request, params }) => {
const id = Number(params.id) || 0;
const group_id = Number(params.id) || 0;
const data = await request.formData();
const email = data.get('email')?.toString();

Expand All @@ -22,9 +22,9 @@ export const actions: Actions = {
const invite: SendInvite = {
sender_id: getUserId(cookies),
receiver_email: email,
group_id: id
group_id: group_id
};
await inviteService.send(invite, cookies);
redirect(302, `/groups/members/${id}`);
redirect(302, `/groups/members/${group_id}`);
}
};
1 change: 1 addition & 0 deletions my-app/src/routes/invites/send/[[id=integer]]/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<ul>
<li><a href="/groups">Grupos</a></li>
<li>Invitaciones</li>
<li>Enviar</li>
</ul>
</nav>

Expand Down
12 changes: 12 additions & 0 deletions my-app/src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,23 @@ declare global {
amount: number;
date: string;
};
type Invite = {
id: Id;
sender_id: Id;
receiver_id: Id;
group_id: Id;
token: string;
status: string;
creation_date: string;
};
type SendInvite = {
sender_id: Id;
receiver_email: string;
group_id: Id;
};
type AcceptInvite = {
token: string;
};
}

export {};

0 comments on commit e3f9f83

Please sign in to comment.