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

Feat: add function to standardize uploaded csv in batch signup #169

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
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
22 changes: 21 additions & 1 deletion src/pages/course/[name]/members.vue
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,32 @@ const forceUpdate = ref(false);
const isProcessingSignup = ref(false);
const errorMsg = ref("");
const previewCSV = ref<{ headers?: string[]; body?: string[][] }>({});

// no csv validation was handled
Copy link
Member

Choose a reason for hiding this comment

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

Could be removed?

const standardizeUsername = (csv: string): string => {
const rows = csv.split("\n");
const header = rows[0];
const body = rows.slice(1).map((r) => r.split(","));
const usernameFieldIndex = header.split(",").findIndex((v) => v === "username");
if (usernameFieldIndex < 0) {
return csv;
}

body.forEach((data) => {
data[usernameFieldIndex] = data[usernameFieldIndex]?.toUpperCase();
});

const bodyString = body.map((data) => data.join(","));
return [header, ...bodyString].join("\n");
};

watch(newMembers, () => {
if (!newMembers.value) return;
const reader = new FileReader();
reader.onload = (evt) => {
if (typeof evt.target?.result !== "string") return;
newMembersCSVString.value = evt.target?.result;
newMembersCSVString.value = standardizeUsername(evt.target?.result || "");

const rows = newMembersCSVString.value.split("\n");
previewCSV.value.headers = rows[0].split(",");
previewCSV.value.body = rows.slice(1).map((r) => r.split(","));
Expand Down
Loading