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

Add lodash dependency and refactor loader functions for edit functionality #30

Merged
merged 13 commits into from
Dec 7, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"javascript-lp-solver": "^0.4.24",
"jsonpath": "^1.1.1",
"localforage": "^1.10.0",
"lodash": "^4.17.21",
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Consider using specific lodash modules instead of the full package.

While lodash is a reliable utility library, importing the entire package can significantly increase your bundle size. Since this is being added for loader function refactoring, consider importing only the specific modules needed.

Example of importing specific modules:

-"lodash": "^4.17.21"
+"lodash.get": "^4.4.2",
+"lodash.set": "^4.3.2"

You can also use the modular syntax in your imports:

import get from 'lodash/get';
import set from 'lodash/set';

"match-sorter": "^6.3.1",
"mathjs": "^12.2.1",
"mdui": "^2.1.2",
Expand Down
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions src/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ function App() {

export default App;

export async function loader() {
App.loader = async function loader() {
const votes = await getDocs(collection(db, "/votes"));
const activeVotes = [];
const expiredVotes = [];
Expand Down Expand Up @@ -162,4 +162,4 @@ export async function loader() {
});

return { activeVotes, expiredVotes, scheduledVotes };
}
};
4 changes: 2 additions & 2 deletions src/Gateway.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export default function Gateway() {
return null;
}

export async function loader({ params }) {
Gateway.loader = async function loader({ params }) {
const vote = await getDoc(doc(db, `/votes/${params.id}`));
if (!vote.exists()) {
new Response("Not Found", {
Expand All @@ -47,4 +47,4 @@ export async function loader({ params }) {
const voteData = { id: vote.id, ...vote.data() };

return { voteData };
}
};
4 changes: 2 additions & 2 deletions src/Vote.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,7 @@ export default function Vote() {
);
}

export async function loader({ params }) {
Vote.loader = async function loader({ params }) {
const vote = await getDoc(doc(db, `/votes/${params.id}`));
if (!vote.exists()) {
throw new Response("Document not found.", {
Expand All @@ -424,4 +424,4 @@ export async function loader({ params }) {
const optionsData = options.docs.map((e) => ({ id: e.id, ...e.data() }));

return { vote: voteData, options: optionsData };
}
};
4 changes: 2 additions & 2 deletions src/admin/Overview.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,11 @@ export default function Overview() {
);
}

export async function loader() {
Overview.loader = async function loader() {
const votes = await getDocs(collection(db, "votes"));
return {
votes: votes.docs.map((e) => {
return { id: e.id, ...e.data() };
}),
};
}
};
4 changes: 2 additions & 2 deletions src/admin/Students.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ export default function Students() {
);
}

export async function loader() {
Students.loader = async function loader() {
const classes = await getDocs(collection(db, "class"));
return {
classes: classes.docs.map((e) => {
Expand All @@ -423,4 +423,4 @@ export async function loader() {
};
}),
};
}
};
4 changes: 2 additions & 2 deletions src/admin/utils.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
export function generateRandomHash() {
export function generateRandomHash(length = 4) {
let hash = "";
const characters =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";

for (let i = 0; i < 4; i++) {
for (let i = 0; i < length; i++) {
hash += characters.charAt(Math.floor(Math.random() * characters.length));
}

Expand Down
2 changes: 1 addition & 1 deletion src/admin/vote/Answers.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ export default function Answers() {
);
}

export async function loader({ params }) {
Answers.loader = async function loader({ params }) {
const { id } = params;
const vote = await getDoc(doc(db, `/votes/${id}`));
const voteData = vote.data();
Expand Down
2 changes: 1 addition & 1 deletion src/admin/vote/Assign.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -800,7 +800,7 @@ export default function Assign() {
);
}

export async function loader({ params }) {
Assign.loader = async function loader({ params }) {
const { id } = params;

const vote = await getDoc(doc(db, `/votes/${id}`));
Expand Down
Loading
Loading