Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Subscription feature #936

Merged
merged 21 commits into from
Oct 12, 2024
Merged
Show file tree
Hide file tree
Changes from 11 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
6 changes: 6 additions & 0 deletions app/components/header.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@
class="nav__element"
href={{this.STATUS_URL}}
>Status</a></li>
{{!-- {{#if @dev}} --}}
tejaskh3 marked this conversation as resolved.
Show resolved Hide resolved
<li>
{{! TODO - remove query for dev when it goes to production }}
<LinkTo @route="subscribe" class="nav__element">Subscribe</LinkTo>
</li>
{{!-- {{/if}} --}}
{{#if @dev}}
<li>
{{! TODO - remove query for dev when it goes to production }}
Expand Down
1 change: 1 addition & 0 deletions app/constants/regex.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const PHONE_REGEX = /^\+91\d{10}$/;
1 change: 1 addition & 0 deletions app/constants/urls.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,4 @@ export const SOCIALS = {
};

export const ANKUSH_TWITTER = 'https://twitter.com/ankushdharkar';
export const RDS_TWITTER = 'https://x.com/realdevsquad';
104 changes: 104 additions & 0 deletions app/controllers/subscribe.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
import Controller from '@ember/controller';
import { tracked } from '@glimmer/tracking';
import { action } from '@ember/object';
import { inject as service } from '@ember/service';
import { RDS_TWITTER, APPS } from '../constants/urls';
import { TOAST_OPTIONS } from '../constants/toast-options';
import { PHONE_REGEX } from '../constants/regex';
export default class SubscribeController extends Controller {
@service login;
@tracked isFormOpen = false;
@tracked email = '';
@tracked phone = '';
@tracked userData = null;
@tracked isLoading = false;
@tracked showSubscriptionModal = false;

RDS_TWITTER = RDS_TWITTER;

constructor() {
super(...arguments);
this.userData = this.login.userData;
}

get isLoggedIn() {
return this.login.isLoggedIn;
}

get isSubscribed() {
return this.login.userData.isSubscribed;
}

get isSubmitDisabled() {
return !this.email || (this.phone && !PHONE_REGEX.test(this.phone));
}

@action
toggleFormModal() {
this.isFormOpen = !this.isFormOpen;
}

@action
toggleSubscriptionModal() {
this.showSubscriptionModal = !this.showSubscriptionModal;
if (!this.showSubscriptionModal) {
window.location.reload();
}
}
tejaskh3 marked this conversation as resolved.
Show resolved Hide resolved

@action
updateEmail(event) {
this.email = event.target.value;
}

@action
updatePhone(event) {
const input = event.target.value;
this.phone = input.replace(/[^\d+]/g, '');
tejaskh3 marked this conversation as resolved.
Show resolved Hide resolved
}

@action
async handleSubmit(event) {
event.preventDefault();
if (!this.isSubmitDisabled) {
this.isLoading = true;
try {
const url = `${APPS.API_BACKEND}/subscription?dev=true`;
const response = await fetch(url, {
method: 'POST',
body: JSON.stringify({
email: this.email,
phoneNumber: this.phone,
}),
headers: {
'Content-Type': 'application/json',
},
credentials: 'include',
});
if (!response.ok) {
this.toast.error(
'Something went wrong. Please try again.',
samarpan1738 marked this conversation as resolved.
Show resolved Hide resolved
'',
TOAST_OPTIONS,
);
} else {
this.login.userData.isSubscribed = true;
this.toggleSubscriptionModal();
this.toast.info('🎉 Thank you for subscribing!', '', TOAST_OPTIONS);
samarpan1738 marked this conversation as resolved.
Show resolved Hide resolved
}
} catch (error) {
console.log(error);
this.toast.error(
`Something went wrong. ${error.message}`,
'',
TOAST_OPTIONS,
);
} finally {
this.isLoading = false;
}
this.toggleFormModal();
this.email = '';
this.phone = '';
}
}
}
3 changes: 3 additions & 0 deletions app/models/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,7 @@ export default class UserModel extends Model {
@attr profileURL;
@attr profileStatus;
@attr website;
@attr isSubscribed;
@attr phoneNumber;
@attr email;
}
1 change: 1 addition & 0 deletions app/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ Router.map(function () {
this.route('goto');
this.route('events');
this.route('debug');
this.route('subscribe');
});
6 changes: 6 additions & 0 deletions app/routes/subscribe.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import Route from '@ember/routing/route';
import { inject as service } from '@ember/service';

export default class SubscribeRoute extends Route {
@service login;
tejaskh3 marked this conversation as resolved.
Show resolved Hide resolved
}
2 changes: 2 additions & 0 deletions app/styles/app.css
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
@import url("tooltip.module.css");
@import url("debug.module.css");
@import url("unauthenticated.module.css");
@import url("subscribe.module.css");
@import url("phone-input.module.css");

* {
margin: 0;
Expand Down
6 changes: 5 additions & 1 deletion app/styles/base-modal.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@
}

.base-modal--show {
display: block;
display: flex;
align-items: center;
justify-content: center;
background-color: #000000b2;
backdrop-filter: blur(5px);
}

.base-modal--hide {
Expand Down
23 changes: 23 additions & 0 deletions app/styles/phone-input.module.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
.phone-input {
display: flex;
align-items: center;
margin-bottom: 10px;
}

.country-code-select {
margin-right: 10px;
}

.phone-number-input {
flex-grow: 1;
}

.phone-input select,
.phone-input input[type="tel"] {
width: 100%;
padding: 5px;
}

button {
padding: 5px 10px;
}
Loading
Loading