Skip to content

Commit

Permalink
Working plugin without needing to log into Discourse
Browse files Browse the repository at this point in the history
  • Loading branch information
jericson committed Mar 28, 2024
1 parent da99213 commit f94462e
Show file tree
Hide file tree
Showing 11 changed files with 110 additions and 38 deletions.
9 changes: 8 additions & 1 deletion app/controllers/contact_controller.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
# coding: utf-8
class ContactController < ApplicationController

# skip_before_action :check_xhr, raise: false
skip_before_action :check_xhr,
#:verify_authenticity_token,
#:redirect_to_login_if_required,
raise: false


def index
Rails.logger.info '🚂 Called the `NotebookController#index` method.'
Rails.logger.info '🚂 Called the `ContactController#index` method.'
end
end
22 changes: 18 additions & 4 deletions app/controllers/contacts_controller.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
class ContactsController < ApplicationController

skip_before_action :check_xhr,
:verify_authenticity_token,
:redirect_to_login_if_required,
raise: false


def index
Rails.logger.info 'Called ContactsController#index'
contacts = ContactStore.get_contacts()
Expand All @@ -21,15 +28,22 @@ def update

ContactStore.add_contact(contact_id, contact)

@date=Time.now.strftime('%a, %-d %b %Y %H:%M:%S %z')
@date=Time.now.strftime('%a, %-d %b %Y %H:%M:%S')

@mail="
Date: #{@date}
From: #{contact['email']}
From: #{contact['email'] || "unknown@example.com"}
To: jons-biography@beta.buildcivitas.com
Subject: #{contact['name']} - #{@date}
Subject: Contact from #{contact['name']} - #{@date}
Name: #{contact['name']}
Phone: #{contact['phone']}
Email: #{contact['email']}
Message:
#{contact['message']}"
> #{contact['message']}"


Mail.new(@mail).message_id.presence
Expand Down
25 changes: 25 additions & 0 deletions assets/javascripts/discourse/components/contact-form.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<form {{ action 'createContact' name email phone message on='submit' }}>
<label>
{{ i18n 'contact.create_contact.text_name_label' }}
{{ input required="required" class="form-control" type="text" value=name }}
</label>

<label>
{{ i18n 'contact.create_contact.text_email_label' }}
{{ input required=true type="text" value=email }}
</label>

<label>
{{ i18n 'contact.create_contact.text_phone_label' }}
{{ input type="text" value=phone }}
</label>

<label>
{{ i18n 'contact.create_contact.text_message_label' }}
{{ textarea value=message }}
</label>

<button type='submit' class='btn btn-primary'>
{{ i18n 'contact.create_contact.submit_label' }}
</button>
</form>
47 changes: 47 additions & 0 deletions assets/javascripts/discourse/components/contact-form.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import Component from '@ember/component';
import Controller from '@ember/controller';

Check failure on line 2 in assets/javascripts/discourse/components/contact-form.js

View workflow job for this annotation

GitHub Actions / ci / linting

'Controller' is defined but never used

export default Component.extend({
init() {
this._super();
this.set('contacts', []);
this.fetchContacts();
},

fetchContacts() {
this.store.findAll('contact')
.then(result => {
for (const contact of result.content) {
this.contacts.pushObject(contact);
}
})
.catch(console.error);

Check failure on line 18 in assets/javascripts/discourse/components/contact-form.js

View workflow job for this annotation

GitHub Actions / ci / linting

Unexpected console statement
},

actions: {
createContact(name, email, phone, message) {

const contactRecord = this.store.createRecord('contact', {
id: Date.now(),
name: name,

Check failure on line 26 in assets/javascripts/discourse/components/contact-form.js

View workflow job for this annotation

GitHub Actions / ci / linting

Expected property shorthand
email: email,

Check failure on line 27 in assets/javascripts/discourse/components/contact-form.js

View workflow job for this annotation

GitHub Actions / ci / linting

Expected property shorthand
phone: phone,

Check failure on line 28 in assets/javascripts/discourse/components/contact-form.js

View workflow job for this annotation

GitHub Actions / ci / linting

Expected property shorthand
message: message

Check failure on line 29 in assets/javascripts/discourse/components/contact-form.js

View workflow job for this annotation

GitHub Actions / ci / linting

Expected property shorthand
});

contactRecord.save()
.then(result => {
this.contacts.pushObject(result.target);
})
.catch(console.error);

Check failure on line 36 in assets/javascripts/discourse/components/contact-form.js

View workflow job for this annotation

GitHub Actions / ci / linting

Unexpected console statement
},

deleteContact(contact) {
this.store.destroyRecord('contact', contact)
.then(() => {
this.contacts.removeObject(contact);
})
.catch(console.error);

Check failure on line 44 in assets/javascripts/discourse/components/contact-form.js

View workflow job for this annotation

GitHub Actions / ci / linting

Unexpected console statement
}
}
})

Check failure on line 47 in assets/javascripts/discourse/components/contact-form.js

View workflow job for this annotation

GitHub Actions / ci / linting

Missing semicolon
1 change: 1 addition & 0 deletions assets/javascripts/discourse/components/hello.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<h1>Hello</h1>
Original file line number Diff line number Diff line change
@@ -1,30 +1,6 @@
{{#if (includes @outletArgs.model.tags 'contact')}}

<ContactForm />

Check failure on line 4 in assets/javascripts/discourse/connectors/topic-above-posts/contact.hbs

View workflow job for this annotation

GitHub Actions / ci / linting

More than 1 blank line not allowed.
<form {{ action 'createContact' name email phone message on='submit' }}>
<label>
{{ i18n 'contact.create_contact.text_name_label' }}:

</label>
{{ input type="text" value=name name='name' }}

<label>
{{ i18n 'contact.create_contact.text_email_label' }}
</label>
{{ input type="text" value=email name='email' }}

<label>
{{ i18n 'contact.create_contact.text_phone_label' }}
</label>
{{ input type="text" value=phone }}

<label>
{{ i18n 'contact.create_contact.text_message_label' }}
</label>
{{ textarea value=message }}

<button type='submit' class='btn btn-primary'>
{{ i18n 'contact.create_contact.submit_label' }}
</button>
</form>
{{/if}}
1 change: 1 addition & 0 deletions assets/javascripts/discourse/controllers/contact.js.es6
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import Component from '@ember/component';
import Controller from '@ember/controller';

export default Controller.extend({

init() {
this._super();
this.set('contacts', []);
Expand Down
6 changes: 0 additions & 6 deletions assets/javascripts/discourse/helpers/sample.js

This file was deleted.

4 changes: 2 additions & 2 deletions assets/javascripts/discourse/routes/contact.js.es6
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import DiscourseRoute from 'discourse/routes/discourse';

export default DiscourseRoute.extend({
renderTemplate() {
// Renders the template `../templates/contact.hbs` Blagh
this.render('contact');
// Renders the template `../templates/contact.hbs`
this.render('ContactForm');
}
});
2 changes: 2 additions & 0 deletions assets/javascripts/discourse/templates/contact.hbs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<h1>{{ i18n 'contact.title' }}</h1>

<Hello />

<form {{ action 'createContact' name email phone message on='submit' }}>
<label>
{{ i18n 'contact.create_contact.text_name_label' }}:
Expand Down
5 changes: 5 additions & 0 deletions assets/stylesheets/contact.scss
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,8 @@ textarea {
min-width: 500px;
resize: vertical;
}

/* https://stackoverflow.com/questions/6046110/styling-form-with-label-above-inputs/6046196#6046196 */
input, input {
display: block;
}

0 comments on commit f94462e

Please sign in to comment.