Skip to content

Commit

Permalink
remembering search settings in cookies
Browse files Browse the repository at this point in the history
  • Loading branch information
ndepaola committed Jun 21, 2021
1 parent 4b55fb4 commit 37d48dc
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 11 deletions.
7 changes: 5 additions & 2 deletions MPCAutofill/cardpicker/management/commands/import_sources.py
Expand Up @@ -47,5 +47,8 @@ class Command(BaseCommand):

def handle(self, *args, **kwargs):
sources = read_sources_csv()
sync_sources(sources)
print("All sources synchronised from CSV to database.")
if sources:
sync_sources(sources)
print("All sources synchronised from CSV to database.")
else:
print("No sources synchronised to database because none were found.")
75 changes: 70 additions & 5 deletions MPCAutofill/cardpicker/static/js/index.js
Expand Up @@ -20,6 +20,71 @@ document.getElementById("id_body").onload = function () {
if (Cookies.get('ga_disabled') === undefined) {
cookie_toast.toast('show');
}

// save search settings to cookie when closing the modal
$('#selectDrivesModal').on('hidden.bs.modal', save_search_settings);
load_search_settings();
}

function save_search_settings() {
let settings = new Object;
// settings["drives"] = new Object;
settings["drives"] = []

// save search mode settings
settings["fuzzy_search"] = "off";
if (document.getElementById("searchtype").checked) {
settings["fuzzy_search"] = "on";
}

// save drive order and enabled/disabled status
let drive_elements = document.getElementsByClassName("drivesource");
for (let i = 0; i < drive_elements.length; i++) {
let drive_enabled = "off";
if (drive_elements[i].checked) {
drive_enabled = "on";
}
settings["drives"].push([drive_elements[i].id, drive_enabled])
}

Cookies.set('search_settings', JSON.stringify(settings));
}

function load_search_settings() {
let settings = Cookies.get('search_settings');
if (settings !== undefined) {
settings = JSON.parse(settings);

let drives = settings["drives"];
let fuzzy_search = settings["fuzzy_search"];

// maintain a set of all drives loaded into the page for making sure any new drives get inserted at the bottom
let all_drive_elems = document.getElementsByClassName("drivesource");
let all_drives = new Set();
for (let i=all_drive_elems.length-1; i>=0; i--) {
all_drives.add(all_drive_elems[i].id);
}

// reorder the drive table elements according to the cookie by inserting them all after the first one
// in the cookie (in reverse order)
$("#" + drives[0][0]).bootstrapToggle(drives[0][1]);
let first_drive_row = $("#" + drives[0][0] + "-row");
all_drives.delete(drives[0][0]);
for (let i=drives.length-1; i>0; i--) {
$("#" + drives[i][0]).bootstrapToggle(drives[i][1]);
$("#" + drives[i][0] + "-row").insertAfter(first_drive_row);
all_drives.delete(drives[i][0]);
}

// any drives left in all_drives at this point were added to the site between now and when the user's
// search settings cookie was last saved
// stick these users onto the end - note that the drives were inserted into the set in reverse order,
// meaning that these will be insertAfter'd in reverse order, which orders the elements correctly
let last_drive_row = $("#" + drives[drives.length-1][0] + "-row");
all_drives.forEach(drive => $("#" + drive + "-row").insertAfter(last_drive_row));

$("#searchtype").bootstrapToggle(fuzzy_search);
}
}

function cookie_toast_shown() {
Expand All @@ -46,12 +111,12 @@ function cookie_toast_opt_out() {

function get_drive_order() {
// get checkbox elements from dom, in order
let driveElements = document.getElementsByClassName("drivesource");
let drives = []
let drive_elements = document.getElementsByClassName("drivesource");
let drives = [];
// for each drive, if it's enabled, add its id to the output list
for (let i = 0; i < driveElements.length; i++) {
if (driveElements[i].checked) {
drives.push(driveElements[i].id)
for (let i = 0; i < drive_elements.length; i++) {
if (drive_elements[i].checked) {
drives.push(drive_elements[i].id)
}
}
// convert to string when outputting
Expand Down
9 changes: 5 additions & 4 deletions MPCAutofill/cardpicker/templates/cardpicker/index.html
Expand Up @@ -3,7 +3,7 @@

{% block content %}

<script src="{% static 'js/index.js' %}?27"></script>
<script src="{% static 'js/index.js' %}?28"></script>
<link href="https://cdn.jsdelivr.net/gh/gitbrent/bootstrap4-toggle@3.6.1/css/bootstrap4-toggle.min.css" rel="stylesheet">
<script src="https://cdn.jsdelivr.net/gh/gitbrent/bootstrap4-toggle@3.6.1/js/bootstrap4-toggle.min.js"></script>

Expand All @@ -20,7 +20,7 @@
</div>
<div class="toast-body">
<p>
MPC Autofill uses cookies for collecting analytics
MPC Autofill uses cookies for remembering your search settings, and for collecting analytics
data to help improve the site. Your data is never shared with anyone. Would you like to opt
out of analytics cookies?
</p>
Expand Down Expand Up @@ -159,10 +159,11 @@ <h5 class="modal-title" id="selectDrivesLabel">Search Settings</h5>
</thead>
<tbody>
{% for source in sources %}
<tr style="cursor: grab">
<tr id="{{ source.id }}-row" style="cursor: grab">
<td style="vertical-align: middle">
<div class="active text-center">
<input type="checkbox" class="drivesource" checked data-toggle="toggle" id="{{ source.id }}" data-size="sm">
<input type="checkbox" class="drivesource" checked
data-toggle="toggle" id="{{ source.id }}" data-size="sm">
</div>
</td>
<td style="vertical-align: middle"><a target="_blank" href="{{ source.reddit }}">{{ source.username }}</a></td>
Expand Down
4 changes: 4 additions & 0 deletions MPCAutofill/cardpicker/templates/cardpicker/legal.html
Expand Up @@ -33,6 +33,10 @@ <h2>Privacy Policy</h2>
option to opt-out of having their data collected by Google Analytics.
</p>
<p>
We also use cookies to remember your search settings &#8212 which drives you have enabled or disabled, their
ordering, and the search mode you selected (precise or fuzzy) &#8212 which is considered core site functionality.
</p>
<p>
MPC Autofill will never share information collected by Google Analytics with third parties.
</p>
<p>
Expand Down

0 comments on commit 37d48dc

Please sign in to comment.