Skip to content
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
121 changes: 59 additions & 62 deletions components/add-member.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,77 +60,76 @@ export default function AddMemberForm({ user }: { user: any }) {

React.useEffect(() => {
async function fetchMemberGroups() {
const { data, error } = await supabase
.from("member_groups")
.select()
.eq("created_by", user.id);
if (error) {
try {

Choose a reason for hiding this comment

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

Suggested change
try {

const { data, error } = await supabase
.from("member_groups")
.select()
.eq("created_by", user.id);
if (error) {
console.error("Error fetching member groups:", error);
} else {

Choose a reason for hiding this comment

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

Suggested change

setSelectedGroups(null);
setMemberGroups(data);
}
} catch (error) {
console.error("Error fetching member groups:", error);
Comment on lines +75 to 76

Choose a reason for hiding this comment

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

Suggested change
} catch (error) {
console.error("Error fetching member groups:", error);

} else {
console.log("member groups", data);
setSelectedGroups(null);
setMemberGroups(data);
}
}
fetchMemberGroups();
}, [supabase, submitted]);
fetchMemberGroups();
}, [supabase, submitted]);

interface MemberGroup {
id: number;
name: string;
}
interface MemberGroup {
id: number;
name: string;
}

async function onSubmit(data: MemberFormValues) {
console.log("DATA", selectedGroups);
try {
// Get the IDs of the selected groups that already exist in memberGroups
const existingGroupIds = selectedGroups
?.filter((group) =>
memberGroups?.map(({ name }) => name).includes(group.value)
)
.map(
(group) => memberGroups.find(({ name }) => name === group.value)?.id
);
async function onSubmit(data: MemberFormValues) {
try {
// Get the IDs of the selected groups that already exist in memberGroups
const existingGroupIds = selectedGroups
?.filter((group) =>
memberGroups?.map(({ name }) => name).includes(group.value)
)
.map(
(group) => memberGroups.find(({ name }) => name === group.value)?.id
);

// let groupIds = [];
// groupIds = groupIds.concat(existingGroupIds);
// let groupIds = [];
// groupIds = groupIds.concat(existingGroupIds);

// Add the IDs of the existing groups to groupIds
console.log("memberGroups", memberGroups);
// Check if the selectedGroups exists in memberGroups
const newGroups =
selectedGroups
// Add the IDs of the existing groups to groupIds
// console.log("memberGroups", memberGroups);
// console.log("newgroups", newGroups);
Comment on lines +101 to +102

Choose a reason for hiding this comment

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

Suggested change
// console.log("memberGroups", memberGroups);
// console.log("newgroups", newGroups);

// Check if the selectedGroups exists in memberGroups
const newGroups =
?.filter(

Choose a reason for hiding this comment

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

Suggested change
?.filter(
selectedGroups
?.filter(

(group) =>
!memberGroups?.map(({ name }) => name).includes(group.value)
)
.map(({ value }) => value) || [];

console.log("newgroups", newGroups);

// groupIds = selectedGroups?.map((group) => group.value);
// groupIds = selectedGroups?.map((group) => group.value);

// If selectedGroups doesn't exist, create a new group
let groupIds = [];
if (!!newGroups.length) {
const groupResponse = await supabase
.from("member_groups")
.insert(newGroups.map((name) => ({ name, created_by: user.id })))
.select();
console.log("gr", groupResponse);
const { data: createdGroups, error: createGroupError } = groupResponse;
console.log("created groups", createdGroups);
if (createGroupError) {
console.error("Error creating new group:", createGroupError);
} else {
if (Array.isArray(createdGroups) && createdGroups.length > 0) {
groupIds = createdGroups.map(({ id }) => id);
// If selectedGroups doesn't exist, create a new group
let groupIds = [];
if (!!newGroups.length) {
.from("member_groups")

Choose a reason for hiding this comment

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

Suggested change
.from("member_groups")
const groupResponse = await supabase
.from("member_groups")

.insert(newGroups.map((name) => ({ name, created_by: user.id })))
.select();

Choose a reason for hiding this comment

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

Suggested change

const { data: createdGroups, error: createGroupError } = groupResponse;
console.log("created groups", createdGroups);
if (createGroupError) {
console.error("Error creating new group:", createGroupError);
} else {
if (Array.isArray(createdGroups) && createdGroups.length > 0) {
groupIds = createdGroups.map(({ id }) => id);
}
}
}
}

// Create the new member
const memberUpdates = {
// Create the new member
const memberUpdates = {
email: data.email,
first_name: data.first_name,
last_name: data.last_name,
Expand All @@ -144,10 +143,9 @@ export default function AddMemberForm({ user }: { user: any }) {
.select();
if (memberError) {
throw memberError;
}
const memberID = newMember[0].id;
} const memberID = newMember[0].id;

Choose a reason for hiding this comment

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

Suggested change
} const memberID = newMember[0].id;
}
const memberID = newMember[0].id;


console.log("SELECT", selectedGroups);

Choose a reason for hiding this comment

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

delete newline

Copy link
Contributor Author

@sweep-ai-deprecated sweep-ai-deprecated bot Nov 29, 2023

Choose a reason for hiding this comment

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

🚀 Wrote Changes

Done.


// Update the joins table to associate the member with the group
if (!!selectedGroups?.length && memberID) {
Expand Down Expand Up @@ -177,10 +175,9 @@ export default function AddMemberForm({ user }: { user: any }) {
toast({
description: "Your member has been added",
});
setSubmitted(true);
router.refresh();
setSubmitted(true); router.refresh();

Choose a reason for hiding this comment

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

Suggested change
setSubmitted(true); router.refresh();
setSubmitted(true);
router.refresh();

} catch (error) {
console.log(error);

Choose a reason for hiding this comment

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

delete newline

Copy link
Contributor Author

@sweep-ai-deprecated sweep-ai-deprecated bot Nov 29, 2023

Choose a reason for hiding this comment

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

🚀 Wrote Changes

Done.

Choose a reason for hiding this comment

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

Suggested change

}
}

Expand Down Expand Up @@ -273,7 +270,7 @@ export default function AddMemberForm({ user }: { user: any }) {
}))} // Convert memberGroups to options
value={selectedGroups}
onChange={(value) => {
console.log("creatable value", value);
// console.log("creatable value", value);

Choose a reason for hiding this comment

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

delete the comment

Choose a reason for hiding this comment

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

Suggested change
// console.log("creatable value", value);

setSelectedGroups(value);
}}
/>
Expand Down
180 changes: 88 additions & 92 deletions components/edit-member.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ export default function EditMemberForm({
if (error) {
console.error("Error fetching member groups:", error);
} else {
console.log("member groups", data);
setMemberGroups(data);
}
}
Expand Down Expand Up @@ -106,123 +105,120 @@ export default function EditMemberForm({
}
}, [existingGroups]);

console.log("Select", selectedGroups);
console.log("EIT", existingGroups);
const existingGroupIds = existingGroups ? existingGroups.split(",") : [];

console.log("existingIDs", existingGroupIds);
// console.log("existingIDs", existingGroupIds);

Choose a reason for hiding this comment

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

delete the comment

Choose a reason for hiding this comment

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

Suggested change
// console.log("existingIDs", existingGroupIds);

async function onSubmit(data: MemberFormValues) {
const {
data: { user },
} = await supabase.auth.getUser();
let groupIds = [];
// Delete all existing associations for the member
const { error: deleteError } = await supabase
const {
data: { user },
} = await supabase.auth.getUser();

let groupIds = [];

// Delete all existing associations for the member
const { error: deleteError } = await supabase
.from("member_group_joins")
.delete()
.eq("member_id", member.id);
if (deleteError) {
if (deleteError) {
console.error("Error deleting existing associations:", deleteError);
}

// Get the IDs of the selected groups that already exist in memberGroups
// Get the IDs of the selected groups that already exist in memberGroups
const existingGroupIds = selectedGroups
?.filter((group) =>
memberGroups?.map(({ name }) => name).includes(group.value)
}

// Get the IDs of the selected groups that already exist in memberGroups
// Get the IDs of the selected groups that already exist in memberGroups
?.filter(
(group) => memberGroups?.map(({ name }) => name).includes(group.value),
)
.map(
(group) => memberGroups.find(({ name }) => name === group.value)?.id,
);

// Add the IDs of the existing groups to groupIds

// Save new groups - if the member group doesn't hold any of the selected groups
const newGroups =
selectedGroups
?.filter(
(group) =>
!memberGroups?.map(({ name }) => name).includes(group.value),
)
.map((group) => memberGroups.find(({ name }) => name === group.value)?.id);

// Add the IDs of the existing groups to groupIds

// Save new groups - if the member group doesn't hold any of the selected groups
const newGroups =
selectedGroups
?.filter(
(group) =>
!memberGroups?.map(({ name }) => name).includes(group.value)
)
.map(({ value }) => value) || [];

//Creating a new group if there are elements in the newgroup array
if (!!newGroups.length) {
const groupResponse = await supabase
.from("member_groups")
.insert(newGroups.map((name) => ({ name })))
.select();
console.log("gr", groupResponse);
const { data: createdGroups, error: createGroupError } = groupResponse;
console.log("created groups", createdGroups);
if (createGroupError) {
console.error("Error creating new group:", createGroupError);
} else {
if (Array.isArray(createdGroups) && createdGroups.length > 0) {
groupIds = createdGroups.map(({ id }) => id);
}
.map(({ value }) => value) || [];

//Creating a new group if there are elements in the newgroup array
if (!!newGroups.length) {
const groupResponse = await supabase
.from("member_groups")
.insert(newGroups.map((name) => ({ name })))
.select();
const { data: createdGroups, error: createGroupError } = groupResponse;
console.log("created groups", createdGroups);
if (createGroupError) {
console.error("Error creating new group:", createGroupError);
} else {
if (Array.isArray(createdGroups) && createdGroups.length > 0) {
groupIds = createdGroups.map(({ id }) => id);
}
}

try {
const updates = {
email: data.email,
first_name: data.first_name,
last_name: data.last_name,
created_by: user?.id,
};

const selectedGroupIds = selectedGroups
? selectedGroups.map((group) => group.value)
: [];

console.log("SId", selectedGroupIds);
if (!!selectedGroups?.length && member.id) {
// Insert new associations
const joinUpdates = selectedGroups
}
}

try {
const updates = {
email: data.email,
first_name: data.first_name,
last_name: data.last_name,
created_by: user?.id,
};

const selectedGroupIds = selectedGroups
? selectedGroups.map((group) => group.value)
: [];


if (!!selectedGroups?.length && member.id) {
// Insert new associations
const joinUpdates = selectedGroups
?.map((group) => memberGroups.find((g) => g.name === group.value)?.id)
.concat(groupIds)
.filter((id) => id !== undefined)
.map((memberGroupId) => ({
member_id: member.id,
group_id: memberGroupId,
}));

const { error: joinError } = await supabase
.from("member_group_joins")
.insert(joinUpdates);
if (joinError) {
console.error("Error updating member_group_joins:", joinError);
}
}
let { error } = await supabase
.from("members")
.update(updates)
.eq("id", member.id);
if (error) {
toast({
description: "An error occurred while updating the member",
});
} else {
toast({
description: "Your member has been updated",
});
router.refresh();

const { error: joinError } = await supabase
.from("member_group_joins")
.insert(joinUpdates);
if (joinError) {
console.error("Error updating member_group_joins:", joinError);
}
} catch (error) {
}
let { error } = await supabase
.from("members")
.update(updates)
.eq("id", member.id);
if (error) {
toast({
description: "An error occurred while updating the member",
});
} else {
toast({
description: "Your member has been updated",
});
router.refresh();
}
} catch (error) {
toast({
description: "An error occurred while updating the member",
});
}
}

// React.useEffect(() => {
// async function fetchMemberGroups() {
// const { data, error } = await supabase.from("member_groups").select();
// if (error) {
// console.error("Error fetching member groups:", error);
// } else {
// console.log("gr", groupResponse);
// console.log("created groups", createdGroups);
Comment on lines -224 to +221

Choose a reason for hiding this comment

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

revert these changes

// console.log("member groups", data);
// setMemberGroups(data);
// }
Expand Down Expand Up @@ -314,7 +310,7 @@ export default function EditMemberForm({
}))}
value={selectedGroups}
onChange={(value) => {
console.log("creatable value", value);
// console.log("creatable value", value);

Choose a reason for hiding this comment

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

delete the comment

Choose a reason for hiding this comment

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

Suggested change
// console.log("creatable value", value);

setSelectedGroups(value);
}}
/>
Expand Down