Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure the teams page auto updates for new teams or users added to a team #88

Merged
merged 1 commit into from
Oct 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion modules/odr_frontend/src/routes/admin/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
</script>

<svelte:head>
<title>OMI Data Pipeline| Admin</title>
<title>Admin | OMI Data Pipeline</title>
</svelte:head>

<code>
Expand Down
45 changes: 30 additions & 15 deletions modules/odr_frontend/src/routes/admin/teams/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
import UserRow from '../users/UserRow.svelte';

export let data;
const teams = data.teams;
const users = data.users;
const teams_users = data.teams_users;

$: teams = data.teams;
$: users = data.users;
$: teams_users = data.teams_users;

let newTeamName = '';
let userSearch = '';
let selected_team: null | number = null;
Expand All @@ -19,8 +21,18 @@
},
body: JSON.stringify({ newTeamName })
});

const res = await req.json();
if (!res.success) {

if (res.success) {
const newTeam = {
...res.team,
created_at: new Date(res.team.created_at),
updated_at: new Date(res.team.created_at),
};
teams = [...teams, newTeam];
newTeamName = '';
} else {
console.error(res.error);
}
}
Expand All @@ -33,19 +45,22 @@
body: JSON.stringify({ userId, teamId })
});
const res = await req.json();
if (!res.success) {
console.error(res.error);
}
if (res.success) {
teams_users = [...teams_users, res.team_user];
userSearch = '';
} else {
console.error(res.error);
}
}

let modal: ModalSettings = {
type: 'confirm',
title: 'Please Confirm',
body: `Are you sure you wish to create a team named ${newTeamName}?`,
// TRUE if confirm pressed, FALSE if cancel pressed
response: (r: boolean) => {
response: async (r: boolean) => {
if (r) {
createTeam();
await createTeam();
}
}
};
Expand All @@ -70,12 +85,12 @@
title: 'Please Confirm',
body: `Are you sure you wish to add <span class="text-primary-400">${event.detail.label}</span> to the <span class="text-secondary-400">${teams.find((t) => t.id === selected_team)?.name}</span> team?`,
// TRUE if confirm pressed, FALSE if cancel pressed
response: (r: boolean) => {
if (r) {
console.log(`Adding user ${event.detail.label} to team ${selected_team}`);
addUserToTeam(parseInt(event.detail.value), selected_team as number);
}
}
response: async (r: boolean) => {
if (r) {
console.log(`Adding user ${event.detail.label} to team ${selected_team}`);
await addUserToTeam(parseInt(event.detail.value), selected_team as number);
}
}
};
modalStore.trigger(add_user_to_team_modal);
}
Expand Down
41 changes: 29 additions & 12 deletions modules/odr_frontend/src/routes/admin/teams/api/+server.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,33 @@
import { pgClient } from '$lib/server/pg';
import type { RequestHandler } from '@sveltejs/kit';

export const POST: RequestHandler = async ({ request }) => {
const body = await request.json();
if (!body.newTeamName) {
return new Response(JSON.stringify({ success: false, error: 'No Team name provided' }));
}
try {
const result = await pgClient.query('INSERT INTO teams (name) VALUES ($1)', [body.newTeamName]);
console.info(`Added team ${body.newTeamName}`);
return new Response(JSON.stringify({ success: true, result: result }));
} catch (e) {
console.error(`Failed to add team ${body.newTeamName}`);
return new Response(JSON.stringify({ success: false, error: e }));
}
const body = await request.json();
if (!body.newTeamName) {
return new Response(JSON.stringify({ success: false, error: 'No Team name provided' }), {
status: 400,
headers: { 'Content-Type': 'application/json' }
});
}

try {
const result = await pgClient.query(
'INSERT INTO teams (name) VALUES ($1) RETURNING id, name, created_at, updated_at',
[body.newTeamName]
);

const newTeam = result.rows[0];
console.info(`Added team ${body.newTeamName} with id ${newTeam.id}`);

return new Response(JSON.stringify({ success: true, team: newTeam }), {
status: 201,
headers: { 'Content-Type': 'application/json' }
});
} catch (e) {
console.error(`Failed to add team ${body.newTeamName}:`, e);
return new Response(JSON.stringify({ success: false, error: e.message }), {
status: 500,
headers: { 'Content-Type': 'application/json' }
});
}
};
60 changes: 38 additions & 22 deletions modules/odr_frontend/src/routes/admin/teams/api/addUser/+server.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,44 @@
import { PG_API, pgClient } from '$lib/server/pg';
import type { RequestHandler } from '@sveltejs/kit';

export const POST: RequestHandler = async ({ request }) => {
const body = await request.json();
if (!body.userId) {
return new Response(JSON.stringify({ success: false, error: 'No user ID provided' }));
}
if (!body.teamId) {
return new Response(JSON.stringify({ success: false, error: 'No team ID provided' }));
}
const body = await request.json();
if (!body.userId) {
return new Response(JSON.stringify({ success: false, error: 'No user ID provided' }), {
status: 400,
headers: { 'Content-Type': 'application/json' }
});
}
if (!body.teamId) {
return new Response(JSON.stringify({ success: false, error: 'No team ID provided' }), {
status: 400,
headers: { 'Content-Type': 'application/json' }
});
}

const team_users = await PG_API.teams.getUsers(body.teamId);
if (team_users.some((user) => user.user_id == body.userId)) {
return new Response(JSON.stringify({ success: false, error: 'User already in team' }));
}
const team_users = await PG_API.teams.getUsers(body.teamId);
if (team_users.some((user) => user.user_id == body.userId)) {
return new Response(JSON.stringify({ success: false, error: 'User already in team' }), {
status: 400,
headers: { 'Content-Type': 'application/json' }
});
}

try {
const result = await pgClient.query(
'INSERT INTO user_teams (team_id, user_id, role) VALUES ($1, $2, $3)',
[body.teamId, body.userId, 'member']
);
console.info(`Added user ${body.userId} to team ${body.teamId}`);
return new Response(JSON.stringify({ success: true, result: result }));
} catch (e) {
console.error(`Failed to add user ${body.userId} to team ${body.teamId}`);
return new Response(JSON.stringify({ success: false, error: e }));
}
try {
const result = await pgClient.query(
'INSERT INTO user_teams (team_id, user_id, role) VALUES ($1, $2, $3) RETURNING *',
[body.teamId, body.userId, 'member']
);
console.info(`Added user ${body.userId} to team ${body.teamId}`);
return new Response(JSON.stringify({ success: true, team_user: result.rows[0] }), {
status: 201,
headers: { 'Content-Type': 'application/json' }
});
} catch (e) {
console.error(`Failed to add user ${body.userId} to team ${body.teamId}:`, e);
return new Response(JSON.stringify({ success: false, error: e.message }), {
status: 500,
headers: { 'Content-Type': 'application/json' }
});
}
};