Skip to content

Commit

Permalink
Added feature to encode data for sharing or cross-session use
Browse files Browse the repository at this point in the history
  • Loading branch information
felpsey committed Dec 15, 2024
1 parent 885e734 commit 546ab3b
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 9 deletions.
57 changes: 53 additions & 4 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useState } from 'react'
import { useEffect, useState } from 'react'

import './App.css'

Expand All @@ -11,17 +11,66 @@ import { Module } from './domain/Module';


function App() {
const [isModalOpen, setIsModalOpen] = useState(false);
const [isModalOpen, setIsModalOpen] = useState<boolean>(false);
const [modules, setModules] = useState<Module[]>([]);

function newModuleEntry() {
// Decode search params when the component is mounted
useEffect(() => {
decodeSearchParams();
}, []);

// Update encoded search params when modules state is changed
useEffect(() => {
if (modules.length !== 0) {
updateSearchParams();
}
}, [modules]);

function decodeSearchParams(): void {
let currentUrl: URL = new URL(document.location.href);
let data: string|null = currentUrl.searchParams.get('data');

if (data) {
importUnstructuredModuleData(JSON.parse(atob(data)));
}
}

function importUnstructuredModuleData(unstructuredModuleData: []): void {
let structuredModuleData = unstructuredModuleData.map((module: any) => {
return new Module(module._code, module._credits, module._stage, module._grade);
});

setModules(structuredModuleData);
}

function encodeModuleData(): string {
// Convert modules array to JSON then base64 encode
return btoa(JSON.stringify(modules));
}

function newModuleEntry(): void {
setIsModalOpen(true);
}

function addModuleData(module: Module) {
function addModuleData(module: Module): void {
// Copy contents of existing modules array and append new module object
setModules([...modules, module]);
}

function updateSearchParams(): void {
// Encode module data in memory
let data = encodeModuleData();

// Fetch the currentl URL, this will change each time this method is called
let currentUrl = new URL(document.location.href);

// Set encoded data on URL object on 'data' property
currentUrl.searchParams.set('data', data);

// Append encoded params to URI without reloading page
history.replaceState({}, "", currentUrl.toString());
}

return (
<div className='container mx-auto h-screen border-l border-r border-gray-100'>
<div className='flex pt-5 pb-5 border-b border-gray-100'>
Expand Down
4 changes: 4 additions & 0 deletions src/components/ModuleTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ export default function Table({ modules }: TableProps) {
<tr>
<th className="border border-gray-200 px-4 py-2">Module</th>
<th className="border border-gray-200 px-4 py-2">Credits</th>
<th className="border border-gray-200 px-4 py-2">Stage</th>
<th className="border border-gray-200 px-4 py-2">Grade</th>
<th className="border border-gray-200 px-4 py-2">Weighted Grade</th>
</tr>
</thead>
<tbody>
Expand All @@ -23,7 +25,9 @@ export default function Table({ modules }: TableProps) {
>
<td className="border border-gray-200 px-4 py-2">{item.code}</td>
<td className="border border-gray-200 px-4 py-2">{item.credits}</td>
<td className="border border-gray-200 px-4 py-2">{item.stage}</td>
<td className="border border-gray-200 px-4 py-2">{item.grade}</td>
<td className="border border-gray-200 px-4 py-2">{item.weightedGrade}</td>
</tr>
))}
</tbody>
Expand Down
10 changes: 5 additions & 5 deletions src/domain/Module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ export class Module {
private _weightedGrade: number;

constructor(code: string, credits: number, stage: number, grade: number) {
this.code = code;
this.credits = credits;
this.stage = stage;
this.grade = grade;
this._code = code;
this._credits = credits;
this._stage = stage;
this._grade = grade;
this._weightedGrade = this.calculateWeightedGrade();
}

Expand Down Expand Up @@ -53,6 +53,6 @@ export class Module {
}

calculateWeightedGrade(): number {
return this._weightedGrade = this.stage * this.grade;
return this._weightedGrade = (this.credits * (this.stage - 1)) * this.grade;
}
}

0 comments on commit 546ab3b

Please sign in to comment.