Skip to content

Commit

Permalink
fix: fixed invite codes.
Browse files Browse the repository at this point in the history
fix: fixed button sharing of the join code.
  • Loading branch information
JasonLovesDoggo committed Dec 21, 2022
1 parent c1eb215 commit 8266ab1
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 31 deletions.
13 changes: 9 additions & 4 deletions scavenger2022/core/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
from django.db import models


def generate_invite_code():
return secrets.token_hex(4)


class User(AbstractUser):
metropolis_id = models.IntegerField()
refresh_token = models.CharField(max_length=128)
Expand Down Expand Up @@ -75,6 +79,11 @@ def hint(self, team: "Team"):
r.random()
return Hint.objects.get(id=r.choice(pks)["pk"])

def save(self, *args, **kwargs):
if self._state.adding: # only generate key on creation not on update
Invite.objects.create(team=self, code=generate_invite_code()).save()
return super().save(*args, **kwargs)


class Hint(models.Model):
qr_code = models.ForeignKey(QrCode, related_name="hints", on_delete=models.CASCADE)
Expand Down Expand Up @@ -135,10 +144,6 @@ def __str__(self):
return str(self.name)


def generate_invite_code():
return secrets.token_hex(4)


class Invite(models.Model):
invites = models.IntegerField(default=0)
team = models.ForeignKey(Team, on_delete=models.CASCADE, related_name="invites")
Expand Down
14 changes: 8 additions & 6 deletions scavenger2022/core/static/core/qr.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
const btn = document.getElementById('share-btn')
const link = window.location.origin + btn.dataset.joinLink
if (navigator.share) {
console.log('share api avail')
btn.value = btn.dataset.shareText
}
btn.addEventListener('click', async (e) => {
await ((navigator.share) ? navigator.share(link) : await navigator.clipboard.writeText(link))
const shareData = {
title: 'MacLyonsDen Scavenger Hunt',
text: 'Scavenger hunt team invite code!',
url: link
};

btn.addEventListener('click', (e) => {
((navigator.share) ? navigator.share(shareData) : navigator.clipboard.writeText(link))
})

new QRCode(document.getElementById("qrcode"), {
Expand Down
46 changes: 26 additions & 20 deletions scavenger2022/core/templates/core/team_invite.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,40 @@
{% load qr %}

{% block title %}
{% translate "Invite Members" %}
{% translate "Invite Members" %}
{% endblock %}

{% block header %}
{% translate "invite members" %}
{% translate "invite members" %}
{% endblock %}

{% block head_end %}
<script src="{% static 'core/qrcode.min.js' %}"></script>
<script type="module" src="{% static 'core/qr.js' %}"></script>
<style>
.qrcode img {
background: #fff;
padding: calc(8px * 4); /* eyeballed to 4 modules */
}
</style>
<script src="{% static 'core/qrcode.min.js' %}"></script>
<script type="module" src="{% static 'core/qr.js' %}"></script>
<style>
.qrcode img {
background: #fff;
padding: calc(8px * 4); /* eyeballed to 4 modules */
}

.qr-uri {
border-radius: 0.3rem;
padding: 0.3rem 0.5rem
}
</style>
{% endblock %}

{% block body %}
{% for invite in request.user.team.invites.all %}
{% join_url invite.code as join_link %}
{% blocktranslate %}
you may either
<input type="button" value="copy the join link" id="share-btn" data-share-text="share the join link" data-join-link="{{ join_link }}" />,
invite via code (<code>{{ invite.code }}</code>),
or allow them to scan the QR code below:
{% endblocktranslate %}
<div class="qrcode" id="qrcode"></div>
{% endfor %}
{% for code in invites %}
{% join_url code as join_link %}
{% blocktranslate %}
you may either
<input type="button" class="qr-uri" value="copy the join link" id="share-btn"
data-share-text="click here to copy the join code" data-join-link="{{ join_link }}"/>,
share via invite code <code>{{ code }}</code>,
or allow them to scan the QR code below:
{% endblocktranslate %}
<div class="qrcode" id="qrcode"></div>
{% endfor %}
{% endblock %}

3 changes: 2 additions & 1 deletion scavenger2022/core/views/team.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,5 @@ def solo(q):
@require_http_methods(["GET"])
@team_required
def invite(q):
return render(q, "core/team_invite.html")
invites = Invite.objects.filter(team=q.user.team).values_list("code", flat=True)
return render(q, "core/team_invite.html", context=dict(invites=invites))

0 comments on commit 8266ab1

Please sign in to comment.