import { api } from "../API/API";

export interface AddMemberInfo {
  userName: string;
  projectName: string;
}

/**
 * Tries to add a member to a project
 * @param {AddMemberInfo} props.membertoAdd - Contains user's name and project's name
 * @returns {Promise<void>}
 */
async function AddMember(props: { memberToAdd: AddMemberInfo }): Promise<void> {
  if (props.memberToAdd.userName === "") {
    alert("You must choose at least one user to add");
    return;
  }
  try {
    const response = await api.addUserToProject(
      props.memberToAdd,
      localStorage.getItem("accessToken") ?? "",
    );
    if (response.success) {
      alert(`[${props.memberToAdd.userName}] added`);
    } else {
      alert(`[${props.memberToAdd.userName}] not added`);
      console.error(response.message);
    }
  } catch (error) {
    alert(`[${props.memberToAdd.userName}] not added`);
    console.error("An error occurred during member add:", error);
  }
}

export default AddMember;
