-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FEATURE][MER-1912] Add ability to enroll users
[FEATURE][MER-1912] Add ability to enroll users
- Loading branch information
Showing
21 changed files
with
912 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
const isValidEmail = (email: string): boolean => | ||
email.match(/^[\w]+@([\w-]+\.)+[\w-]{2,4}$/) !== null; | ||
|
||
const parseEmails = (content: string): string[] => | ||
content.match(/[\w]+@([\w-]+\.)+[\w-]{2,4}/g) || []; | ||
|
||
export const EmailList = { | ||
refresh() { | ||
const element = this.el as HTMLDivElement; | ||
const phxEvent = element.getAttribute('phx-event'); | ||
const input = element.querySelector('input') as HTMLInputElement; | ||
input.focus(); | ||
element.addEventListener('click', (event: any) => { | ||
if (event.target.matches(`#${element.id}`) && !input.getAttribute('focus')) { | ||
input.focus(); | ||
} | ||
}); | ||
input.addEventListener('input', () => { | ||
input.style.width = 'auto'; | ||
input.style.width = `${input.scrollWidth}px`; | ||
}); | ||
input.addEventListener('keypress', (event: KeyboardEvent) => { | ||
if (event.code === 'Enter' || event.code === 'Comma') { | ||
input.blur(); | ||
} | ||
}); | ||
input.addEventListener('blur', () => { | ||
const value = input.value.trim(); | ||
isValidEmail(value) | ||
? this.pushEvent(phxEvent, { | ||
value, | ||
}) | ||
: (input.value = ''); | ||
}); | ||
input.addEventListener('paste', (event: ClipboardEvent) => { | ||
event.preventDefault(); | ||
|
||
const clipboardData = event.clipboardData || (window as any).clipboardData; | ||
const pastedText = clipboardData?.getData('text/plain'); | ||
|
||
const emails = parseEmails(pastedText); | ||
|
||
if (emails.length) { | ||
this.pushEvent(phxEvent, { | ||
value: emails, | ||
}); | ||
} | ||
}); | ||
}, | ||
mounted() { | ||
this.refresh(); | ||
}, | ||
updated() { | ||
this.refresh(); | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -77,6 +77,48 @@ defmodule Oli.Accounts do | |
Repo.all(query) | ||
end | ||
|
||
@spec get_users_by_email(list(String.t())) :: list(User.t()) | ||
def get_users_by_email(email_list) do | ||
User | ||
|> where([u], u.independent_learner == true and u.email in ^email_list) | ||
|> select([u], %{ | ||
id: u.id, | ||
email: u.email | ||
}) | ||
|> Repo.all() | ||
end | ||
|
||
@doc """ | ||
Creates multiple invited users | ||
## Examples | ||
iex> bulk_invite_users(["[email protected]", "[email protected]"], %Author{id: 1}) | ||
[%User{id: 3}, %User{id: 4}] | ||
""" | ||
def bulk_invite_users(user_emails, %Author{} = inviter_user) do | ||
date = DateTime.utc_now() |> DateTime.truncate(:second) | ||
|
||
users = | ||
Enum.map( | ||
user_emails, | ||
fn email -> | ||
User | ||
|> struct() | ||
|> User.invite_changeset(inviter_user, %{ | ||
email: email | ||
}) | ||
|> Map.get(:changes) | ||
|> Map.delete(:invited_by_id) | ||
|> Map.merge(%{ | ||
state: %{}, | ||
inserted_at: date, | ||
updated_at: date | ||
}) | ||
end | ||
) | ||
|
||
Repo.insert_all(User, users, returning: [:id, :invitation_token, :email]) | ||
end | ||
|
||
def browse_authors( | ||
%Paging{limit: limit, offset: offset}, | ||
%Sorting{field: field, direction: direction}, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
defmodule OliWeb.Components.EmailList do | ||
use Phoenix.Component | ||
|
||
attr :id, :string, required: true | ||
attr :users_list, :list, required: true | ||
attr :on_update, :string, required: true | ||
attr :on_remove, :string, required: true | ||
|
||
attr :is_list_empty, :boolean, default: true | ||
attr :current_user, :string, default: "" | ||
|
||
def render(assigns) do | ||
assigns = assign(assigns, :is_list_empty, List.first(assigns.users_list) == nil) | ||
|
||
~H""" | ||
<div id={@id} class="flex flex-wrap rounded-md border border-gray-300 p-4 gap-2 cursor-text" phx-hook="EmailList" phx-event={@on_update}> | ||
<%= for user <- @users_list do %> | ||
<div class="rounded-md bg-gray-100 cursor-default p-2 shadow-md flex items-center gap-2 user-email max-h-80 scroll-y-overflow"> | ||
<p><%= user %></p> | ||
<button | ||
phx-click={@on_remove} | ||
phx-value-user={user} | ||
class="close" | ||
> | ||
<i class="fa-solid fa-xmark mt-0" /> | ||
</button> | ||
</div> | ||
<% end %> | ||
<input placeholder={if @is_list_empty, do: "[email protected]", else: nil} class="p-2 outline-none" value={@current_user} /> | ||
</div> | ||
""" | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.