halogen-mailing-list
A component written in purescript-halogen. The main motivation for publishing this repo is because it answers some questions about halogen that I was unsure about when starting out such as
- How to build a simple halogen component
- How to use asynchronous effects with halogen such as Ajax calls
- How to Decode/Encode json using Purescript
- How to interact with the DOM (using RefLabel)
- How to use environment variables depending on environment
Usage
Run the build script
./build.sh
Note that if the env.js file is not present in your root directory this will fail. See information about the env.js file below.
The types being used to send to and receive mailing lists are stored in src/Model.purs. To retrieve and select a mailing list to send a message to you will have to implement some sort of REST endpoint.
The endpoint which receives mailing lists has to return a list of objects identical to the MailingList type
[{ id: 1, name: "TestList", active: true }]
The endpoint that handles sending messages should receive an object identical to the Message type
{ fromListAddress: "test@example.com" -- From address
, listSubject: "Some subject" -- Email subject
, listContent: "Some content" -- Email content
}
If you just want to test this out without rewriting all the endpoints you can fetch and run mail-service
On using environment variables
The use of env variables is not so straight forward when bundling with pulp/purescript. This is because Purescript's process env is not resolved to process.env when bundling so when using a tool such as envify it cannot locate any env variables being set. To get around this I use a env.js file at the root of the project. Note that this file is purposely being ignored by the repo. You will have to add your own env.js file like so
require('./dist/build.js') -- This file is created by ./build.sh
// env variables go here
process.env.FOO = 'bar'
process.env.BAR = 'foo'
Required env variables
The following env variables are required
MAILING_LISTS_URL
URL which specifies the endpoint for retrieving all mailing lists
MAILING_LISTS_POST_URL
URL which specifies the POST endpoint for sending out messages to a specific mailing list
MAILING_LISTS_FROM_ADDRESS
The address to send from
If any of these variables are missing the component will simply output a log message stating which variables are missing
Testing
On a successfull run with ./build.sh you can test the component by opening the test.html file located at the root of this project.