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

Update/integration #72

Merged
merged 14 commits into from
Jun 22, 2024
46 changes: 17 additions & 29 deletions src/client/src/lib/components/admin/modals/add/+AddWorkspace.svelte
Original file line number Diff line number Diff line change
@@ -1,42 +1,30 @@
<script lang="ts">
import { Button, Modal, Label, Input } from 'flowbite-svelte';
import { organisationName } from '$lib/store';
import { organizations } from '$lib/services/orgs';

let formModal = false;

// Function to store the access token in local storage
function storeOrgID(id: string): void {
localStorage.setItem('organisationID', id);
console.log('organasation ID', id);
}

// Function to handle form submission
async function handleSubmit(event: SubmitEvent) {
console.log('add is being handled');
async function handleSubmit(event) {
console.log('add workspace is being handled');
// Prevent the default form submission behavior
event.preventDefault();

// Create a FormData object from the form
const formData = new FormData(event.target as HTMLFormElement);
const name = formData.get('org_name')?.toString() ?? '';
console.log('This is the name parameter:', name);
const userID = localStorage.getItem('userID') || 'non-existent';
const image =
'https://www.edarabia.com/wp-content/uploads/2013/08/university-of-pretoria-logo-south-africa.jpg';
//console.log("User ID Add Org:", userID);
try {
const response = await organizations(name, userID, image);
const formData = new FormData(event.currentTarget);

console.log('Response:', response);
const organisationId = localStorage.getItem('organisationID') || 'non-existent';
const createdBy = localStorage.getItem('userID') || 'non-existent';

if (response) {
storeOrgID(response._id);
organisationName.set(response.name);
}
} catch (error) {
console.error('create org error:', error);
alert('Create failed');
if (organisationId !== 'non-existent') {
formData.append('organisationId', organisationId);
formData.append('createdBy', createdBy);
const response = await fetch('/admin/workspaces?/add', {
method: 'POST',
body: formData
});
console.log(response);
formModal = false;
} else {
const errorMessage = 'You need to be part of an organisation to create a workspace';
alert(errorMessage);
}
}
</script>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@
import '@fontsource/roboto';

// Function to store the access token in local storage
function storeAccessToken(token: string, id: string): void {
function storeAccessToken(token: string, id: string, organisationID: string): void {
localStorage.setItem('accessToken', token);
localStorage.setItem('userID', id);
localStorage.setItem('organisationID', organisationID);

console.log('This is the ID', id);

console.log('User ID stored:', localStorage.getItem('userID'));
Expand All @@ -38,8 +40,8 @@

console.log('Response:', response);

if (response && response.accessToken) {
storeAccessToken(response.accessToken, response.sub);
if (response && response.accessToken && response.organisations[0]) {
storeAccessToken(response.accessToken, response.sub, response.organisations[0]);
}
goto('/student');
} catch (error) {
Expand Down
72 changes: 72 additions & 0 deletions src/client/src/lib/services/materials.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import axios from 'axios';

export async function materials(
type: boolean,
workspace_id: string,
lecturer_id: string,
title: string,
description: string,
file_path: string
) {
try {
const response = await axios.post('http://localhost:3000/materials', {
type: type,
workspace_id: workspace_id,
lecturer_id: lecturer_id,
title: title,
description: description,
file_path: file_path
});

return response.data;
} catch (error) {
throw new Error('Create Material Failed');
}
}

export async function getMaterial(id: string): Promise<any> {
try {
const response = await axios.get(`http://localhost:3000/materials/${id}`);
return response.data;
} catch (error) {
throw new Error('Get Material Failed');
}
}

export async function updateWorkspace(
id: string,
name: string,
organisationID: string,
users: string[],
image: string
): Promise<any> {
try {
const response = await axios.put(`http://localhost:3000/materials/${id}`, {
name: name,
organisationID: organisationID,
users: users,
image: image
});
return response.data;
} catch (error) {
throw new Error('Update Material Failed');
}
}

export async function deleteMaterial(id: string): Promise<string> {
try {
const response = await axios.delete(`http://localhost:3000/materials/${id}`);
return response.data.message;
} catch (error) {
throw new Error('Delete Material Failed');
}
}

export async function listAllMaterial() {
try {
const response = await axios.get(`http://localhost:3000/materials`);
return response.data;
} catch (error) {
throw new Error('List All Material Failed');
}
}
63 changes: 63 additions & 0 deletions src/client/src/lib/services/workspaces.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import axios from 'axios';

// create workspace POST
export async function workspaces(
name: string,
organisationID: string,
createdBy: string,
users: string[],
image: string
): Promise<any> {
try {
const response = await axios.post('http://localhost:3000/workspaces', {
name: name,
organisationID: organisationID,
createdBy: createdBy,
users: users,
image: image
});

return response.data;
} catch (error) {
throw new Error('Create Workspace Failed');
}
}

// get Workspace GET
export async function getWorkspace(id: string): Promise<any> {
try {
const response = await axios.get(`http://localhost:3000/workspaces/${id}`);
return response.data;
} catch (error) {
throw new Error('Get Workspace Failed');
}
}

export async function updateWorkspace(
id: string,
name: string,
organisationID: string,
users: string[],
image: string
): Promise<any> {
try {
const response = await axios.put(`http://localhost:3000/workspaces/${id}`, {
name: name,
organisationID: organisationID,
users: users,
image: image
});
return response.data;
} catch (error) {
throw new Error('Update Workspace Failed');
}
}

export async function deleteWorkspace(id: string): Promise<string> {
try {
const response = await axios.delete(`http://localhost:3000/workspaces/${id}`);
return response.data.message;
} catch (error) {
throw new Error('Delete Workspace Failed');
}
}
1 change: 1 addition & 0 deletions src/client/src/lib/store/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export const file = writable('');
export const module = writable('');

export const user_details = writable({});
export const module_details = writable({});
export const username = 'e241198014';

export const userInfo = writable<any>({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@ async function addDetails(name, email, role) {
console.log(role);
}

async function addWorkspace(name, email, createdBy, image) {
console.log(name);
console.log(email);
console.log(createdBy);
console.log(image);

//TODO: Based on the api contract you now have all the infomation to create a work space now just make the call;
}

async function editDetails(name, email, role) {
console.log(name);
console.log(email);
Expand Down Expand Up @@ -36,6 +45,31 @@ export const actions = {
status: 200
};
},
add: async ({ request }) => {
const formData = await request.formData();
const name = formData.get('work_name');
const email = formData.get('organisationId');
const createdBy = formData.get('createdBy');
const image = formData.get('image');

console.log(name);
console.log(email);
console.log(createdBy);
console.log(image);

try {
await addWorkspace(name, email, createdBy, image);

return {
status: 200
};
} catch (error) {
console.log(error);
return {
status: 500
};
}
},
edit: async ({ request }) => {
const formData = await request.formData();
const name = formData.get('org_name');
Expand Down
54 changes: 54 additions & 0 deletions src/client/src/routes/student/+page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,60 @@ import { user_details } from '$lib/store';
export function load() {
const details = get(user_details);
console.log('User details:', details);
/**
* Note to intergration:
* now once the user logs in and the user details are stored in the store
* they should be as follows according to the api contract
* {
* "id": "60d21b4667d0d8992e610c85",
* "username": "a24125634",
* "name": "John",
* "surname": "Doe",
* "email": "johndoe@example.com",
* "role": "admin",
* "organisations": ["60d21b4967d0d8992e610c86"],
* "workspaces": ["60d21b4c67d0d8992e610c87"],
* "image": "https://example.com/images/johndoe.jpg",
* "createdAt": "2023-01-01T00:00:00.000Z",
* "updatedAt": "2023-01-01T00:00:00.000Z",
* "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9"
* }
*
* Now what your job if is to take the workspaces array, "workspaces": ["60d21b4c67d0d8992e610c87"],
* and fectch the details if of every a workspace a student is apart of according to the contact this
* should be the infomation on the workspaces you get
* {
* "id": "60d21b4c67d0d8992e610c87",
* "name": "Development",
* "organisationId": "60d21b49667d0d8992e610c86",
* "createdBy": "60d21b4667d0d8992e610c85",
* "users": ["60d21b4667d0d8992e610c85"],
* "image": "https://example.com/images/development.jpg",
* "createdAt": "2023-01-01T00:00:00.000Z",
* "updatedAt": "2023-01-01T00:00:00.000Z"
* }
*
* now we only look to capture the name and id of the workspace and store it in and array as follows
* {
* modules: [
* {
* module_name: 'Computer Graphics',
* module_id: '789123456'
* },
*
* {
* module_name: 'Programming Languages',
* module_id: '789456123'
* }
* ]
* }
*
* please make sure that the how the return object is returned is not
* altered as it is used in later stages, this return object should then be stored in a store named
* module_details, it is advised that you console log the
* current respose just to see what it look s like then you make sure that your respose is the exact same
*
*/
return {
modules: [
{
Expand Down
Loading
Loading