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

Added some comments to the code #11

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 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
4,286 changes: 2,575 additions & 1,711 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"devDependencies": {
"@neodrag/svelte": "^2.0.3",
"@sveltejs/adapter-static": "^1.0.1",
"@sveltejs/kit": "1.3.0",
"@sveltejs/kit": "^1.11.0",
Copy link
Member

Choose a reason for hiding this comment

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

If you update sveltekit then you will probably need to update vitepwa/sveltekit as well, does it still build properly with this change?

Copy link
Author

Choose a reason for hiding this comment

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

It still builds properly. I think I have a whole bunch of stuff thats been updated since polaris was made in this commit, but it doesn't seem to have any impact.

Copy link
Member

Choose a reason for hiding this comment

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

ah no worries if something breaks I can push a hotfix for it

"@tailwindcss/forms": "^0.5.3",
"@typescript-eslint/eslint-plugin": "^5.45.0",
"@typescript-eslint/parser": "^5.45.0",
Expand Down
5 changes: 5 additions & 0 deletions src/lib/components/AddForm.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,15 @@
import { randomID } from "$lib/id";
let team = "";
let match = "";

//This is called onFormSubmit but its really for creating a form?
//I guess because youre submitting like a mini form with team and match number
function onFormSubmit() {
if (!error) {
//assigns it an ID and puts it in activeResponses
const id = randomID();
$activeResponses[id] = {
//data is an array of components by their IDs
data: {},
id: id,
scout: $scout,
Expand Down
7 changes: 7 additions & 0 deletions src/lib/sheet.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//The name and code suggests this file does the spreadsheet/server stuff
Copy link
Member

Choose a reason for hiding this comment

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

Yeah this file has the function get to fetch the data from the spreadsheet and get it in the correct shape for using in the site, and then also append which turns the response queue into what the server wants. The server is in server/server.js I think and on get requests grabs match data from the sheet as well as the form schema. On post requests it appends each row of data (comes in a list of lists) after the last row of data in a sheet that we specify


import { form, keys, lastGet, matches, teams } from "$lib/store";
import type {
Group,
Expand All @@ -13,6 +15,7 @@ import type {
import { get as getStore } from "svelte/store";
import { PUBLIC_API_URL } from "$env/static/public";

//gets data from spreadsheet
export const get = async () => {
try {
const response = await fetch(PUBLIC_API_URL);
Expand All @@ -35,6 +38,7 @@ export const get = async () => {
}
};

//Formats app data so it can be sent to the spreadsheet
export const append = async (responseQueue: Response[]) => {
try {
await fetch(PUBLIC_API_URL, {
Expand All @@ -59,6 +63,8 @@ export const append = async (responseQueue: Response[]) => {
return false;
}
};

//this is where the form gets set
const setForm = (
sections: string[][],
config: string[][],
Expand Down Expand Up @@ -201,5 +207,6 @@ const setMatches = (match_array: number[][]) => {
matches.set(match_obj);
};

//Im not really sure what this does but it is necessary for setform
Copy link
Member

Choose a reason for hiding this comment

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

This is a matrix transpose the data is in a 2d array but the columns and rows are incorrect for what we want to do because Google sheets gives us an array of columns but we want an array of rows I think that's what it is atleast haven't touched this code in a while

const transpose = <T>(matrix: T[][]) =>
matrix[0].map((_, i) => matrix.map((array) => array[i]));
26 changes: 26 additions & 0 deletions src/lib/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,50 @@ import type { Form, Response, Team, Match, Field } from "$lib/types";
import { persisted } from "svelte-local-storage-store";
import { extractGroups } from "./serialize";

//Details of which type of form is being used
export const form = persisted<Form | null>("form", null);

//for autofilling form creation data
export const scout = persisted<string>("scout", "");
export const teams = persisted<Record<number, Team>>("teams", {});
export const matches = persisted<Record<number, Match>>("matches", {});
Comment on lines 10 to 12
Copy link
Member

Choose a reason for hiding this comment

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

These are for autocomplete when adding a new response, scout saves the name so you don't have to type it in again, and teams and matches is used to have a dropdown for them


//The currently active response (the one the user sees on the form page)
export const response = persisted<number | null>("response", null);

//ID of the response we are displaying the QR code of
export const code = persisted<number | null>("code", null);

//list of responses which are "active" eg. can be edited (not yet submitted)
Copy link
Member

Choose a reason for hiding this comment

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

Important part is that this is also structured as a map from the response id to the response, which is useful because we use the id in a bunch of places to refer to the response we are using

//Structured as a map of ids to responses
export const activeResponses = persisted<Record<number, Response>>(
"activeResponses",
{}
);

//something to do with grid, shouldn't be necessary this year
Copy link
Member

Choose a reason for hiding this comment

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

Yeah so the grid won't be necessary but if there is a need for a form field that needs multiple data points then let me know because that is why the grid is much more complicated and needs special handling

export const fields = persisted<Record<number, Record<string, Field>>>(
"fields",
{}
);

//This is used for formatting responses before they are sent to the server (not quite sure how though)
Copy link
Member

Choose a reason for hiding this comment

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

This is the order of the keys because JavaScript objects aren't ordered when we take the data from it we want them to be in the correct order for the server

export const keys = persisted<string[]>("keys", []);

//list of QRs that have already been scanned so they don't get scanned twice
export const scans = persisted<number[]>("scans", []);
Copy link
Member

Choose a reason for hiding this comment

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

Yeah this is just a list of the ids of responses we have already scanned so we don't double scan anything and send it twice to the sheet


//list of submitted responses waiting to go to server
export const responseQueue = persisted<Response[]>("responseQueue", []);

//the time that the get function was last called? not sure what get does yet
export const lastGet = persisted("lastGet", 0);

//check is device connected to internet (writable only works online)
Copy link
Member

Choose a reason for hiding this comment

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

Looks like I don't actually use this at all, but both get and append in sheet.ts instead of causing an error when offline will just return false, which for append is important cause only if it returns true do we know it's safe to remove the submitted responses from the response queue

export const online = writable(true);

//colour theme stuff, not necessary
export const theme = persisted("theme", "arctos");

//looks like a list of IDs and boolean values for whether or not those forms have errors?
Copy link
Member

Choose a reason for hiding this comment

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

Yeah if a form has any errors then we want to be able to disable the submit button in the home page, key thing here is we initially set the error to true when we create a new response because fields won't populate with default values till the form is actually opened

export const errors = persisted<Record<number, boolean>>("errors", {});
2 changes: 2 additions & 0 deletions src/routes/+layout.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
if (Date.now() - $lastGet > 60 * 60 * 1000) {
get();
}

//Sends form to spreadsheet
function sync() {
setTimeout(async () => {
if ($responseQueue.length != 0) {
Expand Down
1 change: 1 addition & 0 deletions src/routes/code/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@
</div>
</div>
{/if}

2 changes: 2 additions & 0 deletions src/routes/form/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
</script>

{#if $form && $response}
<!--This should be the component that shows the actual form questions-->
<!--I assume its not working right now because form isn't set-->
<FormFlow />
{:else}
<FormsList />
Expand Down