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

API mockup + save state #29

Open
wants to merge 6 commits into
base: workflow
Choose a base branch
from
Open
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
11 changes: 11 additions & 0 deletions src/js/api/constants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export const API_BASE = /* BASE URL OF THE API */ 'https://linktoapi.example';

/* Replace the placeholders with the actual values */

export const API_AUTH = '/auth';
export const API_REGISTER = '/register';
export const API_LOGIN = '/login';
export const API_KEY_URL = '/create-api-key';
export const API_LISTINGS = '/auction/listings';
export const API_KEY =
/* RETRIEVE YOUR API KEY FROM THE API AND STORE IT HERE */ 'YOUR_API_KEY';
8 changes: 8 additions & 0 deletions src/js/api/fetch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { headers } from './headers.js';

export async function authFetch(url, options = {}) {
return fetch(url, {
...options,
headers: headers(Boolean(options.body)),
});
}
22 changes: 22 additions & 0 deletions src/js/api/headers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { load } from '../storage/load.js';
import { API_KEY } from './constants.js';

export function headers(hasBody = false) {
const headers = new Headers();

const token = load('token');

if (token) {
headers.append('Authorization', `Bearer ${load('token')}`);
}

if (API_KEY) {
headers.append('X-Noroff-API-Key', API_KEY);
}

if (hasBody) {
headers.append('Content-Type', 'application/json');
}

return headers;
}
42 changes: 36 additions & 6 deletions src/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,57 @@ import spreadsheet from './spreadsheet';
import userColsAndRows from './helpers/userColsAndRows';
import toggleDarkMode from './darkModeToggle/toggleDarkMode.mjs';
import { addCellTargetingEvents } from './spreadsheet/cellNavigation';
import { initDB } from './spreadsheet/db.js';
import { initDB, saveCellValue, getCellValue } from './spreadsheet/db.js';

const spreadsheetContainer = document.querySelector('#spreadsheetContainer');

// indexedDB
// Function to load initial data from IndexedDB
async function loadInitialData(cols, rows) {
const data = [];
for (let row = 0; row < rows; row++) {
const rowData = [];
for (let col = 0; col < cols; col++) {
const cellId = `cell-${row}-${col}`;
const cellValue = await getCellValue(cellId);
rowData.push(cellValue !== null ? cellValue : '');
}
data.push(rowData);
}
return data;
}

// Initialize the spreadsheet with data from IndexedDB or default data
initDB()
.then(() => {
.then(async () => {
console.log('IndexedDB initialized');

// DarkMode
toggleDarkMode();

const [cols, rows] = userColsAndRows();

// Load initial data
const initialData = await loadInitialData(cols, rows);

// Create and append the spreadsheet to the container
spreadsheetContainer.append(spreadsheet(cols, rows));
const hot = spreadsheet(cols, rows, initialData);
spreadsheetContainer.append(hot);

addCellTargetingEvents('#spreadsheetContainer table', (col, row, value) => {
petternikolai marked this conversation as resolved.
Show resolved Hide resolved
// Here you can put the save function with params for cell contents.
console.log('save', col, row, value);
const cellId = `cell-${row}-${col}`;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the save cell was already done in another way from someone else, did you change it by purpose?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is merely a suggestion. We can revert to the old code if you want :)

saveCellValue(cellId, value);
});

// Auto-save every 10 seconds
setInterval(() => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as discussed, I'm not sure they meant to save the whole spreadsheet every 10 seconds, or just the cell that is currently editing

Copy link
Contributor Author

@petternikolai petternikolai May 30, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@WeronikaMartinsen what is your take on this? I don't see any reason not to save the state of the whole spreadsheet every 10 seconds. In the future the code could be extended to save more things on the page if needed as well. I just interpreted "Save state" as saving the state of the whole spreadsheet and not a single cell

hot.getData().forEach((row, rowIndex) => {
row.forEach((cell, colIndex) => {
const cellId = `cell-${rowIndex}-${colIndex}`;
saveCellValue(cellId, cell);
});
});
console.log('Spreadsheet state saved');
}, 10000);
})
.catch((error) => {
console.error('Failed to initialize IndexedDB:', error);
Expand Down