10 Commits

Author SHA1 Message Date
58a33b9487 run mix format
Some checks reported errors
continuous-integration/drone/push Build encountered an error
2022-11-06 19:13:20 -05:00
322456ad2f Merge branch 'dev' into weblate 2022-11-06 19:10:21 -05:00
Ed
c28a1e74f9 Translated using Weblate (Spanish)
All checks were successful
continuous-integration/drone/push Build is passing
Currently translated at 73.9% (34 of 46 strings)

Translation: cannery/prompts
Translate-URL: https://weblate.bubbletea.dev/projects/cannery/prompts/es/
2022-06-06 19:05:50 +00:00
ef15e97fd8 Translated using Weblate (Spanish)
All checks were successful
continuous-integration/drone/push Build is passing
Currently translated at 20.0% (7 of 35 strings)

Translation: cannery/actions
Translate-URL: https://weblate.bubbletea.dev/projects/cannery/actions/es/
2022-05-22 22:52:40 +00:00
Ed
cab70ff12a Translated using Weblate (Spanish)
Currently translated at 50.0% (23 of 46 strings)

Translation: cannery/prompts
Translate-URL: https://weblate.bubbletea.dev/projects/cannery/prompts/es/
2022-05-22 22:52:40 +00:00
Ed
94beb78485 Translated using Weblate (Spanish)
Currently translated at 46.6% (7 of 15 strings)

Translation: cannery/emails
Translate-URL: https://weblate.bubbletea.dev/projects/cannery/emails/es/
2022-05-22 22:52:40 +00:00
4710bcfca9 Translated using Weblate (Spanish)
Currently translated at 46.6% (7 of 15 strings)

Translation: cannery/emails
Translate-URL: https://weblate.bubbletea.dev/projects/cannery/emails/es/
2022-05-22 22:52:39 +00:00
Ed
23c47d4c25 Translated using Weblate (Spanish)
Currently translated at 16.6% (5 of 30 strings)

Translation: cannery/errors
Translate-URL: https://weblate.bubbletea.dev/projects/cannery/errors/es/
2022-05-22 22:52:39 +00:00
8678d27b97 Translated using Weblate (Spanish)
Currently translated at 16.6% (5 of 30 strings)

Translation: cannery/errors
Translate-URL: https://weblate.bubbletea.dev/projects/cannery/errors/es/
2022-05-22 22:52:39 +00:00
Ed
aa516a9919 Translated using Weblate (Spanish)
Currently translated at 0.6% (1 of 157 strings)

Translation: cannery/default
Translate-URL: https://weblate.bubbletea.dev/projects/cannery/default/es/
2022-05-22 22:52:39 +00:00
171 changed files with 15327 additions and 18769 deletions

View File

@ -13,27 +13,23 @@ steps:
mount: mount:
- _build - _build
- deps - deps
- .npm - assets/node_modules/
- .mix
- name: test - name: test
image: elixir:1.14.1-alpine image: elixir:1.13.4-alpine
environment: environment:
TEST_DATABASE_URL: ecto://postgres:postgres@database/cannery_test TEST_DATABASE_URL: ecto://postgres:postgres@database/cannery_test
HOST: testing.example.tld HOST: testing.example.tld
MIX_HOME: /drone/src/.mix
MIX_ARCHIVES: /drone/src/.mix/archives
MIX_ENV: test
commands: commands:
- apk add --no-cache build-base npm git - apk add --no-cache build-base npm git python3
- mix local.rebar --force --if-missing - mix local.rebar --force
- mix local.hex --force --if-missing - mix local.hex --force
- mix deps.get - mix deps.get
- npm set cache .npm - mix deps.compile
- npm --prefix ./assets ci --no-audit --prefer-offline - npm --prefix ./assets ci --progress=false --no-audit --loglevel=error
- npm run --prefix ./assets deploy - npm run --prefix ./assets deploy
- mix do phx.digest, gettext.extract - mix do phx.digest, gettext.extract
- mix test.all - mix test
- name: build and publish stable - name: build and publish stable
image: thegeeklab/drone-docker-buildx image: thegeeklab/drone-docker-buildx
@ -42,7 +38,7 @@ steps:
repo: shibaobun/cannery repo: shibaobun/cannery
purge: true purge: true
compress: true compress: true
platforms: linux/amd64,linux/arm/v7 platforms: linux/amd64,linux/arm64,linux/arm/v7
username: username:
from_secret: docker_username from_secret: docker_username
password: password:
@ -59,7 +55,7 @@ steps:
repo: shibaobun/cannery repo: shibaobun/cannery
purge: true purge: true
compress: true compress: true
platforms: linux/amd64,linux/arm/v7 platforms: linux/amd64,linux/arm64,linux/arm/v7
username: username:
from_secret: docker_username from_secret: docker_username
password: password:
@ -80,8 +76,7 @@ steps:
mount: mount:
- _build - _build
- deps - deps
- .npm - assets/node_modules/
- .mix
services: services:
- name: database - name: database

2
.gitignore vendored
View File

@ -25,7 +25,7 @@ cannery-*.tar
# If NPM crashes, it generates a log, let's ignore it too. # If NPM crashes, it generates a log, let's ignore it too.
npm-debug.log npm-debug.log
# The directory NPM downloads your dependencies sources to. # Ignore assets that are produced by build tools.
/assets/node_modules/ /assets/node_modules/
# Since we are building assets from assets/, # Since we are building assets from assets/,

View File

@ -1,3 +1,3 @@
elixir 1.14.1-otp-25 elixir 1.13.4-otp-24
erlang 25.1.2 erlang 24.2
nodejs 18.9.1 nodejs 16.13.2

View File

@ -1,72 +1,5 @@
# v0.8.3 # v0.5.5
- Improve some styles
- Improve server log
- Various minor improvements
# v0.8.2
- Fix bug with public registration
- Improve templates
- Improve invites, record usage
- Fix padding on more pages when using chrome
- Add oban metrics to server log and live dashboard
# v0.8.1
- Update dependencies
- Show topbar on form submit/page refresh
- Make loading/reconnection less intrusive
- Add QR code for invite link
# v0.8.0
- Add search to catalog, ammo, container, tag and range index pages
- Tweak urls for catalog, ammo, containers, tags and shot records
- Fix bug with shot group chart not drawing lines between days correctly
- Improve cards across app (make them line up with each other)
- Update translations and add spanish!!! (thank you Brea and Hannah!)
# v0.7.2
- Code improvements
# v0.7.1
- Fix table component alignment and styling
- Fix toggle button styling
- Miscellanous code improvements
- Improve container index table
- Fix bug with ammo not updating after deleting shot group
- Replace ammo "added on" with "purchased on"
- Miscellaneous wording improvements
- Update translations
# v0.7.0
- Add shading to table component
- Fix chart to sum by day
- Fix whitespace when copying invite url
- Make ammo type show page also display ammo groups as table
- Make container show page also display ammo groups as table
- Display CPR for ammo packs
- Add original count for ammo packs
- Add ammo pack CPR and original count to json export
# v0.6.0
- Update translations
- Display used-up date on used-up ammo
- Make ammo index page a bit more compact
- Make ammo index page filter used-up ammo
- Make ammo catalog page include ammo count
- Make ammo type show page a bit more compact
- Make ammo type show page include container names for each ammo
- Make ammo type show page filter used-up ammo
- Make container index page optionally display a table
- Make container show page a bit more compact
- Make container show page filter used-up ammo
- Forgot to add the logo as the favicon whoops - Forgot to add the logo as the favicon whoops
- Add graph to range page
- Add JSON export of data
- Add ammo cloning
- Add ammo type cloning
- Add container cloning
- Fix bug with moving ammo packs between containers
- Add button to set rounds left to 0 when creating a shot group
- Update project dependencies
# v0.5.4 # v0.5.4
- Rename "Ammo" tab to "Catalog", and "Manage" tab is now "Ammo" - Rename "Ammo" tab to "Catalog", and "Manage" tab is now "Ammo"

View File

@ -17,8 +17,8 @@ If you're multilingual, this project can use your translations! Visit
functions as short as possible while keeping variable names descriptive! For functions as short as possible while keeping variable names descriptive! For
instance, use inline `do:` blocks for short functions and make your aliases as instance, use inline `do:` blocks for short functions and make your aliases as
short as possible without introducing ambiguity. short as possible without introducing ambiguity.
- I.e. since there's only one `AmmoGroup` in the app, please alias - I.e. since there's only one `Changeset` in the app, please alias
`AmmoGroup.t()` instead of using `Cannery.Ammo.AmmoGroup.t()` `Changeset.t(Type.t())` instead of using `Ecto.Changeset.t(Long.Type.t())`
- Use pipelines when possible. If only calling a single method, a pipeline isn't - Use pipelines when possible. If only calling a single method, a pipeline isn't
strictly necessary but still encouraged for future modification. strictly necessary but still encouraged for future modification.
- Please add typespecs to your functions! Even your private functions may be - Please add typespecs to your functions! Even your private functions may be
@ -63,8 +63,7 @@ And as always, thank you!
[`phx_gen_auth`](https://hexdocs.pm/phx_gen_auth/). [`phx_gen_auth`](https://hexdocs.pm/phx_gen_auth/).
- `Dockerfile` and example `docker-compose.yml` - `Dockerfile` and example `docker-compose.yml`
- Automatic migrations in `MIX_ENV=prod` or Docker image - Automatic migrations in `MIX_ENV=prod` or Docker image
- JS linting with [standard.js](https://standardjs.com), HEEx linting with - JS linting with [standard.js](https://standardjs.com)
[heex_formatter](https://github.com/feliperenan/heex_formatter)
## Docs ## Docs
@ -110,7 +109,7 @@ In `dev` mode, Cannery will listen for these environment variables at runtime.
- `POOL_SIZE`: Controls the pool size to use with PostgreSQL. Defaults to `10`. - `POOL_SIZE`: Controls the pool size to use with PostgreSQL. Defaults to `10`.
- `REGISTRATION`: Controls if user sign-up should be invite only or set to public. Set to `public` to enable public registration. Defaults to `invite`. - `REGISTRATION`: Controls if user sign-up should be invite only or set to public. Set to `public` to enable public registration. Defaults to `invite`.
- `LOCALE`: Sets a custom default locale. Defaults to `en_US`. - `LOCALE`: Sets a custom default locale. Defaults to `en_US`.
- Available options: `en_US`, `de`, `fr`, and `es` - Available options: `en_US`, `de`, and `fr`
## `MIX_ENV=test` ## `MIX_ENV=test`
@ -145,4 +144,3 @@ Thank you so much for your contributions!
- kaia (https://shitposter.club/users/kaia) - kaia (https://shitposter.club/users/kaia)
- duponin (https://udongein.xyz/users/duponin) - duponin (https://udongein.xyz/users/duponin)
- kalli (https://twitter.com/t0kkuro) - kalli (https://twitter.com/t0kkuro)
- brea (https://refusal.biz/users/tarperfume)

View File

@ -1,4 +1,4 @@
FROM elixir:1.14.1-alpine AS build FROM elixir:1.13.4-alpine AS build
# install build dependencies # install build dependencies
RUN apk add --no-cache build-base npm git python3 RUN apk add --no-cache build-base npm git python3
@ -37,7 +37,7 @@ RUN mix do compile, release
FROM alpine:latest AS app FROM alpine:latest AS app
RUN apk upgrade --no-cache && \ RUN apk upgrade --no-cache && \
apk add --no-cache bash openssl libssl1.1 libcrypto1.1 libgcc libstdc++ ncurses-libs apk add --no-cache bash openssl libgcc libstdc++ ncurses-libs
WORKDIR /app WORKDIR /app

View File

@ -64,7 +64,7 @@ You can use the following environment variables to configure Cannery in
- `REGISTRATION`: Controls if user sign-up should be invite only or set to - `REGISTRATION`: Controls if user sign-up should be invite only or set to
public. Set to `public` to enable public registration. Defaults to `invite`. public. Set to `public` to enable public registration. Defaults to `invite`.
- `LOCALE`: Sets a custom default locale. Defaults to `en_US` - `LOCALE`: Sets a custom default locale. Defaults to `en_US`
- Available options: `en_US`, `de`, `fr` and `es` - Available options: `en_US`, `de`, and `fr`
- `SMTP_HOST`: The url for your SMTP email provider. Must be set - `SMTP_HOST`: The url for your SMTP email provider. Must be set
- `SMTP_PORT`: The port for your SMTP relay. Defaults to `587`. - `SMTP_PORT`: The port for your SMTP relay. Defaults to `587`.
- `SMTP_USERNAME`: The username for your SMTP relay. Must be set! - `SMTP_USERNAME`: The username for your SMTP relay. Must be set!

View File

@ -25,13 +25,12 @@ $fa-font-path: "@fortawesome/fontawesome-free/webfonts";
100% { scale: 1.0; opacity: 1; } 100% { scale: 1.0; opacity: 1; }
} }
// disconnect toast .phx-connected > #disconnect, #loading {
.phx-connected > #disconnect {
opacity: 0 !important; opacity: 0 !important;
pointer-events: none; pointer-events: none;
} }
.phx-error > #disconnect { .phx-loading:not(.phx-error) > #loading, .phx-error > #disconnect {
opacity: 0.95 !important; opacity: 0.95 !important;
} }

View File

@ -25,6 +25,7 @@
} }
.btn { .btn {
@apply inline-block break-words min-w-4;
@apply focus:outline-none px-4 py-2 rounded-lg; @apply focus:outline-none px-4 py-2 rounded-lg;
@apply shadow-sm focus:shadow-lg; @apply shadow-sm focus:shadow-lg;
@apply transition-all duration-300 ease-in-out; @apply transition-all duration-300 ease-in-out;
@ -51,6 +52,7 @@
} }
.link { .link {
@apply inline-block break-all min-w-4;
@apply hover:underline; @apply hover:underline;
@apply transition-colors duration-500 ease-in-out; @apply transition-colors duration-500 ease-in-out;
} }

View File

@ -24,9 +24,8 @@ import 'phoenix_html'
// Establish Phoenix Socket and LiveView configuration. // Establish Phoenix Socket and LiveView configuration.
import { Socket } from 'phoenix' import { Socket } from 'phoenix'
import { LiveSocket } from 'phoenix_live_view' import { LiveSocket } from 'phoenix_live_view'
import topbar from 'topbar' import topbar from '../vendor/topbar'
import MaintainAttrs from './maintain_attrs' import MaintainAttrs from './maintain_attrs'
import ShotLogChart from './shot_log_chart'
import Alpine from 'alpinejs' import Alpine from 'alpinejs'
const csrfToken = document.querySelector("meta[name='csrf-token']").getAttribute('content') const csrfToken = document.querySelector("meta[name='csrf-token']").getAttribute('content')
@ -37,7 +36,7 @@ const liveSocket = new LiveSocket('/live', Socket, {
} }
}, },
params: { _csrf_token: csrfToken }, params: { _csrf_token: csrfToken },
hooks: { MaintainAttrs, ShotLogChart } hooks: { MaintainAttrs }
}) })
// alpine.js // alpine.js
@ -48,8 +47,6 @@ Alpine.start()
topbar.config({ barColors: { 0: '#29d' }, shadowColor: 'rgba(0, 0, 0, .3)' }) topbar.config({ barColors: { 0: '#29d' }, shadowColor: 'rgba(0, 0, 0, .3)' })
window.addEventListener('phx:page-loading-start', info => topbar.show()) window.addEventListener('phx:page-loading-start', info => topbar.show())
window.addEventListener('phx:page-loading-stop', info => topbar.hide()) window.addEventListener('phx:page-loading-stop', info => topbar.hide())
window.addEventListener('submit', info => topbar.show())
window.addEventListener('beforeunload', info => topbar.show())
// connect if there are any LiveViews on the page // connect if there are any LiveViews on the page
liveSocket.connect() liveSocket.connect()
@ -69,8 +66,3 @@ window.addEventListener('cannery:clipcopy', (event) => {
window.alert('Sorry, your browser does not support clipboard copy.') window.alert('Sorry, your browser does not support clipboard copy.')
} }
}) })
// Set input value to 0
window.addEventListener('cannery:set-zero', (event) => {
event.target.value = 0
})

View File

@ -1,106 +0,0 @@
import { Chart, Title, Tooltip, Legend, LineController, LineElement, PointElement, TimeScale, LinearScale } from 'chart.js'
import 'chartjs-adapter-date-fns'
Chart.register(Title, Tooltip, Legend, LineController, LineElement, PointElement, TimeScale, LinearScale)
export default {
initalizeChart (el) {
const data = JSON.parse(el.dataset.chartData)
this.el.chart = new Chart(el, {
type: 'line',
data: {
datasets: [{
label: el.dataset.label,
data: data.map(({ date, count, label }) => ({
label,
x: date,
y: count
})),
backgroundColor: `${el.dataset.color}77`,
borderColor: el.dataset.color,
fill: true,
borderWidth: 3,
pointBorderWidth: 1
}]
},
options: {
elements: {
point: {
radius: 9,
hoverRadius: 12
}
},
plugins: {
legend: {
position: 'bottom',
labels: {
padding: 20
}
},
tooltip: {
displayColors: false,
callbacks: {
title: (contexts) => contexts.map(({ raw: { x } }) => Intl.DateTimeFormat([], { timeZone: 'Etc/UTC', dateStyle: 'short' }).format(new Date(x))),
label: ({ raw: { label } }) => label
}
}
},
scales: {
y: {
beginAtZero: true,
stacked: true,
grace: '15%',
ticks: {
padding: 15
}
},
x: {
type: 'time',
time: {
unit: 'day'
}
}
},
transitions: {
show: {
animations: {
x: {
from: 0
}
}
},
hide: {
animations: {
x: {
to: 0
}
}
}
}
}
})
},
updateChart (el) {
const data = JSON.parse(el.dataset.chartData)
this.el.chart.data = {
datasets: [{
label: el.dataset.label,
data: data.map(({ date, count, label }) => ({
label,
x: date,
y: count
})),
backgroundColor: `${el.dataset.color}77`,
borderColor: el.dataset.color,
fill: true,
borderWidth: 3,
pointBorderWidth: 1
}]
}
this.el.chart.update()
},
mounted () { this.initalizeChart(this.el) },
updated () { this.updateChart(this.el) }
}

10811
assets/package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -2,10 +2,6 @@
"repository": {}, "repository": {},
"description": " ", "description": " ",
"license": "MIT", "license": "MIT",
"engines": {
"node": "v18.9.1",
"npm": "8.10.0"
},
"scripts": { "scripts": {
"deploy": "NODE_ENV=production webpack --mode production", "deploy": "NODE_ENV=production webpack --mode production",
"watch": "webpack --mode development --watch --watch-options-stdin", "watch": "webpack --mode development --watch --watch-options-stdin",
@ -15,9 +11,6 @@
"dependencies": { "dependencies": {
"@fortawesome/fontawesome-free": "^6.1.1", "@fortawesome/fontawesome-free": "^6.1.1",
"alpinejs": "^3.10.2", "alpinejs": "^3.10.2",
"chart.js": "^3.9.1",
"chartjs-adapter-date-fns": "^2.0.0",
"date-fns": "^2.29.3",
"phoenix": "file:../deps/phoenix", "phoenix": "file:../deps/phoenix",
"phoenix_html": "file:../deps/phoenix_html", "phoenix_html": "file:../deps/phoenix_html",
"phoenix_live_view": "file:../deps/phoenix_live_view", "phoenix_live_view": "file:../deps/phoenix_live_view",
@ -37,7 +30,6 @@
"postcss-import": "^14.1.0", "postcss-import": "^14.1.0",
"postcss-loader": "^6.2.1", "postcss-loader": "^6.2.1",
"postcss-preset-env": "^7.5.0", "postcss-preset-env": "^7.5.0",
"sass": "^1.56.0",
"sass-loader": "^12.6.0", "sass-loader": "^12.6.0",
"standard": "^17.0.0", "standard": "^17.0.0",
"tailwindcss": "^3.0.24", "tailwindcss": "^3.0.24",

157
assets/vendor/topbar.js vendored Normal file
View File

@ -0,0 +1,157 @@
/**
* @license MIT
* topbar 1.0.0, 2021-01-06
* https://buunguyen.github.io/topbar
* Copyright (c) 2021 Buu Nguyen
*/
(function (window, document) {
"use strict";
// https://gist.github.com/paulirish/1579671
(function () {
var lastTime = 0;
var vendors = ["ms", "moz", "webkit", "o"];
for (var x = 0; x < vendors.length && !window.requestAnimationFrame; ++x) {
window.requestAnimationFrame =
window[vendors[x] + "RequestAnimationFrame"];
window.cancelAnimationFrame =
window[vendors[x] + "CancelAnimationFrame"] ||
window[vendors[x] + "CancelRequestAnimationFrame"];
}
if (!window.requestAnimationFrame)
window.requestAnimationFrame = function (callback, element) {
var currTime = new Date().getTime();
var timeToCall = Math.max(0, 16 - (currTime - lastTime));
var id = window.setTimeout(function () {
callback(currTime + timeToCall);
}, timeToCall);
lastTime = currTime + timeToCall;
return id;
};
if (!window.cancelAnimationFrame)
window.cancelAnimationFrame = function (id) {
clearTimeout(id);
};
})();
var canvas,
progressTimerId,
fadeTimerId,
currentProgress,
showing,
addEvent = function (elem, type, handler) {
if (elem.addEventListener) elem.addEventListener(type, handler, false);
else if (elem.attachEvent) elem.attachEvent("on" + type, handler);
else elem["on" + type] = handler;
},
options = {
autoRun: true,
barThickness: 3,
barColors: {
0: "rgba(26, 188, 156, .9)",
".25": "rgba(52, 152, 219, .9)",
".50": "rgba(241, 196, 15, .9)",
".75": "rgba(230, 126, 34, .9)",
"1.0": "rgba(211, 84, 0, .9)",
},
shadowBlur: 10,
shadowColor: "rgba(0, 0, 0, .6)",
className: null,
},
repaint = function () {
canvas.width = window.innerWidth;
canvas.height = options.barThickness * 5; // need space for shadow
var ctx = canvas.getContext("2d");
ctx.shadowBlur = options.shadowBlur;
ctx.shadowColor = options.shadowColor;
var lineGradient = ctx.createLinearGradient(0, 0, canvas.width, 0);
for (var stop in options.barColors)
lineGradient.addColorStop(stop, options.barColors[stop]);
ctx.lineWidth = options.barThickness;
ctx.beginPath();
ctx.moveTo(0, options.barThickness / 2);
ctx.lineTo(
Math.ceil(currentProgress * canvas.width),
options.barThickness / 2
);
ctx.strokeStyle = lineGradient;
ctx.stroke();
},
createCanvas = function () {
canvas = document.createElement("canvas");
var style = canvas.style;
style.position = "fixed";
style.top = style.left = style.right = style.margin = style.padding = 0;
style.zIndex = 100001;
style.display = "none";
if (options.className) canvas.classList.add(options.className);
document.body.appendChild(canvas);
addEvent(window, "resize", repaint);
},
topbar = {
config: function (opts) {
for (var key in opts)
if (options.hasOwnProperty(key)) options[key] = opts[key];
},
show: function () {
if (showing) return;
showing = true;
if (fadeTimerId !== null) window.cancelAnimationFrame(fadeTimerId);
if (!canvas) createCanvas();
canvas.style.opacity = 1;
canvas.style.display = "block";
topbar.progress(0);
if (options.autoRun) {
(function loop() {
progressTimerId = window.requestAnimationFrame(loop);
topbar.progress(
"+" + 0.05 * Math.pow(1 - Math.sqrt(currentProgress), 2)
);
})();
}
},
progress: function (to) {
if (typeof to === "undefined") return currentProgress;
if (typeof to === "string") {
to =
(to.indexOf("+") >= 0 || to.indexOf("-") >= 0
? currentProgress
: 0) + parseFloat(to);
}
currentProgress = to > 1 ? 1 : to;
repaint();
return currentProgress;
},
hide: function () {
if (!showing) return;
showing = false;
if (progressTimerId != null) {
window.cancelAnimationFrame(progressTimerId);
progressTimerId = null;
}
(function loop() {
if (topbar.progress("+.1") >= 1) {
canvas.style.opacity -= 0.05;
if (canvas.style.opacity <= 0.05) {
canvas.style.display = "none";
fadeTimerId = null;
return;
}
}
fadeTimerId = window.requestAnimationFrame(loop);
})();
},
};
if (typeof module === "object" && typeof module.exports === "object") {
module.exports = topbar;
} else if (typeof define === "function" && define.amd) {
define(function () {
return topbar;
});
} else {
this.topbar = topbar;
}
}.call(this, window, document));

View File

@ -11,8 +11,6 @@ config :cannery,
ecto_repos: [Cannery.Repo], ecto_repos: [Cannery.Repo],
generators: [binary_id: true] generators: [binary_id: true]
config :cannery, Cannery.Accounts, registration: System.get_env("REGISTRATION", "invite")
# Configures the endpoint # Configures the endpoint
config :cannery, CanneryWeb.Endpoint, config :cannery, CanneryWeb.Endpoint,
url: [scheme: "https", host: System.get_env("HOST") || "localhost", port: "443"], url: [scheme: "https", host: System.get_env("HOST") || "localhost", port: "443"],
@ -20,7 +18,8 @@ config :cannery, CanneryWeb.Endpoint,
secret_key_base: "KH59P0iZixX5gP/u+zkxxG8vAAj6vgt0YqnwEB5JP5K+E567SsqkCz69uWShjE7I", secret_key_base: "KH59P0iZixX5gP/u+zkxxG8vAAj6vgt0YqnwEB5JP5K+E567SsqkCz69uWShjE7I",
render_errors: [view: CanneryWeb.ErrorView, accepts: ~w(html json), layout: false], render_errors: [view: CanneryWeb.ErrorView, accepts: ~w(html json), layout: false],
pubsub_server: Cannery.PubSub, pubsub_server: Cannery.PubSub,
live_view: [signing_salt: "zOLgd3lr"] live_view: [signing_salt: "zOLgd3lr"],
registration: System.get_env("REGISTRATION") || "invite"
config :cannery, Cannery.Application, automigrate: false config :cannery, Cannery.Application, automigrate: false

View File

@ -64,9 +64,8 @@ config :cannery, CanneryWeb.Endpoint,
] ]
] ]
config :logger, :console, # Do not include metadata nor timestamps in development logs
format: "[$level] $message $metadata\n\n", config :logger, :console, format: "[$level] $message\n"
metadata: [:data]
# Set a higher stacktrace during development. Avoid configuring such # Set a higher stacktrace during development. Avoid configuring such
# in production as building large stacktraces may be expensive. # in production as building large stacktraces may be expensive.

View File

@ -15,18 +15,17 @@ end
config :cannery, CanneryWeb.ViewHelpers, shibao_mode: System.get_env("SHIBAO_MODE") == "true" config :cannery, CanneryWeb.ViewHelpers, shibao_mode: System.get_env("SHIBAO_MODE") == "true"
# Set default locale # Set default locale
config :gettext, :default_locale, System.get_env("LOCALE", "en_US") config :gettext, :default_locale, System.get_env("LOCALE") || "en_US"
maybe_ipv6 = if System.get_env("ECTO_IPV6") == "true", do: [:inet6], else: [] maybe_ipv6 = if System.get_env("ECTO_IPV6") == "true", do: [:inet6], else: []
database_url = database_url =
if config_env() == :test do if config_env() == :test do
System.get_env( System.get_env("TEST_DATABASE_URL") ||
"TEST_DATABASE_URL",
"ecto://postgres:postgres@localhost/cannery_test#{System.get_env("MIX_TEST_PARTITION")}" "ecto://postgres:postgres@localhost/cannery_test#{System.get_env("MIX_TEST_PARTITION")}"
)
else else
System.get_env("DATABASE_URL", "ecto://postgres:postgres@cannery-db/cannery") System.get_env("DATABASE_URL") ||
"ecto://postgres:postgres@cannery-db/cannery"
end end
host = host =
@ -41,7 +40,7 @@ interface =
config :cannery, Cannery.Repo, config :cannery, Cannery.Repo,
# ssl: true, # ssl: true,
url: database_url, url: database_url,
pool_size: String.to_integer(System.get_env("POOL_SIZE", "10")), pool_size: String.to_integer(System.get_env("POOL_SIZE") || "10"),
socket_options: maybe_ipv6 socket_options: maybe_ipv6
config :cannery, CanneryWeb.Endpoint, config :cannery, CanneryWeb.Endpoint,
@ -50,13 +49,10 @@ config :cannery, CanneryWeb.Endpoint,
# See the documentation on https://hexdocs.pm/plug_cowboy/Plug.Cowboy.html # See the documentation on https://hexdocs.pm/plug_cowboy/Plug.Cowboy.html
# for details about using IPv6 vs IPv4 and loopback vs public addresses. # for details about using IPv6 vs IPv4 and loopback vs public addresses.
ip: interface, ip: interface,
port: String.to_integer(System.get_env("PORT", "4000")) port: String.to_integer(System.get_env("PORT") || "4000")
], ],
server: true server: true,
registration: System.get_env("REGISTRATION") || "invite"
if config_env() in [:dev, :prod] do
config :cannery, Cannery.Accounts, registration: System.get_env("REGISTRATION", "invite")
end
if config_env() == :prod do if config_env() == :prod do
# The secret key base is used to sign/encrypt cookies and other secrets. # The secret key base is used to sign/encrypt cookies and other secrets.
@ -80,12 +76,12 @@ if config_env() == :prod do
config :cannery, Cannery.Mailer, config :cannery, Cannery.Mailer,
adapter: Swoosh.Adapters.SMTP, adapter: Swoosh.Adapters.SMTP,
relay: System.get_env("SMTP_HOST") || raise("No SMTP_HOST set!"), relay: System.get_env("SMTP_HOST") || raise("No SMTP_HOST set!"),
port: System.get_env("SMTP_PORT", "587"), port: System.get_env("SMTP_PORT") || 587,
username: System.get_env("SMTP_USERNAME") || raise("No SMTP_USERNAME set!"), username: System.get_env("SMTP_USERNAME") || raise("No SMTP_USERNAME set!"),
password: System.get_env("SMTP_PASSWORD") || raise("No SMTP_PASSWORD set!"), password: System.get_env("SMTP_PASSWORD") || raise("No SMTP_PASSWORD set!"),
ssl: System.get_env("SMTP_SSL") == "true", ssl: System.get_env("SMTP_SSL") == "true",
email_from: System.get_env("EMAIL_FROM", "no-reply@#{System.get_env("HOST")}"), email_from: System.get_env("EMAIL_FROM") || "no-reply@#{System.get_env("HOST")}",
email_name: System.get_env("EMAIL_NAME", "Cannery") email_name: System.get_env("EMAIL_NAME") || "Cannery"
# ## Using releases # ## Using releases
# #

View File

@ -22,9 +22,6 @@ config :cannery, CanneryWeb.Endpoint,
# In test we don't send emails. # In test we don't send emails.
config :cannery, Cannery.Mailer, adapter: Swoosh.Adapters.Test config :cannery, Cannery.Mailer, adapter: Swoosh.Adapters.Test
# Don't require invites for signups
config :cannery, Cannery.Accounts, registration: "public"
# Print only warnings and errors during test # Print only warnings and errors during test
config :logger, level: :warn config :logger, level: :warn

View File

@ -5,7 +5,7 @@ defmodule Cannery.Accounts do
import Ecto.Query, warn: false import Ecto.Query, warn: false
alias Cannery.{Mailer, Repo} alias Cannery.{Mailer, Repo}
alias Cannery.Accounts.{Invite, Invites, User, UserToken} alias Cannery.Accounts.{User, UserToken}
alias Ecto.{Changeset, Multi} alias Ecto.{Changeset, Multi}
alias Oban.Job alias Oban.Job
@ -23,24 +23,22 @@ defmodule Cannery.Accounts do
nil nil
""" """
@spec get_user_by_email(email :: String.t()) :: User.t() | nil @spec get_user_by_email(String.t()) :: User.t() | nil
def get_user_by_email(email) when is_binary(email) do def get_user_by_email(email) when is_binary(email), do: Repo.get_by(User, email: email)
Repo.get_by(User, email: email)
end
@doc """ @doc """
Gets a user by email and password. Gets a user by email and password.
## Examples ## Examples
iex> get_user_by_email_and_password("foo@example.com", "valid_password") iex> get_user_by_email_and_password("foo@example.com", "correct_password")
%User{} %User{}
iex> get_user_by_email_and_password("foo@example.com", "invalid_password") iex> get_user_by_email_and_password("foo@example.com", "invalid_password")
nil nil
""" """
@spec get_user_by_email_and_password(email :: String.t(), password :: String.t()) :: @spec get_user_by_email_and_password(String.t(), String.t()) ::
User.t() | nil User.t() | nil
def get_user_by_email_and_password(email, password) def get_user_by_email_and_password(email, password)
when is_binary(email) and is_binary(password) do when is_binary(email) and is_binary(password) do
@ -55,30 +53,28 @@ defmodule Cannery.Accounts do
## Examples ## Examples
iex> get_user!(user_id) iex> get_user!(123)
user %User{}
iex> get_user!() iex> get_user!(456)
** (Ecto.NoResultsError) ** (Ecto.NoResultsError)
""" """
@spec get_user!(User.id()) :: User.t() @spec get_user!(User.t()) :: User.t()
def get_user!(id) do def get_user!(id), do: Repo.get!(User, id)
Repo.get!(User, id)
end
@doc """ @doc """
Returns all users grouped by role. Returns all users grouped by role.
## Examples ## Examples
iex> list_all_users_by_role(user1) iex> list_users_by_role(%User{id: 123, role: :admin})
%{admin: [%User{role: :admin}], user: [%User{role: :user}]} [admin: [%User{}], user: [%User{}, %User{}]]
""" """
@spec list_all_users_by_role(User.t()) :: %{User.role() => [User.t()]} @spec list_all_users_by_role(User.t()) :: %{String.t() => [User.t()]}
def list_all_users_by_role(%User{role: :admin}) do def list_all_users_by_role(%User{role: :admin}) do
Repo.all(from u in User, order_by: u.email) |> Enum.group_by(fn %{role: role} -> role end) Repo.all(from u in User, order_by: u.email) |> Enum.group_by(fn user -> user.role end)
end end
@doc """ @doc """
@ -86,12 +82,13 @@ defmodule Cannery.Accounts do
## Examples ## Examples
iex> list_users_by_role(:admin) iex> list_users_by_role(%User{id: 123, role: :admin})
[%User{role: :admin}] [%User{}]
""" """
@spec list_users_by_role(:admin) :: [User.t()] @spec list_users_by_role(:admin | :user) :: [User.t()]
def list_users_by_role(:admin = role) do def list_users_by_role(role) do
role = role |> to_string()
Repo.all(from u in User, where: u.role == ^role, order_by: u.email) Repo.all(from u in User, where: u.role == ^role, order_by: u.email)
end end
@ -102,38 +99,22 @@ defmodule Cannery.Accounts do
## Examples ## Examples
iex> register_user(%{email: "foo@example.com", password: "valid_password"}) iex> register_user(%{field: value})
{:ok, %User{email: "foo@example.com"}} {:ok, %User{}}
iex> register_user(%{email: "foo@example"}) iex> register_user(%{field: bad_value})
{:error, %Changeset{}} {:error, %Changeset{}}
""" """
@spec register_user(attrs :: map()) :: @spec register_user(map()) :: {:ok, User.t()} | {:error, Changeset.t(User.new_user())}
{:ok, User.t()} | {:error, :invalid_token | User.changeset()} def register_user(attrs) do
@spec register_user(attrs :: map(), Invite.token() | nil) :: # if no registered users, make first user an admin
{:ok, User.t()} | {:error, :invalid_token | User.changeset()} role =
def register_user(attrs, invite_token \\ nil) do if Repo.one!(from u in User, select: count(u.id), distinct: true) == 0,
Multi.new() do: "admin",
|> Multi.one(:users_count, from(u in User, select: count(u.id), distinct: true)) else: "user"
|> Multi.run(:use_invite, fn _changes_so_far, _repo ->
if allow_registration?() and invite_token |> is_nil() do %User{} |> User.registration_changeset(attrs |> Map.put("role", role)) |> Repo.insert()
{:ok, nil}
else
Invites.use_invite(invite_token)
end
end)
|> Multi.insert(:add_user, fn %{users_count: count, use_invite: invite} ->
# if no registered users, make first user an admin
role = if count == 0, do: :admin, else: :user
User.registration_changeset(attrs, invite) |> User.role_changeset(role)
end)
|> Repo.transaction()
|> case do
{:ok, %{add_user: user}} -> {:ok, user}
{:error, :use_invite, :invalid_token, _changes_so_far} -> {:error, :invalid_token}
{:error, :add_user, changeset, _changes_so_far} -> {:error, changeset}
end
end end
@doc """ @doc """
@ -141,18 +122,16 @@ defmodule Cannery.Accounts do
## Examples ## Examples
iex> change_user_registration() iex> change_user_registration(user)
%Changeset{} %Changeset{data: %User{}}
iex> change_user_registration(%{password: "hi"}
%Changeset{}
""" """
@spec change_user_registration() :: User.changeset() @spec change_user_registration(User.t() | User.new_user()) ::
@spec change_user_registration(attrs :: map()) :: User.changeset() Changeset.t(User.t() | User.new_user())
def change_user_registration(attrs \\ %{}) do @spec change_user_registration(User.t() | User.new_user(), map()) ::
User.registration_changeset(attrs, nil, hash_password: false) Changeset.t(User.t() | User.new_user())
end def change_user_registration(user, attrs \\ %{}),
do: User.registration_changeset(user, attrs, hash_password: false)
## Settings ## Settings
@ -161,29 +140,24 @@ defmodule Cannery.Accounts do
## Examples ## Examples
iex> change_user_email(%User{email: "foo@example.com"}) iex> change_user_email(user)
%Changeset{} %Changeset{data: %User{}}
""" """
@spec change_user_email(User.t()) :: User.changeset() @spec change_user_email(User.t(), map()) :: Changeset.t(User.t())
@spec change_user_email(User.t(), attrs :: map()) :: User.changeset() def change_user_email(user, attrs \\ %{}), do: User.email_changeset(user, attrs)
def change_user_email(user, attrs \\ %{}) do
User.email_changeset(user, attrs)
end
@doc """ @doc """
Returns an `%Changeset{}` for changing the user role. Returns an `%Changeset{}` for changing the user role.
## Examples ## Examples
iex> change_user_role(%User{}, :user) iex> change_user_role(user)
%Changeset{} %Changeset{data: %User{}}
""" """
@spec change_user_role(User.t(), User.role()) :: User.changeset() @spec change_user_role(User.t(), atom()) :: Changeset.t(User.t())
def change_user_role(user, role) do def change_user_role(user, role), do: User.role_changeset(user, role)
User.role_changeset(user, role)
end
@doc """ @doc """
Emulates that the email will change without actually changing Emulates that the email will change without actually changing
@ -191,15 +165,15 @@ defmodule Cannery.Accounts do
## Examples ## Examples
iex> apply_user_email(user, "valid_password", %{email: "new_email@account.com"}) iex> apply_user_email(user, "valid password", %{email: ...})
{:ok, %User{}} {:ok, %User{}}
iex> apply_user_email(user, "invalid password", %{email: "new_email@account"}) iex> apply_user_email(user, "invalid password", %{email: ...})
{:error, %Changeset{}} {:error, %Changeset{}}
""" """
@spec apply_user_email(User.t(), email :: String.t(), attrs :: map()) :: @spec apply_user_email(User.t(), String.t(), map()) ::
{:ok, User.t()} | {:error, User.changeset()} {:ok, User.t()} | {:error, Changeset.t(User.t())}
def apply_user_email(user, password, attrs) do def apply_user_email(user, password, attrs) do
user user
|> User.email_changeset(attrs) |> User.email_changeset(attrs)
@ -213,7 +187,7 @@ defmodule Cannery.Accounts do
If the token matches, the user email is updated and the token is deleted. If the token matches, the user email is updated and the token is deleted.
The confirmed_at date is also updated to the current time. The confirmed_at date is also updated to the current time.
""" """
@spec update_user_email(User.t(), token :: String.t()) :: :ok | :error @spec update_user_email(User.t(), String.t()) :: :ok | :error
def update_user_email(user, token) do def update_user_email(user, token) do
context = "change:#{user.email}" context = "change:#{user.email}"
@ -226,7 +200,7 @@ defmodule Cannery.Accounts do
end end
end end
@spec user_email_multi(User.t(), email :: String.t(), context :: String.t()) :: Multi.t() @spec user_email_multi(User.t(), String.t(), String.t()) :: Multi.t()
defp user_email_multi(user, email, context) do defp user_email_multi(user, email, context) do
changeset = user |> User.email_changeset(%{email: email}) |> User.confirm_changeset() changeset = user |> User.email_changeset(%{email: email}) |> User.confirm_changeset()
@ -240,12 +214,11 @@ defmodule Cannery.Accounts do
## Examples ## Examples
iex> deliver_update_email_instructions(user, "new_foo@example.com", fn _token -> "example url" end) iex> deliver_update_email_instructions(user, current_email, &Routes.user_update_email_url(conn, :edit, &1))
%Oban.Job{args: %{email: :update_email, user_id: ^user_id, attrs: %{url: "example url"}}} {:ok, %{to: ..., body: ...}}
""" """
@spec deliver_update_email_instructions(User.t(), current_email :: String.t(), function) :: @spec deliver_update_email_instructions(User.t(), String.t(), function) :: Job.t()
Job.t()
def deliver_update_email_instructions(user, current_email, update_email_url_fun) def deliver_update_email_instructions(user, current_email, update_email_url_fun)
when is_function(update_email_url_fun, 1) do when is_function(update_email_url_fun, 1) do
{encoded_token, user_token} = UserToken.build_email_token(user, "change:#{current_email}") {encoded_token, user_token} = UserToken.build_email_token(user, "change:#{current_email}")
@ -258,32 +231,28 @@ defmodule Cannery.Accounts do
## Examples ## Examples
iex> change_user_password(%User{}) iex> change_user_password(user)
%Changeset{} %Changeset{data: %User{}}
""" """
@spec change_user_password(User.t(), attrs :: map()) :: User.changeset() @spec change_user_password(User.t(), map()) :: Changeset.t(User.t())
def change_user_password(user, attrs \\ %{}) do def change_user_password(user, attrs \\ %{}),
User.password_changeset(user, attrs, hash_password: false) do: User.password_changeset(user, attrs, hash_password: false)
end
@doc """ @doc """
Updates the user password. Updates the user password.
## Examples ## Examples
iex> reset_user_password(user, %{ iex> update_user_password(user, "valid password", %{password: ...})
...> password: "new password",
...> password_confirmation: "new password"
...> })
{:ok, %User{}} {:ok, %User{}}
iex> update_user_password(user, "invalid password", %{password: "123"}) iex> update_user_password(user, "invalid password", %{password: ...})
{:error, %Changeset{}} {:error, %Changeset{}}
""" """
@spec update_user_password(User.t(), String.t(), attrs :: map()) :: @spec update_user_password(User.t(), String.t(), map()) ::
{:ok, User.t()} | {:error, User.changeset()} {:ok, User.t()} | {:error, Changeset.t(User.t())}
def update_user_password(user, password, attrs) do def update_user_password(user, password, attrs) do
changeset = changeset =
user user
@ -301,54 +270,49 @@ defmodule Cannery.Accounts do
end end
@doc """ @doc """
Returns an `Ecto.Changeset.t()` for changing the user locale. Returns an `%Changeset{}` for changing the user locale.
## Examples ## Examples
iex> change_user_locale(%User{}) iex> change_user_locale(user)
%Changeset{} %Changeset{data: %User{}}
""" """
@spec change_user_locale(User.t()) :: User.changeset() @spec change_user_locale(User.t()) :: Changeset.t(User.t())
def change_user_locale(%{locale: locale} = user) do def change_user_locale(%{locale: locale} = user), do: User.locale_changeset(user, locale)
User.locale_changeset(user, locale)
end
@doc """ @doc """
Updates the user locale. Updates the user locale.
## Examples ## Examples
iex> update_user_locale(user, "en_US") iex> update_user_locale(user, "valid locale")
{:ok, %User{}} {:ok, %User{}}
iex> update_user_password(user, "invalid locale")
{:error, %Changeset{}}
""" """
@spec update_user_locale(User.t(), locale :: String.t()) :: @spec update_user_locale(User.t(), locale :: String.t()) ::
{:ok, User.t()} | {:error, User.changeset()} {:ok, User.t()} | {:error, Changeset.t(User.t())}
def update_user_locale(user, locale) do def update_user_locale(user, locale),
user |> User.locale_changeset(locale) |> Repo.update() do: user |> User.locale_changeset(locale) |> Repo.update()
end
@doc """ @doc """
Deletes a user. must be performed by an admin or the same user! Deletes a user. must be performed by an admin or the same user!
## Examples ## Examples
iex> delete_user!(user, %User{id: 123, role: :admin}) iex> delete_user!(user_to_delete, %User{id: 123, role: :admin})
%User{} %User{}
iex> delete_user!(user, user) iex> delete_user!(%User{id: 123}, %User{id: 123})
%User{} %User{}
""" """
@spec delete_user!(user_to_delete :: User.t(), User.t()) :: User.t() @spec delete_user!(User.t(), User.t()) :: User.t()
def delete_user!(user, %User{role: :admin}) do def delete_user!(user, %User{role: :admin}), do: user |> Repo.delete!()
user |> Repo.delete!() def delete_user!(%User{id: user_id} = user, %User{id: user_id}), do: user |> Repo.delete!()
end
def delete_user!(%User{id: user_id} = user, %User{id: user_id}) do
user |> Repo.delete!()
end
## Session ## Session
@ -365,7 +329,7 @@ defmodule Cannery.Accounts do
@doc """ @doc """
Gets the user with the given signed token. Gets the user with the given signed token.
""" """
@spec get_user_by_session_token(token :: String.t()) :: User.t() @spec get_user_by_session_token(String.t()) :: User.t()
def get_user_by_session_token(token) do def get_user_by_session_token(token) do
{:ok, query} = UserToken.verify_session_token_query(token) {:ok, query} = UserToken.verify_session_token_query(token)
Repo.one(query) Repo.one(query)
@ -374,9 +338,9 @@ defmodule Cannery.Accounts do
@doc """ @doc """
Deletes the signed token with the given context. Deletes the signed token with the given context.
""" """
@spec delete_session_token(token :: String.t()) :: :ok @spec delete_session_token(String.t()) :: :ok
def delete_session_token(token) do def delete_session_token(token) do
UserToken.token_and_context_query(token, "session") |> Repo.delete_all() Repo.delete_all(UserToken.token_and_context_query(token, "session"))
:ok :ok
end end
@ -385,43 +349,19 @@ defmodule Cannery.Accounts do
""" """
@spec allow_registration?() :: boolean() @spec allow_registration?() :: boolean()
def allow_registration? do def allow_registration? do
Application.get_env(:cannery, Cannery.Accounts)[:registration] == "public" or Application.get_env(:cannery, CanneryWeb.Endpoint)[:registration] == "public" or
list_users_by_role(:admin) |> Enum.empty?() list_users_by_role(:admin) |> Enum.empty?()
end end
@doc """ @doc """
Checks if user is an admin Checks if user is an admin
## Examples
iex> is_admin?(%User{role: :admin})
true
iex> is_admin?(%User{})
false
""" """
@spec is_admin?(User.t()) :: boolean() @spec is_admin?(User.t()) :: boolean()
def is_admin?(%User{id: user_id}) do def is_admin?(%User{id: user_id}) do
Repo.exists?(from u in User, where: u.id == ^user_id, where: u.role == :admin) Repo.one(from u in User, where: u.id == ^user_id and u.role == :admin)
|> is_nil()
end end
@doc """
Checks to see if user has the admin role
## Examples
iex> is_already_admin?(%User{role: :admin})
true
iex> is_already_admin?(%User{})
false
"""
@spec is_already_admin?(User.t() | nil) :: boolean()
def is_already_admin?(%User{role: :admin}), do: true
def is_already_admin?(_invalid_user), do: false
## Confirmation ## Confirmation
@doc """ @doc """
@ -429,10 +369,10 @@ defmodule Cannery.Accounts do
## Examples ## Examples
iex> deliver_user_confirmation_instructions(user, fn _token -> "example url" end) iex> deliver_user_confirmation_instructions(user, &Routes.user_confirmation_url(conn, :confirm, &1))
%Oban.Job{args: %{email: :welcome, user_id: ^user_id, attrs: %{url: "example url"}}} {:ok, %{to: ..., body: ...}}
iex> deliver_user_confirmation_instructions(user, fn _token -> "example url" end) iex> deliver_user_confirmation_instructions(confirmed_user, &Routes.user_confirmation_url(conn, :confirm, &1))
{:error, :already_confirmed} {:error, :already_confirmed}
""" """
@ -454,7 +394,7 @@ defmodule Cannery.Accounts do
If the token matches, the user account is marked as confirmed If the token matches, the user account is marked as confirmed
and the token is deleted. and the token is deleted.
""" """
@spec confirm_user(token :: String.t()) :: {:ok, User.t()} | :error @spec confirm_user(String.t()) :: {:ok, User.t()} | atom()
def confirm_user(token) do def confirm_user(token) do
with {:ok, query} <- UserToken.verify_email_token_query(token, "confirm"), with {:ok, query} <- UserToken.verify_email_token_query(token, "confirm"),
%User{} = user <- Repo.one(query), %User{} = user <- Repo.one(query),
@ -479,8 +419,8 @@ defmodule Cannery.Accounts do
## Examples ## Examples
iex> deliver_user_reset_password_instructions(user, fn _token -> "example url" end) iex> deliver_user_reset_password_instructions(user, &Routes.user_reset_password_url(conn, :edit, &1))
%Oban.Job{args: %{email: :reset_password, user_id: ^user_id, attrs: %{url: "example url"}}} {:ok, %{to: ..., body: ...}}
""" """
@spec deliver_user_reset_password_instructions(User.t(), function()) :: Job.t() @spec deliver_user_reset_password_instructions(User.t(), function()) :: Job.t()
@ -496,14 +436,14 @@ defmodule Cannery.Accounts do
## Examples ## Examples
iex> get_user_by_reset_password_token(encoded_token) iex> get_user_by_reset_password_token("validtoken")
%User{} %User{}
iex> get_user_by_reset_password_token("invalidtoken") iex> get_user_by_reset_password_token("invalidtoken")
nil nil
""" """
@spec get_user_by_reset_password_token(token :: String.t()) :: User.t() | nil @spec get_user_by_reset_password_token(String.t()) :: User.t() | nil
def get_user_by_reset_password_token(token) do def get_user_by_reset_password_token(token) do
with {:ok, query} <- UserToken.verify_email_token_query(token, "reset_password"), with {:ok, query} <- UserToken.verify_email_token_query(token, "reset_password"),
%User{} = user <- Repo.one(query) do %User{} = user <- Repo.one(query) do
@ -518,18 +458,14 @@ defmodule Cannery.Accounts do
## Examples ## Examples
iex> reset_user_password(user, %{ iex> reset_user_password(user, %{password: "new long password", password_confirmation: "new long password"})
...> password: "new password",
...> password_confirmation: "new password"
...> })
{:ok, %User{}} {:ok, %User{}}
iex> reset_user_password(user, %{password: "valid", password_confirmation: "not the same"}) iex> reset_user_password(user, %{password: "valid", password_confirmation: "not the same"})
{:error, %Changeset{}} {:error, %Changeset{}}
""" """
@spec reset_user_password(User.t(), attrs :: map()) :: @spec reset_user_password(User.t(), map()) :: {:ok, User.t()} | {:error, Changeset.t(User.t())}
{:ok, User.t()} | {:error, User.changeset()}
def reset_user_password(user, attrs) do def reset_user_password(user, attrs) do
Multi.new() Multi.new()
|> Multi.update(:user, User.password_changeset(user, attrs)) |> Multi.update(:user, User.password_changeset(user, attrs))

View File

@ -27,21 +27,21 @@ defmodule Cannery.Email do
@spec generate_email(key :: String.t(), User.t(), attrs :: map()) :: t() @spec generate_email(key :: String.t(), User.t(), attrs :: map()) :: t()
def generate_email("welcome", user, %{"url" => url}) do def generate_email("welcome", user, %{"url" => url}) do
user user
|> base_email(dgettext("emails", "Confirm your Cannery account")) |> base_email(dgettext("emails", "Confirm your %{name} account", name: "Cannery"))
|> render_body("confirm_email.html", %{user: user, url: url}) |> render_body("confirm_email.html", %{user: user, url: url})
|> text_body(EmailView.render("confirm_email.txt", %{user: user, url: url})) |> text_body(EmailView.render("confirm_email.txt", %{user: user, url: url}))
end end
def generate_email("reset_password", user, %{"url" => url}) do def generate_email("reset_password", user, %{"url" => url}) do
user user
|> base_email(dgettext("emails", "Reset your Cannery password")) |> base_email(dgettext("emails", "Reset your %{name} password", name: "Cannery"))
|> render_body("reset_password.html", %{user: user, url: url}) |> render_body("reset_password.html", %{user: user, url: url})
|> text_body(EmailView.render("reset_password.txt", %{user: user, url: url})) |> text_body(EmailView.render("reset_password.txt", %{user: user, url: url}))
end end
def generate_email("update_email", user, %{"url" => url}) do def generate_email("update_email", user, %{"url" => url}) do
user user
|> base_email(dgettext("emails", "Update your Cannery email")) |> base_email(dgettext("emails", "Update your %{name} email", name: "Cannery"))
|> render_body("update_email.html", %{user: user, url: url}) |> render_body("update_email.html", %{user: user, url: url})
|> text_body(EmailView.render("update_email.txt", %{user: user, url: url})) |> text_body(EmailView.render("update_email.txt", %{user: user, url: url}))
end end

View File

@ -1,198 +0,0 @@
defmodule Cannery.Accounts.Invites do
@moduledoc """
The Invites context.
"""
import Ecto.Query, warn: false
alias Ecto.Multi
alias Cannery.Accounts.{Invite, User}
alias Cannery.Repo
@invite_token_length 20
@doc """
Returns the list of invites.
## Examples
iex> list_invites(%User{id: 123, role: :admin})
[%Invite{}, ...]
"""
@spec list_invites(User.t()) :: [Invite.t()]
def list_invites(%User{role: :admin}) do
Repo.all(from i in Invite, order_by: i.name)
end
@doc """
Gets a single invite for a user
Raises `Ecto.NoResultsError` if the Invite does not exist.
## Examples
iex> get_invite!(123, %User{id: 123, role: :admin})
%Invite{}
> get_invite!(456, %User{id: 123, role: :admin})
** (Ecto.NoResultsError)
"""
@spec get_invite!(Invite.id(), User.t()) :: Invite.t()
def get_invite!(id, %User{role: :admin}) do
Repo.get!(Invite, id)
end
@doc """
Returns if an invite token is still valid
## Examples
iex> valid_invite_token?("valid_token")
%Invite{}
iex> valid_invite_token?("invalid_token")
nil
"""
@spec valid_invite_token?(Invite.token() | nil) :: boolean()
def valid_invite_token?(token) when token in [nil, ""], do: false
def valid_invite_token?(token) do
Repo.exists?(
from i in Invite,
where: i.token == ^token,
where: i.disabled_at |> is_nil()
)
end
@doc """
Uses invite by decrementing uses_left, or marks invite invalid if it's been
completely used.
"""
@spec use_invite(Invite.token()) :: {:ok, Invite.t()} | {:error, :invalid_token}
def use_invite(invite_token) do
Multi.new()
|> Multi.run(:invite, fn _changes_so_far, _repo ->
invite_token |> get_invite_by_token()
end)
|> Multi.update(:decrement_invite, fn %{invite: invite} ->
decrement_invite_changeset(invite)
end)
|> Repo.transaction()
|> case do
{:ok, %{decrement_invite: invite}} -> {:ok, invite}
{:error, :invite, :invalid_token, _changes_so_far} -> {:error, :invalid_token}
end
end
@spec get_invite_by_token(Invite.token() | nil) :: {:ok, Invite.t()} | {:error, :invalid_token}
defp get_invite_by_token(token) when token in [nil, ""], do: {:error, :invalid_token}
defp get_invite_by_token(token) do
Repo.one(
from i in Invite,
where: i.token == ^token,
where: i.disabled_at |> is_nil()
)
|> case do
nil -> {:error, :invalid_token}
invite -> {:ok, invite}
end
end
@spec get_use_count(Invite.t(), User.t()) :: non_neg_integer()
def get_use_count(%Invite{id: invite_id}, %User{role: :admin}) do
Repo.one(
from u in User,
where: u.invite_id == ^invite_id,
select: count(u.id)
)
end
@spec decrement_invite_changeset(Invite.t()) :: Invite.changeset()
defp decrement_invite_changeset(%Invite{uses_left: nil} = invite) do
invite |> Invite.update_changeset(%{})
end
defp decrement_invite_changeset(%Invite{uses_left: 1} = invite) do
now = NaiveDateTime.utc_now() |> NaiveDateTime.truncate(:second)
invite |> Invite.update_changeset(%{uses_left: 0, disabled_at: now})
end
defp decrement_invite_changeset(%Invite{uses_left: uses_left} = invite) do
invite |> Invite.update_changeset(%{uses_left: uses_left - 1})
end
@doc """
Creates a invite.
## Examples
iex> create_invite(%User{id: 123, role: :admin}, %{field: value})
{:ok, %Invite{}}
iex> create_invite(%User{id: 123, role: :admin}, %{field: bad_value})
{:error, %Changeset{}}
"""
@spec create_invite(User.t(), attrs :: map()) ::
{:ok, Invite.t()} | {:error, Invite.changeset()}
def create_invite(%User{role: :admin} = user, attrs) do
token =
:crypto.strong_rand_bytes(@invite_token_length)
|> Base.url_encode64()
|> binary_part(0, @invite_token_length)
Invite.create_changeset(user, token, attrs) |> Repo.insert()
end
@doc """
Updates a invite.
## Examples
iex> update_invite(invite, %{field: new_value}, %User{id: 123, role: :admin})
{:ok, %Invite{}}
iex> update_invite(invite, %{field: bad_value}, %User{id: 123, role: :admin})
{:error, %Changeset{}}
"""
@spec update_invite(Invite.t(), attrs :: map(), User.t()) ::
{:ok, Invite.t()} | {:error, Invite.changeset()}
def update_invite(invite, attrs, %User{role: :admin}) do
invite |> Invite.update_changeset(attrs) |> Repo.update()
end
@doc """
Deletes a invite.
## Examples
iex> delete_invite(invite, %User{id: 123, role: :admin})
{:ok, %Invite{}}
iex> delete_invite(invite, %User{id: 123, role: :admin})
{:error, %Changeset{}}
"""
@spec delete_invite(Invite.t(), User.t()) ::
{:ok, Invite.t()} | {:error, Invite.changeset()}
def delete_invite(invite, %User{role: :admin}) do
invite |> Repo.delete()
end
@doc """
Deletes a invite.
## Examples
iex> delete_invite(invite, %User{id: 123, role: :admin})
%Invite{}
"""
@spec delete_invite!(Invite.t(), User.t()) :: Invite.t()
def delete_invite!(invite, %User{role: :admin}) do
invite |> Repo.delete!()
end
end

View File

@ -1,24 +1,14 @@
defmodule Cannery.Accounts.User do defmodule Cannery.Accounts.User do
@moduledoc """ @moduledoc """
A Cannery user A cannery user
""" """
use Ecto.Schema use Ecto.Schema
import Ecto.Changeset import Ecto.Changeset
import CanneryWeb.Gettext import CanneryWeb.Gettext
alias Ecto.{Association, Changeset, UUID} alias Ecto.{Changeset, UUID}
alias Cannery.Accounts.{Invite, User} alias Cannery.{Accounts.User, Invites.Invite}
@derive {Jason.Encoder,
only: [
:id,
:email,
:confirmed_at,
:role,
:locale,
:inserted_at,
:updated_at
]}
@derive {Inspect, except: [:password]} @derive {Inspect, except: [:password]}
@primary_key {:id, :binary_id, autogenerate: true} @primary_key {:id, :binary_id, autogenerate: true}
@foreign_key_type :binary_id @foreign_key_type :binary_id
@ -30,9 +20,7 @@ defmodule Cannery.Accounts.User do
field :role, Ecto.Enum, values: [:admin, :user], default: :user field :role, Ecto.Enum, values: [:admin, :user], default: :user
field :locale, :string field :locale, :string
has_many :created_invites, Invite, foreign_key: :created_by_id has_many :invites, Invite, on_delete: :delete_all
belongs_to :invite, Invite
timestamps() timestamps()
end end
@ -43,18 +31,14 @@ defmodule Cannery.Accounts.User do
password: String.t(), password: String.t(),
hashed_password: String.t(), hashed_password: String.t(),
confirmed_at: NaiveDateTime.t(), confirmed_at: NaiveDateTime.t(),
role: role(), role: atom(),
locale: String.t() | nil, locale: String.t() | nil,
created_invites: [Invite.t()] | Association.NotLoaded.t(), invites: [Invite.t()],
invite: Invite.t() | nil | Association.NotLoaded.t(),
invite_id: Invite.id() | nil,
inserted_at: NaiveDateTime.t(), inserted_at: NaiveDateTime.t(),
updated_at: NaiveDateTime.t() updated_at: NaiveDateTime.t()
} }
@type new_user :: %User{} @type new_user :: %User{}
@type id :: UUID.t() @type id :: UUID.t()
@type changeset :: Changeset.t(t() | new_user())
@type role :: :admin | :user
@doc """ @doc """
A user changeset for registration. A user changeset for registration.
@ -73,25 +57,26 @@ defmodule Cannery.Accounts.User do
validations on a LiveView form), this option can be set to `false`. validations on a LiveView form), this option can be set to `false`.
Defaults to `true`. Defaults to `true`.
""" """
@spec registration_changeset(attrs :: map(), Invite.t() | nil) :: changeset() @spec registration_changeset(t() | new_user(), attrs :: map()) :: Changeset.t(t() | new_user())
@spec registration_changeset(attrs :: map(), Invite.t() | nil, opts :: keyword()) :: changeset() @spec registration_changeset(t() | new_user(), attrs :: map(), opts :: keyword()) ::
def registration_changeset(attrs, invite, opts \\ []) do Changeset.t(t() | new_user())
%User{} def registration_changeset(user, attrs, opts \\ []) do
|> cast(attrs, [:email, :password, :locale]) user
|> put_change(:invite_id, if(invite, do: invite.id)) |> cast(attrs, [:email, :password, :role, :locale])
|> validate_email() |> validate_email()
|> validate_password(opts) |> validate_password(opts)
end end
@doc """ @doc """
A user changeset for role. A user changeset for role.
""" """
@spec role_changeset(t() | new_user() | changeset(), role()) :: changeset() @spec role_changeset(t(), role :: atom()) :: Changeset.t(t())
def role_changeset(user, role) do def role_changeset(user, role) do
user |> change(role: role) user |> cast(%{"role" => role}, [:role])
end end
@spec validate_email(changeset()) :: changeset() @spec validate_email(Changeset.t(t() | new_user())) :: Changeset.t(t() | new_user())
defp validate_email(changeset) do defp validate_email(changeset) do
changeset changeset
|> validate_required([:email]) |> validate_required([:email])
@ -103,8 +88,8 @@ defmodule Cannery.Accounts.User do
|> unique_constraint(:email) |> unique_constraint(:email)
end end
@spec validate_password(changeset(), opts :: keyword()) :: @spec validate_password(Changeset.t(t() | new_user()), opts :: keyword()) ::
changeset() Changeset.t(t() | new_user())
defp validate_password(changeset, opts) do defp validate_password(changeset, opts) do
changeset changeset
|> validate_required([:password]) |> validate_required([:password])
@ -115,7 +100,8 @@ defmodule Cannery.Accounts.User do
|> maybe_hash_password(opts) |> maybe_hash_password(opts)
end end
@spec maybe_hash_password(changeset(), opts :: keyword()) :: changeset() @spec maybe_hash_password(Changeset.t(t() | new_user()), opts :: keyword()) ::
Changeset.t(t() | new_user())
defp maybe_hash_password(changeset, opts) do defp maybe_hash_password(changeset, opts) do
hash_password? = Keyword.get(opts, :hash_password, true) hash_password? = Keyword.get(opts, :hash_password, true)
password = get_change(changeset, :password) password = get_change(changeset, :password)
@ -134,7 +120,7 @@ defmodule Cannery.Accounts.User do
It requires the email to change otherwise an error is added. It requires the email to change otherwise an error is added.
""" """
@spec email_changeset(t(), attrs :: map()) :: changeset() @spec email_changeset(t(), attrs :: map()) :: Changeset.t(t())
def email_changeset(user, attrs) do def email_changeset(user, attrs) do
user user
|> cast(attrs, [:email]) |> cast(attrs, [:email])
@ -157,8 +143,8 @@ defmodule Cannery.Accounts.User do
validations on a LiveView form), this option can be set to `false`. validations on a LiveView form), this option can be set to `false`.
Defaults to `true`. Defaults to `true`.
""" """
@spec password_changeset(t(), attrs :: map()) :: changeset() @spec password_changeset(t(), attrs :: map()) :: Changeset.t(t())
@spec password_changeset(t(), attrs :: map(), opts :: keyword()) :: changeset() @spec password_changeset(t(), attrs :: map(), opts :: keyword()) :: Changeset.t(t())
def password_changeset(user, attrs, opts \\ []) do def password_changeset(user, attrs, opts \\ []) do
user user
|> cast(attrs, [:password]) |> cast(attrs, [:password])
@ -169,7 +155,7 @@ defmodule Cannery.Accounts.User do
@doc """ @doc """
Confirms the account by setting `confirmed_at`. Confirms the account by setting `confirmed_at`.
""" """
@spec confirm_changeset(t() | changeset()) :: changeset() @spec confirm_changeset(t() | Changeset.t(t())) :: Changeset.t(t())
def confirm_changeset(user_or_changeset) do def confirm_changeset(user_or_changeset) do
now = NaiveDateTime.utc_now() |> NaiveDateTime.truncate(:second) now = NaiveDateTime.utc_now() |> NaiveDateTime.truncate(:second)
user_or_changeset |> change(confirmed_at: now) user_or_changeset |> change(confirmed_at: now)
@ -195,7 +181,7 @@ defmodule Cannery.Accounts.User do
@doc """ @doc """
Validates the current password otherwise adds an error to the changeset. Validates the current password otherwise adds an error to the changeset.
""" """
@spec validate_current_password(changeset(), String.t()) :: changeset() @spec validate_current_password(Changeset.t(t()), String.t()) :: Changeset.t(t())
def validate_current_password(changeset, password) do def validate_current_password(changeset, password) do
if valid_password?(changeset.data, password), if valid_password?(changeset.data, password),
do: changeset, do: changeset,
@ -205,7 +191,7 @@ defmodule Cannery.Accounts.User do
@doc """ @doc """
A changeset for changing the user's locale A changeset for changing the user's locale
""" """
@spec locale_changeset(t() | changeset(), locale :: String.t() | nil) :: changeset() @spec locale_changeset(t() | Changeset.t(t()), locale :: String.t() | nil) :: Changeset.t(t())
def locale_changeset(user_or_changeset, locale) do def locale_changeset(user_or_changeset, locale) do
user_or_changeset user_or_changeset
|> cast(%{"locale" => locale}, [:locale]) |> cast(%{"locale" => locale}, [:locale])

View File

@ -1,12 +1,12 @@
defmodule Cannery.Accounts.UserToken do defmodule Cannery.Accounts.UserToken do
@moduledoc """ @moduledoc """
Schema for a user's session token Schema for serialized user session and authentication tokens
""" """
use Ecto.Schema use Ecto.Schema
import Ecto.Query import Ecto.Query
alias Cannery.Accounts.User alias Ecto.{Query, UUID}
alias Ecto.{Association, UUID} alias Cannery.{Accounts.User, Accounts.UserToken}
@hash_algorithm :sha256 @hash_algorithm :sha256
@rand_size 32 @rand_size 32
@ -30,27 +30,27 @@ defmodule Cannery.Accounts.UserToken do
timestamps(updated_at: false) timestamps(updated_at: false)
end end
@type t :: %__MODULE__{ @type t :: %UserToken{
id: id(), id: id(),
token: token(), token: String.t(),
context: String.t(), context: String.t(),
sent_to: String.t(), sent_to: String.t(),
user: User.t() | Association.NotLoaded.t(), user: User.t(),
user_id: User.id() | nil, user_id: User.id(),
inserted_at: NaiveDateTime.t() inserted_at: NaiveDateTime.t()
} }
@type new_user_token :: %__MODULE__{} @type new_token :: %UserToken{}
@type id :: UUID.t() @type id :: UUID.t()
@type token :: binary()
@doc """ @doc """
Generates a token that will be stored in a signed place, Generates a token that will be stored in a signed place,
such as session or cookie. As they are signed, those such as session or cookie. As they are signed, those
tokens do not need to be hashed. tokens do not need to be hashed.
""" """
def build_session_token(user) do @spec build_session_token(User.t()) :: {token :: String.t(), new_token()}
def build_session_token(%{id: user_id}) do
token = :crypto.strong_rand_bytes(@rand_size) token = :crypto.strong_rand_bytes(@rand_size)
{token, %__MODULE__{token: token, context: "session", user_id: user.id}} {token, %UserToken{token: token, context: "session", user_id: user_id}}
end end
@doc """ @doc """
@ -58,6 +58,7 @@ defmodule Cannery.Accounts.UserToken do
The query returns the user found by the token. The query returns the user found by the token.
""" """
@spec verify_session_token_query(token :: String.t()) :: {:ok, Query.t()}
def verify_session_token_query(token) do def verify_session_token_query(token) do
query = query =
from token in token_and_context_query(token, "session"), from token in token_and_context_query(token, "session"),
@ -76,16 +77,19 @@ defmodule Cannery.Accounts.UserToken do
The token is valid for a week as long as users don't change The token is valid for a week as long as users don't change
their email. their email.
""" """
@spec build_email_token(User.t(), context :: String.t()) :: {token :: String.t(), new_token()}
def build_email_token(user, context) do def build_email_token(user, context) do
build_hashed_token(user, context, user.email) build_hashed_token(user, context, user.email)
end end
@spec build_hashed_token(User.t(), String.t(), String.t()) ::
{String.t(), new_token()}
defp build_hashed_token(user, context, sent_to) do defp build_hashed_token(user, context, sent_to) do
token = :crypto.strong_rand_bytes(@rand_size) token = :crypto.strong_rand_bytes(@rand_size)
hashed_token = :crypto.hash(@hash_algorithm, token) hashed_token = :crypto.hash(@hash_algorithm, token)
{Base.url_encode64(token, padding: false), {Base.url_encode64(token, padding: false),
%__MODULE__{ %UserToken{
token: hashed_token, token: hashed_token,
context: context, context: context,
sent_to: sent_to, sent_to: sent_to,
@ -98,6 +102,8 @@ defmodule Cannery.Accounts.UserToken do
The query returns the user found by the token. The query returns the user found by the token.
""" """
@spec verify_email_token_query(token :: String.t(), context :: String.t()) ::
{:ok, Query.t()} | :error
def verify_email_token_query(token, context) do def verify_email_token_query(token, context) do
case Base.url_decode64(token, padding: false) do case Base.url_decode64(token, padding: false) do
{:ok, decoded_token} -> {:ok, decoded_token} ->
@ -117,6 +123,7 @@ defmodule Cannery.Accounts.UserToken do
end end
end end
@spec days_for_context(context :: <<_::56>>) :: non_neg_integer()
defp days_for_context("confirm"), do: @confirm_validity_in_days defp days_for_context("confirm"), do: @confirm_validity_in_days
defp days_for_context("reset_password"), do: @reset_password_validity_in_days defp days_for_context("reset_password"), do: @reset_password_validity_in_days
@ -125,6 +132,8 @@ defmodule Cannery.Accounts.UserToken do
The query returns the user token record. The query returns the user token record.
""" """
@spec verify_change_email_token_query(token :: String.t(), context :: String.t()) ::
{:ok, Query.t()} | :error
def verify_change_email_token_query(token, context) do def verify_change_email_token_query(token, context) do
case Base.url_decode64(token, padding: false) do case Base.url_decode64(token, padding: false) do
{:ok, decoded_token} -> {:ok, decoded_token} ->
@ -144,18 +153,21 @@ defmodule Cannery.Accounts.UserToken do
@doc """ @doc """
Returns the given token with the given context. Returns the given token with the given context.
""" """
@spec token_and_context_query(token :: String.t(), context :: String.t()) :: Query.t()
def token_and_context_query(token, context) do def token_and_context_query(token, context) do
from __MODULE__, where: [token: ^token, context: ^context] from UserToken, where: [token: ^token, context: ^context]
end end
@doc """ @doc """
Gets all tokens for the given user for the given contexts. Gets all tokens for the given user for the given contexts.
""" """
def user_and_contexts_query(user, :all) do @spec user_and_contexts_query(User.t(), contexts :: :all | nonempty_maybe_improper_list()) ::
from t in __MODULE__, where: t.user_id == ^user.id Query.t()
def user_and_contexts_query(%{id: user_id}, :all) do
from t in UserToken, where: t.user_id == ^user_id
end end
def user_and_contexts_query(user, [_ | _] = contexts) do def user_and_contexts_query(%{id: user_id}, [_ | _] = contexts) do
from t in __MODULE__, where: t.user_id == ^user.id and t.context in ^contexts from t in UserToken, where: t.user_id == ^user_id and t.context in ^contexts
end end
end end

View File

@ -5,7 +5,7 @@ defmodule Cannery.ActivityLog do
import Ecto.Query, warn: false import Ecto.Query, warn: false
alias Cannery.{Accounts.User, ActivityLog.ShotGroup, Ammo.AmmoGroup, Repo} alias Cannery.{Accounts.User, ActivityLog.ShotGroup, Ammo.AmmoGroup, Repo}
alias Ecto.Multi alias Ecto.{Changeset, Multi}
@doc """ @doc """
Returns the list of shot_groups. Returns the list of shot_groups.
@ -15,50 +15,10 @@ defmodule Cannery.ActivityLog do
iex> list_shot_groups(%User{id: 123}) iex> list_shot_groups(%User{id: 123})
[%ShotGroup{}, ...] [%ShotGroup{}, ...]
iex> list_shot_groups("cool", %User{id: 123})
[%ShotGroup{notes: "My cool shot group"}, ...]
""" """
@spec list_shot_groups(User.t()) :: [ShotGroup.t()] @spec list_shot_groups(User.t()) :: [ShotGroup.t()]
@spec list_shot_groups(search :: nil | String.t(), User.t()) :: [ShotGroup.t()] def list_shot_groups(%User{id: user_id}) do
def list_shot_groups(search \\ nil, user) Repo.all(from(sg in ShotGroup, where: sg.user_id == ^user_id))
def list_shot_groups(search, %{id: user_id}) when search |> is_nil() or search == "",
do: Repo.all(from sg in ShotGroup, where: sg.user_id == ^user_id)
def list_shot_groups(search, %{id: user_id}) when search |> is_binary() do
trimmed_search = String.trim(search)
Repo.all(
from sg in ShotGroup,
left_join: ag in assoc(sg, :ammo_group),
left_join: at in assoc(ag, :ammo_type),
where: sg.user_id == ^user_id,
where:
fragment(
"? @@ websearch_to_tsquery('english', ?)",
sg.search,
^trimmed_search
) or
fragment(
"? @@ websearch_to_tsquery('english', ?)",
ag.search,
^trimmed_search
) or
fragment(
"? @@ websearch_to_tsquery('english', ?)",
at.search,
^trimmed_search
),
order_by: {
:desc,
fragment(
"ts_rank_cd(?, websearch_to_tsquery('english', ?), 4)",
sg.search,
^trimmed_search
)
}
)
end end
@doc """ @doc """
@ -98,7 +58,7 @@ defmodule Cannery.ActivityLog do
""" """
@spec create_shot_group(attrs :: map(), User.t(), AmmoGroup.t()) :: @spec create_shot_group(attrs :: map(), User.t(), AmmoGroup.t()) ::
{:ok, ShotGroup.t()} | {:error, ShotGroup.changeset() | nil} {:ok, ShotGroup.t()} | {:error, Changeset.t(ShotGroup.t()) | nil}
def create_shot_group(attrs, user, ammo_group) do def create_shot_group(attrs, user, ammo_group) do
Multi.new() Multi.new()
|> Multi.insert( |> Multi.insert(
@ -139,7 +99,7 @@ defmodule Cannery.ActivityLog do
""" """
@spec update_shot_group(ShotGroup.t(), attrs :: map(), User.t()) :: @spec update_shot_group(ShotGroup.t(), attrs :: map(), User.t()) ::
{:ok, ShotGroup.t()} | {:error, ShotGroup.changeset() | nil} {:ok, ShotGroup.t()} | {:error, Changeset.t(ShotGroup.t()) | nil}
def update_shot_group( def update_shot_group(
%ShotGroup{count: count, user_id: user_id} = shot_group, %ShotGroup{count: count, user_id: user_id} = shot_group,
attrs, attrs,
@ -189,7 +149,7 @@ defmodule Cannery.ActivityLog do
""" """
@spec delete_shot_group(ShotGroup.t(), User.t()) :: @spec delete_shot_group(ShotGroup.t(), User.t()) ::
{:ok, ShotGroup.t()} | {:error, ShotGroup.changeset()} {:ok, ShotGroup.t()} | {:error, Changeset.t(ShotGroup.t())}
def delete_shot_group( def delete_shot_group(
%ShotGroup{user_id: user_id} = shot_group, %ShotGroup{user_id: user_id} = shot_group,
%User{id: user_id} %User{id: user_id}

View File

@ -9,14 +9,6 @@ defmodule Cannery.ActivityLog.ShotGroup do
alias Cannery.{Accounts.User, ActivityLog.ShotGroup, Ammo.AmmoGroup, Repo} alias Cannery.{Accounts.User, ActivityLog.ShotGroup, Ammo.AmmoGroup, Repo}
alias Ecto.{Changeset, UUID} alias Ecto.{Changeset, UUID}
@derive {Jason.Encoder,
only: [
:id,
:count,
:date,
:notes,
:ammo_group_id
]}
@primary_key {:id, :binary_id, autogenerate: true} @primary_key {:id, :binary_id, autogenerate: true}
@foreign_key_type :binary_id @foreign_key_type :binary_id
schema "shot_groups" do schema "shot_groups" do
@ -44,7 +36,6 @@ defmodule Cannery.ActivityLog.ShotGroup do
} }
@type new_shot_group :: %ShotGroup{} @type new_shot_group :: %ShotGroup{}
@type id :: UUID.t() @type id :: UUID.t()
@type changeset :: Changeset.t(t() | new_shot_group())
@doc false @doc false
@spec create_changeset( @spec create_changeset(
@ -52,7 +43,8 @@ defmodule Cannery.ActivityLog.ShotGroup do
User.t() | any(), User.t() | any(),
AmmoGroup.t() | any(), AmmoGroup.t() | any(),
attrs :: map() attrs :: map()
) :: changeset() ) ::
Changeset.t(new_shot_group())
def create_changeset( def create_changeset(
shot_group, shot_group,
%User{id: user_id}, %User{id: user_id},
@ -66,7 +58,7 @@ defmodule Cannery.ActivityLog.ShotGroup do
|> cast(attrs, [:count, :notes, :date]) |> cast(attrs, [:count, :notes, :date])
|> validate_number(:count, greater_than: 0) |> validate_number(:count, greater_than: 0)
|> validate_create_shot_group_count(ammo_group) |> validate_create_shot_group_count(ammo_group)
|> validate_required([:count, :date, :ammo_group_id, :user_id]) |> validate_required([:count, :ammo_group_id, :user_id])
end end
def create_changeset(shot_group, _invalid_user, _invalid_ammo_group, attrs) do def create_changeset(shot_group, _invalid_user, _invalid_ammo_group, attrs) do
@ -74,7 +66,7 @@ defmodule Cannery.ActivityLog.ShotGroup do
|> cast(attrs, [:count, :notes, :date]) |> cast(attrs, [:count, :notes, :date])
|> validate_number(:count, greater_than: 0) |> validate_number(:count, greater_than: 0)
|> validate_required([:count, :ammo_group_id, :user_id]) |> validate_required([:count, :ammo_group_id, :user_id])
|> add_error(:invalid, dgettext("errors", "Please select a valid user and ammo pack")) |> add_error(:invalid, dgettext("errors", "Please select a valid user and ammo group"))
end end
defp validate_create_shot_group_count(changeset, %AmmoGroup{count: ammo_group_count}) do defp validate_create_shot_group_count(changeset, %AmmoGroup{count: ammo_group_count}) do
@ -87,7 +79,8 @@ defmodule Cannery.ActivityLog.ShotGroup do
end end
@doc false @doc false
@spec update_changeset(t() | new_shot_group(), User.t(), attrs :: map()) :: changeset() @spec update_changeset(t() | new_shot_group(), User.t(), attrs :: map()) ::
Changeset.t(t() | new_shot_group())
def update_changeset( def update_changeset(
%ShotGroup{user_id: user_id} = shot_group, %ShotGroup{user_id: user_id} = shot_group,
%User{id: user_id} = user, %User{id: user_id} = user,
@ -97,7 +90,7 @@ defmodule Cannery.ActivityLog.ShotGroup do
shot_group shot_group
|> cast(attrs, [:count, :notes, :date]) |> cast(attrs, [:count, :notes, :date])
|> validate_number(:count, greater_than: 0) |> validate_number(:count, greater_than: 0)
|> validate_required([:count, :date]) |> validate_required([:count])
|> validate_update_shot_group_count(shot_group, user) |> validate_update_shot_group_count(shot_group, user)
end end

View File

@ -5,7 +5,7 @@ defmodule Cannery.Ammo do
import CanneryWeb.Gettext import CanneryWeb.Gettext
import Ecto.Query, warn: false import Ecto.Query, warn: false
alias Cannery.{Accounts.User, Containers, Containers.Container, Repo} alias Cannery.{Accounts.User, Containers, Repo}
alias Cannery.ActivityLog.ShotGroup alias Cannery.ActivityLog.ShotGroup
alias Cannery.Ammo.{AmmoGroup, AmmoType} alias Cannery.Ammo.{AmmoGroup, AmmoType}
alias Ecto.Changeset alias Ecto.Changeset
@ -20,38 +20,11 @@ defmodule Cannery.Ammo do
iex> list_ammo_types(%User{id: 123}) iex> list_ammo_types(%User{id: 123})
[%AmmoType{}, ...] [%AmmoType{}, ...]
iex> list_ammo_types("cool", %User{id: 123})
[%AmmoType{name: "My cool ammo type"}, ...]
""" """
@spec list_ammo_types(User.t()) :: [AmmoType.t()] @spec list_ammo_types(User.t()) :: [AmmoType.t()]
@spec list_ammo_types(search :: nil | String.t(), User.t()) :: [AmmoType.t()] def list_ammo_types(%User{id: user_id}),
def list_ammo_types(search \\ nil, user)
def list_ammo_types(search, %{id: user_id}) when search |> is_nil() or search == "",
do: Repo.all(from at in AmmoType, where: at.user_id == ^user_id, order_by: at.name) do: Repo.all(from at in AmmoType, where: at.user_id == ^user_id, order_by: at.name)
def list_ammo_types(search, %{id: user_id}) when search |> is_binary() do
trimmed_search = String.trim(search)
Repo.all(
from at in AmmoType,
where: at.user_id == ^user_id,
where:
fragment(
"search @@ websearch_to_tsquery('english', ?)",
^trimmed_search
),
order_by: {
:desc,
fragment(
"ts_rank_cd(search, websearch_to_tsquery('english', ?), 4)",
^trimmed_search
)
}
)
end
@doc """ @doc """
Returns a count of ammo_types. Returns a count of ammo_types.
@ -128,7 +101,7 @@ defmodule Cannery.Ammo do
## Examples ## Examples
iex> get_round_count_for_ammo_type(123, %User{id: 123}) iex> get_round_count_for_ammo_type(123, %User{id: 123})
35 %AmmoType{}
iex> get_round_count_for_ammo_type(456, %User{id: 123}) iex> get_round_count_for_ammo_type(456, %User{id: 123})
** (Ecto.NoResultsError) ** (Ecto.NoResultsError)
@ -154,7 +127,7 @@ defmodule Cannery.Ammo do
## Examples ## Examples
iex> get_used_count_for_ammo_type(123, %User{id: 123}) iex> get_used_count_for_ammo_type(123, %User{id: 123})
35 %AmmoType{}
iex> get_used_count_for_ammo_type(456, %User{id: 123}) iex> get_used_count_for_ammo_type(456, %User{id: 123})
** (Ecto.NoResultsError) ** (Ecto.NoResultsError)
@ -173,29 +146,6 @@ defmodule Cannery.Ammo do
) || 0 ) || 0
end end
@doc """
Gets the total number of ammo ever bought for an ammo type
Raises `Ecto.NoResultsError` if the Ammo type does not exist.
## Examples
iex> get_historical_count_for_ammo_type(123, %User{id: 123})
%AmmoType{}
iex> get_historical_count_for_ammo_type(456, %User{id: 123})
** (Ecto.NoResultsError)
"""
@spec get_historical_count_for_ammo_type(AmmoType.t(), User.t()) :: non_neg_integer()
def get_historical_count_for_ammo_type(
%AmmoType{user_id: user_id} = ammo_type,
%User{id: user_id} = user
) do
get_round_count_for_ammo_type(ammo_type, user) +
get_used_count_for_ammo_type(ammo_type, user)
end
@doc """ @doc """
Creates a ammo_type. Creates a ammo_type.
@ -209,7 +159,7 @@ defmodule Cannery.Ammo do
""" """
@spec create_ammo_type(attrs :: map(), User.t()) :: @spec create_ammo_type(attrs :: map(), User.t()) ::
{:ok, AmmoType.t()} | {:error, AmmoType.changeset()} {:ok, AmmoType.t()} | {:error, Changeset.t(AmmoType.new_ammo_type())}
def create_ammo_type(attrs \\ %{}, %User{} = user), def create_ammo_type(attrs \\ %{}, %User{} = user),
do: %AmmoType{} |> AmmoType.create_changeset(user, attrs) |> Repo.insert() do: %AmmoType{} |> AmmoType.create_changeset(user, attrs) |> Repo.insert()
@ -226,7 +176,7 @@ defmodule Cannery.Ammo do
""" """
@spec update_ammo_type(AmmoType.t(), attrs :: map(), User.t()) :: @spec update_ammo_type(AmmoType.t(), attrs :: map(), User.t()) ::
{:ok, AmmoType.t()} | {:error, AmmoType.changeset()} {:ok, AmmoType.t()} | {:error, Changeset.t(AmmoType.t())}
def update_ammo_type(%AmmoType{user_id: user_id} = ammo_type, attrs, %User{id: user_id}), def update_ammo_type(%AmmoType{user_id: user_id} = ammo_type, attrs, %User{id: user_id}),
do: ammo_type |> AmmoType.update_changeset(attrs) |> Repo.update() do: ammo_type |> AmmoType.update_changeset(attrs) |> Repo.update()
@ -243,7 +193,7 @@ defmodule Cannery.Ammo do
""" """
@spec delete_ammo_type(AmmoType.t(), User.t()) :: @spec delete_ammo_type(AmmoType.t(), User.t()) ::
{:ok, AmmoType.t()} | {:error, AmmoType.changeset()} {:ok, AmmoType.t()} | {:error, Changeset.t(AmmoType.t())}
def delete_ammo_type(%AmmoType{user_id: user_id} = ammo_type, %User{id: user_id}), def delete_ammo_type(%AmmoType{user_id: user_id} = ammo_type, %User{id: user_id}),
do: ammo_type |> Repo.delete() do: ammo_type |> Repo.delete()
@ -270,15 +220,7 @@ defmodule Cannery.Ammo do
""" """
@spec list_ammo_groups_for_type(AmmoType.t(), User.t()) :: [AmmoGroup.t()] @spec list_ammo_groups_for_type(AmmoType.t(), User.t()) :: [AmmoGroup.t()]
@spec list_ammo_groups_for_type(AmmoType.t(), User.t(), include_empty :: boolean()) :: def list_ammo_groups_for_type(%AmmoType{id: ammo_type_id, user_id: user_id}, %User{id: user_id}) do
[AmmoGroup.t()]
def list_ammo_groups_for_type(ammo_type, user, include_empty \\ false)
def list_ammo_groups_for_type(
%AmmoType{id: ammo_type_id, user_id: user_id},
%User{id: user_id},
true = _include_empty
) do
Repo.all( Repo.all(
from ag in AmmoGroup, from ag in AmmoGroup,
left_join: sg in assoc(ag, :shot_groups), left_join: sg in assoc(ag, :shot_groups),
@ -289,221 +231,35 @@ defmodule Cannery.Ammo do
) )
end end
def list_ammo_groups_for_type(
%AmmoType{id: ammo_type_id, user_id: user_id},
%User{id: user_id},
false = _include_empty
) do
Repo.all(
from ag in AmmoGroup,
left_join: sg in assoc(ag, :shot_groups),
where: ag.ammo_type_id == ^ammo_type_id,
where: ag.user_id == ^user_id,
where: not (ag.count == 0),
preload: [shot_groups: sg],
order_by: ag.id
)
end
@doc """ @doc """
Returns the list of ammo_groups for a user and container. Returns the list of ammo_groups for a user.
## Examples
iex> list_ammo_groups_for_container(%AmmoType{id: 123}, %User{id: 123})
[%AmmoGroup{}, ...]
"""
@spec list_ammo_groups_for_container(Container.t(), User.t()) :: [AmmoGroup.t()]
@spec list_ammo_groups_for_container(Container.t(), User.t(), include_empty :: boolean()) ::
[AmmoGroup.t()]
def list_ammo_groups_for_container(container, user, include_empty \\ false)
def list_ammo_groups_for_container(
%Container{id: container_id, user_id: user_id},
%User{id: user_id},
true = _include_empty
) do
Repo.all(
from ag in AmmoGroup,
left_join: sg in assoc(ag, :shot_groups),
where: ag.container_id == ^container_id,
where: ag.user_id == ^user_id,
preload: [shot_groups: sg],
order_by: ag.id
)
end
def list_ammo_groups_for_container(
%Container{id: container_id, user_id: user_id},
%User{id: user_id},
false = _include_empty
) do
Repo.all(
from ag in AmmoGroup,
left_join: sg in assoc(ag, :shot_groups),
where: ag.container_id == ^container_id,
where: ag.user_id == ^user_id,
where: not (ag.count == 0),
preload: [shot_groups: sg],
order_by: ag.id
)
end
@doc """
Returns the count of ammo_groups for an ammo type.
## Examples
iex> get_ammo_groups_count_for_type(%AmmoType{id: 123}, %User{id: 123})
3
iex> get_ammo_groups_count_for_type(%AmmoType{id: 123}, %User{id: 123}, true)
5
"""
@spec get_ammo_groups_count_for_type(AmmoType.t(), User.t()) :: [AmmoGroup.t()]
@spec get_ammo_groups_count_for_type(AmmoType.t(), User.t(), include_empty :: boolean()) ::
[AmmoGroup.t()]
def get_ammo_groups_count_for_type(ammo_type, user, include_empty \\ false)
def get_ammo_groups_count_for_type(
%AmmoType{id: ammo_type_id, user_id: user_id},
%User{id: user_id},
true = _include_empty
) do
Repo.one!(
from ag in AmmoGroup,
where: ag.user_id == ^user_id,
where: ag.ammo_type_id == ^ammo_type_id,
distinct: true,
select: count(ag.id)
) || 0
end
def get_ammo_groups_count_for_type(
%AmmoType{id: ammo_type_id, user_id: user_id},
%User{id: user_id},
false = _include_empty
) do
Repo.one!(
from ag in AmmoGroup,
where: ag.user_id == ^user_id,
where: ag.ammo_type_id == ^ammo_type_id,
where: not (ag.count == 0),
distinct: true,
select: count(ag.id)
) || 0
end
@doc """
Returns the count of used ammo_groups for an ammo type.
## Examples
iex> get_used_ammo_groups_count_for_type(%AmmoType{id: 123}, %User{id: 123})
3
"""
@spec get_used_ammo_groups_count_for_type(AmmoType.t(), User.t()) :: [AmmoGroup.t()]
def get_used_ammo_groups_count_for_type(
%AmmoType{id: ammo_type_id, user_id: user_id},
%User{id: user_id}
) do
Repo.one!(
from ag in AmmoGroup,
where: ag.user_id == ^user_id,
where: ag.ammo_type_id == ^ammo_type_id,
where: ag.count == 0,
distinct: true,
select: count(ag.id)
) || 0
end
@doc """
Returns the list of ammo_groups.
## Examples ## Examples
iex> list_ammo_groups(%User{id: 123}) iex> list_ammo_groups(%User{id: 123})
[%AmmoGroup{}, ...] [%AmmoGroup{}, ...]
iex> list_ammo_groups("cool", true, %User{id: 123})
[%AmmoGroup{notes: "My cool ammo group"}, ...]
""" """
@spec list_ammo_groups(User.t()) :: [AmmoGroup.t()] @spec list_ammo_groups(User.t()) :: [AmmoGroup.t()]
@spec list_ammo_groups(search :: nil | String.t(), User.t()) :: [AmmoGroup.t()] @spec list_ammo_groups(User.t(), include_empty :: boolean()) :: [AmmoGroup.t()]
@spec list_ammo_groups(search :: nil | String.t(), include_empty :: boolean(), User.t()) :: def list_ammo_groups(%User{id: user_id}, include_empty \\ false) do
[AmmoGroup.t()] if include_empty do
def list_ammo_groups(search \\ nil, include_empty \\ false, %{id: user_id}) do from ag in AmmoGroup,
from( left_join: sg in assoc(ag, :shot_groups),
ag in AmmoGroup, where: ag.user_id == ^user_id,
as: :ag, preload: [shot_groups: sg],
left_join: sg in assoc(ag, :shot_groups), order_by: ag.id
as: :sg, else
join: at in assoc(ag, :ammo_type), from ag in AmmoGroup,
as: :at, left_join: sg in assoc(ag, :shot_groups),
join: c in assoc(ag, :container), where: ag.user_id == ^user_id,
as: :c, where: not (ag.count == 0),
left_join: t in assoc(c, :tags), preload: [shot_groups: sg],
as: :t, order_by: ag.id
where: ag.user_id == ^user_id, end
preload: [shot_groups: sg, ammo_type: at, container: {c, tags: t}],
order_by: ag.id
)
|> list_ammo_groups_include_empty(include_empty)
|> list_ammo_groups_search(search)
|> Repo.all() |> Repo.all()
end end
defp list_ammo_groups_include_empty(query, true), do: query
defp list_ammo_groups_include_empty(query, false) do
query |> where([ag], not (ag.count == 0))
end
defp list_ammo_groups_search(query, nil), do: query
defp list_ammo_groups_search(query, ""), do: query
defp list_ammo_groups_search(query, search) do
trimmed_search = String.trim(search)
query
|> where(
[ag: ag, at: at, c: c, t: t],
fragment(
"? @@ websearch_to_tsquery('english', ?)",
ag.search,
^trimmed_search
) or
fragment(
"? @@ websearch_to_tsquery('english', ?)",
at.search,
^trimmed_search
) or
fragment(
"? @@ websearch_to_tsquery('english', ?)",
c.search,
^trimmed_search
) or
fragment(
"? @@ websearch_to_tsquery('english', ?)",
t.search,
^trimmed_search
)
)
|> order_by(
[ag: ag],
desc:
fragment(
"ts_rank_cd(?, websearch_to_tsquery('english', ?), 4)",
ag.search,
^trimmed_search
)
)
end
@doc """ @doc """
Returns the list of staged ammo_groups for a user. Returns the list of staged ammo_groups for a user.
@ -562,17 +318,6 @@ defmodule Cannery.Ammo do
|> Enum.sum() |> Enum.sum()
end end
@doc """
Returns the last entered shot group for an ammo group
"""
@spec get_last_used_shot_group(AmmoGroup.t()) :: ShotGroup.t() | nil
def get_last_used_shot_group(%AmmoGroup{} = ammo_group) do
ammo_group
|> Repo.preload(:shot_groups)
|> Map.fetch!(:shot_groups)
|> Enum.max_by(fn %{date: date} -> date end, Date, fn -> nil end)
end
@doc """ @doc """
Calculates the percentage remaining of an ammo group out of 100 Calculates the percentage remaining of an ammo group out of 100
""" """
@ -583,35 +328,11 @@ defmodule Cannery.Ammo do
ammo_group = ammo_group |> Repo.preload(:shot_groups) ammo_group = ammo_group |> Repo.preload(:shot_groups)
shot_group_sum = shot_group_sum =
ammo_group.shot_groups ammo_group.shot_groups |> Enum.map(fn %{count: count} -> count end) |> Enum.sum()
|> Enum.map(fn %{count: count} -> count end)
|> Enum.sum()
round(count / (count + shot_group_sum) * 100) round(count / (count + shot_group_sum) * 100)
end end
@doc """
Gets the original count for an ammo group
"""
@spec get_original_count(AmmoGroup.t()) :: non_neg_integer()
def get_original_count(%AmmoGroup{count: count} = ammo_group) do
count + get_used_count(ammo_group)
end
@doc """
Calculates the CPR for a single ammo group
"""
@spec get_cpr(AmmoGroup.t()) :: nil | float()
def get_cpr(%AmmoGroup{price_paid: nil}), do: nil
def get_cpr(%AmmoGroup{price_paid: price_paid} = ammo_group),
do: calculate_cpr(price_paid, get_original_count(ammo_group))
@spec calculate_cpr(price_paid :: float() | nil, count :: integer()) :: float() | nil
defp calculate_cpr(nil, _count), do: nil
defp calculate_cpr(_price_paid, 0), do: nil
defp calculate_cpr(price_paid, total_count), do: price_paid / total_count
@doc """ @doc """
Creates multiple ammo_groups at once. Creates multiple ammo_groups at once.
@ -626,7 +347,7 @@ defmodule Cannery.Ammo do
""" """
@spec create_ammo_groups(attrs :: map(), multiplier :: non_neg_integer(), User.t()) :: @spec create_ammo_groups(attrs :: map(), multiplier :: non_neg_integer(), User.t()) ::
{:ok, {count :: non_neg_integer(), [AmmoGroup.t()] | nil}} {:ok, {count :: non_neg_integer(), [AmmoGroup.t()] | nil}}
| {:error, AmmoGroup.changeset()} | {:error, Changeset.t(AmmoGroup.new_ammo_group())}
def create_ammo_groups( def create_ammo_groups(
%{"ammo_type_id" => ammo_type_id, "container_id" => container_id} = attrs, %{"ammo_type_id" => ammo_type_id, "container_id" => container_id} = attrs,
multiplier, multiplier,
@ -705,13 +426,9 @@ defmodule Cannery.Ammo do
""" """
@spec update_ammo_group(AmmoGroup.t(), attrs :: map(), User.t()) :: @spec update_ammo_group(AmmoGroup.t(), attrs :: map(), User.t()) ::
{:ok, AmmoGroup.t()} | {:error, AmmoGroup.changeset()} {:ok, AmmoGroup.t()} | {:error, Changeset.t(AmmoGroup.t())}
def update_ammo_group( def update_ammo_group(%AmmoGroup{user_id: user_id} = ammo_group, attrs, %User{id: user_id}),
%AmmoGroup{user_id: user_id} = ammo_group, do: ammo_group |> AmmoGroup.update_changeset(attrs) |> Repo.update()
attrs,
%User{id: user_id} = user
),
do: ammo_group |> AmmoGroup.update_changeset(attrs, user) |> Repo.update()
@doc """ @doc """
Deletes a ammo_group. Deletes a ammo_group.
@ -726,7 +443,7 @@ defmodule Cannery.Ammo do
""" """
@spec delete_ammo_group(AmmoGroup.t(), User.t()) :: @spec delete_ammo_group(AmmoGroup.t(), User.t()) ::
{:ok, AmmoGroup.t()} | {:error, AmmoGroup.changeset()} {:ok, AmmoGroup.t()} | {:error, Changeset.t(AmmoGroup.t())}
def delete_ammo_group(%AmmoGroup{user_id: user_id} = ammo_group, %User{id: user_id}), def delete_ammo_group(%AmmoGroup{user_id: user_id} = ammo_group, %User{id: user_id}),
do: ammo_group |> Repo.delete() do: ammo_group |> Repo.delete()

View File

@ -10,19 +10,9 @@ defmodule Cannery.Ammo.AmmoGroup do
import CanneryWeb.Gettext import CanneryWeb.Gettext
import Ecto.Changeset import Ecto.Changeset
alias Cannery.Ammo.{AmmoGroup, AmmoType} alias Cannery.Ammo.{AmmoGroup, AmmoType}
alias Cannery.{Accounts.User, ActivityLog.ShotGroup, Containers, Containers.Container} alias Cannery.{Accounts.User, ActivityLog.ShotGroup, Containers.Container}
alias Ecto.{Changeset, UUID} alias Ecto.{Changeset, UUID}
@derive {Jason.Encoder,
only: [
:id,
:count,
:notes,
:price_paid,
:staged,
:ammo_type_id,
:container_id
]}
@primary_key {:id, :binary_id, autogenerate: true} @primary_key {:id, :binary_id, autogenerate: true}
@foreign_key_type :binary_id @foreign_key_type :binary_id
schema "ammo_groups" do schema "ammo_groups" do
@ -30,7 +20,6 @@ defmodule Cannery.Ammo.AmmoGroup do
field :notes, :string field :notes, :string
field :price_paid, :float field :price_paid, :float
field :staged, :boolean, default: false field :staged, :boolean, default: false
field :purchased_on, :date
belongs_to :ammo_type, AmmoType belongs_to :ammo_type, AmmoType
belongs_to :container, Container belongs_to :container, Container
@ -47,7 +36,6 @@ defmodule Cannery.Ammo.AmmoGroup do
notes: String.t() | nil, notes: String.t() | nil,
price_paid: float() | nil, price_paid: float() | nil,
staged: boolean(), staged: boolean(),
purchased_on: Date.t(),
ammo_type: AmmoType.t() | nil, ammo_type: AmmoType.t() | nil,
ammo_type_id: AmmoType.id(), ammo_type_id: AmmoType.id(),
container: Container.t() | nil, container: Container.t() | nil,
@ -59,7 +47,6 @@ defmodule Cannery.Ammo.AmmoGroup do
} }
@type new_ammo_group :: %AmmoGroup{} @type new_ammo_group :: %AmmoGroup{}
@type id :: UUID.t() @type id :: UUID.t()
@type changeset :: Changeset.t(t() | new_ammo_group())
@doc false @doc false
@spec create_changeset( @spec create_changeset(
@ -68,7 +55,7 @@ defmodule Cannery.Ammo.AmmoGroup do
Container.t() | nil, Container.t() | nil,
User.t(), User.t(),
attrs :: map() attrs :: map()
) :: changeset() ) :: Changeset.t(new_ammo_group())
def create_changeset( def create_changeset(
ammo_group, ammo_group,
%AmmoType{id: ammo_type_id}, %AmmoType{id: ammo_type_id},
@ -82,9 +69,9 @@ defmodule Cannery.Ammo.AmmoGroup do
|> change(ammo_type_id: ammo_type_id) |> change(ammo_type_id: ammo_type_id)
|> change(user_id: user_id) |> change(user_id: user_id)
|> change(container_id: container_id) |> change(container_id: container_id)
|> cast(attrs, [:count, :price_paid, :notes, :staged, :purchased_on]) |> cast(attrs, [:count, :price_paid, :notes, :staged])
|> validate_number(:count, greater_than: 0) |> validate_number(:count, greater_than: 0)
|> validate_required([:count, :staged, :purchased_on, :ammo_type_id, :container_id, :user_id]) |> validate_required([:count, :staged, :ammo_type_id, :container_id, :user_id])
end end
@doc """ @doc """
@ -98,30 +85,21 @@ defmodule Cannery.Ammo.AmmoGroup do
end end
@doc false @doc false
@spec update_changeset(t() | new_ammo_group(), attrs :: map(), User.t()) :: changeset() @spec update_changeset(t() | new_ammo_group(), attrs :: map()) ::
def update_changeset(ammo_group, attrs, user) do Changeset.t(t() | new_ammo_group())
def update_changeset(ammo_group, attrs) do
ammo_group ammo_group
|> cast(attrs, [:count, :price_paid, :notes, :staged, :purchased_on, :container_id]) |> cast(attrs, [:count, :price_paid, :notes, :staged])
|> validate_number(:count, greater_than_or_equal_to: 0) |> validate_number(:count, greater_than_or_equal_to: 0)
|> validate_container_id(user) |> validate_required([:count, :staged])
|> validate_required([:count, :staged, :purchased_on, :container_id])
end
defp validate_container_id(changeset, user) do
container_id = changeset |> Changeset.get_field(:container_id)
if container_id do
Containers.get_container!(container_id, user)
end
changeset
end end
@doc """ @doc """
This range changeset is used when "using up" ammo groups, and allows for This range changeset is used when "using up" ammo groups, and allows for
updating the count to 0 updating the count to 0
""" """
@spec range_changeset(t() | new_ammo_group(), attrs :: map()) :: changeset() @spec range_changeset(t() | new_ammo_group(), attrs :: map()) ::
Changeset.t(t() | new_ammo_group())
def range_changeset(ammo_group, attrs) do def range_changeset(ammo_group, attrs) do
ammo_group ammo_group
|> cast(attrs, [:count, :staged]) |> cast(attrs, [:count, :staged])

View File

@ -11,31 +11,6 @@ defmodule Cannery.Ammo.AmmoType do
alias Cannery.Ammo.{AmmoGroup, AmmoType} alias Cannery.Ammo.{AmmoGroup, AmmoType}
alias Ecto.{Changeset, UUID} alias Ecto.{Changeset, UUID}
@derive {Jason.Encoder,
only: [
:id,
:name,
:desc,
:bullet_type,
:bullet_core,
:cartridge,
:caliber,
:case_material,
:jacket_type,
:muzzle_velocity,
:powder_type,
:powder_grains_per_charge,
:grains,
:pressure,
:primer_type,
:firing_type,
:tracer,
:incendiary,
:blank,
:corrosive,
:manufacturer,
:upc
]}
@primary_key {:id, :binary_id, autogenerate: true} @primary_key {:id, :binary_id, autogenerate: true}
@foreign_key_type :binary_id @foreign_key_type :binary_id
schema "ammo_types" do schema "ammo_types" do
@ -102,7 +77,6 @@ defmodule Cannery.Ammo.AmmoType do
} }
@type new_ammo_type :: %AmmoType{} @type new_ammo_type :: %AmmoType{}
@type id :: UUID.t() @type id :: UUID.t()
@type changeset :: Changeset.t(t() | new_ammo_type())
@spec changeset_fields() :: [atom()] @spec changeset_fields() :: [atom()]
defp changeset_fields, defp changeset_fields,
@ -131,7 +105,8 @@ defmodule Cannery.Ammo.AmmoType do
] ]
@doc false @doc false
@spec create_changeset(new_ammo_type(), User.t(), attrs :: map()) :: changeset() @spec create_changeset(new_ammo_type(), User.t(), attrs :: map()) ::
Changeset.t(new_ammo_type())
def create_changeset(ammo_type, %User{id: user_id}, attrs) do def create_changeset(ammo_type, %User{id: user_id}, attrs) do
ammo_type ammo_type
|> change(user_id: user_id) |> change(user_id: user_id)
@ -140,7 +115,8 @@ defmodule Cannery.Ammo.AmmoType do
end end
@doc false @doc false
@spec update_changeset(t() | new_ammo_type(), attrs :: map()) :: changeset() @spec update_changeset(t() | new_ammo_type(), attrs :: map()) ::
Changeset.t(t() | new_ammo_type())
def update_changeset(ammo_type, attrs) do def update_changeset(ammo_type, attrs) do
ammo_type ammo_type
|> cast(attrs, changeset_fields()) |> cast(attrs, changeset_fields())

View File

@ -4,7 +4,6 @@ defmodule Cannery.Application do
@moduledoc false @moduledoc false
use Application use Application
alias Cannery.Logger
@impl true @impl true
def start(_type, _args) do def start(_type, _args) do
@ -18,24 +17,16 @@ defmodule Cannery.Application do
# Start the Endpoint (http/https) # Start the Endpoint (http/https)
CanneryWeb.Endpoint, CanneryWeb.Endpoint,
# Add Oban # Add Oban
{Oban, oban_config()}, {Oban, oban_config()}
Cannery.Repo.Migrator
# Start a worker by calling: Cannery.Worker.start_link(arg) # Start a worker by calling: Cannery.Worker.start_link(arg)
# {Cannery.Worker, arg} # {Cannery.Worker, arg}
] ]
# Oban events logging https://hexdocs.pm/oban/Oban.html#module-reporting-errors # Automatically migrate on start in prod
:ok = children =
:telemetry.attach_many( if Application.get_env(:cannery, Cannery.Application, automigrate: false)[:automigrate],
"oban-logger", do: children ++ [Cannery.Repo.Migrator],
[ else: children
[:oban, :job, :exception],
[:oban, :job, :start],
[:oban, :job, :stop]
],
&Logger.handle_event/4,
[]
)
# See https://hexdocs.pm/elixir/Supervisor.html # See https://hexdocs.pm/elixir/Supervisor.html
# for other strategies and supported options # for other strategies and supported options

View File

@ -17,55 +17,16 @@ defmodule Cannery.Containers do
iex> list_containers(%User{id: 123}) iex> list_containers(%User{id: 123})
[%Container{}, ...] [%Container{}, ...]
iex> list_containers("cool", %User{id: 123})
[%Container{name: "my cool container"}, ...]
""" """
@spec list_containers(User.t()) :: [Container.t()] @spec list_containers(User.t()) :: [Container.t()]
@spec list_containers(search :: nil | String.t(), User.t()) :: [Container.t()] def list_containers(%User{id: user_id}) do
def list_containers(search \\ nil, %User{id: user_id}) do Repo.all(
from(c in Container, from c in Container,
as: :c, left_join: t in assoc(c, :tags),
left_join: t in assoc(c, :tags), left_join: ag in assoc(c, :ammo_groups),
as: :t, where: c.user_id == ^user_id,
left_join: ag in assoc(c, :ammo_groups), order_by: c.name,
as: :ag, preload: [tags: t, ammo_groups: ag]
where: c.user_id == ^user_id,
order_by: c.name,
preload: [tags: t, ammo_groups: ag]
)
|> list_containers_search(search)
|> Repo.all()
end
defp list_containers_search(query, nil), do: query
defp list_containers_search(query, ""), do: query
defp list_containers_search(query, search) do
trimmed_search = String.trim(search)
query
|> where(
[c: c, t: t],
fragment(
"? @@ websearch_to_tsquery('english', ?)",
c.search,
^trimmed_search
) or
fragment(
"? @@ websearch_to_tsquery('english', ?)",
t.search,
^trimmed_search
)
)
|> order_by(
[c: c],
desc:
fragment(
"ts_rank_cd(?, websearch_to_tsquery('english', ?), 4)",
c.search,
^trimmed_search
)
) )
end end
@ -128,7 +89,7 @@ defmodule Cannery.Containers do
""" """
@spec create_container(attrs :: map(), User.t()) :: @spec create_container(attrs :: map(), User.t()) ::
{:ok, Container.t()} | {:error, Container.changeset()} {:ok, Container.t()} | {:error, Changeset.t(Container.new_container())}
def create_container(attrs, %User{} = user) do def create_container(attrs, %User{} = user) do
%Container{} |> Container.create_changeset(user, attrs) |> Repo.insert() %Container{} |> Container.create_changeset(user, attrs) |> Repo.insert()
end end
@ -146,7 +107,7 @@ defmodule Cannery.Containers do
""" """
@spec update_container(Container.t(), User.t(), attrs :: map()) :: @spec update_container(Container.t(), User.t(), attrs :: map()) ::
{:ok, Container.t()} | {:error, Container.changeset()} {:ok, Container.t()} | {:error, Changeset.t(Container.t())}
def update_container(%Container{user_id: user_id} = container, %User{id: user_id}, attrs) do def update_container(%Container{user_id: user_id} = container, %User{id: user_id}, attrs) do
container |> Container.update_changeset(attrs) |> Repo.update() container |> Container.update_changeset(attrs) |> Repo.update()
end end
@ -164,7 +125,7 @@ defmodule Cannery.Containers do
""" """
@spec delete_container(Container.t(), User.t()) :: @spec delete_container(Container.t(), User.t()) ::
{:ok, Container.t()} | {:error, Container.changeset()} {:ok, Container.t()} | {:error, Changeset.t(Container.t())}
def delete_container(%Container{user_id: user_id} = container, %User{id: user_id}) do def delete_container(%Container{user_id: user_id} = container, %User{id: user_id}) do
Repo.one( Repo.one(
from ag in AmmoGroup, from ag in AmmoGroup,
@ -251,7 +212,6 @@ defmodule Cannery.Containers do
container container
|> Repo.preload(:ammo_groups) |> Repo.preload(:ammo_groups)
|> Map.fetch!(:ammo_groups) |> Map.fetch!(:ammo_groups)
|> Enum.reject(fn %{count: count} -> count == 0 end)
|> Enum.count() |> Enum.count()
end end

View File

@ -9,15 +9,6 @@ defmodule Cannery.Containers.Container do
alias Cannery.Containers.{Container, ContainerTag} alias Cannery.Containers.{Container, ContainerTag}
alias Cannery.{Accounts.User, Ammo.AmmoGroup, Tags.Tag} alias Cannery.{Accounts.User, Ammo.AmmoGroup, Tags.Tag}
@derive {Jason.Encoder,
only: [
:id,
:name,
:desc,
:location,
:type,
:tags
]}
@primary_key {:id, :binary_id, autogenerate: true} @primary_key {:id, :binary_id, autogenerate: true}
@foreign_key_type :binary_id @foreign_key_type :binary_id
schema "containers" do schema "containers" do
@ -49,10 +40,10 @@ defmodule Cannery.Containers.Container do
} }
@type new_container :: %Container{} @type new_container :: %Container{}
@type id :: UUID.t() @type id :: UUID.t()
@type changeset :: Changeset.t(t() | new_container())
@doc false @doc false
@spec create_changeset(new_container(), User.t(), attrs :: map()) :: changeset() @spec create_changeset(new_container(), User.t(), attrs :: map()) ::
Changeset.t(new_container())
def create_changeset(container, %User{id: user_id}, attrs) do def create_changeset(container, %User{id: user_id}, attrs) do
container container
|> change(user_id: user_id) |> change(user_id: user_id)
@ -61,7 +52,8 @@ defmodule Cannery.Containers.Container do
end end
@doc false @doc false
@spec update_changeset(t() | new_container(), attrs :: map()) :: changeset() @spec update_changeset(t() | new_container(), attrs :: map()) ::
Changeset.t(t() | new_container())
def update_changeset(container, attrs) do def update_changeset(container, attrs) do
container container
|> cast(attrs, [:name, :desc, :type, :location]) |> cast(attrs, [:name, :desc, :type, :location])

View File

@ -29,10 +29,10 @@ defmodule Cannery.Containers.ContainerTag do
} }
@type new_container_tag :: %ContainerTag{} @type new_container_tag :: %ContainerTag{}
@type id :: UUID.t() @type id :: UUID.t()
@type changeset :: Changeset.t(t() | new_container_tag())
@doc false @doc false
@spec create_changeset(new_container_tag(), Tag.t(), Container.t()) :: changeset() @spec create_changeset(new_container_tag(), Tag.t(), Container.t()) ::
Changeset.t(new_container_tag())
def create_changeset( def create_changeset(
container_tag, container_tag,
%Tag{id: tag_id, user_id: user_id}, %Tag{id: tag_id, user_id: user_id},

156
lib/cannery/invites.ex Normal file
View File

@ -0,0 +1,156 @@
defmodule Cannery.Invites do
@moduledoc """
The Invites context.
"""
import Ecto.Query, warn: false
alias Cannery.{Accounts.User, Invites.Invite, Repo}
alias Ecto.Changeset
@invite_token_length 20
@doc """
Returns the list of invites.
## Examples
iex> list_invites(%User{id: 123, role: :admin})
[%Invite{}, ...]
"""
@spec list_invites(User.t()) :: [Invite.t()]
def list_invites(%User{role: :admin}) do
Repo.all(from i in Invite, order_by: i.name)
end
@doc """
Gets a single invite.
Raises `Ecto.NoResultsError` if the Invite does not exist.
## Examples
iex> get_invite!(123, %User{id: 123, role: :admin})
%Invite{}
iex> get_invite!(456, %User{id: 123, role: :admin})
** (Ecto.NoResultsError)
"""
@spec get_invite!(Invite.id(), User.t()) :: Invite.t()
def get_invite!(id, %User{role: :admin}) do
Repo.get!(Invite, id)
end
@doc """
Returns a valid invite or nil based on the attempted token
## Examples
iex> get_invite_by_token("valid_token")
%Invite{}
iex> get_invite_by_token("invalid_token")
nil
"""
@spec get_invite_by_token(token :: String.t() | nil) :: Invite.t() | nil
def get_invite_by_token(nil), do: nil
def get_invite_by_token(""), do: nil
def get_invite_by_token(token) do
Repo.one(
from(i in Invite,
where: i.token == ^token and i.disabled_at |> is_nil()
)
)
end
@doc """
Uses invite by decrementing uses_left, or marks invite invalid if it's been
completely used.
"""
@spec use_invite!(Invite.t()) :: Invite.t()
def use_invite!(%Invite{uses_left: nil} = invite), do: invite
def use_invite!(%Invite{uses_left: uses_left} = invite) do
new_uses_left = uses_left - 1
attrs =
if new_uses_left <= 0 do
now = NaiveDateTime.utc_now() |> NaiveDateTime.truncate(:second)
%{"uses_left" => 0, "disabled_at" => now}
else
%{"uses_left" => new_uses_left}
end
invite |> Invite.update_changeset(attrs) |> Repo.update!()
end
@doc """
Creates a invite.
## Examples
iex> create_invite(%User{id: 123, role: :admin}, %{field: value})
{:ok, %Invite{}}
iex> create_invite(%User{id: 123, role: :admin}, %{field: bad_value})
{:error, %Changeset{}}
"""
@spec create_invite(User.t(), attrs :: map()) ::
{:ok, Invite.t()} | {:error, Changeset.t(Invite.new_invite())}
def create_invite(%User{role: :admin} = user, attrs) do
token =
:crypto.strong_rand_bytes(@invite_token_length)
|> Base.url_encode64()
|> binary_part(0, @invite_token_length)
%Invite{} |> Invite.create_changeset(user, token, attrs) |> Repo.insert()
end
@doc """
Updates a invite.
## Examples
iex> update_invite(invite, %{field: new_value}, %User{id: 123, role: :admin})
{:ok, %Invite{}}
iex> update_invite(invite, %{field: bad_value}, %User{id: 123, role: :admin})
{:error, %Changeset{}}
"""
@spec update_invite(Invite.t(), attrs :: map(), User.t()) ::
{:ok, Invite.t()} | {:error, Changeset.t(Invite.t())}
def update_invite(invite, attrs, %User{role: :admin}),
do: invite |> Invite.update_changeset(attrs) |> Repo.update()
@doc """
Deletes a invite.
## Examples
iex> delete_invite(invite, %User{id: 123, role: :admin})
{:ok, %Invite{}}
iex> delete_invite(invite, %User{id: 123, role: :admin})
{:error, %Changeset{}}
"""
@spec delete_invite(Invite.t(), User.t()) ::
{:ok, Invite.t()} | {:error, Changeset.t(Invite.t())}
def delete_invite(invite, %User{role: :admin}), do: invite |> Repo.delete()
@doc """
Deletes a invite.
## Examples
iex> delete_invite(invite, %User{id: 123, role: :admin})
%Invite{}
"""
@spec delete_invite!(Invite.t(), User.t()) :: Invite.t()
def delete_invite!(invite, %User{role: :admin}), do: invite |> Repo.delete!()
end

View File

@ -1,4 +1,4 @@
defmodule Cannery.Accounts.Invite do defmodule Cannery.Invites.Invite do
@moduledoc """ @moduledoc """
An invite, created by an admin to allow someone to join their instance. An An invite, created by an admin to allow someone to join their instance. An
invite can be enabled or disabled, and can have an optional number of uses if invite can be enabled or disabled, and can have an optional number of uses if
@ -7,8 +7,8 @@ defmodule Cannery.Accounts.Invite do
use Ecto.Schema use Ecto.Schema
import Ecto.Changeset import Ecto.Changeset
alias Cannery.Accounts.User alias Ecto.{Changeset, UUID}
alias Ecto.{Association, Changeset, UUID} alias Cannery.{Accounts.User, Invites.Invite}
@primary_key {:id, :binary_id, autogenerate: true} @primary_key {:id, :binary_id, autogenerate: true}
@foreign_key_type :binary_id @foreign_key_type :binary_id
@ -18,42 +18,38 @@ defmodule Cannery.Accounts.Invite do
field :uses_left, :integer, default: nil field :uses_left, :integer, default: nil
field :disabled_at, :naive_datetime field :disabled_at, :naive_datetime
belongs_to :created_by, User belongs_to :user, User
has_many :users, User
timestamps() timestamps()
end end
@type t :: %__MODULE__{ @type t :: %Invite{
id: id(), id: id(),
name: String.t(), name: String.t(),
token: token(), token: String.t(),
uses_left: integer() | nil, uses_left: integer() | nil,
disabled_at: NaiveDateTime.t(), disabled_at: NaiveDateTime.t(),
created_by: User.t() | nil | Association.NotLoaded.t(), user: User.t(),
created_by_id: User.id() | nil, user_id: User.id(),
users: [User.t()] | Association.NotLoaded.t(),
inserted_at: NaiveDateTime.t(), inserted_at: NaiveDateTime.t(),
updated_at: NaiveDateTime.t() updated_at: NaiveDateTime.t()
} }
@type new_invite :: %__MODULE__{} @type new_invite :: %Invite{}
@type id :: UUID.t() @type id :: UUID.t()
@type changeset :: Changeset.t(t() | new_invite())
@type token :: String.t()
@doc false @doc false
@spec create_changeset(User.t(), token(), attrs :: map()) :: changeset() @spec create_changeset(new_invite(), User.t(), token :: binary(), attrs :: map()) ::
def create_changeset(%User{id: user_id}, token, attrs) do Changeset.t(new_invite())
%__MODULE__{} def create_changeset(invite, %User{id: user_id}, token, attrs) do
|> change(token: token, created_by_id: user_id) invite
|> change(token: token, user_id: user_id)
|> cast(attrs, [:name, :uses_left, :disabled_at]) |> cast(attrs, [:name, :uses_left, :disabled_at])
|> validate_required([:name, :token, :created_by_id]) |> validate_required([:name, :token, :user_id])
|> validate_number(:uses_left, greater_than_or_equal_to: 0) |> validate_number(:uses_left, greater_than_or_equal_to: 0)
end end
@doc false @doc false
@spec update_changeset(t() | new_invite(), attrs :: map()) :: changeset() @spec update_changeset(t() | new_invite(), attrs :: map()) :: Changeset.t(t() | new_invite())
def update_changeset(invite, attrs) do def update_changeset(invite, attrs) do
invite invite
|> cast(attrs, [:name, :uses_left, :disabled_at]) |> cast(attrs, [:name, :uses_left, :disabled_at])

View File

@ -1,63 +0,0 @@
defmodule Cannery.Logger do
@moduledoc """
Custom logger for telemetry events
Oban implementation taken from
https://hexdocs.pm/oban/Oban.html#module-reporting-errors
"""
require Logger
def handle_event([:oban, :job, :exception], measure, %{stacktrace: stacktrace} = meta, _config) do
data =
get_oban_job_data(meta, measure)
|> Map.put(:stacktrace, Exception.format_stacktrace(stacktrace))
|> pretty_encode()
Logger.error(meta.reason, data: data)
end
def handle_event([:oban, :job, :start], measure, meta, _config) do
data = get_oban_job_data(meta, measure) |> pretty_encode()
Logger.info("Started oban job", data: data)
end
def handle_event([:oban, :job, :stop], measure, meta, _config) do
data = get_oban_job_data(meta, measure) |> pretty_encode()
Logger.info("Finished oban job", data: data)
end
def handle_event([:oban, :job, unhandled_event], measure, meta, _config) do
data =
get_oban_job_data(meta, measure)
|> Map.put(:event, unhandled_event)
|> pretty_encode()
Logger.warning("Unhandled oban job event", data: data)
end
def handle_event(unhandled_event, measure, meta, config) do
data =
pretty_encode(%{
event: unhandled_event,
meta: meta,
measurements: measure,
config: config
})
Logger.warning("Unhandled telemetry event", data: data)
end
defp get_oban_job_data(%{job: job}, measure) do
%{
job: job |> Map.take([:id, :args, :meta, :queue, :worker]),
measurements: measure
}
end
defp pretty_encode(data) do
data
|> Jason.encode!()
|> Jason.Formatter.pretty_print()
end
end

View File

@ -9,9 +9,7 @@ defmodule Cannery.Release do
def rollback(repo, version) do def rollback(repo, version) do
load_app() load_app()
{:ok, _fun, _opts} = Ecto.Migrator.with_repo(repo, &Ecto.Migrator.run(&1, :down, to: version))
{:ok, _fun_return, _apps} =
Ecto.Migrator.with_repo(repo, &Ecto.Migrator.run(&1, :down, to: version))
end end
defp load_app do defp load_app do
@ -22,8 +20,7 @@ defmodule Cannery.Release do
load_app() load_app()
for repo <- Application.fetch_env!(@app, :ecto_repos) do for repo <- Application.fetch_env!(@app, :ecto_repos) do
{:ok, _fun_return, _apps} = {:ok, _fun, _opts} = Ecto.Migrator.with_repo(repo, &Ecto.Migrator.run(&1, :up, all: true))
Ecto.Migrator.with_repo(repo, &Ecto.Migrator.run(&1, :up, all: true))
end end
end end
end end

View File

@ -1,6 +1,6 @@
defmodule Cannery.Repo.Migrator do defmodule Cannery.Repo.Migrator do
@moduledoc """ @moduledoc """
Genserver to automatically perform all migration on app start Genserver to automatically run migrations in prod env
""" """
use GenServer use GenServer
@ -11,15 +11,12 @@ defmodule Cannery.Repo.Migrator do
end end
def init(_opts) do def init(_opts) do
{:ok, if(automigrate_enabled?(), do: migrate!())} migrate!()
{:ok, nil}
end end
def migrate! do def migrate! do
path = Application.app_dir(:cannery, "priv/repo/migrations") path = Application.app_dir(:cannery, "priv/repo/migrations")
Ecto.Migrator.run(Cannery.Repo, path, :up, all: true) Ecto.Migrator.run(Cannery.Repo, path, :up, all: true)
end end
defp automigrate_enabled? do
Application.get_env(:cannery, Cannery.Application, automigrate: false)[:automigrate]
end
end end

View File

@ -6,6 +6,7 @@ defmodule Cannery.Tags do
import Ecto.Query, warn: false import Ecto.Query, warn: false
import CanneryWeb.Gettext import CanneryWeb.Gettext
alias Cannery.{Accounts.User, Repo, Tags.Tag} alias Cannery.{Accounts.User, Repo, Tags.Tag}
alias Ecto.Changeset
@doc """ @doc """
Returns the list of tags. Returns the list of tags.
@ -15,38 +16,11 @@ defmodule Cannery.Tags do
iex> list_tags(%User{id: 123}) iex> list_tags(%User{id: 123})
[%Tag{}, ...] [%Tag{}, ...]
iex> list_tags("cool", %User{id: 123})
[%Tag{name: "my cool tag"}, ...]
""" """
@spec list_tags(User.t()) :: [Tag.t()] @spec list_tags(User.t()) :: [Tag.t()]
@spec list_tags(search :: nil | String.t(), User.t()) :: [Tag.t()] def list_tags(%{id: user_id}),
def list_tags(search \\ nil, user)
def list_tags(search, %{id: user_id}) when search |> is_nil() or search == "",
do: Repo.all(from t in Tag, where: t.user_id == ^user_id, order_by: t.name) do: Repo.all(from t in Tag, where: t.user_id == ^user_id, order_by: t.name)
def list_tags(search, %{id: user_id}) when search |> is_binary() do
trimmed_search = String.trim(search)
Repo.all(
from t in Tag,
where: t.user_id == ^user_id,
where:
fragment(
"search @@ websearch_to_tsquery('english', ?)",
^trimmed_search
),
order_by: {
:desc,
fragment(
"ts_rank_cd(search, websearch_to_tsquery('english', ?), 4)",
^trimmed_search
)
}
)
end
@doc """ @doc """
Gets a single tag. Gets a single tag.
@ -99,7 +73,7 @@ defmodule Cannery.Tags do
""" """
@spec create_tag(attrs :: map(), User.t()) :: @spec create_tag(attrs :: map(), User.t()) ::
{:ok, Tag.t()} | {:error, Tag.changeset()} {:ok, Tag.t()} | {:error, Changeset.t(Tag.new_tag())}
def create_tag(attrs, %User{} = user), def create_tag(attrs, %User{} = user),
do: %Tag{} |> Tag.create_changeset(user, attrs) |> Repo.insert() do: %Tag{} |> Tag.create_changeset(user, attrs) |> Repo.insert()
@ -116,7 +90,7 @@ defmodule Cannery.Tags do
""" """
@spec update_tag(Tag.t(), attrs :: map(), User.t()) :: @spec update_tag(Tag.t(), attrs :: map(), User.t()) ::
{:ok, Tag.t()} | {:error, Tag.changeset()} {:ok, Tag.t()} | {:error, Changeset.t(Tag.t())}
def update_tag(%Tag{user_id: user_id} = tag, attrs, %User{id: user_id}), def update_tag(%Tag{user_id: user_id} = tag, attrs, %User{id: user_id}),
do: tag |> Tag.update_changeset(attrs) |> Repo.update() do: tag |> Tag.update_changeset(attrs) |> Repo.update()
@ -132,7 +106,7 @@ defmodule Cannery.Tags do
{:error, %Changeset{}} {:error, %Changeset{}}
""" """
@spec delete_tag(Tag.t(), User.t()) :: {:ok, Tag.t()} | {:error, Tag.changeset()} @spec delete_tag(Tag.t(), User.t()) :: {:ok, Tag.t()} | {:error, Changeset.t(Tag.t())}
def delete_tag(%Tag{user_id: user_id} = tag, %User{id: user_id}), do: tag |> Repo.delete() def delete_tag(%Tag{user_id: user_id} = tag, %User{id: user_id}), do: tag |> Repo.delete()
@doc """ @doc """
@ -146,4 +120,18 @@ defmodule Cannery.Tags do
""" """
@spec delete_tag!(Tag.t(), User.t()) :: Tag.t() @spec delete_tag!(Tag.t(), User.t()) :: Tag.t()
def delete_tag!(%Tag{user_id: user_id} = tag, %User{id: user_id}), do: tag |> Repo.delete!() def delete_tag!(%Tag{user_id: user_id} = tag, %User{id: user_id}), do: tag |> Repo.delete!()
@doc """
Get a random tag bg_color in `#ffffff` hex format
## Examples
iex> random_color()
"#cc0066"
"""
@spec random_bg_color() :: <<_::7>>
def random_bg_color do
["#cc0066", "#ff6699", "#6666ff", "#0066cc", "#00cc66", "#669900", "#ff9900", "#996633"]
|> Enum.random()
end
end end

View File

@ -9,13 +9,6 @@ defmodule Cannery.Tags.Tag do
alias Ecto.{Changeset, UUID} alias Ecto.{Changeset, UUID}
alias Cannery.{Accounts.User, Tags.Tag} alias Cannery.{Accounts.User, Tags.Tag}
@derive {Jason.Encoder,
only: [
:id,
:name,
:bg_color,
:text_color
]}
@primary_key {:id, :binary_id, autogenerate: true} @primary_key {:id, :binary_id, autogenerate: true}
@foreign_key_type :binary_id @foreign_key_type :binary_id
schema "tags" do schema "tags" do
@ -40,10 +33,9 @@ defmodule Cannery.Tags.Tag do
} }
@type new_tag() :: %Tag{} @type new_tag() :: %Tag{}
@type id() :: UUID.t() @type id() :: UUID.t()
@type changeset() :: Changeset.t(t() | new_tag())
@doc false @doc false
@spec create_changeset(new_tag(), User.t(), attrs :: map()) :: changeset() @spec create_changeset(new_tag(), User.t(), attrs :: map()) :: Changeset.t(new_tag())
def create_changeset(tag, %User{id: user_id}, attrs) do def create_changeset(tag, %User{id: user_id}, attrs) do
tag tag
|> change(user_id: user_id) |> change(user_id: user_id)
@ -52,7 +44,7 @@ defmodule Cannery.Tags.Tag do
end end
@doc false @doc false
@spec update_changeset(t() | new_tag(), attrs :: map()) :: changeset() @spec update_changeset(t() | new_tag(), attrs :: map()) :: Changeset.t(t() | new_tag())
def update_changeset(tag, attrs) do def update_changeset(tag, attrs) do
tag tag
|> cast(attrs, [:name, :bg_color, :text_color]) |> cast(attrs, [:name, :bg_color, :text_color])

View File

@ -72,14 +72,16 @@ defmodule CanneryWeb do
quote do quote do
use Phoenix.Router use Phoenix.Router
import Phoenix.{Controller, LiveView.Router}
# credo:disable-for-next-line Credo.Check.Consistency.MultiAliasImportRequireUse # credo:disable-for-next-line Credo.Check.Consistency.MultiAliasImportRequireUse
import Plug.Conn import Plug.Conn
import Phoenix.Controller
import Phoenix.LiveView.Router
end end
end end
def channel do def channel do
quote do quote do
# credo:disable-for-next-line Credo.Check.Consistency.MultiAliasImportRequireUse
use Phoenix.Channel use Phoenix.Channel
# credo:disable-for-next-line Credo.Check.Consistency.MultiAliasImportRequireUse # credo:disable-for-next-line Credo.Check.Consistency.MultiAliasImportRequireUse
import CanneryWeb.Gettext import CanneryWeb.Gettext
@ -93,11 +95,15 @@ defmodule CanneryWeb do
use Phoenix.HTML use Phoenix.HTML
# Import LiveView and .heex helpers (live_render, live_patch, <.form>, etc) # Import LiveView and .heex helpers (live_render, live_patch, <.form>, etc)
# Import basic rendering functionality (render, render_layout, etc) # credo:disable-for-next-line Credo.Check.Consistency.MultiAliasImportRequireUse
import CanneryWeb.{ErrorHelpers, Gettext, LiveHelpers, ViewHelpers} import Phoenix.LiveView.Helpers
import Phoenix.{Component, View}
alias CanneryWeb.Endpoint # Import basic rendering functionality (render, render_layout, etc)
# credo:disable-for-next-line Credo.Check.Consistency.MultiAliasImportRequireUse
import Phoenix.View
# credo:disable-for-next-line Credo.Check.Consistency.MultiAliasImportRequireUse
import CanneryWeb.{ErrorHelpers, Gettext, LiveHelpers, ViewHelpers}
alias CanneryWeb.Router.Helpers, as: Routes alias CanneryWeb.Router.Helpers, as: Routes
end end
end end

View File

@ -5,7 +5,7 @@ defmodule CanneryWeb.Components.AddShotGroupComponent do
use CanneryWeb, :live_component use CanneryWeb, :live_component
alias Cannery.{Accounts.User, ActivityLog, ActivityLog.ShotGroup, Ammo.AmmoGroup} alias Cannery.{Accounts.User, ActivityLog, ActivityLog.ShotGroup, Ammo.AmmoGroup}
alias Phoenix.LiveView.{JS, Socket} alias Phoenix.LiveView.Socket
@impl true @impl true
@spec update( @spec update(
@ -54,7 +54,7 @@ defmodule CanneryWeb.Components.AddShotGroupComponent do
|> case do |> case do
{:ok, _shot_group} -> {:ok, _shot_group} ->
prompt = dgettext("prompts", "Shots recorded successfully") prompt = dgettext("prompts", "Shots recorded successfully")
socket |> put_flash(:info, prompt) |> push_navigate(to: return_to) socket |> put_flash(:info, prompt) |> push_redirect(to: return_to)
{:error, %Ecto.Changeset{} = changeset} -> {:error, %Ecto.Changeset{} = changeset} ->
socket |> assign(changeset: changeset) socket |> assign(changeset: changeset)

View File

@ -4,7 +4,7 @@
</h2> </h2>
<.form <.form
:let={f} let={f}
for={@changeset} for={@changeset}
id="shot-group-form" id="shot-group-form"
class="flex flex-col space-y-4 sm:space-y-0 sm:grid sm:grid-cols-3 sm:gap-4 justify-center items-center" class="flex flex-col space-y-4 sm:space-y-0 sm:grid sm:grid-cols-3 sm:gap-4 justify-center items-center"
@ -12,27 +12,19 @@
phx-change="validate" phx-change="validate"
phx-submit="save" phx-submit="save"
> >
<div <%= if @changeset.action && not @changeset.valid? do %>
:if={@changeset.action && not @changeset.valid?()} <div class="invalid-feedback col-span-3 text-center">
class="invalid-feedback col-span-3 text-center" <%= changeset_errors(@changeset) %>
> </div>
<%= changeset_errors(@changeset) %> <% end %>
</div>
<%= label(f, :ammo_left, gettext("Rounds left"), class: "title text-lg text-primary-600") %> <%= label(f, :ammo_left, gettext("Rounds left"), class: "title text-lg text-primary-600") %>
<%= number_input(f, :ammo_left, <%= number_input(f, :ammo_left,
min: 0, min: 0,
max: @ammo_group.count - 1, max: @ammo_group.count - 1,
placeholder: gettext("Rounds left"), placeholder: 0,
class: "input input-primary" class: "input input-primary col-span-2"
) %> ) %>
<button
type="button"
class="mx-2 my-1 text-sm btn btn-primary"
phx-click={JS.dispatch("cannery:set-zero", to: "#shot-group-form_ammo_left")}
>
<%= gettext("Used up!") %>
</button>
<%= error_tag(f, :ammo_left, "col-span-3") %> <%= error_tag(f, :ammo_left, "col-span-3") %>
<%= label(f, :notes, gettext("Notes"), class: "title text-lg text-primary-600") %> <%= label(f, :notes, gettext("Notes"), class: "title text-lg text-primary-600") %>
@ -43,7 +35,7 @@
) %> ) %>
<%= error_tag(f, :notes, "col-span-3") %> <%= error_tag(f, :notes, "col-span-3") %>
<%= label(f, :date, gettext("Date"), class: "title text-lg text-primary-600") %> <%= label(f, :date, gettext("Date (UTC)"), class: "title text-lg text-primary-600") %>
<%= date_input(f, :date, <%= date_input(f, :date,
class: "input input-primary col-span-2", class: "input input-primary col-span-2",
phx_update: "ignore", phx_update: "ignore",

View File

@ -4,63 +4,42 @@ defmodule CanneryWeb.Components.AmmoGroupCard do
""" """
use CanneryWeb, :component use CanneryWeb, :component
alias Cannery.{Ammo, Ammo.AmmoGroup, Repo} alias Cannery.Repo
alias CanneryWeb.Endpoint alias CanneryWeb.Endpoint
attr :ammo_group, AmmoGroup, required: true def ammo_group_card(assigns) do
attr :show_container, :boolean, default: false assigns = assigns |> assign(:ammo_group, assigns.ammo_group |> Repo.preload(:ammo_type))
slot(:inner_block)
def ammo_group_card(%{ammo_group: ammo_group} = assigns) do
assigns =
%{show_container: show_container} = assigns |> assign_new(:show_container, fn -> false end)
preloads = if show_container, do: [:ammo_type, :container], else: [:ammo_type]
ammo_group = ammo_group |> Repo.preload(preloads)
assigns = assigns |> assign(:ammo_group, ammo_group)
~H""" ~H"""
<div <div
id={"ammo_group-#{@ammo_group.id}"} id={"ammo_group-#{@ammo_group.id}"}
class="mx-4 my-2 px-8 py-4 class="mx-4 my-2 px-8 py-4 flex flex-col justify-center items-center
flex flex-col justify-center items-center border border-gray-400 rounded-lg shadow-lg hover:shadow-md
border border-gray-400 rounded-lg shadow-lg hover:shadow-md transition-all duration-300 ease-in-out"
transition-all duration-300 ease-in-out"
> >
<.link navigate={Routes.ammo_group_show_path(Endpoint, :show, @ammo_group)} class="mb-2 link"> <%= live_redirect to: Routes.ammo_group_show_path(Endpoint, :show, @ammo_group),
class: "mb-2 link" do %>
<h1 class="title text-xl title-primary-500"> <h1 class="title text-xl title-primary-500">
<%= @ammo_group.ammo_type.name %> <%= @ammo_group.ammo_type.name %>
</h1> </h1>
</.link> <% end %>
<div class="flex flex-col justify-center items-center"> <div class="flex flex-col justify-center items-center">
<span class="rounded-lg title text-lg"> <span class="rounded-lg title text-lg">
<%= gettext("Count:") %> <%= gettext("Count:") %>
<%= if @ammo_group.count == 0, do: gettext("Empty"), else: @ammo_group.count %> <%= @ammo_group.count %>
</span> </span>
<span <%= if @ammo_group.notes do %>
:if={@ammo_group |> Ammo.get_original_count() != @ammo_group.count} <span class="rounded-lg title text-lg">
class="rounded-lg title text-lg" <%= gettext("Notes:") %>
> <%= @ammo_group.notes %>
<%= gettext("Original Count:") %> </span>
<%= @ammo_group |> Ammo.get_original_count() %> <% end %>
</span>
<span :if={@ammo_group.notes} class="rounded-lg title text-lg">
<%= gettext("Notes:") %>
<%= @ammo_group.notes %>
</span>
<span class="rounded-lg title text-lg"> <span class="rounded-lg title text-lg">
<%= gettext("Purchased on:") %> <%= gettext("Added on:") %>
<.date date={@ammo_group.purchased_on} /> <%= @ammo_group.inserted_at |> display_datetime() %>
</span>
<span :if={@ammo_group |> Ammo.get_last_used_shot_group()} class="rounded-lg title text-lg">
<%= gettext("Last used on:") %>
<.date date={@ammo_group |> Ammo.get_last_used_shot_group() |> Map.get(:date)} />
</span> </span>
<%= if @ammo_group.price_paid do %> <%= if @ammo_group.price_paid do %>
@ -70,33 +49,14 @@ defmodule CanneryWeb.Components.AmmoGroupCard do
amount: @ammo_group.price_paid |> :erlang.float_to_binary(decimals: 2) amount: @ammo_group.price_paid |> :erlang.float_to_binary(decimals: 2)
) %> ) %>
</span> </span>
<span class="rounded-lg title text-lg">
<%= gettext("CPR:") %>
<%= gettext("$%{amount}",
amount: @ammo_group |> Ammo.get_cpr() |> :erlang.float_to_binary(decimals: 2)
) %>
</span>
<% end %> <% end %>
<span :if={@show_container and @ammo_group.container} class="rounded-lg title text-lg">
<%= gettext("Container:") %>
<.link
navigate={Routes.container_show_path(Endpoint, :show, @ammo_group.container)}
class="link"
>
<%= @ammo_group.container.name %>
</.link>
</span>
</div> </div>
<div <%= if assigns |> Map.has_key?(:inner_block) do %>
:if={assigns |> Map.has_key?(:inner_block)} <div class="mt-4 flex space-x-4 justify-center items-center">
class="mt-4 flex space-x-4 justify-center items-center" <%= render_slot(@inner_block) %>
> </div>
<%= render_slot(@inner_block) %> <% end %>
</div>
</div> </div>
""" """
end end

View File

@ -1,234 +0,0 @@
defmodule CanneryWeb.Components.AmmoGroupTableComponent do
@moduledoc """
A component that displays a list of ammo groups
"""
use CanneryWeb, :live_component
alias Cannery.{Accounts.User, Ammo, Ammo.AmmoGroup, Repo}
alias Ecto.UUID
alias Phoenix.LiveView.{Rendered, Socket}
@impl true
@spec update(
%{
required(:id) => UUID.t(),
required(:current_user) => User.t(),
required(:ammo_groups) => [AmmoGroup.t()],
optional(:ammo_type) => Rendered.t(),
optional(:range) => Rendered.t(),
optional(:container) => Rendered.t(),
optional(:actions) => Rendered.t(),
optional(any()) => any()
},
Socket.t()
) :: {:ok, Socket.t()}
def update(%{id: _id, ammo_groups: _ammo_group, current_user: _current_user} = assigns, socket) do
socket =
socket
|> assign(assigns)
|> assign_new(:ammo_type, fn -> [] end)
|> assign_new(:range, fn -> [] end)
|> assign_new(:container, fn -> [] end)
|> assign_new(:actions, fn -> [] end)
|> display_ammo_groups()
{:ok, socket}
end
defp display_ammo_groups(
%{
assigns: %{
ammo_groups: ammo_groups,
current_user: current_user,
ammo_type: ammo_type,
range: range,
container: container,
actions: actions
}
} = socket
) do
columns =
if actions == [] do
[]
else
[%{label: nil, key: :actions, sortable: false}]
end
columns = [
%{label: gettext("Purchased on"), key: :purchased_on},
%{label: gettext("Last used on"), key: :used_up_on} | columns
]
columns =
if container == [] do
columns
else
[%{label: gettext("Container"), key: :container} | columns]
end
columns =
if range == [] do
columns
else
[%{label: gettext("Range"), key: :range} | columns]
end
columns = [
%{label: gettext("Count"), key: :count},
%{label: gettext("Original Count"), key: :original_count},
%{label: gettext("Price paid"), key: :price_paid},
%{label: gettext("CPR"), key: :cpr},
%{label: gettext("% left"), key: :remaining},
%{label: gettext("Notes"), key: :notes}
| columns
]
columns =
if ammo_type == [] do
columns
else
[%{label: gettext("Ammo type"), key: :ammo_type} | columns]
end
extra_data = %{
current_user: current_user,
ammo_type: ammo_type,
columns: columns,
container: container,
actions: actions,
range: range
}
rows =
ammo_groups
|> Repo.preload([:ammo_type, :container])
|> Enum.map(fn ammo_group ->
ammo_group |> get_row_data_for_ammo_group(extra_data)
end)
socket |> assign(columns: columns, rows: rows)
end
@impl true
def render(assigns) do
~H"""
<div id={@id} class="w-full">
<.live_component
module={CanneryWeb.Components.TableComponent}
id={"table-#{@id}"}
columns={@columns}
rows={@rows}
/>
</div>
"""
end
@spec get_row_data_for_ammo_group(AmmoGroup.t(), additional_data :: map()) :: map()
defp get_row_data_for_ammo_group(ammo_group, %{columns: columns} = additional_data) do
ammo_group = ammo_group |> Repo.preload([:ammo_type, :container])
columns
|> Map.new(fn %{key: key} ->
{key, get_value_for_key(key, ammo_group, additional_data)}
end)
end
@spec get_value_for_key(atom(), AmmoGroup.t(), additional_data :: map()) ::
any() | {any(), Rendered.t()}
defp get_value_for_key(
:ammo_type,
%{ammo_type: %{name: ammo_type_name} = ammo_type},
%{ammo_type: ammo_type_block}
) do
assigns = %{ammo_type: ammo_type, ammo_type_block: ammo_type_block}
{ammo_type_name,
~H"""
<%= render_slot(@ammo_type_block, @ammo_type) %>
"""}
end
defp get_value_for_key(:price_paid, %{price_paid: nil}, _additional_data), do: {"", nil}
defp get_value_for_key(:price_paid, %{price_paid: price_paid}, _additional_data),
do: gettext("$%{amount}", amount: price_paid |> :erlang.float_to_binary(decimals: 2))
defp get_value_for_key(:purchased_on, %{purchased_on: purchased_on}, _additional_data) do
assigns = %{purchased_on: purchased_on}
{purchased_on,
~H"""
<.date date={@purchased_on} />
"""}
end
defp get_value_for_key(:used_up_on, ammo_group, _additional_data) do
last_shot_group_date =
case ammo_group |> Ammo.get_last_used_shot_group() do
%{date: last_shot_group_date} -> last_shot_group_date
_no_shot_groups -> nil
end
assigns = %{last_shot_group_date: last_shot_group_date}
{last_shot_group_date,
~H"""
<%= if @last_shot_group_date do %>
<.date date={@last_shot_group_date} />
<% else %>
<%= gettext("Never used") %>
<% end %>
"""}
end
defp get_value_for_key(:range, %{staged: staged} = ammo_group, %{range: range}) do
assigns = %{range: range, ammo_group: ammo_group}
{staged,
~H"""
<%= render_slot(@range, @ammo_group) %>
"""}
end
defp get_value_for_key(:remaining, ammo_group, _additional_data),
do: gettext("%{percentage}%", percentage: ammo_group |> Ammo.get_percentage_remaining())
defp get_value_for_key(:actions, ammo_group, %{actions: actions}) do
assigns = %{actions: actions, ammo_group: ammo_group}
~H"""
<%= render_slot(@actions, @ammo_group) %>
"""
end
defp get_value_for_key(:container, %{container: nil}, _additional_data), do: {nil, nil}
defp get_value_for_key(
:container,
%{container: %{name: container_name}} = ammo_group,
%{container: container}
) do
assigns = %{container: container, ammo_group: ammo_group}
{container_name,
~H"""
<%= render_slot(@container, @ammo_group) %>
"""}
end
defp get_value_for_key(:original_count, ammo_group, _additional_data),
do: ammo_group |> Ammo.get_original_count()
defp get_value_for_key(:cpr, %{price_paid: nil}, _additional_data),
do: gettext("No cost information")
defp get_value_for_key(:cpr, ammo_group, _additional_data) do
gettext("$%{amount}",
amount: ammo_group |> Ammo.get_cpr() |> :erlang.float_to_binary(decimals: 2)
)
end
defp get_value_for_key(:count, %{count: count}, _additional_data),
do: if(count == 0, do: gettext("Empty"), else: count)
defp get_value_for_key(key, ammo_group, _additional_data), do: ammo_group |> Map.get(key)
end

View File

@ -1,209 +0,0 @@
defmodule CanneryWeb.Components.AmmoTypeTableComponent do
@moduledoc """
A component that displays a list of ammo type
"""
use CanneryWeb, :live_component
alias Cannery.{Accounts.User, Ammo, Ammo.AmmoType}
alias Ecto.UUID
alias Phoenix.LiveView.{Rendered, Socket}
@impl true
@spec update(
%{
required(:id) => UUID.t(),
required(:current_user) => User.t(),
optional(:show_used) => boolean(),
optional(:ammo_types) => [AmmoType.t()],
optional(:actions) => Rendered.t(),
optional(any()) => any()
},
Socket.t()
) :: {:ok, Socket.t()}
def update(%{id: _id, ammo_types: _ammo_types, current_user: _current_user} = assigns, socket) do
socket =
socket
|> assign(assigns)
|> assign_new(:show_used, fn -> false end)
|> assign_new(:actions, fn -> [] end)
|> display_ammo_types()
{:ok, socket}
end
defp display_ammo_types(
%{
assigns: %{
ammo_types: ammo_types,
current_user: current_user,
show_used: show_used,
actions: actions
}
} = socket
) do
columns =
[
%{label: gettext("Name"), key: :name, type: :name},
%{label: gettext("Bullet type"), key: :bullet_type, type: :string},
%{label: gettext("Bullet core"), key: :bullet_core, type: :string},
%{label: gettext("Cartridge"), key: :cartridge, type: :string},
%{label: gettext("Caliber"), key: :caliber, type: :string},
%{label: gettext("Case material"), key: :case_material, type: :string},
%{label: gettext("Jacket type"), key: :jacket_type, type: :string},
%{label: gettext("Muzzle velocity"), key: :muzzle_velocity, type: :string},
%{label: gettext("Powder type"), key: :powder_type, type: :string},
%{
label: gettext("Powder grains per charge"),
key: :powder_grains_per_charge,
type: :string
},
%{label: gettext("Grains"), key: :grains, type: :string},
%{label: gettext("Pressure"), key: :pressure, type: :string},
%{label: gettext("Primer type"), key: :primer_type, type: :string},
%{label: gettext("Firing type"), key: :firing_type, type: :string},
%{label: gettext("Tracer"), key: :tracer, type: :boolean},
%{label: gettext("Incendiary"), key: :incendiary, type: :boolean},
%{label: gettext("Blank"), key: :blank, type: :boolean},
%{label: gettext("Corrosive"), key: :corrosive, type: :boolean},
%{label: gettext("Manufacturer"), key: :manufacturer, type: :string},
%{label: gettext("UPC"), key: "upc", type: :string}
]
|> Enum.filter(fn %{key: key, type: type} ->
# remove columns if all values match defaults
default_value = if type == :boolean, do: false, else: nil
ammo_types
|> Enum.any?(fn ammo_type ->
not (ammo_type |> Map.get(key) == default_value)
end)
end)
|> Kernel.++([
%{label: gettext("Rounds"), key: :round_count, type: :round_count}
])
|> Kernel.++(
if show_used do
[
%{
label: gettext("Used rounds"),
key: :used_round_count,
type: :used_round_count
},
%{
label: gettext("Total ever rounds"),
key: :historical_round_count,
type: :historical_round_count
}
]
else
[]
end
)
|> Kernel.++([%{label: gettext("Packs"), key: :ammo_count, type: :ammo_count}])
|> Kernel.++(
if show_used do
[
%{
label: gettext("Used packs"),
key: :used_ammo_count,
type: :used_ammo_count
},
%{
label: gettext("Total ever packs"),
key: :historical_ammo_count,
type: :historical_ammo_count
}
]
else
[]
end
)
|> Kernel.++([
%{label: gettext("Average CPR"), key: :avg_price_paid, type: :avg_price_paid},
%{label: nil, key: "actions", type: :actions, sortable: false}
])
extra_data = %{actions: actions, current_user: current_user}
rows =
ammo_types
|> Enum.map(fn ammo_type ->
ammo_type |> get_ammo_type_values(columns, extra_data)
end)
socket |> assign(columns: columns, rows: rows)
end
@impl true
def render(assigns) do
~H"""
<div id={@id} class="w-full">
<.live_component
module={CanneryWeb.Components.TableComponent}
id={"table-#{@id}"}
columns={@columns}
rows={@rows}
/>
</div>
"""
end
defp get_ammo_type_values(ammo_type, columns, extra_data) do
columns
|> Map.new(fn %{key: key, type: type} ->
{key, get_ammo_type_value(type, key, ammo_type, extra_data)}
end)
end
defp get_ammo_type_value(:boolean, key, ammo_type, _other_data),
do: ammo_type |> Map.get(key) |> humanize()
defp get_ammo_type_value(:round_count, _key, ammo_type, %{current_user: current_user}),
do: ammo_type |> Ammo.get_round_count_for_ammo_type(current_user)
defp get_ammo_type_value(:historical_round_count, _key, ammo_type, %{current_user: current_user}),
do: ammo_type |> Ammo.get_historical_count_for_ammo_type(current_user)
defp get_ammo_type_value(:used_round_count, _key, ammo_type, %{current_user: current_user}),
do: ammo_type |> Ammo.get_used_count_for_ammo_type(current_user)
defp get_ammo_type_value(:historical_ammo_count, _key, ammo_type, %{current_user: current_user}),
do: ammo_type |> Ammo.get_ammo_groups_count_for_type(current_user, true)
defp get_ammo_type_value(:used_ammo_count, _key, ammo_type, %{current_user: current_user}),
do: ammo_type |> Ammo.get_used_ammo_groups_count_for_type(current_user)
defp get_ammo_type_value(:ammo_count, _key, ammo_type, %{current_user: current_user}),
do: ammo_type |> Ammo.get_ammo_groups_count_for_type(current_user)
defp get_ammo_type_value(:avg_price_paid, _key, ammo_type, %{current_user: current_user}) do
case ammo_type |> Ammo.get_average_cost_for_ammo_type!(current_user) do
nil -> gettext("No cost information")
count -> gettext("$%{amount}", amount: count |> :erlang.float_to_binary(decimals: 2))
end
end
defp get_ammo_type_value(:name, _key, ammo_type, _other_data) do
assigns = %{ammo_type: ammo_type}
~H"""
<.link
navigate={Routes.ammo_type_show_path(Endpoint, :show, @ammo_type)}
class="link"
data-qa={"view-name-#{@ammo_type.id}"}
>
<%= @ammo_type.name %>
</.link>
"""
end
defp get_ammo_type_value(:actions, _key, ammo_type, %{actions: actions}) do
assigns = %{actions: actions, ammo_type: ammo_type}
~H"""
<%= render_slot(@actions, @ammo_type) %>
"""
end
defp get_ammo_type_value(nil, _key, _ammo_type, _other_data), do: nil
defp get_ammo_type_value(_other, key, ammo_type, _other_data), do: ammo_type |> Map.get(key)
end

View File

@ -5,50 +5,45 @@ defmodule CanneryWeb.Components.ContainerCard do
use CanneryWeb, :component use CanneryWeb, :component
import CanneryWeb.Components.TagCard import CanneryWeb.Components.TagCard
alias Cannery.{Containers, Containers.Container, Repo} alias Cannery.{Containers, Repo}
alias CanneryWeb.Endpoint alias CanneryWeb.Endpoint
alias Phoenix.LiveView.Rendered
attr :container, Container, required: true
slot(:tag_actions)
slot(:inner_block)
@spec container_card(assigns :: map()) :: Rendered.t()
def container_card(%{container: container} = assigns) do def container_card(%{container: container} = assigns) do
assigns = assigns = assigns |> Map.put(:container, container |> Repo.preload([:tags, :ammo_groups]))
assigns
|> assign(container: container |> Repo.preload([:tags, :ammo_groups]))
|> assign_new(:tag_actions, fn -> [] end)
~H""" ~H"""
<div <div
id={"container-#{@container.id}"} id={"container-#{@container.id}"}
class="overflow-hidden max-w-full mx-4 mb-4 px-8 py-4 class="overflow-hidden max-w-full mx-4 my-2 px-8 py-4 flex flex-col justify-center items-center space-y-4
flex flex-col justify-center items-center space-y-4
border border-gray-400 rounded-lg shadow-lg hover:shadow-md border border-gray-400 rounded-lg shadow-lg hover:shadow-md
transition-all duration-300 ease-in-out" transition-all duration-300 ease-in-out"
> >
<div class="max-w-full mb-4 flex flex-col justify-center items-center space-y-2"> <div class="max-w-full mb-4 flex flex-col justify-center items-center space-y-2">
<.link navigate={Routes.container_show_path(Endpoint, :show, @container)} class="link"> <%= live_redirect to: Routes.container_show_path(Endpoint, :show, @container),
class: "link" do %>
<h1 class="px-4 py-2 rounded-lg title text-xl"> <h1 class="px-4 py-2 rounded-lg title text-xl">
<%= @container.name %> <%= @container.name %>
</h1> </h1>
</.link> <% end %>
<span :if={@container.desc} class="rounded-lg title text-lg"> <%= if @container.desc do %>
<%= gettext("Description:") %> <span class="rounded-lg title text-lg">
<%= @container.desc %> <%= gettext("Description:") %>
</span> <%= @container.desc %>
</span>
<% end %>
<span class="rounded-lg title text-lg"> <span class="rounded-lg title text-lg">
<%= gettext("Type:") %> <%= gettext("Type:") %>
<%= @container.type %> <%= @container.type %>
</span> </span>
<span :if={@container.location} class="rounded-lg title text-lg"> <%= if @container.location do %>
<%= gettext("Location:") %> <span class="rounded-lg title text-lg">
<%= @container.location %> <%= gettext("Location:") %>
</span> <%= @container.location %>
</span>
<% end %>
<%= unless @container.ammo_groups |> Enum.empty?() do %> <%= unless @container.ammo_groups |> Enum.empty?() do %>
<span class="rounded-lg title text-lg"> <span class="rounded-lg title text-lg">
@ -63,18 +58,23 @@ defmodule CanneryWeb.Components.ContainerCard do
<% end %> <% end %>
<div class="flex flex-wrap justify-center items-center"> <div class="flex flex-wrap justify-center items-center">
<.simple_tag_card :for={tag <- @container.tags} :if={@container.tags} tag={tag} /> <%= unless @container.tags |> Enum.empty?() do %>
<%= for tag <- @container.tags do %>
<.simple_tag_card tag={tag} />
<% end %>
<% end %>
<%= render_slot(@tag_actions) %> <%= if assigns |> Map.has_key?(:tag_actions) do %>
<%= render_slot(@tag_actions) %>
<% end %>
</div> </div>
</div> </div>
<div <%= if assigns |> Map.has_key?(:inner_block) do %>
:if={assigns |> Map.has_key?(:inner_block)} <div class="flex space-x-4 justify-center items-center">
class="flex space-x-4 justify-center items-center" <%= render_slot(@inner_block) %>
> </div>
<%= render_slot(@inner_block) %> <% end %>
</div>
</div> </div>
""" """
end end

View File

@ -1,154 +0,0 @@
defmodule CanneryWeb.Components.ContainerTableComponent do
@moduledoc """
A component that displays a list of containers
"""
use CanneryWeb, :live_component
alias Cannery.{Accounts.User, Containers, Containers.Container, Repo}
alias CanneryWeb.Components.TagCard
alias Ecto.UUID
alias Phoenix.LiveView.{Rendered, Socket}
@impl true
@spec update(
%{
required(:id) => UUID.t(),
required(:current_user) => User.t(),
optional(:containers) => [Container.t()],
optional(:tag_actions) => Rendered.t(),
optional(:actions) => Rendered.t(),
optional(any()) => any()
},
Socket.t()
) :: {:ok, Socket.t()}
def update(%{id: _id, containers: _containers, current_user: _current_user} = assigns, socket) do
socket =
socket
|> assign(assigns)
|> assign_new(:tag_actions, fn -> [] end)
|> assign_new(:actions, fn -> [] end)
|> display_containers()
{:ok, socket}
end
defp display_containers(
%{
assigns: %{
containers: containers,
current_user: current_user,
tag_actions: tag_actions,
actions: actions
}
} = socket
) do
columns =
[
%{label: gettext("Name"), key: :name, type: :string},
%{label: gettext("Description"), key: :desc, type: :string},
%{label: gettext("Location"), key: :location, type: :string},
%{label: gettext("Type"), key: :type, type: :string},
%{label: gettext("Packs"), key: :packs, type: :integer},
%{label: gettext("Rounds"), key: :rounds, type: :string},
%{label: gettext("Tags"), key: :tags, type: :tags},
%{label: nil, key: :actions, sortable: false, type: :actions}
]
|> Enum.filter(fn %{key: key, type: type} ->
# remove columns if all values match defaults
default_value =
case type do
:boolean -> false
_other_type -> nil
end
containers
|> Enum.any?(fn container ->
type in [:tags, :actions] or not (container |> Map.get(key) == default_value)
end)
end)
extra_data = %{
current_user: current_user,
tag_actions: tag_actions,
actions: actions
}
rows =
containers
|> Enum.map(fn container ->
container |> get_row_data_for_container(columns, extra_data)
end)
socket
|> assign(
columns: columns,
rows: rows
)
end
@impl true
def render(assigns) do
~H"""
<div id={@id} class="w-full">
<.live_component
module={CanneryWeb.Components.TableComponent}
id={"table-#{@id}"}
columns={@columns}
rows={@rows}
/>
</div>
"""
end
@spec get_row_data_for_container(Container.t(), columns :: [map()], extra_data :: map) :: map()
defp get_row_data_for_container(container, columns, extra_data) do
container = container |> Repo.preload([:ammo_groups, :tags])
columns
|> Map.new(fn %{key: key} -> {key, get_value_for_key(key, container, extra_data)} end)
end
@spec get_value_for_key(atom(), Container.t(), extra_data :: map) :: any()
defp get_value_for_key(:name, %{id: id, name: container_name}, _extra_data) do
assigns = %{id: id, container_name: container_name}
{container_name,
~H"""
<div class="flex flex-wrap justify-center items-center">
<.link navigate={Routes.container_show_path(Endpoint, :show, @id)} class="link">
<%= @container_name %>
</.link>
</div>
"""}
end
defp get_value_for_key(:packs, container, _extra_data) do
container |> Containers.get_container_ammo_group_count!()
end
defp get_value_for_key(:rounds, container, _extra_data) do
container |> Containers.get_container_rounds!()
end
defp get_value_for_key(:tags, container, %{tag_actions: tag_actions}) do
assigns = %{tag_actions: tag_actions, container: container}
{container.tags |> Enum.map(fn %{name: name} -> name end),
~H"""
<div class="flex flex-wrap justify-center items-center">
<TagCard.simple_tag_card :for={tag <- @container.tags} :if={@container.tags} tag={tag} />
<%= render_slot(@tag_actions, @container) %>
</div>
"""}
end
defp get_value_for_key(:actions, container, %{actions: actions}) do
assigns = %{actions: actions, container: container}
~H"""
<%= render_slot(@actions, @container) %>
"""
end
defp get_value_for_key(key, container, _extra_data), do: container |> Map.get(key)
end

View File

@ -4,20 +4,9 @@ defmodule CanneryWeb.Components.InviteCard do
""" """
use CanneryWeb, :component use CanneryWeb, :component
alias Cannery.Accounts.{Invite, Invites, User}
alias CanneryWeb.Endpoint alias CanneryWeb.Endpoint
attr :invite, Invite, required: true def invite_card(assigns) do
attr :current_user, User, required: true
slot(:inner_block)
slot(:code_actions)
def invite_card(%{invite: invite, current_user: current_user} = assigns) do
assigns =
assigns
|> assign(:use_count, Invites.get_use_count(invite, current_user))
|> assign_new(:code_actions, fn -> [] end)
~H""" ~H"""
<div class="mx-4 my-2 px-8 py-4 flex flex-col justify-center items-center space-y-4 <div class="mx-4 my-2 px-8 py-4 flex flex-col justify-center items-center space-y-4
border border-gray-400 rounded-lg shadow-lg hover:shadow-md border border-gray-400 rounded-lg shadow-lg hover:shadow-md
@ -28,14 +17,8 @@ defmodule CanneryWeb.Components.InviteCard do
<%= if @invite.disabled_at |> is_nil() do %> <%= if @invite.disabled_at |> is_nil() do %>
<h2 class="title text-md"> <h2 class="title text-md">
<%= if @invite.uses_left do %> <%= gettext("Uses Left:") %>
<%= gettext( <%= @invite.uses_left || "Unlimited" %>
"Uses Left: %{uses_left_count}",
uses_left_count: @invite.uses_left
) %>
<% else %>
<%= gettext("Uses Left: Unlimited") %>
<% end %>
</h2> </h2>
<% else %> <% else %>
<h2 class="title text-md"> <h2 class="title text-md">
@ -43,27 +26,24 @@ defmodule CanneryWeb.Components.InviteCard do
</h2> </h2>
<% end %> <% end %>
<.qr_code
content={Routes.user_registration_url(Endpoint, :new, invite: @invite.token)}
filename={@invite.name}
/>
<h2 :if={@use_count != 0} class="title text-md">
<%= gettext("Uses: %{uses_count}", uses_count: @use_count) %>
</h2>
<div class="flex flex-row flex-wrap justify-center items-center"> <div class="flex flex-row flex-wrap justify-center items-center">
<code <code
id={"code-#{@invite.id}"} id={"code-#{@invite.id}"}
class="mx-2 my-1 text-xs px-4 py-2 rounded-lg text-center break-all text-gray-100 bg-primary-800" class="mx-2 my-1 text-xs px-4 py-2 rounded-lg text-center break-all text-gray-100 bg-primary-800"
phx-no-format >
><%= Routes.user_registration_url(Endpoint, :new, invite: @invite.token) %></code> <%= Routes.user_registration_url(Endpoint, :new, invite: @invite.token) %>
<%= render_slot(@code_actions) %> </code>
<%= if @code_actions do %>
<%= render_slot(@code_actions) %>
<% end %>
</div> </div>
<div :if={@inner_block} class="flex space-x-4 justify-center items-center"> <%= if @inner_block do %>
<%= render_slot(@inner_block) %> <div class="flex space-x-4 justify-center items-center">
</div> <%= render_slot(@inner_block) %>
</div>
<% end %>
</div> </div>
""" """
end end

View File

@ -22,7 +22,7 @@ defmodule CanneryWeb.Components.MoveAmmoGroupComponent do
assigns, assigns,
socket socket
) do ) do
changeset = ammo_group |> AmmoGroup.update_changeset(%{}, current_user) changeset = ammo_group |> AmmoGroup.update_changeset(%{})
containers = containers =
Containers.list_containers(current_user) Containers.list_containers(current_user)
@ -52,7 +52,7 @@ defmodule CanneryWeb.Components.MoveAmmoGroupComponent do
{:ok, _ammo_group} -> {:ok, _ammo_group} ->
prompt = dgettext("prompts", "Ammo moved to %{name} successfully", name: container_name) prompt = dgettext("prompts", "Ammo moved to %{name} successfully", name: container_name)
socket |> put_flash(:info, prompt) |> push_navigate(to: return_to) socket |> put_flash(:info, prompt) |> push_redirect(to: return_to)
{:error, %Ecto.Changeset{} = changeset} -> {:error, %Ecto.Changeset{} = changeset} ->
socket |> assign(changeset: changeset) socket |> assign(changeset: changeset)
@ -77,7 +77,7 @@ defmodule CanneryWeb.Components.MoveAmmoGroupComponent do
~H""" ~H"""
<div class="w-full flex flex-col space-y-8 justify-center items-center"> <div class="w-full flex flex-col space-y-8 justify-center items-center">
<h2 class="mb-8 text-center title text-xl text-primary-600"> <h2 class="mb-8 text-center title text-xl text-primary-600">
<%= dgettext("actions", "Move ammo") %> <%= gettext("Move ammo") %>
</h2> </h2>
<%= if @containers |> Enum.empty?() do %> <%= if @containers |> Enum.empty?() do %>
@ -86,9 +86,10 @@ defmodule CanneryWeb.Components.MoveAmmoGroupComponent do
<%= display_emoji("😔") %> <%= display_emoji("😔") %>
</h2> </h2>
<.link navigate={Routes.container_index_path(Endpoint, :new)} class="btn btn-primary"> <%= live_patch(dgettext("actions", "Add another container!"),
<%= dgettext("actions", "Add another container!") %> to: Routes.container_index_path(Endpoint, :new),
</.link> class: "btn btn-primary"
) %>
<% else %> <% else %>
<.live_component <.live_component
module={CanneryWeb.Components.TableComponent} module={CanneryWeb.Components.TableComponent}
@ -106,7 +107,7 @@ defmodule CanneryWeb.Components.MoveAmmoGroupComponent do
containers containers
|> Enum.map(fn container -> |> Enum.map(fn container ->
columns columns
|> Map.new(fn %{key: key} -> {key, get_row_value_by_key(key, container, assigns)} end) |> Enum.into(%{}, fn %{key: key} -> {key, get_row_value_by_key(key, container, assigns)} end)
end) end)
end end
@ -121,7 +122,7 @@ defmodule CanneryWeb.Components.MoveAmmoGroupComponent do
class="btn btn-primary" class="btn btn-primary"
phx-click="move" phx-click="move"
phx-target={@myself} phx-target={@myself}
phx-value-container_id={@container.id} phx-value-container_id={container.id}
> >
<%= dgettext("actions", "Select") %> <%= dgettext("actions", "Select") %>
</button> </button>

View File

@ -1,121 +0,0 @@
defmodule CanneryWeb.Components.ShotGroupTableComponent do
@moduledoc """
A component that displays a list of shot groups
"""
use CanneryWeb, :live_component
alias Cannery.{Accounts.User, ActivityLog.ShotGroup, Repo}
alias Ecto.UUID
alias Phoenix.LiveView.{Rendered, Socket}
@impl true
@spec update(
%{
required(:id) => UUID.t(),
required(:current_user) => User.t(),
optional(:shot_groups) => [ShotGroup.t()],
optional(:actions) => Rendered.t(),
optional(any()) => any()
},
Socket.t()
) :: {:ok, Socket.t()}
def update(%{id: _id, shot_groups: _shot_groups, current_user: _current_user} = assigns, socket) do
socket =
socket
|> assign(assigns)
|> assign_new(:actions, fn -> [] end)
|> display_shot_groups()
{:ok, socket}
end
defp display_shot_groups(
%{
assigns: %{
shot_groups: shot_groups,
current_user: current_user,
actions: actions
}
} = socket
) do
columns = [
%{label: gettext("Ammo"), key: :name},
%{label: gettext("Rounds shot"), key: :count},
%{label: gettext("Notes"), key: :notes},
%{label: gettext("Date"), key: :date},
%{label: nil, key: :actions, sortable: false}
]
extra_data = %{current_user: current_user, actions: actions}
rows =
shot_groups
|> Enum.map(fn shot_group ->
shot_group |> get_row_data_for_shot_group(columns, extra_data)
end)
socket
|> assign(
columns: columns,
rows: rows
)
end
@impl true
def render(assigns) do
~H"""
<div id={@id} class="w-full">
<.live_component
module={CanneryWeb.Components.TableComponent}
id={"table-#{@id}"}
columns={@columns}
rows={@rows}
initial_key={:date}
initial_sort_mode={:desc}
/>
</div>
"""
end
@spec get_row_data_for_shot_group(ShotGroup.t(), columns :: [map()], extra_data :: map()) ::
map()
defp get_row_data_for_shot_group(shot_group, columns, extra_data) do
shot_group = shot_group |> Repo.preload(ammo_group: :ammo_type)
columns
|> Map.new(fn %{key: key} ->
{key, get_row_value(key, shot_group, extra_data)}
end)
end
defp get_row_value(
:name,
%{ammo_group: %{ammo_type: %{name: ammo_type_name} = ammo_group}},
_extra_data
) do
assigns = %{ammo_group: ammo_group, ammo_type_name: ammo_type_name}
name_block = ~H"""
<.link navigate={Routes.ammo_group_show_path(Endpoint, :show, @ammo_group)} class="link">
<%= @ammo_type_name %>
</.link>
"""
{ammo_type_name, name_block}
end
defp get_row_value(:date, %{date: _date} = assigns, _extra_data) do
~H"""
<.date date={@date} />
"""
end
defp get_row_value(:actions, shot_group, %{actions: actions}) do
assigns = %{actions: actions, shot_group: shot_group}
~H"""
<%= render_slot(@actions, @shot_group) %>
"""
end
defp get_row_value(key, shot_group, _extra_data), do: shot_group |> Map.get(key)
end

View File

@ -6,7 +6,7 @@ defmodule CanneryWeb.Components.TableComponent do
- `:columns`: An array of maps containing the following keys - `:columns`: An array of maps containing the following keys
- `:label`: A gettext'd or otherwise user-facing string label for the - `:label`: A gettext'd or otherwise user-facing string label for the
column. Can be nil column. Can be nil
- `:key`: An atom key used for sorting - `:key`: A string key used for sorting
- `:class`: Extra classes to be applied to the column element, if desired. - `:class`: Extra classes to be applied to the column element, if desired.
Optional Optional
- `:sortable`: If false, will prevent the user from sorting with it. - `:sortable`: If false, will prevent the user from sorting with it.
@ -21,7 +21,6 @@ defmodule CanneryWeb.Components.TableComponent do
use CanneryWeb, :live_component use CanneryWeb, :live_component
alias Phoenix.LiveView.Socket alias Phoenix.LiveView.Socket
require Integer
@impl true @impl true
@spec update( @spec update(
@ -29,50 +28,26 @@ defmodule CanneryWeb.Components.TableComponent do
required(:columns) => required(:columns) =>
list(%{ list(%{
required(:label) => String.t() | nil, required(:label) => String.t() | nil,
required(:key) => atom() | nil, required(:key) => String.t() | nil,
optional(:class) => String.t(), optional(:class) => String.t(),
optional(:row_class) => String.t(),
optional(:alternate_row_class) => String.t(),
optional(:sortable) => false optional(:sortable) => false
}), }),
required(:rows) => required(:rows) =>
list(%{ list(%{
(key :: atom()) => any() | {custom_sort_value :: String.t(), value :: any()} (key :: String.t()) => any() | {custom_sort_value :: String.t(), value :: any()}
}), }),
optional(:inital_key) => atom(),
optional(:initial_sort_mode) => atom(),
optional(any()) => any() optional(any()) => any()
}, },
Socket.t() Socket.t()
) :: {:ok, Socket.t()} ) :: {:ok, Socket.t()}
def update(%{columns: columns, rows: rows} = assigns, socket) do def update(%{columns: columns, rows: rows} = assigns, socket) do
initial_key = initial_key = columns |> List.first() |> Map.get(:key)
if assigns |> Map.has_key?(:initial_key) do rows = rows |> Enum.sort_by(fn row -> row |> Map.get(initial_key) end, :asc)
assigns.initial_key
else
columns |> List.first(%{}) |> Map.get(:key)
end
initial_sort_mode =
if assigns |> Map.has_key?(:initial_sort_mode) do
assigns.initial_sort_mode
else
:asc
end
rows = rows |> sort_by_custom_sort_value_or_value(initial_key, initial_sort_mode)
socket = socket =
socket socket
|> assign(assigns) |> assign(assigns)
|> assign( |> assign(columns: columns, rows: rows, last_sort_key: initial_key, sort_mode: :asc)
columns: columns,
rows: rows,
last_sort_key: initial_key,
sort_mode: initial_sort_mode
)
|> assign_new(:row_class, fn -> "bg-white" end)
|> assign_new(:alternate_row_class, fn -> "bg-gray-200" end)
{:ok, socket} {:ok, socket}
end end
@ -81,19 +56,20 @@ defmodule CanneryWeb.Components.TableComponent do
def handle_event( def handle_event(
"sort_by", "sort_by",
%{"sort-key" => key}, %{"sort-key" => key},
%{assigns: %{rows: rows, last_sort_key: last_sort_key, sort_mode: sort_mode}} = socket %{assigns: %{rows: rows, last_sort_key: key, sort_mode: sort_mode}} = socket
) do ) do
key = key |> String.to_existing_atom() sort_mode = if sort_mode == :asc, do: :desc, else: :asc
sort_mode =
case {key, sort_mode} do
{^last_sort_key, :asc} -> :desc
{^last_sort_key, :desc} -> :asc
{_new_sort_key, _last_sort_mode} -> :asc
end
rows = rows |> sort_by_custom_sort_value_or_value(key, sort_mode) rows = rows |> sort_by_custom_sort_value_or_value(key, sort_mode)
{:noreply, socket |> assign(last_sort_key: key, sort_mode: sort_mode, rows: rows)} {:noreply, socket |> assign(sort_mode: sort_mode, rows: rows)}
end
def handle_event(
"sort_by",
%{"sort-key" => key},
%{assigns: %{rows: rows}} = socket
) do
rows = rows |> sort_by_custom_sort_value_or_value(key, :asc)
{:noreply, socket |> assign(last_sort_key: key, sort_mode: :asc, rows: rows)}
end end
defp sort_by_custom_sort_value_or_value(rows, key, sort_mode) do defp sort_by_custom_sort_value_or_value(rows, key, sort_mode) do

View File

@ -1,32 +1,31 @@
<div id={@id} class="w-full overflow-x-auto border border-gray-600 rounded-lg shadow-lg bg-white"> <div class="w-full overflow-x-auto border border-gray-600 rounded-lg shadow-lg bg-black">
<table class="min-w-full table-auto text-center bg-white"> <table class="min-w-full table-auto text-center bg-white">
<thead class="border-b border-primary-600"> <thead class="border-b border-primary-600">
<tr> <tr>
<%= for %{key: key, label: label} = column <- @columns do %> <%= for %{key: key, label: label} = column <- @columns do %>
<%= if column |> Map.get(:sortable, true) do %> <%= if column |> Map.get(:sortable, true) do %>
<th class={["p-2", column[:class]]}> <th class={"p-2 #{column[:class]}"}>
<span <span
class="cursor-pointer flex justify-center items-center space-x-2" class="cursor-pointer"
phx-click="sort_by" phx-click="sort_by"
phx-value-sort-key={key} phx-value-sort-key={key}
phx-target={@myself} phx-target={@myself}
> >
<i class="w-0 float-right fas fa-sm fa-chevron-up opacity-0"></i> <span class="underline"><%= label %></span>
<span class={if @last_sort_key == key, do: "underline"}><%= label %></span>
<%= if @last_sort_key == key do %> <%= if @last_sort_key == key do %>
<%= case @sort_mode do %> <%= case @sort_mode do %>
<% :asc -> %> <% :asc -> %>
<i class="w-0 float-right fas fa-sm fa-chevron-down"></i> <i class="fas fa-sm fa-chevron-down"></i>
<% :desc -> %> <% :desc -> %>
<i class="w-0 float-right fas fa-sm fa-chevron-up"></i> <i class="fas fa-sm fa-chevron-up"></i>
<% end %> <% end %>
<% else %> <% else %>
<i class="w-0 float-right fas fa-sm fa-chevron-up opacity-0"></i> <i class="fas fa-sm fa-chevron-up opacity-0"></i>
<% end %> <% end %>
</span> </span>
</th> </th>
<% else %> <% else %>
<th class={["p-2 cursor-not-allowed", column[:class]]}> <th class={"p-2 #{column[:class]}"}>
<%= label %> <%= label %>
</th> </th>
<% end %> <% end %>
@ -34,19 +33,20 @@
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
<tr <%= for values <- @rows do %>
:for={{values, i} <- @rows |> Enum.with_index()} <tr>
class={if i |> Integer.is_even(), do: @row_class, else: @alternate_row_class} <%= for %{key: key} = value <- @columns do %>
> <td class={"p-2 #{value[:class]}"}>
<td :for={%{key: key} = value <- @columns} class={["p-2", value[:class]]}> <%= case values |> Map.get(key) do %>
<%= case values |> Map.get(key) do %> <% {_custom_sort_value, value} -> %>
<% {_custom_sort_value, value} -> %> <%= value %>
<%= value %> <% value -> %>
<% value -> %> <%= value %>
<%= value %> <% end %>
</td>
<% end %> <% end %>
</td> </tr>
</tr> <% end %>
</tbody> </tbody>
</table> </table>
</div> </div>

View File

@ -4,18 +4,14 @@ defmodule CanneryWeb.Components.TagCard do
""" """
use CanneryWeb, :component use CanneryWeb, :component
alias Cannery.Tags.Tag
attr :tag, Tag, required: true
slot(:inner_block, required: true)
def tag_card(assigns) do def tag_card(assigns) do
~H""" ~H"""
<div <div
id={"tag-#{@tag.id}"} id={"tag-#{@tag.id}"}
class="mx-4 mb-4 px-8 py-4 space-x-4 flex justify-center items-center class="mx-4 my-2 px-8 py-4 space-x-4 flex justify-center items-center
border border-gray-400 rounded-lg shadow-lg hover:shadow-md border border-gray-400 rounded-lg shadow-lg hover:shadow-md
transition-all duration-300 ease-in-out" transition-all duration-300 ease-in-out"
> >
<.simple_tag_card tag={@tag} /> <.simple_tag_card tag={@tag} />
<%= render_slot(@inner_block) %> <%= render_slot(@inner_block) %>
@ -23,8 +19,6 @@ defmodule CanneryWeb.Components.TagCard do
""" """
end end
attr :tag, Tag, required: true
def simple_tag_card(assigns) do def simple_tag_card(assigns) do
~H""" ~H"""
<h1 <h1

View File

@ -6,7 +6,7 @@ defmodule CanneryWeb.Components.Topbar do
use CanneryWeb, :component use CanneryWeb, :component
alias Cannery.Accounts alias Cannery.Accounts
alias CanneryWeb.HomeLive alias CanneryWeb.{Endpoint, HomeLive}
def topbar(assigns) do def topbar(assigns) do
assigns = assigns =
@ -16,17 +16,16 @@ defmodule CanneryWeb.Components.Topbar do
<nav role="navigation" class="mb-8 px-8 py-4 w-full bg-primary-400"> <nav role="navigation" class="mb-8 px-8 py-4 w-full bg-primary-400">
<div class="flex flex-col sm:flex-row justify-between items-center"> <div class="flex flex-col sm:flex-row justify-between items-center">
<div class="mb-4 sm:mb-0 sm:mr-8 flex flex-row justify-start items-center space-x-2"> <div class="mb-4 sm:mb-0 sm:mr-8 flex flex-row justify-start items-center space-x-2">
<.link <%= live_redirect to: Routes.live_path(Endpoint, HomeLive),
navigate={Routes.live_path(Endpoint, HomeLive)} class: "inline mx-2 my-1 leading-5 text-xl text-white"
class="inline mx-2 my-1 leading-5 text-xl text-white" do %>
>
<img <img
src={Routes.static_path(Endpoint, "/images/cannery.svg")} src={Routes.static_path(Endpoint, "/images/cannery.svg")}
alt={gettext("Cannery logo")} alt={gettext("Cannery logo")}
class="inline-block h-8 mx-1" class="inline-block h-8 mx-1"
/> />
<h1 class="inline hover:underline">Cannery</h1> <h1 class="inline hover:underline">Cannery</h1>
</.link> <% end %>
<%= if @title_content do %> <%= if @title_content do %>
<span class="mx-2 my-1"> <span class="mx-2 my-1">
@ -42,100 +41,78 @@ defmodule CanneryWeb.Components.Topbar do
text-lg text-white text-ellipsis"> text-lg text-white text-ellipsis">
<%= if @current_user do %> <%= if @current_user do %>
<li class="mx-2 my-1"> <li class="mx-2 my-1">
<.link <%= live_redirect(gettext("Tags"),
navigate={Routes.tag_index_path(Endpoint, :index)} to: Routes.tag_index_path(Endpoint, :index),
class="text-primary-600 text-white hover:underline" class: "text-primary-600 text-white hover:underline"
> ) %>
<%= gettext("Tags") %>
</.link>
</li> </li>
<li class="mx-2 my-1"> <li class="mx-2 my-1">
<.link <%= live_redirect(gettext("Containers"),
navigate={Routes.container_index_path(Endpoint, :index)} to: Routes.container_index_path(Endpoint, :index),
class="text-primary-600 text-white hover:underline" class: "text-primary-600 text-white hover:underline"
> ) %>
<%= gettext("Containers") %>
</.link>
</li> </li>
<li class="mx-2 my-1"> <li class="mx-2 my-1">
<.link <%= live_redirect(gettext("Catalog"),
navigate={Routes.ammo_type_index_path(Endpoint, :index)} to: Routes.ammo_type_index_path(Endpoint, :index),
class="text-primary-600 text-white hover:underline" class: "text-primary-600 text-white hover:underline"
> ) %>
<%= gettext("Catalog") %>
</.link>
</li> </li>
<li class="mx-2 my-1"> <li class="mx-2 my-1">
<.link <%= live_redirect(gettext("Ammo"),
navigate={Routes.ammo_group_index_path(Endpoint, :index)} to: Routes.ammo_group_index_path(Endpoint, :index),
class="text-primary-600 text-white hover:underline" class: "text-primary-600 text-white hover:underline"
> ) %>
<%= gettext("Ammo") %>
</.link>
</li> </li>
<li class="mx-2 my-1"> <li class="mx-2 my-1">
<.link <%= live_redirect(gettext("Range"),
navigate={Routes.range_index_path(Endpoint, :index)} to: Routes.range_index_path(Endpoint, :index),
class="text-primary-600 text-white hover:underline" class: "text-primary-600 text-white hover:underline"
> ) %>
<%= gettext("Range") %>
</.link>
</li> </li>
<li :if={@current_user |> Accounts.is_already_admin?()} class="mx-2 my-1"> <%= if @current_user.role == :admin do %>
<.link <li class="mx-2 my-1">
navigate={Routes.invite_index_path(Endpoint, :index)} <%= live_redirect(gettext("Invites"),
class="text-primary-600 text-white hover:underline" to: Routes.invite_index_path(Endpoint, :index),
> class: "text-primary-600 text-white hover:underline"
<%= gettext("Invites") %> ) %>
</.link> </li>
<% end %>
<li class="mx-2 my-1">
<%= live_redirect(@current_user.email,
to: Routes.user_settings_path(Endpoint, :edit),
class: "text-primary-600 text-white hover:underline truncate"
) %>
</li> </li>
<li class="mx-2 my-1"> <li class="mx-2 my-1">
<.link <%= link to: Routes.user_session_path(Endpoint, :delete),
href={Routes.user_settings_path(Endpoint, :edit)} method: :delete,
class="text-primary-600 text-white hover:underline truncate" data: [confirm: dgettext("prompts", "Are you sure you want to log out?")] do %>
>
<%= @current_user.email %>
</.link>
</li>
<li class="mx-2 my-1">
<.link
href={Routes.user_session_path(Endpoint, :delete)}
method="delete"
data-confirm={dgettext("prompts", "Are you sure you want to log out?")}
>
<i class="fas fa-sign-out-alt"></i> <i class="fas fa-sign-out-alt"></i>
</.link> <% end %>
</li>
<li
:if={
@current_user |> Accounts.is_already_admin?() and
function_exported?(Routes, :live_dashboard_path, 2)
}
class="mx-2 my-1"
>
<.link
navigate={Routes.live_dashboard_path(Endpoint, :home)}
class="text-white text-white hover:underline"
>
<i class="fas fa-gauge"></i>
</.link>
</li> </li>
<%= if @current_user.role == :admin and function_exported?(Routes, :live_dashboard_path, 2) do %>
<li class="mx-2 my-1">
<%= live_redirect to: Routes.live_dashboard_path(Endpoint, :home),
class: "text-primary-600 text-white hover:underline" do %>
<i class="fas fa-gauge"></i>
<% end %>
</li>
<% end %>
<% else %> <% else %>
<li :if={Accounts.allow_registration?()} class="mx-2 my-1"> <%= if Accounts.allow_registration?() do %>
<.link <li class="mx-2 my-1">
href={Routes.user_registration_path(Endpoint, :new)} <%= live_redirect(dgettext("actions", "Register"),
class="text-white hover:underline truncate" to: Routes.user_registration_path(Endpoint, :new),
> class: "text-primary-600 text-white hover:underline truncate"
<%= dgettext("actions", "Register") %> ) %>
</.link> </li>
</li> <% end %>
<li class="mx-2 my-1"> <li class="mx-2 my-1">
<.link <%= live_redirect(dgettext("actions", "Log in"),
href={Routes.user_session_path(Endpoint, :new)} to: Routes.user_session_path(Endpoint, :new),
class="text-white hover:underline truncate" class: "text-primary-600 text-white hover:underline truncate"
> ) %>
<%= dgettext("actions", "Log in") %>
</.link>
</li> </li>
<% end %> <% end %>
</ul> </ul>

View File

@ -4,18 +4,14 @@ defmodule CanneryWeb.Components.UserCard do
""" """
use CanneryWeb, :component use CanneryWeb, :component
alias Cannery.Accounts.User
attr :user, User, required: true
slot(:inner_block, required: true)
def user_card(assigns) do def user_card(assigns) do
~H""" ~H"""
<div <div
id={"user-#{@user.id}"} id={"user-#{@user.id}"}
class="mx-4 my-2 px-8 py-4 flex flex-col justify-center items-center text-center class="mx-4 my-2 px-8 py-4 flex flex-col justify-center items-center text-center
border border-gray-400 rounded-lg shadow-lg hover:shadow-md border border-gray-400 rounded-lg shadow-lg hover:shadow-md
transition-all duration-300 ease-in-out" transition-all duration-300 ease-in-out"
> >
<h1 class="px-4 py-2 rounded-lg title text-xl break-all"> <h1 class="px-4 py-2 rounded-lg title text-xl break-all">
<%= @user.email %> <%= @user.email %>
@ -23,29 +19,24 @@ defmodule CanneryWeb.Components.UserCard do
<h3 class="px-4 py-2 rounded-lg title text-lg"> <h3 class="px-4 py-2 rounded-lg title text-lg">
<p> <p>
<%= if @user.confirmed_at do %> <%= if @user.confirmed_at |> is_nil() do %>
<%= gettext( Email unconfirmed
"User was confirmed at%{confirmed_datetime}",
confirmed_datetime: ""
) %>
<.datetime datetime={@user.confirmed_at} />
<% else %> <% else %>
<%= gettext("Email unconfirmed") %> User was confirmed at <%= @user.confirmed_at |> display_datetime() %>
<% end %> <% end %>
</p> </p>
<p> <p>
<%= gettext( <%= gettext("User registered on") %>
"User registered on%{registered_datetime}", <%= @user.inserted_at |> display_datetime() %>
registered_datetime: ""
) %>
<.datetime datetime={@user.inserted_at} />
</p> </p>
</h3> </h3>
<div :if={@inner_block} class="px-4 py-2 flex space-x-4 justify-center items-center"> <%= if @inner_block do %>
<%= render_slot(@inner_block) %> <div class="px-4 py-2 flex space-x-4 justify-center items-center">
</div> <%= render_slot(@inner_block) %>
</div>
<% end %>
</div> </div>
""" """
end end

View File

@ -1,69 +0,0 @@
defmodule CanneryWeb.ExportController do
use CanneryWeb, :controller
alias Cannery.{ActivityLog, Ammo, Containers}
def export(%{assigns: %{current_user: current_user}} = conn, %{"mode" => "json"}) do
ammo_types =
Ammo.list_ammo_types(current_user)
|> Enum.map(fn ammo_type ->
average_cost = ammo_type |> Ammo.get_average_cost_for_ammo_type!(current_user)
round_count = ammo_type |> Ammo.get_round_count_for_ammo_type(current_user)
used_count = ammo_type |> Ammo.get_used_count_for_ammo_type(current_user)
ammo_group_count = ammo_type |> Ammo.get_ammo_groups_count_for_type(current_user, true)
ammo_type
|> Jason.encode!()
|> Jason.decode!()
|> Map.merge(%{
"average_cost" => average_cost,
"round_count" => round_count,
"used_count" => used_count,
"ammo_group_count" => ammo_group_count
})
end)
ammo_groups =
Ammo.list_ammo_groups(nil, true, current_user)
|> Enum.map(fn ammo_group ->
cpr = ammo_group |> Ammo.get_cpr()
used_count = ammo_group |> Ammo.get_used_count()
original_count = ammo_group |> Ammo.get_original_count()
percentage_remaining = ammo_group |> Ammo.get_percentage_remaining()
ammo_group
|> Jason.encode!()
|> Jason.decode!()
|> Map.merge(%{
"used_count" => used_count,
"percentage_remaining" => percentage_remaining,
"original_count" => original_count,
"cpr" => cpr
})
end)
shot_groups = ActivityLog.list_shot_groups(current_user)
containers =
Containers.list_containers(current_user)
|> Enum.map(fn container ->
ammo_group_count = container |> Containers.get_container_ammo_group_count!()
round_count = container |> Containers.get_container_rounds!()
container
|> Jason.encode!()
|> Jason.decode!()
|> Map.merge(%{
"ammo_group_count" => ammo_group_count,
"round_count" => round_count
})
end)
json(conn, %{
user: current_user,
ammo_types: ammo_types,
ammo_groups: ammo_groups,
shot_groups: shot_groups,
containers: containers
})
end
end

View File

@ -1,8 +1,4 @@
defmodule CanneryWeb.HomeController do defmodule CanneryWeb.HomeController do
@moduledoc """
Controller for home page
"""
use CanneryWeb, :controller use CanneryWeb, :controller
def index(conn, _params) do def index(conn, _params) do

View File

@ -1,12 +1,15 @@
defmodule CanneryWeb.UserRegistrationController do defmodule CanneryWeb.UserRegistrationController do
use CanneryWeb, :controller use CanneryWeb, :controller
import CanneryWeb.Gettext import CanneryWeb.Gettext
alias Cannery.{Accounts, Accounts.Invites} alias Cannery.{Accounts, Invites}
alias Cannery.Accounts.User
alias CanneryWeb.{Endpoint, HomeLive} alias CanneryWeb.{Endpoint, HomeLive}
def new(conn, %{"invite" => invite_token}) do def new(conn, %{"invite" => invite_token}) do
if Invites.valid_invite_token?(invite_token) do invite = Invites.get_invite_by_token(invite_token)
conn |> render_new(invite_token)
if invite do
conn |> render_new(invite)
else else
conn conn
|> put_flash(:error, dgettext("errors", "Sorry, this invite was not found or expired")) |> put_flash(:error, dgettext("errors", "Sorry, this invite was not found or expired"))
@ -25,17 +28,19 @@ defmodule CanneryWeb.UserRegistrationController do
end end
# renders new user registration page # renders new user registration page
defp render_new(conn, invite_token \\ nil) do defp render_new(conn, invite \\ nil) do
render(conn, "new.html", render(conn, "new.html",
changeset: Accounts.change_user_registration(), changeset: Accounts.change_user_registration(%User{}),
invite_token: invite_token, invite: invite,
page_title: gettext("Register") page_title: gettext("Register")
) )
end end
def create(conn, %{"user" => %{"invite_token" => invite_token}} = attrs) do def create(conn, %{"user" => %{"invite_token" => invite_token}} = attrs) do
if Invites.valid_invite_token?(invite_token) do invite = Invites.get_invite_by_token(invite_token)
conn |> create_user(attrs, invite_token)
if invite do
conn |> create_user(attrs, invite)
else else
conn conn
|> put_flash(:error, dgettext("errors", "Sorry, this invite was not found or expired")) |> put_flash(:error, dgettext("errors", "Sorry, this invite was not found or expired"))
@ -53,9 +58,13 @@ defmodule CanneryWeb.UserRegistrationController do
end end
end end
defp create_user(conn, %{"user" => user_params}, invite_token \\ nil) do defp create_user(conn, %{"user" => user_params}, invite \\ nil) do
case Accounts.register_user(user_params, invite_token) do case Accounts.register_user(user_params) do
{:ok, user} -> {:ok, user} ->
unless invite |> is_nil() do
invite |> Invites.use_invite!()
end
Accounts.deliver_user_confirmation_instructions( Accounts.deliver_user_confirmation_instructions(
user, user,
&Routes.user_confirmation_url(conn, :confirm, &1) &Routes.user_confirmation_url(conn, :confirm, &1)
@ -65,13 +74,8 @@ defmodule CanneryWeb.UserRegistrationController do
|> put_flash(:info, dgettext("prompts", "Please check your email to verify your account")) |> put_flash(:info, dgettext("prompts", "Please check your email to verify your account"))
|> redirect(to: Routes.user_session_path(Endpoint, :new)) |> redirect(to: Routes.user_session_path(Endpoint, :new))
{:error, :invalid_token} ->
conn
|> put_flash(:error, dgettext("errors", "Sorry, this invite was not found or expired"))
|> redirect(to: Routes.live_path(Endpoint, HomeLive))
{:error, %Ecto.Changeset{} = changeset} -> {:error, %Ecto.Changeset{} = changeset} ->
conn |> render("new.html", changeset: changeset, invite_token: invite_token) conn |> render("new.html", changeset: changeset, invite: invite)
end end
end end
end end

View File

@ -73,14 +73,14 @@ defmodule CanneryWeb.AmmoGroupLive.FormComponent do
ammo_group_params ammo_group_params
) do ) do
changeset_action = changeset_action =
cond do case action do
action in [:new, :clone] -> :insert :new -> :insert
action == :edit -> :update :edit -> :update
end end
changeset = changeset =
cond do case action do
action in [:new, :clone] -> :new ->
ammo_type = ammo_type =
if ammo_group_params |> Map.has_key?("ammo_type_id"), if ammo_group_params |> Map.has_key?("ammo_type_id"),
do: ammo_group_params |> Map.get("ammo_type_id") |> Ammo.get_ammo_type!(user), do: ammo_group_params |> Map.get("ammo_type_id") |> Ammo.get_ammo_type!(user),
@ -93,8 +93,8 @@ defmodule CanneryWeb.AmmoGroupLive.FormComponent do
ammo_group |> AmmoGroup.create_changeset(ammo_type, container, user, ammo_group_params) ammo_group |> AmmoGroup.create_changeset(ammo_type, container, user, ammo_group_params)
action == :edit -> :edit ->
ammo_group |> AmmoGroup.update_changeset(ammo_group_params, user) ammo_group |> AmmoGroup.update_changeset(ammo_group_params)
end end
changeset = changeset =
@ -116,7 +116,7 @@ defmodule CanneryWeb.AmmoGroupLive.FormComponent do
case Ammo.update_ammo_group(ammo_group, ammo_group_params, current_user) do case Ammo.update_ammo_group(ammo_group, ammo_group_params, current_user) do
{:ok, _ammo_group} -> {:ok, _ammo_group} ->
prompt = dgettext("prompts", "Ammo updated successfully") prompt = dgettext("prompts", "Ammo updated successfully")
socket |> put_flash(:info, prompt) |> push_navigate(to: return_to) socket |> put_flash(:info, prompt) |> push_redirect(to: return_to)
{:error, %Changeset{} = changeset} -> {:error, %Changeset{} = changeset} ->
socket |> assign(:changeset, changeset) socket |> assign(:changeset, changeset)
@ -127,10 +127,9 @@ defmodule CanneryWeb.AmmoGroupLive.FormComponent do
defp save_ammo_group( defp save_ammo_group(
%{assigns: %{changeset: changeset}} = socket, %{assigns: %{changeset: changeset}} = socket,
action, :new,
%{"multiplier" => multiplier_str} = ammo_group_params %{"multiplier" => multiplier_str} = ammo_group_params
) ) do
when action in [:new, :clone] do
socket = socket =
case multiplier_str |> Integer.parse() do case multiplier_str |> Integer.parse() do
{multiplier, _remainder} {multiplier, _remainder}
@ -182,7 +181,7 @@ defmodule CanneryWeb.AmmoGroupLive.FormComponent do
count count
) )
socket |> put_flash(:info, prompt) |> push_navigate(to: return_to) socket |> put_flash(:info, prompt) |> push_redirect(to: return_to)
{:error, %Changeset{} = changeset} -> {:error, %Changeset{} = changeset} ->
socket |> assign(changeset: changeset) socket |> assign(changeset: changeset)

View File

@ -4,7 +4,7 @@
</h2> </h2>
<.form <.form
:let={f} let={f}
for={@changeset} for={@changeset}
id="ammo_group-form" id="ammo_group-form"
phx-target={@myself} phx-target={@myself}
@ -12,12 +12,11 @@
phx-submit="save" phx-submit="save"
class="flex flex-col space-y-4 sm:space-y-0 sm:grid sm:grid-cols-3 sm:gap-4 justify-center items-center" class="flex flex-col space-y-4 sm:space-y-0 sm:grid sm:grid-cols-3 sm:gap-4 justify-center items-center"
> >
<div <%= if @changeset.action && not @changeset.valid? do %>
:if={@changeset.action && not @changeset.valid?()} <div class="invalid-feedback col-span-3 text-center">
class="invalid-feedback col-span-3 text-center" <%= changeset_errors(@changeset) %>
> </div>
<%= changeset_errors(@changeset) %> <% end %>
</div>
<%= label(f, :ammo_type_id, gettext("Ammo type"), class: "title text-lg text-primary-600") %> <%= label(f, :ammo_type_id, gettext("Ammo type"), class: "title text-lg text-primary-600") %>
<%= select(f, :ammo_type_id, ammo_type_options(@ammo_types), <%= select(f, :ammo_type_id, ammo_type_options(@ammo_types),
@ -39,14 +38,6 @@
) %> ) %>
<%= error_tag(f, :price_paid, "col-span-3 text-center") %> <%= error_tag(f, :price_paid, "col-span-3 text-center") %>
<%= label(f, :purchased_on, gettext("Purchased on"), class: "title text-lg text-primary-600") %>
<%= date_input(f, :purchased_on,
class: "input input-primary col-span-2",
phx_update: "ignore",
value: @changeset |> Changeset.get_field(:purchased_on) || Date.utc_today()
) %>
<%= error_tag(f, :purchased_on, "col-span-3 text-center") %>
<%= label(f, :notes, gettext("Notes"), class: "title text-lg text-primary-600") %> <%= label(f, :notes, gettext("Notes"), class: "title text-lg text-primary-600") %>
<%= textarea(f, :notes, <%= textarea(f, :notes,
class: "text-center col-span-2 input input-primary", class: "text-center col-span-2 input input-primary",
@ -61,7 +52,7 @@
<%= error_tag(f, :container_id, "col-span-3 text-center") %> <%= error_tag(f, :container_id, "col-span-3 text-center") %>
<%= case @action do %> <%= case @action do %>
<% action when action in [:new, :clone] -> %> <% :new -> %>
<hr class="hr col-span-3" /> <hr class="hr col-span-3" />
<%= label(f, :multiplier, gettext("Copies"), class: "title text-lg text-primary-600") %> <%= label(f, :multiplier, gettext("Copies"), class: "title text-lg text-primary-600") %>

View File

@ -4,20 +4,17 @@ defmodule CanneryWeb.AmmoGroupLive.Index do
""" """
use CanneryWeb, :live_view use CanneryWeb, :live_view
alias Cannery.{Ammo, Ammo.AmmoGroup, Containers} alias Cannery.{Ammo, Ammo.AmmoGroup, Containers, Repo}
alias CanneryWeb.Endpoint
@impl true @impl true
def mount(%{"search" => search}, _session, socket) do
{:ok, socket |> assign(show_used: false, search: search) |> display_ammo_groups()}
end
def mount(_params, _session, socket) do def mount(_params, _session, socket) do
{:ok, socket |> assign(show_used: false, search: nil) |> display_ammo_groups()} {:ok, socket |> display_ammo_groups()}
end end
@impl true @impl true
def handle_params(params, _url, %{assigns: %{live_action: live_action}} = socket) do def handle_params(params, _url, %{assigns: %{live_action: live_action}} = socket) do
{:noreply, apply_action(socket, live_action, params) |> display_ammo_groups()} {:noreply, apply_action(socket, live_action, params)}
end end
defp apply_action( defp apply_action(
@ -26,67 +23,37 @@ defmodule CanneryWeb.AmmoGroupLive.Index do
%{"id" => id} %{"id" => id}
) do ) do
socket socket
|> assign( |> assign(:page_title, gettext("Record shots"))
page_title: gettext("Record shots"), |> assign(:ammo_group, Ammo.get_ammo_group!(id, current_user))
ammo_group: Ammo.get_ammo_group!(id, current_user)
)
end end
defp apply_action(%{assigns: %{current_user: current_user}} = socket, :move, %{"id" => id}) do defp apply_action(%{assigns: %{current_user: current_user}} = socket, :move, %{"id" => id}) do
socket socket
|> assign( |> assign(:page_title, gettext("Move Ammo group"))
page_title: gettext("Move ammo"), |> assign(:ammo_group, Ammo.get_ammo_group!(id, current_user))
ammo_group: Ammo.get_ammo_group!(id, current_user)
)
end end
defp apply_action(%{assigns: %{current_user: current_user}} = socket, :edit, %{"id" => id}) do defp apply_action(%{assigns: %{current_user: current_user}} = socket, :edit, %{"id" => id}) do
socket socket
|> assign( |> assign(:page_title, gettext("Edit Ammo group"))
page_title: gettext("Edit ammo"), |> assign(:ammo_group, Ammo.get_ammo_group!(id, current_user))
ammo_group: Ammo.get_ammo_group!(id, current_user)
)
end
defp apply_action(%{assigns: %{current_user: current_user}} = socket, :clone, %{"id" => id}) do
socket
|> assign(
page_title: dgettext("actions", "Add Ammo"),
ammo_group: %{Ammo.get_ammo_group!(id, current_user) | id: nil}
)
end end
defp apply_action(socket, :new, _params) do defp apply_action(socket, :new, _params) do
socket socket
|> assign( |> assign(:page_title, dgettext("actions", "Add Ammo"))
page_title: dgettext("actions", "Add Ammo"), |> assign(:ammo_group, %AmmoGroup{})
ammo_group: %AmmoGroup{}
)
end end
defp apply_action(socket, :index, _params) do defp apply_action(socket, :index, _params) do
socket socket |> assign(:page_title, gettext("Ammo groups")) |> assign(:ammo_group, nil)
|> assign(
page_title: gettext("Ammo"),
search: nil,
ammo_group: nil
)
end
defp apply_action(socket, :search, %{"search" => search}) do
socket
|> assign(
page_title: gettext("Ammo"),
search: search,
ammo_group: nil
)
end end
@impl true @impl true
def handle_event("delete", %{"id" => id}, %{assigns: %{current_user: current_user}} = socket) do def handle_event("delete", %{"id" => id}, %{assigns: %{current_user: current_user}} = socket) do
Ammo.get_ammo_group!(id, current_user) |> Ammo.delete_ammo_group!(current_user) Ammo.get_ammo_group!(id, current_user) |> Ammo.delete_ammo_group!(current_user)
prompt = dgettext("prompts", "Ammo deleted succesfully") prompt = dgettext("prompts", "Ammo group deleted succesfully")
{:noreply, socket |> put_flash(:info, prompt) |> display_ammo_groups()} {:noreply, socket |> put_flash(:info, prompt) |> display_ammo_groups()}
end end
@ -105,35 +72,140 @@ defmodule CanneryWeb.AmmoGroupLive.Index do
{:noreply, socket |> display_ammo_groups()} {:noreply, socket |> display_ammo_groups()}
end end
@impl true defp display_ammo_groups(%{assigns: %{current_user: current_user}} = socket) do
def handle_event("toggle_show_used", _params, %{assigns: %{show_used: show_used}} = socket) do ammo_groups = Ammo.list_ammo_groups(current_user) |> Repo.preload([:ammo_type, :container])
{:noreply, socket |> assign(:show_used, !show_used) |> display_ammo_groups()}
end
@impl true
def handle_event("search", %{"search" => %{"search_term" => ""}}, socket) do
{:noreply, socket |> push_patch(to: Routes.ammo_group_index_path(Endpoint, :index))}
end
def handle_event("search", %{"search" => %{"search_term" => search_term}}, socket) do
socket =
socket |> push_patch(to: Routes.ammo_group_index_path(Endpoint, :search, search_term))
{:noreply, socket}
end
defp display_ammo_groups(
%{assigns: %{search: search, current_user: current_user, show_used: show_used}} = socket
) do
ammo_groups = Ammo.list_ammo_groups(search, show_used, current_user)
ammo_types_count = Ammo.get_ammo_types_count!(current_user) ammo_types_count = Ammo.get_ammo_types_count!(current_user)
containers_count = Containers.get_containers_count!(current_user) containers_count = Containers.get_containers_count!(current_user)
columns = [
%{label: gettext("Ammo type"), key: "ammo_type"},
%{label: gettext("Count"), key: "count"},
%{label: gettext("Price paid"), key: "price_paid"},
%{label: gettext("% left"), key: "remaining"},
%{label: gettext("Range"), key: "range"},
%{label: gettext("Container"), key: "container"},
%{label: gettext("Added on"), key: "added_on"},
%{label: nil, key: "actions", sortable: false}
]
rows =
ammo_groups
|> Enum.map(fn ammo_group -> ammo_group |> get_row_data_for_ammo_group(columns) end)
socket socket
|> assign( |> assign(
ammo_groups: ammo_groups, ammo_groups: ammo_groups,
ammo_types_count: ammo_types_count, ammo_types_count: ammo_types_count,
containers_count: containers_count containers_count: containers_count,
columns: columns,
rows: rows
) )
end end
@spec get_row_data_for_ammo_group(AmmoGroup.t(), [map()]) :: [map()]
defp get_row_data_for_ammo_group(ammo_group, columns) do
ammo_group = ammo_group |> Repo.preload([:ammo_type, :container])
columns
|> Enum.into(%{}, fn %{key: key} -> {key, get_value_for_key(key, ammo_group)} end)
end
@spec get_value_for_key(String.t(), AmmoGroup.t()) :: any()
defp get_value_for_key("ammo_type", %{ammo_type: ammo_type}) do
{ammo_type.name,
live_patch(ammo_type.name,
to: Routes.ammo_type_show_path(Endpoint, :show, ammo_type),
class: "link"
)}
end
defp get_value_for_key("price_paid", %{price_paid: nil}), do: {"a", nil}
defp get_value_for_key("price_paid", %{price_paid: price_paid}),
do: gettext("$%{amount}", amount: price_paid |> :erlang.float_to_binary(decimals: 2))
defp get_value_for_key("added_on", %{inserted_at: inserted_at}) do
assigns = %{inserted_at: inserted_at}
{inserted_at,
~H"""
<%= @inserted_at |> display_datetime() %>
"""}
end
defp get_value_for_key("range", %{staged: staged} = ammo_group) do
assigns = %{ammo_group: ammo_group}
{staged,
~H"""
<div class="min-w-20 py-2 px-4 h-full flex flex-col justify-center items-center">
<button
type="button"
class="mx-2 my-1 btn btn-primary"
phx-click="toggle_staged"
phx-value-ammo_group_id={ammo_group.id}
>
<%= if ammo_group.staged, do: gettext("Unstage"), else: gettext("Stage") %>
</button>
<%= live_patch(dgettext("actions", "Record shots"),
to: Routes.ammo_group_index_path(Endpoint, :add_shot_group, ammo_group),
class: "mx-2 my-1 btn btn-primary"
) %>
</div>
"""}
end
defp get_value_for_key("remaining", ammo_group),
do: "#{ammo_group |> Ammo.get_percentage_remaining()}%"
defp get_value_for_key("actions", ammo_group) do
assigns = %{ammo_group: ammo_group}
~H"""
<div class="py-2 px-4 h-full space-x-4 flex justify-center items-center">
<%= live_redirect to: Routes.ammo_group_show_path(Endpoint, :show, ammo_group),
class: "text-primary-600 link",
data: [qa: "view-#{ammo_group.id}"] do %>
<i class="fa-fw fa-lg fas fa-eye"></i>
<% end %>
<%= live_patch to: Routes.ammo_group_index_path(Endpoint, :edit, ammo_group),
class: "text-primary-600 link",
data: [qa: "edit-#{ammo_group.id}"] do %>
<i class="fa-fw fa-lg fas fa-edit"></i>
<% end %>
<%= link to: "#",
class: "text-primary-600 link",
phx_click: "delete",
phx_value_id: ammo_group.id,
data: [
confirm: dgettext("prompts", "Are you sure you want to delete this ammo?"),
qa: "delete-#{ammo_group.id}"
] do %>
<i class="fa-fw fa-lg fas fa-trash"></i>
<% end %>
</div>
"""
end
defp get_value_for_key("container", %{container: nil}), do: {nil, nil}
defp get_value_for_key("container", %{container: %{name: container_name}} = ammo_group) do
assigns = %{ammo_group: ammo_group}
{container_name,
~H"""
<div class="min-w-20 py-2 px-4 h-full space-x-4 flex justify-center items-center">
<%= live_patch(@ammo_group.container.name,
to: Routes.ammo_group_index_path(Endpoint, :move, @ammo_group),
class: "btn btn-primary"
) %>
</div>
"""}
end
defp get_value_for_key(key, ammo_group),
do: ammo_group |> Map.get(key |> String.to_existing_atom())
end end

View File

@ -3,13 +3,12 @@
<%= gettext("Ammo") %> <%= gettext("Ammo") %>
</h1> </h1>
<h2 <%= if @ammo_groups |> Enum.empty?() do %>
:if={@ammo_groups |> Enum.empty?() and @search |> is_nil()} <h2 class="title text-xl text-primary-600">
class="title text-xl text-primary-600" <%= gettext("No Ammo") %>
> <%= display_emoji("😔") %>
<%= gettext("No Ammo") %> </h2>
<%= display_emoji("😔") %> <% end %>
</h2>
<%= cond do %> <%= cond do %>
<% @containers_count == 0 -> %> <% @containers_count == 0 -> %>
@ -18,9 +17,10 @@
<%= dgettext("prompts", "You'll need to") %> <%= dgettext("prompts", "You'll need to") %>
</h2> </h2>
<.link navigate={Routes.container_index_path(Endpoint, :new)} class="btn btn-primary"> <%= live_patch(dgettext("actions", "add a container first"),
<%= dgettext("actions", "add a container first") %> to: Routes.container_index_path(Endpoint, :new),
</.link> class: "btn btn-primary"
) %>
</div> </div>
<% @ammo_types_count == 0 -> %> <% @ammo_types_count == 0 -> %>
<div class="flex justify-center items-center"> <div class="flex justify-center items-center">
@ -28,141 +28,36 @@
<%= dgettext("prompts", "You'll need to") %> <%= dgettext("prompts", "You'll need to") %>
</h2> </h2>
<.link navigate={Routes.ammo_type_index_path(Endpoint, :new)} class="btn btn-primary"> <%= live_patch(dgettext("actions", "add an ammo type first"),
<%= dgettext("actions", "add an ammo type first") %> to: Routes.ammo_type_index_path(Endpoint, :new),
</.link> class: "btn btn-primary"
) %>
</div> </div>
<% @ammo_groups |> Enum.empty?() and @search |> is_nil() -> %> <% @ammo_groups |> Enum.empty?() -> %>
<.link patch={Routes.ammo_group_index_path(Endpoint, :new)} class="btn btn-primary"> <%= live_patch(dgettext("actions", "Add your first box!"),
<%= dgettext("actions", "Add your first box!") %> to: Routes.ammo_group_index_path(Endpoint, :new),
</.link> class: "btn btn-primary"
) %>
<% true -> %> <% true -> %>
<.link patch={Routes.ammo_group_index_path(Endpoint, :new)} class="btn btn-primary"> <%= live_patch(dgettext("actions", "Add Ammo"),
<%= dgettext("actions", "Add Ammo") %> to: Routes.ammo_group_index_path(Endpoint, :new),
</.link> class: "btn btn-primary"
) %>
<% end %> <% end %>
<div class="w-full flex flex-col sm:flex-row justify-center items-center space-y-4 sm:space-y-0 sm:space-x-4 max-w-xl"> <%= unless @ammo_groups |> Enum.empty?() do %>
<.form
:let={f}
for={:search}
phx-change="search"
phx-submit="search"
class="grow self-stretch flex flex-col items-stretch"
data-qa="ammo_group_search"
>
<%= text_input(f, :search_term,
class: "input input-primary",
value: @search,
phx_debounce: 300,
placeholder: gettext("Search ammo")
) %>
</.form>
<.toggle_button action="toggle_show_used" value={@show_used}>
<span class="title text-lg text-primary-600">
<%= gettext("Show used") %>
</span>
</.toggle_button>
</div>
<%= if @ammo_groups |> Enum.empty?() do %>
<h2 class="title text-xl text-primary-600">
<%= gettext("No Ammo") %>
<%= display_emoji("😔") %>
</h2>
<% else %>
<.live_component <.live_component
module={CanneryWeb.Components.AmmoGroupTableComponent} module={CanneryWeb.Components.TableComponent}
id="ammo-group-index-table" id="ammo_groups_index_table"
ammo_groups={@ammo_groups} action={@live_action}
current_user={@current_user} columns={@columns}
> rows={@rows}
<:ammo_type :let={%{name: ammo_type_name} = ammo_type}> />
<.link navigate={Routes.ammo_type_show_path(Endpoint, :show, ammo_type)} class="link">
<%= ammo_type_name %>
</.link>
</:ammo_type>
<:range :let={ammo_group}>
<div class="min-w-20 py-2 px-4 h-full flex flew-wrap justify-center items-center">
<button
type="button"
class="mx-2 my-1 text-sm btn btn-primary"
phx-click="toggle_staged"
phx-value-ammo_group_id={ammo_group.id}
>
<%= if ammo_group.staged, do: gettext("Unstage"), else: gettext("Stage") %>
</button>
<.link
patch={Routes.ammo_group_index_path(Endpoint, :add_shot_group, ammo_group)}
class="mx-2 my-1 text-sm btn btn-primary"
>
<%= dgettext("actions", "Record shots") %>
</.link>
</div>
</:range>
<:container :let={%{container: %{name: container_name} = container} = ammo_group}>
<div class="min-w-20 py-2 px-4 h-full flex flew-wrap justify-center items-center">
<.link
navigate={Routes.container_show_path(Endpoint, :show, container)}
class="mx-2 my-1 link"
>
<%= container_name %>
</.link>
<.link
patch={Routes.ammo_group_index_path(Endpoint, :move, ammo_group)}
class="mx-2 my-1 text-sm btn btn-primary"
>
<%= gettext("Move ammo") %>
</.link>
</div>
</:container>
<:actions :let={ammo_group}>
<div class="py-2 px-4 h-full space-x-4 flex justify-center items-center">
<.link
navigate={Routes.ammo_group_show_path(Endpoint, :show, ammo_group)}
class="text-primary-600 link"
data-qa={"view-#{ammo_group.id}"}
>
<i class="fa-fw fa-lg fas fa-eye"></i>
</.link>
<.link
patch={Routes.ammo_group_index_path(Endpoint, :edit, ammo_group)}
class="text-primary-600 link"
data-qa={"edit-#{ammo_group.id}"}
>
<i class="fa-fw fa-lg fas fa-edit"></i>
</.link>
<.link
patch={Routes.ammo_group_index_path(Endpoint, :clone, ammo_group)}
class="text-primary-600 link"
data-qa={"clone-#{ammo_group.id}"}
>
<i class="fa-fw fa-lg fas fa-copy"></i>
</.link>
<.link
href="#"
class="text-primary-600 link"
phx-click="delete"
phx-value-id={ammo_group.id}
data-confirm={dgettext("prompts", "Are you sure you want to delete this ammo?")}
data-qa={"delete-#{ammo_group.id}"}
>
<i class="fa-fw fa-lg fas fa-trash"></i>
</.link>
</div>
</:actions>
</.live_component>
<% end %> <% end %>
</div> </div>
<%= cond do %> <%= cond do %>
<% @live_action in [:new, :edit, :clone] -> %> <% @live_action in [:new, :edit] -> %>
<.modal return_to={Routes.ammo_group_index_path(Endpoint, :index)}> <.modal return_to={Routes.ammo_group_index_path(Endpoint, :index)}>
<.live_component <.live_component
module={CanneryWeb.AmmoGroupLive.FormComponent} module={CanneryWeb.AmmoGroupLive.FormComponent}

View File

@ -30,12 +30,7 @@ defmodule CanneryWeb.AmmoGroupLive.Show do
@impl true @impl true
def handle_params(%{"id" => id}, _url, %{assigns: %{live_action: live_action}} = socket) do def handle_params(%{"id" => id}, _url, %{assigns: %{live_action: live_action}} = socket) do
socket = {:noreply, socket |> assign(page_title: page_title(live_action)) |> display_ammo_group(id)}
socket
|> assign(page_title: page_title(live_action))
|> display_ammo_group(id)
{:noreply, socket}
end end
defp page_title(:add_shot_group), do: gettext("Record Shots") defp page_title(:add_shot_group), do: gettext("Record Shots")
@ -55,7 +50,7 @@ defmodule CanneryWeb.AmmoGroupLive.Show do
prompt = dgettext("prompts", "Ammo deleted succesfully") prompt = dgettext("prompts", "Ammo deleted succesfully")
redirect_to = Routes.ammo_group_index_path(socket, :index) redirect_to = Routes.ammo_group_index_path(socket, :index)
{:noreply, socket |> put_flash(:info, prompt) |> push_navigate(to: redirect_to)} {:noreply, socket |> put_flash(:info, prompt) |> push_redirect(to: redirect_to)}
end end
@impl true @impl true
@ -74,14 +69,14 @@ defmodule CanneryWeb.AmmoGroupLive.Show do
def handle_event( def handle_event(
"delete_shot_group", "delete_shot_group",
%{"id" => id}, %{"id" => id},
%{assigns: %{ammo_group: %{id: ammo_group_id}, current_user: current_user}} = socket %{assigns: %{ammo_group: ammo_group, current_user: current_user}} = socket
) do ) do
{:ok, _} = {:ok, _} =
ActivityLog.get_shot_group!(id, current_user) ActivityLog.get_shot_group!(id, current_user)
|> ActivityLog.delete_shot_group(current_user) |> ActivityLog.delete_shot_group(current_user)
prompt = dgettext("prompts", "Shot records deleted succesfully") prompt = dgettext("prompts", "Shot records deleted succesfully")
{:noreply, socket |> put_flash(:info, prompt) |> display_ammo_group(ammo_group_id)} {:noreply, socket |> put_flash(:info, prompt) |> display_ammo_group(ammo_group)}
end end
@spec display_ammo_group(Socket.t(), AmmoGroup.t() | AmmoGroup.id()) :: Socket.t() @spec display_ammo_group(Socket.t(), AmmoGroup.t() | AmmoGroup.id()) :: Socket.t()
@ -89,10 +84,10 @@ defmodule CanneryWeb.AmmoGroupLive.Show do
ammo_group = ammo_group |> Repo.preload([:container, :ammo_type, :shot_groups], force: true) ammo_group = ammo_group |> Repo.preload([:container, :ammo_type, :shot_groups], force: true)
columns = [ columns = [
%{label: gettext("Rounds shot"), key: :count}, %{label: gettext("Rounds shot"), key: "count"},
%{label: gettext("Notes"), key: :notes}, %{label: gettext("Notes"), key: "notes"},
%{label: gettext("Date"), key: :date}, %{label: gettext("Date"), key: "date"},
%{label: nil, key: :actions, sortable: false} %{label: nil, key: "actions", sortable: false}
] ]
rows = rows =
@ -107,48 +102,41 @@ defmodule CanneryWeb.AmmoGroupLive.Show do
defp display_ammo_group(%{assigns: %{current_user: current_user}} = socket, id), defp display_ammo_group(%{assigns: %{current_user: current_user}} = socket, id),
do: display_ammo_group(socket, Ammo.get_ammo_group!(id, current_user)) do: display_ammo_group(socket, Ammo.get_ammo_group!(id, current_user))
@spec get_table_row_for_shot_group(AmmoGroup.t(), ShotGroup.t(), [map()]) :: map() @spec get_table_row_for_shot_group(AmmoGroup.t(), ShotGroup.t(), [map()]) :: [map()]
defp get_table_row_for_shot_group(ammo_group, %{date: date} = shot_group, columns) do defp get_table_row_for_shot_group(ammo_group, %{date: date} = shot_group, columns) do
assigns = %{ammo_group: ammo_group, shot_group: shot_group} assigns = %{ammo_group: ammo_group, shot_group: shot_group}
columns columns
|> Map.new(fn %{key: key} -> |> Enum.into(%{}, fn %{key: key} ->
value = value =
case key do case key do
:date -> "date" ->
assigns = %{date: date} {date, date |> display_date()}
{date, "actions" ->
~H"""
<.date date={@date} />
"""}
:actions ->
~H""" ~H"""
<div class="px-4 py-2 space-x-4 flex justify-center items-center"> <div class="px-4 py-2 space-x-4 flex justify-center items-center">
<.link <%= live_patch to: Routes.ammo_group_show_path(Endpoint, :edit_shot_group, @ammo_group, shot_group),
patch={Routes.ammo_group_show_path(Endpoint, :edit_shot_group, @ammo_group, @shot_group)} class: "text-primary-600 link",
class="text-primary-600 link" data: [qa: "edit-#{shot_group.id}"] do %>
data-qa={"edit-#{@shot_group.id}"}
>
<i class="fa-fw fa-lg fas fa-edit"></i> <i class="fa-fw fa-lg fas fa-edit"></i>
</.link> <% end %>
<.link <%= link to: "#",
href="#" class: "text-primary-600 link",
class="text-primary-600 link" phx_click: "delete_shot_group",
phx-click="delete_shot_group" phx_value_id: shot_group.id,
phx-value-id={@shot_group.id} data: [
data-confirm={dgettext("prompts", "Are you sure you want to delete this shot record?")} confirm: dgettext("prompts", "Are you sure you want to delete this shot record?"),
data-qa={"delete-#{@shot_group.id}"} qa: "delete-#{shot_group.id}"
> ] do %>
<i class="fa-fw fa-lg fas fa-trash"></i> <i class="fa-fw fa-lg fas fa-trash"></i>
</.link> <% end %>
</div> </div>
""" """
key -> key ->
shot_group |> Map.get(key) shot_group |> Map.get(key |> String.to_existing_atom())
end end
{key, value} {key, value}

View File

@ -11,12 +11,12 @@
<span class="rounded-lg title text-lg"> <span class="rounded-lg title text-lg">
<%= gettext("Original count:") %> <%= gettext("Original count:") %>
<%= Ammo.get_original_count(@ammo_group) %> <%= @ammo_group.count + Ammo.get_used_count(@ammo_group) %>
</span> </span>
<span class="rounded-lg title text-lg"> <span class="rounded-lg title text-lg">
<%= gettext("Percentage left:") %> <%= gettext("Percentage left:") %>
<%= gettext("%{percentage}%", percentage: @ammo_group |> Ammo.get_percentage_remaining()) %> <%= "#{@ammo_group |> Ammo.get_percentage_remaining()}%" %>
</span> </span>
<%= if @ammo_group.notes do %> <%= if @ammo_group.notes do %>
@ -27,8 +27,8 @@
<% end %> <% end %>
<span class="rounded-lg title text-lg"> <span class="rounded-lg title text-lg">
<%= gettext("Purchased on:") %> <%= gettext("Added on:") %>
<.date date={@ammo_group.purchased_on} /> <%= @ammo_group.inserted_at |> display_datetime() %>
</span> </span>
<%= if @ammo_group.price_paid do %> <%= if @ammo_group.price_paid do %>
@ -52,54 +52,46 @@
<div class="flex flex-col justify-center items-center"> <div class="flex flex-col justify-center items-center">
<div class="flex flex-wrap justify-center items-center text-primary-600"> <div class="flex flex-wrap justify-center items-center text-primary-600">
<.link <%= live_patch(dgettext("actions", "View in Catalog"),
navigate={Routes.ammo_type_show_path(Endpoint, :show, @ammo_group.ammo_type)} to: Routes.ammo_type_show_path(Endpoint, :show, @ammo_group.ammo_type),
class="mx-4 my-2 btn btn-primary" class: "mx-4 my-2 btn btn-primary",
data-qa="details" data: [qa: "details"]
> ) %>
<%= dgettext("actions", "View in Catalog") %>
</.link>
<.link <%= live_patch to: Routes.ammo_group_show_path(Endpoint, :edit, @ammo_group),
patch={Routes.ammo_group_show_path(Endpoint, :edit, @ammo_group)} class: "mx-4 my-2 text-primary-600 link",
class="mx-4 my-2 text-primary-600 link" data: [qa: "edit"] do %>
data-qa="edit"
>
<i class="fa-fw fa-lg fas fa-edit"></i> <i class="fa-fw fa-lg fas fa-edit"></i>
</.link> <% end %>
<.link <%= link to: "#",
href="#" class: "mx-4 my-2 text-primary-600 link",
class="mx-4 my-2 text-primary-600 link" phx_click: "delete",
phx-click="delete" data: [
data-confirm={dgettext("prompts", "Are you sure you want to delete this ammo?")} confirm: dgettext("prompts", "Are you sure you want to delete this ammo?"),
data-qa="delete" qa: "delete"
> ] do %>
<i class="fa-fw fa-lg fas fa-trash"></i> <i class="fa-fw fa-lg fas fa-trash"></i>
</.link> <% end %>
</div> </div>
<div class="flex flex-wrap justify-center items-center text-primary-600"> <div class="flex flex-wrap justify-center items-center text-primary-600">
<button type="button" class="mx-4 my-2 btn btn-primary" phx-click="toggle_staged"> <button type="button" class="mx-4 my-2 btn btn-primary" phx-click="toggle_staged">
<%= if @ammo_group.staged, <%= if @ammo_group.staged,
do: dgettext("actions", "Unstage from range"), do: gettext("Unstage from range"),
else: dgettext("actions", "Stage for range") %> else: gettext("Stage for range") %>
</button> </button>
<.link <%= live_patch(dgettext("actions", "Move containers"),
patch={Routes.ammo_group_show_path(Endpoint, :move, @ammo_group)} to: Routes.ammo_group_show_path(Endpoint, :move, @ammo_group),
class="btn btn-primary" class: "btn btn-primary",
data-qa="move" data: [qa: "move"]
> ) %>
<%= dgettext("actions", "Move containers") %>
</.link>
<.link <%= live_patch(dgettext("actions", "Record shots"),
patch={Routes.ammo_group_show_path(Endpoint, :add_shot_group, @ammo_group)} to: Routes.ammo_group_show_path(Endpoint, :add_shot_group, @ammo_group),
class="mx-4 my-2 btn btn-primary" class: "mx-4 my-2 btn btn-primary"
> ) %>
<%= dgettext("actions", "Record shots") %>
</.link>
</div> </div>
</div> </div>

View File

@ -35,15 +35,15 @@ defmodule CanneryWeb.AmmoTypeLive.FormComponent do
ammo_type_params ammo_type_params
) do ) do
changeset_action = changeset_action =
cond do case action do
action in [:new, :clone] -> :insert :new -> :insert
action == :edit -> :update :edit -> :update
end end
changeset = changeset =
cond do case action do
action in [:new, :clone] -> ammo_type |> AmmoType.create_changeset(user, ammo_type_params) :new -> ammo_type |> AmmoType.create_changeset(user, ammo_type_params)
action == :edit -> ammo_type |> AmmoType.update_changeset(ammo_type_params) :edit -> ammo_type |> AmmoType.update_changeset(ammo_type_params)
end end
changeset = changeset =
@ -65,7 +65,7 @@ defmodule CanneryWeb.AmmoTypeLive.FormComponent do
case Ammo.update_ammo_type(ammo_type, ammo_type_params, current_user) do case Ammo.update_ammo_type(ammo_type, ammo_type_params, current_user) do
{:ok, %{name: ammo_type_name}} -> {:ok, %{name: ammo_type_name}} ->
prompt = dgettext("prompts", "%{name} updated successfully", name: ammo_type_name) prompt = dgettext("prompts", "%{name} updated successfully", name: ammo_type_name)
socket |> put_flash(:info, prompt) |> push_navigate(to: return_to) socket |> put_flash(:info, prompt) |> push_redirect(to: return_to)
{:error, %Changeset{} = changeset} -> {:error, %Changeset{} = changeset} ->
socket |> assign(:changeset, changeset) socket |> assign(:changeset, changeset)
@ -76,15 +76,14 @@ defmodule CanneryWeb.AmmoTypeLive.FormComponent do
defp save_ammo_type( defp save_ammo_type(
%{assigns: %{current_user: current_user, return_to: return_to}} = socket, %{assigns: %{current_user: current_user, return_to: return_to}} = socket,
action, :new,
ammo_type_params ammo_type_params
) ) do
when action in [:new, :clone] do
socket = socket =
case Ammo.create_ammo_type(ammo_type_params, current_user) do case Ammo.create_ammo_type(ammo_type_params, current_user) do
{:ok, %{name: ammo_type_name}} -> {:ok, %{name: ammo_type_name}} ->
prompt = dgettext("prompts", "%{name} created successfully", name: ammo_type_name) prompt = dgettext("prompts", "%{name} created successfully", name: ammo_type_name)
socket |> put_flash(:info, prompt) |> push_navigate(to: return_to) socket |> put_flash(:info, prompt) |> push_redirect(to: return_to)
{:error, %Changeset{} = changeset} -> {:error, %Changeset{} = changeset} ->
socket |> assign(changeset: changeset) socket |> assign(changeset: changeset)

View File

@ -3,7 +3,7 @@
<%= @title %> <%= @title %>
</h2> </h2>
<.form <.form
:let={f} let={f}
for={@changeset} for={@changeset}
id="ammo_type-form" id="ammo_type-form"
phx-target={@myself} phx-target={@myself}
@ -11,12 +11,11 @@
phx-submit="save" phx-submit="save"
class="flex flex-col space-y-4 sm:space-y-0 sm:grid sm:grid-cols-3 sm:gap-4 justify-center items-center" class="flex flex-col space-y-4 sm:space-y-0 sm:grid sm:grid-cols-3 sm:gap-4 justify-center items-center"
> >
<div <%= if @changeset.action && not @changeset.valid? do %>
:if={@changeset.action && not @changeset.valid?()} <div class="invalid-feedback col-span-3 text-center">
class="invalid-feedback col-span-3 text-center" <%= changeset_errors(@changeset) %>
> </div>
<%= changeset_errors(@changeset) %> <% end %>
</div>
<%= label(f, :name, gettext("Name"), class: "title text-lg text-primary-600") %> <%= label(f, :name, gettext("Name"), class: "title text-lg text-primary-600") %>
<%= text_input(f, :name, class: "text-center col-span-2 input input-primary") %> <%= text_input(f, :name, class: "text-center col-span-2 input input-primary") %>

View File

@ -4,15 +4,13 @@ defmodule CanneryWeb.AmmoTypeLive.Index do
""" """
use CanneryWeb, :live_view use CanneryWeb, :live_view
alias Cannery.{Ammo, Ammo.AmmoType} alias Cannery.{Ammo, Ammo.AmmoType}
alias CanneryWeb.Endpoint
@impl true @impl true
def mount(%{"search" => search}, _session, socket) do
{:ok, socket |> assign(show_used: false, search: search) |> list_ammo_types()}
end
def mount(_params, _session, socket) do def mount(_params, _session, socket) do
{:ok, socket |> assign(show_used: false, search: nil) |> list_ammo_types()} {:ok, socket |> list_ammo_types()}
end end
@impl true @impl true
@ -21,49 +19,19 @@ defmodule CanneryWeb.AmmoTypeLive.Index do
end end
defp apply_action(%{assigns: %{current_user: current_user}} = socket, :edit, %{"id" => id}) do defp apply_action(%{assigns: %{current_user: current_user}} = socket, :edit, %{"id" => id}) do
%{name: ammo_type_name} = ammo_type = Ammo.get_ammo_type!(id, current_user)
socket socket
|> assign( |> assign(:page_title, gettext("Edit Ammo type"))
page_title: gettext("Edit %{ammo_type_name}", ammo_type_name: ammo_type_name), |> assign(:ammo_type, Ammo.get_ammo_type!(id, current_user))
ammo_type: ammo_type
)
end
defp apply_action(%{assigns: %{current_user: current_user}} = socket, :clone, %{"id" => id}) do
socket
|> assign(
page_title: gettext("New Ammo type"),
ammo_type: %{Ammo.get_ammo_type!(id, current_user) | id: nil}
)
end end
defp apply_action(socket, :new, _params) do defp apply_action(socket, :new, _params) do
socket socket
|> assign( |> assign(:page_title, gettext("New Ammo type"))
page_title: gettext("New Ammo type"), |> assign(:ammo_type, %AmmoType{})
ammo_type: %AmmoType{}
)
end end
defp apply_action(socket, :index, _params) do defp apply_action(socket, :index, _params) do
socket socket |> assign(:page_title, gettext("Ammo types")) |> assign(:ammo_type, nil)
|> assign(
page_title: gettext("Catalog"),
search: nil,
ammo_type: nil
)
|> list_ammo_types()
end
defp apply_action(socket, :search, %{"search" => search}) do
socket
|> assign(
page_title: gettext("Catalog"),
search: search,
ammo_type: nil
)
|> list_ammo_types()
end end
@impl true @impl true
@ -75,22 +43,111 @@ defmodule CanneryWeb.AmmoTypeLive.Index do
{:noreply, socket |> put_flash(:info, prompt) |> list_ammo_types()} {:noreply, socket |> put_flash(:info, prompt) |> list_ammo_types()}
end end
@impl true defp list_ammo_types(%{assigns: %{current_user: current_user}} = socket) do
def handle_event("toggle_show_used", _params, %{assigns: %{show_used: show_used}} = socket) do ammo_types = Ammo.list_ammo_types(current_user)
{:noreply, socket |> assign(:show_used, !show_used) |> list_ammo_types()}
columns =
[
%{label: gettext("Name"), key: "name", type: :string},
%{label: gettext("Bullet type"), key: "bullet_type", type: :string},
%{label: gettext("Bullet core"), key: "bullet_core", type: :string},
%{label: gettext("Cartridge"), key: "cartridge", type: :string},
%{label: gettext("Caliber"), key: "caliber", type: :string},
%{label: gettext("Case material"), key: "case_material", type: :string},
%{label: gettext("Jacket type"), key: "jacket_type", type: :string},
%{label: gettext("Muzzle velocity"), key: "muzzle_velocity", type: :string},
%{label: gettext("Powder type"), key: "powder_type", type: :string},
%{
label: gettext("Powder grains per charge"),
key: "powder_grains_per_charge",
type: :string
},
%{label: gettext("Grains"), key: "grains", type: :string},
%{label: gettext("Pressure"), key: "pressure", type: :string},
%{label: gettext("Primer type"), key: "primer_type", type: :string},
%{label: gettext("Firing type"), key: "firing_type", type: :string},
%{label: gettext("Tracer"), key: "tracer", type: :boolean},
%{label: gettext("Incendiary"), key: "incendiary", type: :boolean},
%{label: gettext("Blank"), key: "blank", type: :boolean},
%{label: gettext("Corrosive"), key: "corrosive", type: :boolean},
%{label: gettext("Manufacturer"), key: "manufacturer", type: :string},
%{label: gettext("UPC"), key: "upc", type: :string}
]
|> Enum.filter(fn %{key: key, type: type} ->
# remove columns if all values match defaults
default_value = if type == :boolean, do: false, else: nil
ammo_types
|> Enum.any?(fn ammo_type ->
not (ammo_type |> Map.get(key |> String.to_existing_atom()) == default_value)
end)
end)
|> Kernel.++([
%{label: gettext("Total # of rounds"), key: "round_count", type: :round_count},
%{label: gettext("Average Price paid"), key: "avg_price_paid", type: :avg_price_paid},
%{label: nil, key: "actions", type: :actions, sortable: false}
])
rows =
ammo_types
|> Enum.map(fn ammo_type -> ammo_type |> get_ammo_type_values(columns, current_user) end)
socket |> assign(columns: columns, rows: rows)
end end
@impl true defp get_ammo_type_values(ammo_type, columns, current_user) do
def handle_event("search", %{"search" => %{"search_term" => ""}}, socket) do columns
{:noreply, socket |> push_patch(to: Routes.ammo_type_index_path(Endpoint, :index))} |> Enum.into(%{}, fn %{key: key, type: type} ->
{key, get_ammo_type_value(type, key, ammo_type, current_user)}
end)
end end
def handle_event("search", %{"search" => %{"search_term" => search_term}}, socket) do defp get_ammo_type_value(:boolean, key, ammo_type, _current_user),
{:noreply, do: ammo_type |> Map.get(key |> String.to_existing_atom()) |> humanize()
socket |> push_patch(to: Routes.ammo_type_index_path(Endpoint, :search, search_term))}
defp get_ammo_type_value(:round_count, _key, ammo_type, current_user),
do: ammo_type |> Ammo.get_round_count_for_ammo_type(current_user)
defp get_ammo_type_value(:avg_price_paid, _key, ammo_type, current_user) do
case ammo_type |> Ammo.get_average_cost_for_ammo_type!(current_user) do
nil -> gettext("No cost information")
count -> gettext("$%{amount}", amount: count |> :erlang.float_to_binary(decimals: 2))
end
end end
defp list_ammo_types(%{assigns: %{search: search, current_user: current_user}} = socket) do defp get_ammo_type_value(:actions, _key, ammo_type, _current_user) do
socket |> assign(ammo_types: Ammo.list_ammo_types(search, current_user)) assigns = %{ammo_type: ammo_type}
~H"""
<div class="px-4 py-2 space-x-4 flex justify-center items-center">
<%= live_redirect to: Routes.ammo_type_show_path(Endpoint, :show, ammo_type),
class: "text-primary-600 link",
data: [qa: "view-#{ammo_type.id}"] do %>
<i class="fa-fw fa-lg fas fa-eye"></i>
<% end %>
<%= live_patch to: Routes.ammo_type_index_path(Endpoint, :edit, ammo_type),
class: "text-primary-600 link",
data: [qa: "edit-#{ammo_type.id}"] do %>
<i class="fa-fw fa-lg fas fa-edit"></i>
<% end %>
<%= link to: "#",
class: "text-primary-600 link",
phx_click: "delete",
phx_value_id: ammo_type.id,
data: [
confirm: dgettext("prompts", "Are you sure you want to delete %{name}? This will delete all %{name} type ammo as well!", name: ammo_type.name),
qa: "delete-#{ammo_type.id}"
] do %>
<i class="fa-lg fas fa-trash"></i>
<% end %>
</div>
"""
end end
defp get_ammo_type_value(nil, _key, _ammo_type, _current_user), do: nil
defp get_ammo_type_value(_other, key, ammo_type, _current_user),
do: ammo_type |> Map.get(key |> String.to_existing_atom())
end end

View File

@ -3,119 +3,43 @@
<%= gettext("Catalog") %> <%= gettext("Catalog") %>
</h1> </h1>
<%= if @ammo_types |> Enum.empty?() and @search |> is_nil() do %> <%= if @rows |> Enum.empty?() do %>
<h2 class="title text-xl text-primary-600"> <h2 class="title text-xl text-primary-600">
<%= gettext("No Ammo types") %> <%= gettext("No Ammo Types") %>
<%= display_emoji("😔") %> <%= display_emoji("😔") %>
</h2> </h2>
<.link patch={Routes.ammo_type_index_path(Endpoint, :new)} class="btn btn-primary"> <%= live_patch(dgettext("actions", "Add your first type!"),
<%= dgettext("actions", "Add your first type!") %> to: Routes.ammo_type_index_path(Endpoint, :new),
</.link> class: "btn btn-primary"
) %>
<% else %> <% else %>
<.link patch={Routes.ammo_type_index_path(Endpoint, :new)} class="btn btn-primary"> <%= live_patch(dgettext("actions", "New Ammo type"),
<%= dgettext("actions", "New Ammo type") %> to: Routes.ammo_type_index_path(Endpoint, :new),
</.link> class: "btn btn-primary"
) %>
<div class="w-full flex flex-col sm:flex-row justify-center items-center space-y-4 sm:space-y-0 sm:space-x-4 max-w-xl"> <.live_component
<.form module={CanneryWeb.Components.TableComponent}
:let={f} id="ammo_types_index_table"
for={:search} action={@live_action}
phx-change="search" columns={@columns}
phx-submit="search" rows={@rows}
class="grow self-stretch flex flex-col items-stretch" />
data-qa="ammo_type_search"
>
<%= text_input(f, :search_term,
class: "input input-primary",
value: @search,
phx_debounce: 300,
placeholder: gettext("Search catalog")
) %>
</.form>
<.toggle_button action="toggle_show_used" value={@show_used}>
<span class="title text-lg text-primary-600">
<%= gettext("Show used") %>
</span>
</.toggle_button>
</div>
<%= if @ammo_types |> Enum.empty?() do %>
<h2 class="title text-xl text-primary-600">
<%= gettext("No Ammo types") %>
<%= display_emoji("😔") %>
</h2>
<% else %>
<.live_component
module={CanneryWeb.Components.AmmoTypeTableComponent}
id="ammo_types_index_table"
action={@live_action}
ammo_types={@ammo_types}
current_user={@current_user}
show_used={@show_used}
>
<:actions :let={ammo_type}>
<div class="px-4 py-2 space-x-4 flex justify-center items-center">
<.link
navigate={Routes.ammo_type_show_path(Endpoint, :show, ammo_type)}
class="text-primary-600 link"
data-qa={"view-#{ammo_type.id}"}
>
<i class="fa-fw fa-lg fas fa-eye"></i>
</.link>
<.link
patch={Routes.ammo_type_index_path(Endpoint, :edit, ammo_type)}
class="text-primary-600 link"
data-qa={"edit-#{ammo_type.id}"}
>
<i class="fa-fw fa-lg fas fa-edit"></i>
</.link>
<.link
patch={Routes.ammo_type_index_path(Endpoint, :clone, ammo_type)}
class="text-primary-600 link"
data-qa={"clone-#{ammo_type.id}"}
>
<i class="fa-fw fa-lg fas fa-copy"></i>
</.link>
<.link
href="#"
class="text-primary-600 link"
phx-click="delete"
phx-value-id={ammo_type.id}
data-confirm={
dgettext(
"prompts",
"Are you sure you want to delete %{name}? This will delete all %{name} type ammo as well!",
name: ammo_type.name
)
}
data-qa={"delete-#{ammo_type.id}"}
>
<i class="fa-lg fas fa-trash"></i>
</.link>
</div>
</:actions>
</.live_component>
<% end %>
<% end %> <% end %>
</div> </div>
<.modal <%= if @live_action in [:new, :edit] do %>
:if={@live_action in [:new, :edit, :clone]} <.modal return_to={Routes.ammo_type_index_path(Endpoint, :index)}>
return_to={Routes.ammo_type_index_path(Endpoint, :index)} <.live_component
> module={CanneryWeb.AmmoTypeLive.FormComponent}
<.live_component id={@ammo_type.id || :new}
module={CanneryWeb.AmmoTypeLive.FormComponent} title={@page_title}
id={@ammo_type.id || :new} action={@live_action}
title={@page_title} ammo_type={@ammo_type}
action={@live_action} return_to={Routes.ammo_type_index_path(Endpoint, :index)}
ammo_type={@ammo_type} current_user={@current_user}
return_to={Routes.ammo_type_index_path(Endpoint, :index)} }
current_user={@current_user} />
} </.modal>
/> <% end %>
</.modal>

View File

@ -5,41 +5,24 @@ defmodule CanneryWeb.AmmoTypeLive.Show do
use CanneryWeb, :live_view use CanneryWeb, :live_view
import CanneryWeb.Components.AmmoGroupCard import CanneryWeb.Components.AmmoGroupCard
alias Cannery.{Ammo, Ammo.AmmoType} alias Cannery.Ammo
alias CanneryWeb.Endpoint alias CanneryWeb.Endpoint
@fields_list [ @impl true
%{label: gettext("Bullet type:"), key: :bullet_type, type: :string}, def mount(_params, _session, socket), do: {:ok, socket}
%{label: gettext("Bullet core:"), key: :bullet_core, type: :string},
%{label: gettext("Cartridge:"), key: :cartridge, type: :string},
%{label: gettext("Caliber:"), key: :caliber, type: :string},
%{label: gettext("Case material:"), key: :case_material, type: :string},
%{label: gettext("Jacket type:"), key: :jacket_type, type: :string},
%{label: gettext("Muzzle velocity:"), key: :muzzle_velocity, type: :string},
%{label: gettext("Powder type:"), key: :powder_type, type: :string},
%{label: gettext("Powder grains per charge:"), key: :powder_grains_per_charge, type: :string},
%{label: gettext("Grains:"), key: :grains, type: :string},
%{label: gettext("Pressure:"), key: :pressure, type: :string},
%{label: gettext("Primer type:"), key: :primer_type, type: :string},
%{label: gettext("Firing type:"), key: :firing_type, type: :string},
%{label: gettext("Tracer:"), key: :tracer, type: :boolean},
%{label: gettext("Incendiary:"), key: :incendiary, type: :boolean},
%{label: gettext("Blank:"), key: :blank, type: :boolean},
%{label: gettext("Corrosive:"), key: :corrosive, type: :boolean},
%{label: gettext("Manufacturer:"), key: :manufacturer, type: :string},
%{label: gettext("UPC:"), key: :upc, type: :string}
]
@impl true @impl true
def mount(_params, _session, %{assigns: %{live_action: live_action}} = socket), def handle_params(%{"id" => id}, _params, %{assigns: %{current_user: current_user}} = socket) do
do: {:ok, socket |> assign(show_used: false, view_table: live_action == :table)} ammo_type = Ammo.get_ammo_type!(id, current_user)
@impl true
def handle_params(%{"id" => id}, _params, %{assigns: %{live_action: live_action}} = socket) do
socket = socket =
socket socket
|> assign(view_table: live_action == :table) |> assign(
|> display_ammo_type(id) page_title: page_title(socket.assigns.live_action),
ammo_type: ammo_type,
ammo_groups: ammo_type |> Ammo.list_ammo_groups_for_type(current_user),
avg_cost_per_round: ammo_type |> Ammo.get_average_cost_for_ammo_type!(current_user)
)
{:noreply, socket} {:noreply, socket}
end end
@ -55,67 +38,9 @@ defmodule CanneryWeb.AmmoTypeLive.Show do
prompt = dgettext("prompts", "%{name} deleted succesfully", name: ammo_type_name) prompt = dgettext("prompts", "%{name} deleted succesfully", name: ammo_type_name)
redirect_to = Routes.ammo_type_index_path(socket, :index) redirect_to = Routes.ammo_type_index_path(socket, :index)
{:noreply, socket |> put_flash(:info, prompt) |> push_navigate(to: redirect_to)} {:noreply, socket |> put_flash(:info, prompt) |> push_redirect(to: redirect_to)}
end end
@impl true defp page_title(:show), do: gettext("Show Ammo type")
def handle_event("toggle_show_used", _params, %{assigns: %{show_used: show_used}} = socket) do defp page_title(:edit), do: gettext("Edit Ammo type")
{:noreply, socket |> assign(:show_used, !show_used) |> display_ammo_type()}
end
@impl true
def handle_event(
"toggle_table",
_params,
%{assigns: %{view_table: view_table, ammo_type: ammo_type}} = socket
) do
new_path =
if view_table,
do: Routes.ammo_type_show_path(Endpoint, :show, ammo_type),
else: Routes.ammo_type_show_path(Endpoint, :table, ammo_type)
{:noreply, socket |> push_patch(to: new_path)}
end
defp display_ammo_type(
%{assigns: %{live_action: live_action, current_user: current_user, show_used: show_used}} =
socket,
%AmmoType{} = ammo_type
) do
fields_to_display =
@fields_list
|> Enum.any?(fn %{key: field, type: type} ->
default_value =
case type do
:boolean -> false
_other_type -> nil
end
ammo_type |> Map.get(field) != default_value
end)
socket
|> assign(
page_title: page_title(live_action, ammo_type),
ammo_type: ammo_type,
ammo_groups: ammo_type |> Ammo.list_ammo_groups_for_type(current_user, show_used),
avg_cost_per_round: ammo_type |> Ammo.get_average_cost_for_ammo_type!(current_user),
fields_list: @fields_list,
fields_to_display: fields_to_display
)
end
defp display_ammo_type(%{assigns: %{current_user: current_user}} = socket, ammo_type_id) do
socket |> display_ammo_type(Ammo.get_ammo_type!(ammo_type_id, current_user))
end
defp display_ammo_type(%{assigns: %{ammo_type: ammo_type}} = socket) do
socket |> display_ammo_type(ammo_type)
end
defp page_title(action, %{name: ammo_type_name}) when action in [:show, :table],
do: ammo_type_name
defp page_title(:edit, %{name: ammo_type_name}),
do: gettext("Edit %{ammo_type_name}", ammo_type_name: ammo_type_name)
end end

View File

@ -1,71 +1,78 @@
<div class="space-y-4 flex flex-col justify-center items-center"> <div class="mx-auto px-4 sm:px-8 space-y-4 max-w-3xl
flex flex-col justify-center items-center">
<h1 class="title text-2xl title-primary-500"> <h1 class="title text-2xl title-primary-500">
<%= @ammo_type.name %> <%= @ammo_type.name %>
</h1> </h1>
<span <%= if @ammo_type.desc do %>
:if={@ammo_type.desc} <span class="max-w-2xl w-full px-8 py-4 rounded-lg
class="max-w-2xl w-full px-8 py-4 rounded-lg text-center title text-lg
text-center title text-lg border border-primary-600">
border border-primary-600" <%= @ammo_type.desc %>
> </span>
<%= @ammo_type.desc %> <% end %>
</span>
<div class="flex space-x-4 justify-center items-center text-primary-600"> <div class="flex space-x-4 justify-center items-center text-primary-600">
<.link <%= live_patch to: Routes.ammo_type_show_path(Endpoint, :edit, @ammo_type),
patch={Routes.ammo_type_show_path(Endpoint, :edit, @ammo_type)} class: "text-primary-600 link",
class="text-primary-600 link" data: [qa: "edit"] do %>
data-qa="edit"
>
<i class="fa-fw fa-lg fas fa-edit"></i> <i class="fa-fw fa-lg fas fa-edit"></i>
</.link> <% end %>
<.link <%= link to: "#",
href="#" class: "text-primary-600 link",
class="text-primary-600 link" phx_click: "delete",
phx-click="delete" data: [
data-confirm={ confirm:
dgettext( dgettext("prompts", "Are you sure you want to delete %{name}? This will delete all %{name} type ammo as well!", name: @ammo_type.name),
"prompts", qa: "delete"
"Are you sure you want to delete %{name}? This will delete all %{name} type ammo as well!", ] do %>
name: @ammo_type.name
)
}
data-qa="delete"
>
<i class="fa-fw fa-lg fas fa-trash"></i> <i class="fa-fw fa-lg fas fa-trash"></i>
</.link> <% end %>
</div> </div>
<hr class="hr" /> <hr class="hr" />
<%= if @fields_to_display do %>
<div class="grid sm:grid-cols-2 gap-4 text-center justify-center items-center">
<%= for %{label: label, key: key, type: type} <- @fields_list do %>
<%= if @ammo_type |> Map.get(key) do %>
<h3 class="title text-lg">
<%= label %>
</h3>
<span class="text-primary-600">
<%= case type do %>
<% :boolean -> %>
<%= @ammo_type |> Map.get(key) |> humanize() %>
<% _ -> %>
<%= @ammo_type |> Map.get(key) %>
<% end %>
</span>
<% end %>
<% end %>
</div>
<hr class="hr" />
<% end %>
<div class="grid sm:grid-cols-2 gap-4 text-center justify-center items-center"> <div class="grid sm:grid-cols-2 gap-4 text-center justify-center items-center">
<%= for {field_name, field, type} <- [
{gettext("Bullet type"), :bullet_type, :string},
{gettext("Bullet core"), :bullet_core, :string},
{gettext("Cartridge"), :cartridge, :string},
{gettext("Caliber"), :caliber, :string},
{gettext("Case material"), :case_material, :string},
{gettext("Jacket type"), :jacket_type, :string},
{gettext("Muzzle velocity"), :muzzle_velocity, :string},
{gettext("Powder type"), :powder_type, :string},
{gettext("Powder grains per charge"), :powder_grains_per_charge, :string},
{gettext("Grains"), :grains, :string},
{gettext("Pressure"), :pressure, :string},
{gettext("Primer type"), :primer_type, :string},
{gettext("Firing type"), :firing_type, :string},
{gettext("Tracer"), :tracer, :boolean},
{gettext("Incendiary"), :incendiary, :boolean},
{gettext("Blank"), :blank, :boolean},
{gettext("Corrosive"), :corrosive, :boolean},
{gettext("Manufacturer"), :manufacturer, :string},
{gettext("UPC"), :upc, :string}
] do %>
<%= if @ammo_type |> Map.get(field) do %>
<h3 class="title text-lg">
<%= field_name %>:
</h3>
<span class="text-primary-600">
<%= case type do %>
<% :boolean -> %>
<%= @ammo_type |> Map.get(field) |> humanize() %>
<% _ -> %>
<%= @ammo_type |> Map.get(field) %>
<% end %>
</span>
<% end %>
<% end %>
<h3 class="title text-lg"> <h3 class="title text-lg">
<%= gettext("Rounds:") %> <%= gettext("Current # of rounds:") %>
</h3> </h3>
<span class="text-primary-600"> <span class="text-primary-600">
@ -73,64 +80,24 @@
</span> </span>
<h3 class="title text-lg"> <h3 class="title text-lg">
<%= gettext("Used rounds:") %> <%= gettext("Total rounds shot:") %>
</h3> </h3>
<span class="text-primary-600"> <span class="text-primary-600">
<%= @ammo_type |> Ammo.get_used_count_for_ammo_type(@current_user) %> <%= @ammo_type |> Ammo.get_used_count_for_ammo_type(@current_user) %>
</span> </span>
<h3 class="title text-lg">
<%= gettext("Total ever rounds:") %>
</h3>
<span class="text-primary-600">
<%= @ammo_type |> Ammo.get_historical_count_for_ammo_type(@current_user) %>
</span>
</div>
<hr class="hr" />
<div class="grid sm:grid-cols-2 gap-4 text-center justify-center items-center">
<h3 class="title text-lg">
<%= gettext("Packs:") %>
</h3>
<span class="text-primary-600">
<%= @ammo_type |> Ammo.get_ammo_groups_count_for_type(@current_user) %>
</span>
<h3 class="title text-lg">
<%= gettext("Used packs:") %>
</h3>
<span class="text-primary-600">
<%= @ammo_type |> Ammo.get_used_ammo_groups_count_for_type(@current_user) %>
</span>
<h3 class="title text-lg">
<%= gettext("Total ever packs:") %>
</h3>
<span class="text-primary-600">
<%= @ammo_type |> Ammo.get_ammo_groups_count_for_type(@current_user, true) %>
</span>
</div>
<hr class="hr" />
<div class="grid sm:grid-cols-2 gap-4 text-center justify-center items-center">
<h3 class="title text-lg"> <h3 class="title text-lg">
<%= gettext("Added on:") %> <%= gettext("Added on:") %>
</h3> </h3>
<span class="text-primary-600"> <span class="text-primary-600">
<.datetime datetime={@ammo_type.inserted_at} /> <%= @ammo_type.inserted_at |> display_datetime() %>
</span> </span>
<%= if @avg_cost_per_round do %> <%= if @avg_cost_per_round do %>
<h3 class="title text-lg"> <h3 class="title text-lg">
<%= gettext("Average CPR") %>: <%= gettext("Average Price paid") %>:
</h3> </h3>
<span class="text-primary-600"> <span class="text-primary-600">
@ -147,67 +114,32 @@
<hr class="hr" /> <hr class="hr" />
<div class="flex justify-center items-center space-x-4"> <div>
<.toggle_button action="toggle_show_used" value={@show_used}>
<span class="title text-lg text-primary-600">
<%= gettext("Show used") %>
</span>
</.toggle_button>
<.toggle_button action="toggle_table" value={@view_table}>
<span class="title text-lg text-primary-600">
<%= gettext("View as table") %>
</span>
</.toggle_button>
</div>
<div class="w-full p-4">
<%= if @ammo_groups |> Enum.empty?() do %> <%= if @ammo_groups |> Enum.empty?() do %>
<h2 class="px-4 title text-lg text-primary-600"> <h2 class="mx-8 my-4 title text-lg text-primary-600">
<%= gettext("No ammo for this type") %> <%= gettext("No ammo for this type") %>
<%= display_emoji("😔") %> <%= display_emoji("😔") %>
</h2> </h2>
<% else %> <% else %>
<%= if @view_table do %> <div class="flex flex-wrap justify-center items-center">
<.live_component <%= for ammo_group <- @ammo_groups do %>
module={CanneryWeb.Components.AmmoGroupTableComponent} <.ammo_group_card ammo_group={ammo_group} />
id="ammo-type-show-table" <% end %>
ammo_groups={@ammo_groups} </div>
current_user={@current_user}
>
<:container :let={%{container: %{name: container_name} = container}}>
<.link
navigate={Routes.container_show_path(Endpoint, :show, container)}
class="mx-2 my-1 link"
>
<%= container_name %>
</.link>
</:container>
</.live_component>
<% else %>
<div class="flex flex-wrap justify-center items-stretch">
<.ammo_group_card
:for={ammo_group <- @ammo_groups}
ammo_group={ammo_group}
show_container={true}
/>
</div>
<% end %>
<% end %> <% end %>
</div> </div>
</div> </div>
<.modal <%= if @live_action in [:edit] do %>
:if={@live_action == :edit} <.modal return_to={Routes.ammo_type_show_path(Endpoint, :show, @ammo_type)}>
return_to={Routes.ammo_type_show_path(Endpoint, :show, @ammo_type)} <.live_component
> module={CanneryWeb.AmmoTypeLive.FormComponent}
<.live_component id={@ammo_type.id}
module={CanneryWeb.AmmoTypeLive.FormComponent} title={@page_title}
id={@ammo_type.id} action={@live_action}
title={@page_title} ammo_type={@ammo_type}
action={@live_action} return_to={Routes.ammo_type_show_path(Endpoint, :show, @ammo_type)}
ammo_type={@ammo_type} current_user={@current_user}
return_to={Routes.ammo_type_show_path(Endpoint, :show, @ammo_type)} />
current_user={@current_user} </.modal>
/> <% end %>
</.modal>

View File

@ -4,38 +4,40 @@
</h2> </h2>
<div class="flex flex-wrap justify-center items-center"> <div class="flex flex-wrap justify-center items-center">
<.link <%= for tag <- @container.tags do %>
:for={tag <- @container.tags} <%= link to: "#",
href="#" class: "mx-2 my-1 px-4 py-2 rounded-lg title text-xl",
class="mx-2 my-1 px-4 py-2 rounded-lg title text-xl" style: "color: #{tag.text_color}; background-color: #{tag.bg_color}",
style={"color: #{tag.text_color}; background-color: #{tag.bg_color}"} phx_click: "delete",
phx-click="delete" phx_value_tag_id: tag.id,
phx-value-tag-id={tag.id} phx_target: @myself,
phx-target={@myself} data: [
data-confirm={ confirm:
dgettext( dgettext(
"prompts", "prompts",
"Are you sure you want to remove the %{tag_name} tag from %{container_name}?", "Are you sure you want to remove the %{tag_name} tag from %{container_name}?",
tag_name: tag.name, tag_name: tag.name,
container_name: @container.name container_name: @container.name
) )
} ] do %>
> <%= tag.name %>
<%= tag.name %> <i class="fa-fw fa-sm fas fa-trash"></i>
<i class="fa-fw fa-sm fas fa-trash"></i> <% end %>
</.link> <% end %>
<h2 :if={@container.tags |> Enum.empty?()} class="title text-xl text-primary-600"> <%= if @container.tags |> Enum.empty?() do %>
<%= gettext("No tags") %> <h2 class="title text-xl text-primary-600">
<%= display_emoji("😔") %> <%= gettext("No tags") %>
</h2> <%= display_emoji("😔") %>
</h2>
<% end %>
</div> </div>
<%= unless tag_options(@tags, @container) |> Enum.empty?() do %> <%= unless tag_options(@tags, @container) |> Enum.empty?() do %>
<hr class="hr" /> <hr class="hr" />
<.form <.form
:let={f} let={f}
for={:tag} for={:tag}
id="add-tag-to-container-form" id="add-tag-to-container-form"
class="flex flex-col space-y-4 sm:space-y-0 sm:grid sm:grid-cols-3 sm:gap-4 justify-center items-center" class="flex flex-col space-y-4 sm:space-y-0 sm:grid sm:grid-cols-3 sm:gap-4 justify-center items-center"

View File

@ -35,18 +35,15 @@ defmodule CanneryWeb.ContainerLive.FormComponent do
container_params container_params
) do ) do
changeset_action = changeset_action =
cond do case action do
action in [:new, :clone] -> :insert :new -> :insert
action == :edit -> :update :edit -> :update
end end
changeset = changeset =
cond do case action do
action in [:new, :clone] -> :new -> container |> Container.create_changeset(user, container_params)
container |> Container.create_changeset(user, container_params) :edit -> container |> Container.update_changeset(container_params)
action == :edit ->
container |> Container.update_changeset(container_params)
end end
changeset = changeset =
@ -68,7 +65,7 @@ defmodule CanneryWeb.ContainerLive.FormComponent do
case Containers.update_container(container, current_user, container_params) do case Containers.update_container(container, current_user, container_params) do
{:ok, %{name: container_name}} -> {:ok, %{name: container_name}} ->
prompt = dgettext("prompts", "%{name} updated successfully", name: container_name) prompt = dgettext("prompts", "%{name} updated successfully", name: container_name)
socket |> put_flash(:info, prompt) |> push_navigate(to: return_to) socket |> put_flash(:info, prompt) |> push_redirect(to: return_to)
{:error, %Changeset{} = changeset} -> {:error, %Changeset{} = changeset} ->
socket |> assign(:changeset, changeset) socket |> assign(:changeset, changeset)
@ -79,15 +76,14 @@ defmodule CanneryWeb.ContainerLive.FormComponent do
defp save_container( defp save_container(
%{assigns: %{current_user: current_user, return_to: return_to}} = socket, %{assigns: %{current_user: current_user, return_to: return_to}} = socket,
action, :new,
container_params container_params
) ) do
when action in [:new, :clone] do
socket = socket =
case Containers.create_container(container_params, current_user) do case Containers.create_container(container_params, current_user) do
{:ok, %{name: container_name}} -> {:ok, %{name: container_name}} ->
prompt = dgettext("prompts", "%{name} created successfully", name: container_name) prompt = dgettext("prompts", "%{name} created successfully", name: container_name)
socket |> put_flash(:info, prompt) |> push_navigate(to: return_to) socket |> put_flash(:info, prompt) |> push_redirect(to: return_to)
{:error, %Changeset{} = changeset} -> {:error, %Changeset{} = changeset} ->
socket |> assign(changeset: changeset) socket |> assign(changeset: changeset)

View File

@ -3,7 +3,7 @@
<%= @title %> <%= @title %>
</h2> </h2>
<.form <.form
:let={f} let={f}
for={@changeset} for={@changeset}
id="container-form" id="container-form"
class="flex flex-col space-y-4 sm:space-y-0 sm:grid sm:grid-cols-3 sm:gap-4 justify-center items-center" class="flex flex-col space-y-4 sm:space-y-0 sm:grid sm:grid-cols-3 sm:gap-4 justify-center items-center"
@ -11,12 +11,11 @@
phx-change="validate" phx-change="validate"
phx-submit="save" phx-submit="save"
> >
<div <%= if @changeset.action && not @changeset.valid? do %>
:if={@changeset.action && not @changeset.valid?()} <div class="invalid-feedback col-span-3 text-center">
class="invalid-feedback col-span-3 text-center" <%= changeset_errors(@changeset) %>
> </div>
<%= changeset_errors(@changeset) %> <% end %>
</div>
<%= label(f, :name, gettext("Name"), class: "title text-lg text-primary-600") %> <%= label(f, :name, gettext("Name"), class: "title text-lg text-primary-600") %>
<%= text_input(f, :name, <%= text_input(f, :name,

View File

@ -6,16 +6,11 @@ defmodule CanneryWeb.ContainerLive.Index do
use CanneryWeb, :live_view use CanneryWeb, :live_view
import CanneryWeb.Components.ContainerCard import CanneryWeb.Components.ContainerCard
alias Cannery.{Containers, Containers.Container, Repo} alias Cannery.{Containers, Containers.Container, Repo}
alias CanneryWeb.Endpoint
alias Ecto.Changeset alias Ecto.Changeset
@impl true @impl true
def mount(%{"search" => search}, _session, socket) do def mount(_params, _session, socket), do: {:ok, socket}
{:ok, socket |> assign(view_table: true, search: search) |> display_containers()}
end
def mount(_params, _session, socket) do
{:ok, socket |> assign(view_table: true, search: nil) |> display_containers()}
end
@impl true @impl true
def handle_params(params, _url, %{assigns: %{live_action: live_action}} = socket) do def handle_params(params, _url, %{assigns: %{live_action: live_action}} = socket) do
@ -26,7 +21,7 @@ defmodule CanneryWeb.ContainerLive.Index do
%{name: container_name} = %{name: container_name} =
container = container =
Containers.get_container!(id, current_user) Containers.get_container!(id, current_user)
|> Repo.preload([:tags, :ammo_groups]) |> Repo.preload([:tags, :ammo_groups], force: true)
socket socket
|> assign(page_title: gettext("Edit %{name}", name: container_name), container: container) |> assign(page_title: gettext("Edit %{name}", name: container_name), container: container)
@ -36,29 +31,11 @@ defmodule CanneryWeb.ContainerLive.Index do
socket |> assign(:page_title, gettext("New Container")) |> assign(:container, %Container{}) socket |> assign(:page_title, gettext("New Container")) |> assign(:container, %Container{})
end end
defp apply_action(%{assigns: %{current_user: current_user}} = socket, :clone, %{"id" => id}) do
container = Containers.get_container!(id, current_user)
socket
|> assign(page_title: gettext("New Container"), container: %{container | id: nil})
end
defp apply_action(socket, :index, _params) do defp apply_action(socket, :index, _params) do
socket socket
|> assign( |> assign(:page_title, gettext("Containers"))
page_title: gettext("Containers"), |> assign(:container, nil)
container: nil, |> display_containers()
search: nil
)
end
defp apply_action(socket, :search, %{"search" => search}) do
socket
|> assign(
page_title: gettext("Containers"),
container: nil,
search: search
)
end end
defp apply_action(%{assigns: %{current_user: current_user}} = socket, :edit_tags, %{"id" => id}) do defp apply_action(%{assigns: %{current_user: current_user}} = socket, :edit_tags, %{"id" => id}) do
@ -106,26 +83,10 @@ defmodule CanneryWeb.ContainerLive.Index do
{:noreply, socket} {:noreply, socket}
end end
@impl true defp display_containers(%{assigns: %{current_user: current_user}} = socket) do
def handle_event("toggle_table", _params, %{assigns: %{view_table: view_table}} = socket) do
{:noreply, socket |> assign(:view_table, !view_table) |> display_containers()}
end
@impl true
def handle_event("search", %{"search" => %{"search_term" => ""}}, socket) do
{:noreply, socket |> push_patch(to: Routes.container_index_path(Endpoint, :index))}
end
def handle_event("search", %{"search" => %{"search_term" => search_term}}, socket) do
{:noreply,
socket |> push_patch(to: Routes.container_index_path(Endpoint, :search, search_term))}
end
defp display_containers(%{assigns: %{search: search, current_user: current_user}} = socket) do
containers = containers =
Containers.list_containers(search, current_user) Containers.list_containers(current_user) |> Repo.preload([:tags, :ammo_groups], force: true)
|> Repo.preload([:tags, :ammo_groups])
socket |> assign(:containers, containers) socket |> assign(containers: containers)
end end
end end

View File

@ -3,169 +3,79 @@
<%= gettext("Containers") %> <%= gettext("Containers") %>
</h1> </h1>
<%= if @containers |> Enum.empty?() and @search |> is_nil() do %>
<h2 class="title text-xl text-primary-600">
<%= gettext("No containers") %>
<%= display_emoji("😔") %>
</h2>
<.link patch={Routes.container_index_path(Endpoint, :new)} class="btn btn-primary">
<%= dgettext("actions", "Add your first container!") %>
</.link>
<% else %>
<.link patch={Routes.container_index_path(Endpoint, :new)} class="btn btn-primary">
<%= dgettext("actions", "New Container") %>
</.link>
<div class="w-full flex flex-col sm:flex-row justify-center items-center space-y-4 sm:space-y-0 sm:space-x-4 max-w-xl">
<.form
:let={f}
for={:search}
phx-change="search"
phx-submit="search"
class="grow self-stretch flex flex-col items-stretch"
data-qa="container_search"
>
<%= text_input(f, :search_term,
class: "input input-primary",
value: @search,
phx_debounce: 300,
placeholder: gettext("Search containers")
) %>
</.form>
<.toggle_button action="toggle_table" value={@view_table}>
<span class="title text-lg text-primary-600">
<%= gettext("View as table") %>
</span>
</.toggle_button>
</div>
<% end %>
<%= if @containers |> Enum.empty?() do %> <%= if @containers |> Enum.empty?() do %>
<h2 class="title text-xl text-primary-600"> <h2 class="title text-xl text-primary-600">
<%= gettext("No containers") %> <%= gettext("No containers") %>
<%= display_emoji("😔") %> <%= display_emoji("😔") %>
</h2> </h2>
<%= live_patch(dgettext("actions", "Add your first container!"),
to: Routes.container_index_path(Endpoint, :new),
class: "btn btn-primary"
) %>
<% else %> <% else %>
<%= if @view_table do %> <%= live_patch(dgettext("actions", "New Container"),
<.live_component to: Routes.container_index_path(Endpoint, :new),
module={CanneryWeb.Components.ContainerTableComponent} class: "btn btn-primary"
id="containers_index_table" ) %>
action={@live_action} <% end %>
containers={@containers}
current_user={@current_user} <div class="max-w-full flex flex-row flex-wrap justify-center items-center">
> <%= for container <- @containers do %>
<:tag_actions :let={container}> <.container_card container={container}>
<:tag_actions>
<div class="mx-4 my-2"> <div class="mx-4 my-2">
<.link <%= live_patch to: Routes.container_index_path(Endpoint, :edit_tags, container),
patch={Routes.container_index_path(Endpoint, :edit_tags, container)} class: "text-primary-600 link" do %>
class="text-primary-600 link"
>
<i class="fa-fw fa-lg fas fa-tags"></i> <i class="fa-fw fa-lg fas fa-tags"></i>
</.link> <% end %>
</div> </div>
</:tag_actions> </:tag_actions>
<:actions :let={container}> <%= live_patch to: Routes.container_index_path(Endpoint, :edit, container),
<.link class: "text-primary-600 link",
patch={Routes.container_index_path(Endpoint, :edit, container)} data: [qa: "edit-#{container.id}"] do %>
class="text-primary-600 link" <i class="fa-fw fa-lg fas fa-edit"></i>
data-qa={"edit-#{container.id}"} <% end %>
>
<i class="fa-fw fa-lg fas fa-edit"></i>
</.link>
<.link <%= link to: "#",
patch={Routes.container_index_path(Endpoint, :clone, container)} class: "text-primary-600 link",
class="text-primary-600 link" phx_click: "delete",
data-qa={"clone-#{container.id}"} phx_value_id: container.id,
> data: [
<i class="fa-fw fa-lg fas fa-copy"></i> confirm:
</.link> dgettext("prompts", "Are you sure you want to delete %{name}?", name: container.name),
qa: "delete-#{container.id}"
<.link ] do %>
href="#" <i class="fa-fw fa-lg fas fa-trash"></i>
class="text-primary-600 link" <% end %>
phx-click="delete" </.container_card>
phx-value-id={container.id}
data-confirm={
dgettext("prompts", "Are you sure you want to delete %{name}?", name: container.name)
}
data-qa={"delete-#{container.id}"}
>
<i class="fa-fw fa-lg fas fa-trash"></i>
</.link>
</:actions>
</.live_component>
<% else %>
<div class="w-full flex flex-row flex-wrap justify-center items-stretch">
<.container_card :for={container <- @containers} container={container}>
<:tag_actions>
<div class="mx-4 my-2">
<.link
patch={Routes.container_index_path(Endpoint, :edit_tags, container)}
class="text-primary-600 link"
>
<i class="fa-fw fa-lg fas fa-tags"></i>
</.link>
</div>
</:tag_actions>
<.link
patch={Routes.container_index_path(Endpoint, :edit, container)}
class="text-primary-600 link"
data-qa={"edit-#{container.id}"}
>
<i class="fa-fw fa-lg fas fa-edit"></i>
</.link>
<.link
patch={Routes.container_index_path(Endpoint, :clone, container)}
class="text-primary-600 link"
data-qa={"clone-#{container.id}"}
>
<i class="fa-fw fa-lg fas fa-copy"></i>
</.link>
<.link
href="#"
class="text-primary-600 link"
phx-click="delete"
phx-value-id={container.id}
data-confirm={
dgettext("prompts", "Are you sure you want to delete %{name}?", name: container.name)
}
data-qa={"delete-#{container.id}"}
>
<i class="fa-fw fa-lg fas fa-trash"></i>
</.link>
</.container_card>
</div>
<% end %> <% end %>
<% end %> </div>
</div> </div>
<.modal <%= if @live_action in [:new, :edit] do %>
:if={@live_action in [:new, :edit, :clone]} <.modal return_to={Routes.container_index_path(Endpoint, :index)}>
return_to={Routes.container_index_path(Endpoint, :index)} <.live_component
> module={CanneryWeb.ContainerLive.FormComponent}
<.live_component id={@container.id || :new}
module={CanneryWeb.ContainerLive.FormComponent} title={@page_title}
id={@container.id || :new} action={@live_action}
title={@page_title} container={@container}
action={@live_action} return_to={Routes.container_index_path(Endpoint, :index)}
container={@container} current_user={@current_user}
return_to={Routes.container_index_path(Endpoint, :index)} />
current_user={@current_user} </.modal>
/> <% end %>
</.modal>
<.modal :if={@live_action == :edit_tags} return_to={Routes.container_index_path(Endpoint, :index)}> <%= if @live_action == :edit_tags do %>
<.live_component <.modal return_to={Routes.container_index_path(Endpoint, :index)}>
module={CanneryWeb.ContainerLive.EditTagsComponent} <.live_component
id={@container.id} module={CanneryWeb.ContainerLive.EditTagsComponent}
title={@page_title} id={@container.id}
action={@live_action} title={@page_title}
container={@container} action={@live_action}
current_user={@current_user} container={@container}
/> current_user={@current_user}
</.modal> />
</.modal>
<% end %>

View File

@ -5,23 +5,21 @@ defmodule CanneryWeb.ContainerLive.Show do
use CanneryWeb, :live_view use CanneryWeb, :live_view
import CanneryWeb.Components.{AmmoGroupCard, TagCard} import CanneryWeb.Components.{AmmoGroupCard, TagCard}
alias Cannery.{Accounts.User, Ammo, Containers, Containers.Container, Repo, Tags} alias Cannery.{Accounts.User, Containers, Containers.Container, Repo, Tags}
alias CanneryWeb.Endpoint alias CanneryWeb.Endpoint
alias Ecto.Changeset alias Ecto.Changeset
alias Phoenix.LiveView.Socket alias Phoenix.LiveView.Socket
@impl true @impl true
def mount(_params, _session, socket), def mount(_params, _session, socket), do: {:ok, socket}
do: {:ok, socket |> assign(show_used: false, view_table: true)}
@impl true @impl true
def handle_params(%{"id" => id}, _session, %{assigns: %{current_user: current_user}} = socket) do def handle_params(
socket = %{"id" => id},
socket _session,
|> assign(view_table: true) %{assigns: %{current_user: current_user}} = socket
|> render_container(id, current_user) ) do
{:noreply, socket |> render_container(id, current_user)}
{:noreply, socket}
end end
@impl true @impl true
@ -41,7 +39,7 @@ defmodule CanneryWeb.ContainerLive.Show do
container_name: container.name container_name: container.name
) )
socket |> put_flash(:info, prompt) |> render_container() socket |> put_flash(:info, prompt) |> render_container(container.id, current_user)
{:error, error_string} -> {:error, error_string} ->
socket |> put_flash(:error, error_string) socket |> put_flash(:error, error_string)
@ -64,7 +62,7 @@ defmodule CanneryWeb.ContainerLive.Show do
socket socket
|> put_flash(:info, prompt) |> put_flash(:info, prompt)
|> push_navigate(to: Routes.container_index_path(socket, :index)) |> push_redirect(to: Routes.container_index_path(socket, :index))
{:error, %{action: :delete, errors: [ammo_groups: _error], valid?: false} = changeset} -> {:error, %{action: :delete, errors: [ammo_groups: _error], valid?: false} = changeset} ->
ammo_groups_error = changeset |> changeset_errors(:ammo_groups) |> Enum.join(", ") ammo_groups_error = changeset |> changeset_errors(:ammo_groups) |> Enum.join(", ")
@ -84,43 +82,20 @@ defmodule CanneryWeb.ContainerLive.Show do
{:noreply, socket} {:noreply, socket}
end end
@impl true
def handle_event("toggle_show_used", _params, %{assigns: %{show_used: show_used}} = socket) do
{:noreply, socket |> assign(:show_used, !show_used) |> render_container()}
end
@impl true
def handle_event("toggle_table", _params, %{assigns: %{view_table: view_table}} = socket) do
{:noreply, socket |> assign(:view_table, !view_table) |> render_container()}
end
@spec render_container(Socket.t(), Container.id(), User.t()) :: Socket.t() @spec render_container(Socket.t(), Container.id(), User.t()) :: Socket.t()
defp render_container( defp render_container(%{assigns: %{live_action: live_action}} = socket, id, current_user) do
%{assigns: %{live_action: live_action, show_used: show_used}} = socket,
id,
current_user
) do
%{name: container_name} = %{name: container_name} =
container = container =
Containers.get_container!(id, current_user) Containers.get_container!(id, current_user)
|> Repo.preload([:tags], force: true) |> Repo.preload([:ammo_groups, :tags], force: true)
ammo_groups = Ammo.list_ammo_groups_for_container(container, current_user, show_used)
page_title = page_title =
case live_action do case live_action do
action when action in [:show, :table] -> container_name :show -> gettext("Show %{name}", name: container_name)
:edit -> gettext("Edit %{name}", name: container_name) :edit -> gettext("Edit %{name}", name: container_name)
:edit_tags -> gettext("Edit %{name} tags", name: container_name) :edit_tags -> gettext("Edit %{name} tags", name: container_name)
end end
socket |> assign(container: container, ammo_groups: ammo_groups, page_title: page_title) socket |> assign(container: container, page_title: page_title)
end
@spec render_container(Socket.t()) :: Socket.t()
defp render_container(
%{assigns: %{container: %{id: container_id}, current_user: current_user}} = socket
) do
socket |> render_container(container_id, current_user)
end end
end end

View File

@ -1,63 +1,56 @@
<div class="space-y-4 flex flex-col justify-center items-center"> <div class="mx-auto space-y-4 max-w-3xl flex flex-col justify-center items-center">
<h1 class="title text-2xl title-primary-500"> <h1 class="title text-2xl title-primary-500">
<%= @container.name %> <%= @container.name %>
</h1> </h1>
<span :if={@container.desc} class="rounded-lg title text-lg"> <%= if @container.desc do %>
<%= gettext("Description:") %> <span class="rounded-lg title text-lg">
<%= @container.desc %> <%= gettext("Description:") %>
</span> <%= @container.desc %>
</span>
<% end %>
<span class="rounded-lg title text-lg"> <span class="rounded-lg title text-lg">
<%= gettext("Type:") %> <%= gettext("Type:") %>
<%= @container.type %> <%= @container.type %>
</span> </span>
<span :if={@container.location} class="rounded-lg title text-lg"> <%= if @container.location do %>
<%= gettext("Location:") %>
<%= @container.location %>
</span>
<%= unless @ammo_groups |> Enum.empty?() do %>
<span class="rounded-lg title text-lg"> <span class="rounded-lg title text-lg">
<%= if @show_used do %> <%= gettext("Location:") %>
<%= gettext("Total packs:") %> <%= @container.location %>
<% else %> </span>
<%= gettext("Packs:") %> <% end %>
<% end %>
<%= Enum.count(@ammo_groups) %> <%= unless @container.ammo_groups |> Enum.empty?() do %>
<span class="rounded-lg title text-lg">
<%= gettext("Packs:") %>
<%= @container |> Containers.get_container_ammo_group_count!() %>
</span> </span>
<span class="rounded-lg title text-lg"> <span class="rounded-lg title text-lg">
<%= if @show_used do %> <%= gettext("Rounds:") %>
<%= gettext("Total rounds:") %>
<% else %>
<%= gettext("Rounds:") %>
<% end %>
<%= @container |> Containers.get_container_rounds!() %> <%= @container |> Containers.get_container_rounds!() %>
</span> </span>
<% end %> <% end %>
<div class="flex space-x-4 justify-center items-center text-primary-600"> <div class="flex space-x-4 justify-center items-center text-primary-600">
<.link <%= live_patch to: Routes.container_show_path(Endpoint, :edit, @container),
patch={Routes.container_show_path(Endpoint, :edit, @container)} class: "text-primary-600 link",
class="text-primary-600 link" data: [qa: "edit"] do %>
data-qa="edit"
>
<i class="fa-fw fa-lg fas fa-edit"></i> <i class="fa-fw fa-lg fas fa-edit"></i>
</.link> <% end %>
<.link <%= link to: "#",
href="#" class: "text-primary-600 link",
class="text-primary-600 link" phx_click: "delete_container",
phx-click="delete_container" data: [
data-confirm={ confirm:
dgettext("prompts", "Are you sure you want to delete %{name}?", name: @container.name) dgettext("prompts", "Are you sure you want to delete %{name}?", name: @container.name),
} qa: "delete"
data-qa="delete" ] do %>
>
<i class="fa-fw fa-lg fas fa-trash"></i> <i class="fa-fw fa-lg fas fa-trash"></i>
</.link> <% end %>
</div> </div>
<hr class="mb-4 hr" /> <hr class="mb-4 hr" />
@ -69,98 +62,67 @@
<%= display_emoji("😔") %> <%= display_emoji("😔") %>
</h2> </h2>
<.link <%= live_patch(dgettext("actions", "Why not add one?"),
patch={Routes.container_show_path(Endpoint, :edit_tags, @container)} to: Routes.container_show_path(Endpoint, :edit_tags, @container),
class="btn btn-primary" class: "btn btn-primary"
> ) %>
<%= dgettext("actions", "Why not add one?") %>
</.link>
</div> </div>
<% else %> <% else %>
<div class="flex flex-wrap justify-center items-center"> <div class="flex flex-wrap justify-center items-center">
<.simple_tag_card :for={tag <- @container.tags} tag={tag} /> <%= for tag <- @container.tags do %>
<.simple_tag_card tag={tag} />
<% end %>
<div class="mx-4 my-2"> <div class="mx-4 my-2">
<.link <%= live_patch to: Routes.container_show_path(Endpoint, :edit_tags, @container),
patch={Routes.container_show_path(Endpoint, :edit_tags, @container)} class: "text-primary-600 link" do %>
class="text-primary-600 link"
>
<i class="fa-fw fa-lg fas fa-tags"></i> <i class="fa-fw fa-lg fas fa-tags"></i>
</.link> <% end %>
</div> </div>
</div> </div>
<% end %> <% end %>
<hr class="mb-4 hr" /> <hr class="mb-4 hr" />
<div class="flex justify-center items-center space-x-4"> <div>
<.toggle_button action="toggle_show_used" value={@show_used}> <%= if @container.ammo_groups |> Enum.empty?() do %>
<span class="title text-lg text-primary-600"> <h2 class="mx-8 my-4 title text-lg text-primary-600">
<%= gettext("Show used") %>
</span>
</.toggle_button>
<.toggle_button action="toggle_table" value={@view_table}>
<span class="title text-lg text-primary-600">
<%= gettext("View as table") %>
</span>
</.toggle_button>
</div>
<div class="w-full p-4">
<%= if @ammo_groups |> Enum.empty?() do %>
<h2 class="mx-4 title text-lg text-primary-600 text-center">
<%= gettext("No ammo in this container") %> <%= gettext("No ammo in this container") %>
</h2> </h2>
<% else %> <% else %>
<%= if @view_table do %> <div class="flex flex-wrap justify-center items-center">
<.live_component <%= for ammo_group <- @container.ammo_groups do %>
module={CanneryWeb.Components.AmmoGroupTableComponent} <.ammo_group_card ammo_group={ammo_group} />
id="ammo-type-show-table" <% end %>
ammo_groups={@ammo_groups} </div>
current_user={@current_user}
>
<:ammo_type :let={%{name: ammo_type_name} = ammo_type}>
<.link navigate={Routes.ammo_type_show_path(Endpoint, :show, ammo_type)} class="link">
<%= ammo_type_name %>
</.link>
</:ammo_type>
</.live_component>
<% else %>
<div class="flex flex-wrap justify-center items-stretch">
<.ammo_group_card :for={ammo_group <- @ammo_groups} ammo_group={ammo_group} />
</div>
<% end %>
<% end %> <% end %>
</div> </div>
</div> </div>
<.modal <%= if @live_action in [:edit] do %>
:if={@live_action == :edit} <.modal return_to={Routes.container_show_path(Endpoint, :show, @container)}>
return_to={Routes.container_show_path(Endpoint, :show, @container)} <.live_component
> module={CanneryWeb.ContainerLive.FormComponent}
<.live_component id={@container.id}
module={CanneryWeb.ContainerLive.FormComponent} title={@page_title}
id={@container.id} action={@live_action}
title={@page_title} container={@container}
action={@live_action} return_to={Routes.container_show_path(Endpoint, :show, @container)}
container={@container} current_user={@current_user}
return_to={Routes.container_show_path(Endpoint, :show, @container)} />
current_user={@current_user} </.modal>
/> <% end %>
</.modal>
<.modal <%= if @live_action == :edit_tags do %>
:if={@live_action == :edit_tags} <.modal return_to={Routes.container_show_path(Endpoint, :show, @container)}>
return_to={Routes.container_show_path(Endpoint, :show, @container)} <.live_component
> module={CanneryWeb.ContainerLive.EditTagsComponent}
<.live_component id={@container.id}
module={CanneryWeb.ContainerLive.EditTagsComponent} title={@page_title}
id={@container.id} action={@live_action}
title={@page_title} container={@container}
action={@live_action} return_to={Routes.container_show_path(Endpoint, :show, @container)}
container={@container} current_user={@current_user}
return_to={Routes.container_show_path(Endpoint, :show, @container)} />
current_user={@current_user} </.modal>
/> <% end %>
</.modal>

View File

@ -7,12 +7,187 @@ defmodule CanneryWeb.HomeLive do
alias Cannery.Accounts alias Cannery.Accounts
alias CanneryWeb.Endpoint alias CanneryWeb.Endpoint
@version Mix.Project.config()[:version]
@impl true @impl true
def mount(_params, _session, socket) do def mount(_params, _session, socket) do
admins = Accounts.list_users_by_role(:admin) admins = Accounts.list_users_by_role(:admin)
socket = socket |> assign(page_title: gettext("Home"), admins: admins, version: @version) socket = socket |> assign(page_title: "Home", query: "", results: %{}, admins: admins)
{:ok, socket} {:ok, socket}
end end
@impl true
def handle_event("suggest", %{"q" => query}, socket) do
{:noreply, socket |> assign(results: search(query), query: query)}
end
@impl true
def handle_event("search", %{"q" => query}, socket) do
case search(query) do
%{^query => vsn} ->
{:noreply, redirect(socket, external: "https://hexdocs.pm/#{query}/#{vsn}")}
_no_query ->
{:noreply,
socket
|> put_flash(:error, "No dependencies found matching \"#{query}\"")
|> assign(results: %{}, query: query)}
end
end
@impl true
def render(assigns) do
~H"""
<div class="mx-auto px-8 sm:px-16 flex flex-col justify-center items-center text-center space-y-4 max-w-3xl">
<img
src={Routes.static_path(Endpoint, "/images/cannery.svg")}
alt={gettext("Cannery logo")}
class="inline-block w-32 hover:-mt-2 hover:mb-2 transition-all duration-500 ease-in-out"
title={gettext("isn't he cute >:3")}
/>
<h1 class="title text-primary-600 text-2xl">
<%= gettext("Welcome to %{name}", name: "Cannery") %>
</h1>
<h2 class="title text-primary-600 text-lg">
<%= gettext("The self-hosted firearm tracker website") %>
</h2>
<hr class="hr" />
<ul class="flex flex-col space-y-4 text-center">
<li class="flex flex-col justify-center items-center
space-y-2">
<b class="whitespace-nowrap">
<%= gettext("Easy to Use:") %>
</b>
<p>
<%= gettext(
"%{name} lets you easily keep an eye on your ammo levels before and after range day",
name: "Cannery"
) %>
</p>
</li>
<li class="flex flex-col justify-center items-center
space-y-2">
<b class="whitespace-nowrap">
<%= gettext("Secure:") %>
</b>
<p>
<%= gettext("Self-host your own instance, or use an instance from someone you trust.") %>
<%= gettext("Your data stays with you, period") %>
</p>
</li>
<li class="flex flex-col justify-center items-center
space-y-2">
<b class="whitespace-nowrap">
<%= gettext("Simple:") %>
</b>
<p>
<%= gettext("Access from any internet-capable device") %>
</p>
</li>
</ul>
<hr class="hr" />
<ul class="flex flex-col space-y-2 text-center justify-center">
<h2 class="title text-primary-600 text-lg">
<%= gettext("Instance Information") %>
</h2>
<li class="flex flex-col justify-center space-x-2">
<b>
<%= gettext("Admins:") %>
</b>
<p>
<%= if @admins |> Enum.empty?() do %>
<%= link(dgettext("prompts", "Register to setup %{name}", name: "Cannery"),
class: "hover:underline",
to: Routes.user_registration_path(CanneryWeb.Endpoint, :new)
) %>
<% else %>
<div class="flex flex-wrap justify-center space-x-2">
<%= for admin <- @admins do %>
<a class="hover:underline" href={"mailto:#{admin.email}"}>
<%= admin.email %>
</a>
<% end %>
</div>
<% end %>
</p>
</li>
<li class="flex flex-row justify-center space-x-2">
<b>Registration:</b>
<p>
<%= Application.get_env(:cannery, CanneryWeb.Endpoint)[:registration]
|> case do
"public" -> gettext("Public Signups")
_ -> gettext("Invite Only")
end %>
</p>
</li>
<li class="flex flex-row justify-center items-center space-x-2">
<b>Version:</b>
<%= link class: "flex flex-row justify-center items-center space-x-2 hover:underline",
to: "https://gitea.bubbletea.dev/shibao/cannery/src/branch/stable/CHANGELOG.md",
target: "_blank",
rel: "noopener noreferrer" do %>
<p>0.5.5</p>
<i class="fas fa-md fa-info-circle"></i>
<% end %>
</li>
</ul>
<hr class="hr" />
<ul class="flex flex-col space-y-2 text-center justify-center">
<h2 class="title text-primary-600 text-lg">
<%= gettext("Get involved!") %>
</h2>
<li class="flex flex-col justify-center space-x-2">
<%= link class: "flex flex-row justify-center items-center space-x-2 hover:underline",
to: "https://gitea.bubbletea.dev/shibao/cannery",
target: "_blank",
rel: "noopener noreferrer" do %>
<p><%= gettext("View the source code") %></p>
<i class="fas fa-md fa-code"></i>
<% end %>
</li>
<li class="flex flex-col justify-center space-x-2">
<%= link class: "flex flex-row justify-center items-center space-x-2 hover:underline",
to: "https://weblate.bubbletea.dev/engage/cannery",
target: "_blank",
rel: "noopener noreferrer" do %>
<p><%= gettext("Help translate") %></p>
<i class="fas fa-md fa-language"></i>
<% end %>
</li>
<li class="flex flex-col justify-center space-x-2">
<%= link class: "flex flex-row justify-center items-center space-x-2 hover:underline",
to: "https://gitea.bubbletea.dev/shibao/cannery/issues/new",
target: "_blank",
rel: "noopener noreferrer" do %>
<p><%= gettext("Report bugs or request features") %></p>
<i class="fas fa-md fa-spider"></i>
<% end %>
</li>
</ul>
</div>
"""
end
defp search(query) do
if not CanneryWeb.Endpoint.config(:code_reloader) do
raise "action disabled when not in development"
end
for {app, desc, vsn} <- Application.started_applications(),
app = to_string(app),
String.starts_with?(app, query) and not List.starts_with?(desc, ~c"ERTS"),
into: %{},
do: {app, vsn}
end
end end

View File

@ -1,144 +0,0 @@
<div class="mx-auto px-8 sm:px-16 flex flex-col justify-center items-center text-center space-y-4 max-w-3xl">
<img
src={Routes.static_path(Endpoint, "/images/cannery.svg")}
alt={gettext("Cannery logo")}
class="inline-block w-32 hover:-mt-2 hover:mb-2 transition-all duration-500 ease-in-out"
title={gettext("isn't he cute >:3")}
/>
<h1 class="title text-primary-600 text-2xl">
<%= gettext("Welcome to Cannery") %>
</h1>
<h2 class="title text-primary-600 text-lg">
<%= gettext("The self-hosted firearm tracker website") %>
</h2>
<hr class="hr" />
<ul class="flex flex-col space-y-4 text-center">
<li class="flex flex-col justify-center items-center
space-y-2">
<b class="whitespace-nowrap">
<%= gettext("Easy to Use:") %>
</b>
<p>
<%= gettext(
"Cannery lets you easily keep an eye on your ammo levels before and after range day"
) %>
</p>
</li>
<li class="flex flex-col justify-center items-center space-y-2">
<b class="whitespace-nowrap">
<%= gettext("Secure:") %>
</b>
<p>
<%= gettext("Self-host your own instance, or use an instance from someone you trust.") %>
<%= gettext("Your data stays with you, period") %>
</p>
</li>
<li class="flex flex-col justify-center items-center
space-y-2">
<b class="whitespace-nowrap">
<%= gettext("Simple:") %>
</b>
<p>
<%= gettext("Access from any internet-capable device") %>
</p>
</li>
</ul>
<hr class="hr" />
<ul class="flex flex-col space-y-2 text-center justify-center">
<h2 class="title text-primary-600 text-lg">
<%= gettext("Instance Information") %>
</h2>
<li class="flex flex-col justify-center space-x-2">
<b>
<%= gettext("Admins:") %>
</b>
<p>
<%= if @admins |> Enum.empty?() do %>
<.link href={Routes.user_registration_path(Endpoint, :new)} class="hover:underline">
<%= dgettext("prompts", "Register to setup Cannery") %>
</.link>
<% else %>
<div class="flex flex-wrap justify-center space-x-2">
<a :for={%{email: email} <- @admins} class="hover:underline" href={"mailto:#{email}"}>
<%= email %>
</a>
</div>
<% end %>
</p>
</li>
<li class="flex flex-row justify-center space-x-2">
<b><%= gettext("Registration:") %></b>
<p>
<%= case Application.get_env(:cannery, Cannery.Accounts)[:registration] do
"public" -> gettext("Public Signups")
_ -> gettext("Invite Only")
end %>
</p>
</li>
<li class="flex flex-row justify-center items-center space-x-2">
<b><%= gettext("Version:") %></b>
<.link
href="https://gitea.bubbletea.dev/shibao/cannery/src/branch/stable/CHANGELOG.md"
class="flex flex-row justify-center items-center space-x-2 hover:underline"
target="_blank"
rel="noopener noreferrer"
>
<p>
<%= @version %>
</p>
<i class="fas fa-md fa-info-circle"></i>
</.link>
</li>
</ul>
<hr class="hr" />
<ul class="flex flex-col space-y-2 text-center justify-center">
<h2 class="title text-primary-600 text-lg">
<%= gettext("Get involved!") %>
</h2>
<li class="flex flex-col justify-center space-x-2">
<.link
class="flex flex-row justify-center items-center space-x-2 hover:underline"
href="https://gitea.bubbletea.dev/shibao/cannery"
target="_blank"
rel="noopener noreferrer"
>
<p><%= gettext("View the source code") %></p>
<i class="fas fa-md fa-code"></i>
</.link>
</li>
<li class="flex flex-col justify-center space-x-2">
<.link
class="flex flex-row justify-center items-center space-x-2 hover:underline"
href="https://weblate.bubbletea.dev/engage/cannery"
target="_blank"
rel="noopener noreferrer"
>
<p><%= gettext("Help translate") %></p>
<i class="fas fa-md fa-language"></i>
</.link>
</li>
<li class="flex flex-col justify-center space-x-2">
<.link
class="flex flex-row justify-center items-center space-x-2 hover:underline"
href="https://gitea.bubbletea.dev/shibao/cannery/issues/new"
target="_blank"
rel="noopener noreferrer"
>
<p><%= gettext("Report bugs or request features") %></p>
<i class="fas fa-md fa-spider"></i>
</.link>
</li>
</ul>
</div>

View File

@ -2,7 +2,7 @@ defmodule CanneryWeb.InitAssigns do
@moduledoc """ @moduledoc """
Ensures common `assigns` are applied to all LiveViews attaching this hook. Ensures common `assigns` are applied to all LiveViews attaching this hook.
""" """
import Phoenix.Component import Phoenix.LiveView
alias Cannery.Accounts alias Cannery.Accounts
def on_mount(:default, _params, %{"locale" => locale, "user_token" => user_token}, socket) do def on_mount(:default, _params, %{"locale" => locale, "user_token" => user_token}, socket) do

View File

@ -1,11 +1,11 @@
defmodule CanneryWeb.InviteLive.FormComponent do defmodule CanneryWeb.InviteLive.FormComponent do
@moduledoc """ @moduledoc """
Livecomponent that can update or create an Cannery.Accounts.Invite Livecomponent that can update or create an Cannery.Invites.Invite
""" """
use CanneryWeb, :live_component use CanneryWeb, :live_component
alias Cannery.{Accounts.User, Invites, Invites.Invite}
alias Ecto.Changeset alias Ecto.Changeset
alias Cannery.Accounts.{Invite, Invites, User}
alias Phoenix.LiveView.Socket alias Phoenix.LiveView.Socket
@impl true @impl true
@ -38,7 +38,7 @@ defmodule CanneryWeb.InviteLive.FormComponent do
changeset = changeset =
case action do case action do
:new -> Invite.create_changeset(user, "example_token", invite_params) :new -> invite |> Invite.create_changeset(user, "example_token", invite_params)
:edit -> invite |> Invite.update_changeset(invite_params) :edit -> invite |> Invite.update_changeset(invite_params)
end end
@ -60,7 +60,7 @@ defmodule CanneryWeb.InviteLive.FormComponent do
case invite |> Invites.update_invite(invite_params, current_user) do case invite |> Invites.update_invite(invite_params, current_user) do
{:ok, %{name: invite_name}} -> {:ok, %{name: invite_name}} ->
prompt = dgettext("prompts", "%{name} updated successfully", name: invite_name) prompt = dgettext("prompts", "%{name} updated successfully", name: invite_name)
socket |> put_flash(:info, prompt) |> push_navigate(to: return_to) socket |> put_flash(:info, prompt) |> push_redirect(to: return_to)
{:error, %Changeset{} = changeset} -> {:error, %Changeset{} = changeset} ->
socket |> assign(:changeset, changeset) socket |> assign(:changeset, changeset)
@ -78,7 +78,7 @@ defmodule CanneryWeb.InviteLive.FormComponent do
case current_user |> Invites.create_invite(invite_params) do case current_user |> Invites.create_invite(invite_params) do
{:ok, %{name: invite_name}} -> {:ok, %{name: invite_name}} ->
prompt = dgettext("prompts", "%{name} created successfully", name: invite_name) prompt = dgettext("prompts", "%{name} created successfully", name: invite_name)
socket |> put_flash(:info, prompt) |> push_navigate(to: return_to) socket |> put_flash(:info, prompt) |> push_redirect(to: return_to)
{:error, %Changeset{} = changeset} -> {:error, %Changeset{} = changeset} ->
socket |> assign(changeset: changeset) socket |> assign(changeset: changeset)

View File

@ -3,7 +3,7 @@
<%= @title %> <%= @title %>
</h2> </h2>
<.form <.form
:let={f} let={f}
for={@changeset} for={@changeset}
id="invite-form" id="invite-form"
class="flex flex-col space-y-4 sm:space-y-0 sm:grid sm:grid-cols-3 sm:gap-4 justify-center items-center" class="flex flex-col space-y-4 sm:space-y-0 sm:grid sm:grid-cols-3 sm:gap-4 justify-center items-center"
@ -11,12 +11,11 @@
phx-change="validate" phx-change="validate"
phx-submit="save" phx-submit="save"
> >
<div <%= if @changeset.action && not @changeset.valid? do %>
:if={@changeset.action && not @changeset.valid?()} <div class="invalid-feedback col-span-3 text-center">
class="invalid-feedback col-span-3 text-center" <%= changeset_errors(@changeset) %>
> </div>
<%= changeset_errors(@changeset) %> <% end %>
</div>
<%= label(f, :name, gettext("Name"), class: "title text-lg text-primary-600") %> <%= label(f, :name, gettext("Name"), class: "title text-lg text-primary-600") %>
<%= text_input(f, :name, class: "input input-primary col-span-2") %> <%= text_input(f, :name, class: "input input-primary col-span-2") %>
@ -26,7 +25,7 @@
<%= number_input(f, :uses_left, min: 0, class: "input input-primary col-span-2") %> <%= number_input(f, :uses_left, min: 0, class: "input input-primary col-span-2") %>
<%= error_tag(f, :uses_left, "col-span-3") %> <%= error_tag(f, :uses_left, "col-span-3") %>
<span class="col-span-3 text-primary-400 italic text-center"> <span class="col-span-3 text-primary-400 italic text-center">
<%= gettext(~s/Leave "Uses left" blank to make invite unlimited/) %> <%= gettext("Leave \"Uses left\" blank to make invite unlimited") %>
</span> </span>
<%= submit(dgettext("actions", "Save"), <%= submit(dgettext("actions", "Save"),

View File

@ -1,13 +1,12 @@
defmodule CanneryWeb.InviteLive.Index do defmodule CanneryWeb.InviteLive.Index do
@moduledoc """ @moduledoc """
Liveview to show a Cannery.Accounts.Invite index Liveview to show a Cannery.Invites.Invite index
""" """
use CanneryWeb, :live_view use CanneryWeb, :live_view
import CanneryWeb.Components.{InviteCard, UserCard} import CanneryWeb.Components.{InviteCard, UserCard}
alias Cannery.Accounts alias Cannery.{Accounts, Invites, Invites.Invite}
alias Cannery.Accounts.{Invite, Invites} alias CanneryWeb.{Endpoint, HomeLive}
alias CanneryWeb.HomeLive
alias Phoenix.LiveView.JS alias Phoenix.LiveView.JS
@impl true @impl true
@ -51,7 +50,7 @@ defmodule CanneryWeb.InviteLive.Index do
%{name: invite_name} = %{name: invite_name} =
id |> Invites.get_invite!(current_user) |> Invites.delete_invite!(current_user) id |> Invites.get_invite!(current_user) |> Invites.delete_invite!(current_user)
prompt = dgettext("prompts", "%{invite_name} deleted succesfully", invite_name: invite_name) prompt = dgettext("prompts", "%{name} deleted succesfully", name: invite_name)
{:noreply, socket |> put_flash(:info, prompt) |> display_invites()} {:noreply, socket |> put_flash(:info, prompt) |> display_invites()}
end end
@ -62,12 +61,10 @@ defmodule CanneryWeb.InviteLive.Index do
) do ) do
socket = socket =
Invites.get_invite!(id, current_user) Invites.get_invite!(id, current_user)
|> Invites.update_invite(%{uses_left: nil}, current_user) |> Invites.update_invite(%{"uses_left" => nil}, current_user)
|> case do |> case do
{:ok, %{name: invite_name}} -> {:ok, %{name: invite_name}} ->
prompt = prompt = dgettext("prompts", "%{name} updated succesfully", name: invite_name)
dgettext("prompts", "%{invite_name} updated succesfully", invite_name: invite_name)
socket |> put_flash(:info, prompt) |> display_invites() socket |> put_flash(:info, prompt) |> display_invites()
{:error, changeset} -> {:error, changeset} ->
@ -84,12 +81,10 @@ defmodule CanneryWeb.InviteLive.Index do
) do ) do
socket = socket =
Invites.get_invite!(id, current_user) Invites.get_invite!(id, current_user)
|> Invites.update_invite(%{uses_left: nil, disabled_at: nil}, current_user) |> Invites.update_invite(%{"uses_left" => nil, "disabled_at" => nil}, current_user)
|> case do |> case do
{:ok, %{name: invite_name}} -> {:ok, %{name: invite_name}} ->
prompt = prompt = dgettext("prompts", "%{name} enabled succesfully", name: invite_name)
dgettext("prompts", "%{invite_name} enabled succesfully", invite_name: invite_name)
socket |> put_flash(:info, prompt) |> display_invites() socket |> put_flash(:info, prompt) |> display_invites()
{:error, changeset} -> {:error, changeset} ->
@ -108,12 +103,10 @@ defmodule CanneryWeb.InviteLive.Index do
socket = socket =
Invites.get_invite!(id, current_user) Invites.get_invite!(id, current_user)
|> Invites.update_invite(%{uses_left: 0, disabled_at: now}, current_user) |> Invites.update_invite(%{"uses_left" => 0, "disabled_at" => now}, current_user)
|> case do |> case do
{:ok, %{name: invite_name}} -> {:ok, %{name: invite_name}} ->
prompt = prompt = dgettext("prompts", "%{name} disabled succesfully", name: invite_name)
dgettext("prompts", "%{invite_name} disabled succesfully", invite_name: invite_name)
socket |> put_flash(:info, prompt) |> display_invites() socket |> put_flash(:info, prompt) |> display_invites()
{:error, changeset} -> {:error, changeset} ->
@ -137,7 +130,7 @@ defmodule CanneryWeb.InviteLive.Index do
) do ) do
%{email: user_email} = Accounts.get_user!(id) |> Accounts.delete_user!(current_user) %{email: user_email} = Accounts.get_user!(id) |> Accounts.delete_user!(current_user)
prompt = dgettext("prompts", "%{user_email} deleted succesfully", user_email: user_email) prompt = dgettext("prompts", "%{name} deleted succesfully", name: user_email)
{:noreply, socket |> put_flash(:info, prompt) |> display_invites()} {:noreply, socket |> put_flash(:info, prompt) |> display_invites()}
end end

View File

@ -1,4 +1,4 @@
<div class="mx-auto flex flex-col justify-center items-center space-y-4 max-w-3xl"> <div class="w-full flex flex-col space-y-8 justify-center items-center">
<h1 class="title text-2xl title-primary-500"> <h1 class="title text-2xl title-primary-500">
<%= gettext("Invites") %> <%= gettext("Invites") %>
</h1> </h1>
@ -9,75 +9,78 @@
<%= display_emoji("😔") %> <%= display_emoji("😔") %>
</h1> </h1>
<.link patch={Routes.invite_index_path(Endpoint, :new)} class="btn btn-primary"> <%= live_patch(dgettext("actions", "Invite someone new!"),
<%= dgettext("actions", "Invite someone new!") %> to: Routes.invite_index_path(Endpoint, :new),
</.link> class: "btn btn-primary"
) %>
<% else %> <% else %>
<.link patch={Routes.invite_index_path(Endpoint, :new)} class="btn btn-primary"> <%= live_patch(dgettext("actions", "Create Invite"),
<%= dgettext("actions", "Create Invite") %> to: Routes.invite_index_path(Endpoint, :new),
</.link> class: "btn btn-primary"
) %>
<% end %> <% end %>
<div class="flex flex-col justify-center items-stretch space-y-4"> <div class="w-full flex flex-row flex-wrap justify-center items-center">
<.invite_card :for={invite <- @invites} invite={invite} current_user={@current_user}> <%= for invite <- @invites do %>
<:code_actions> <.invite_card invite={invite}>
<form phx-submit="copy_to_clipboard"> <:code_actions>
<button <form phx-submit="copy_to_clipboard">
type="submit" <button
class="mx-2 my-1 btn btn-primary" type="submit"
phx-click={JS.dispatch("cannery:clipcopy", to: "#code-#{invite.id}")} class="mx-2 my-1 btn btn-primary"
phx-click={JS.dispatch("cannery:clipcopy", to: "#code-#{invite.id}")}
>
<%= dgettext("actions", "Copy to clipboard") %>
</button>
</form>
</:code_actions>
<%= live_patch to: Routes.invite_index_path(Endpoint, :edit, invite),
class: "text-primary-600 link",
data: [qa: "edit-#{invite.id}"] do %>
<i class="fa-fw fa-lg fas fa-edit"></i>
<% end %>
<%= link to: "#",
class: "text-primary-600 link",
phx_click: "delete_invite",
phx_value_id: invite.id,
data: [
confirm:
dgettext("prompts", "Are you sure you want to delete the invite for %{name}?",
name: invite.name
),
qa: "delete-#{invite.id}"
] do %>
<i class="fa-fw fa-lg fas fa-trash"></i>
<% end %>
<%= if invite.disabled_at |> is_nil() do %>
<a href="#" class="btn btn-primary" phx-click="disable_invite" phx-value-id={invite.id}>
<%= gettext("Disable") %>
</a>
<% else %>
<a href="#" class="btn btn-primary" phx-click="enable_invite" phx-value-id={invite.id}>
<%= gettext("Enable") %>
</a>
<% end %>
<%= if invite.disabled_at |> is_nil() and not (invite.uses_left |> is_nil()) do %>
<a
href="#"
class="btn btn-primary"
phx-click="set_unlimited"
phx-value-id={invite.id}
data-confirm={
dgettext("prompts", "Are you sure you want to make %{name} unlimited?",
name: invite.name
)
}
> >
<%= dgettext("actions", "Copy to clipboard") %> <%= gettext("Set Unlimited") %>
</button> </a>
</form> <% end %>
</:code_actions> </.invite_card>
<.link <% end %>
patch={Routes.invite_index_path(Endpoint, :edit, invite)}
class="text-primary-600 link"
data-qa={"edit-#{invite.id}"}
>
<i class="fa-fw fa-lg fas fa-edit"></i>
</.link>
<.link
href="#"
class="text-primary-600 link"
phx-click="delete_invite"
phx-value-id={invite.id}
data-confirm={
dgettext("prompts", "Are you sure you want to delete the invite for %{invite_name}?",
invite_name: invite.name
)
}
data-qa={"delete-#{invite.id}"}
>
<i class="fa-fw fa-lg fas fa-trash"></i>
</.link>
<a
href="#"
class="btn btn-primary"
phx-click={if invite.disabled_at, do: "enable_invite", else: "disable_invite"}
phx-value-id={invite.id}
>
<%= if invite.disabled_at, do: gettext("Enable"), else: gettext("Disable") %>
</a>
<a
:if={invite.disabled_at |> is_nil() and not (invite.uses_left |> is_nil())}
href="#"
class="btn btn-primary"
phx-click="set_unlimited"
phx-value-id={invite.id}
data-confirm={
dgettext("prompts", "Are you sure you want to make %{invite_name} unlimited?",
invite_name: invite.name
)
}
>
<%= dgettext("actions", "Set Unlimited") %>
</a>
</.invite_card>
</div> </div>
<%= unless @admins |> Enum.empty?() do %> <%= unless @admins |> Enum.empty?() do %>
@ -87,24 +90,25 @@
<%= gettext("Admins") %> <%= gettext("Admins") %>
</h1> </h1>
<div class="flex flex-col justify-center items-stretch space-y-4"> <div class="w-full flex flex-row flex-wrap justify-center items-center">
<.user_card :for={admin <- @admins} user={admin}> <%= for admin <- @admins do %>
<.link <.user_card user={admin}>
href="#" <%= link to: "#",
class="text-primary-600 link" class: "text-primary-600 link",
phx-click="delete_user" phx_click: "delete_user",
phx-value-id={admin.id} phx_value_id: admin.id,
data-confirm={ data: [
dgettext( confirm:
"prompts", dgettext(
"Are you sure you want to delete %{email}? This action is permanent!", "prompts",
email: admin.email "Are you sure you want to delete %{email}? This action is permanent!",
) email: admin.email
} )
> ] do %>
<i class="fa-fw fa-lg fas fa-trash"></i> <i class="fa-fw fa-lg fas fa-trash"></i>
</.link> <% end %>
</.user_card> </.user_card>
<% end %>
</div> </div>
<% end %> <% end %>
@ -115,36 +119,39 @@
<%= gettext("Users") %> <%= gettext("Users") %>
</h1> </h1>
<div class="flex flex-col justify-center items-stretch space-y-4"> <div class="w-full flex flex-row flex-wrap justify-center items-center">
<.user_card :for={user <- @users} user={user}> <%= for user <- @users do %>
<.link <.user_card user={user}>
href="#" <%= link to: "#",
class="text-primary-600 link" class: "text-primary-600 link",
phx-click="delete_user" phx_click: "delete_user",
phx-value-id={user.id} phx_value_id: user.id,
data-confirm={ data: [
dgettext( confirm:
"prompts", dgettext(
"Are you sure you want to delete %{email}? This action is permanent!", "prompts",
email: user.email "Are you sure you want to delete %{email}? This action is permanent!",
) email: user.email
} )
> ] do %>
<i class="fa-fw fa-lg fas fa-trash"></i> <i class="fa-fw fa-lg fas fa-trash"></i>
</.link> <% end %>
</.user_card> </.user_card>
<% end %>
</div> </div>
<% end %> <% end %>
</div> </div>
<.modal :if={@live_action in [:new, :edit]} return_to={Routes.invite_index_path(Endpoint, :index)}> <%= if @live_action in [:new, :edit] do %>
<.live_component <.modal return_to={Routes.invite_index_path(Endpoint, :index)}>
module={CanneryWeb.InviteLive.FormComponent} <.live_component
id={@invite.id || :new} module={CanneryWeb.InviteLive.FormComponent}
title={@page_title} id={@invite.id || :new}
action={@live_action} title={@page_title}
invite={@invite} action={@live_action}
return_to={Routes.invite_index_path(Endpoint, :index)} invite={@invite}
current_user={@current_user} return_to={Routes.invite_index_path(Endpoint, :index)}
/> current_user={@current_user}
</.modal> />
</.modal>
<% end %>

View File

@ -3,7 +3,7 @@ defmodule CanneryWeb.LiveHelpers do
Contains common helper functions for liveviews Contains common helper functions for liveviews
""" """
import Phoenix.Component import Phoenix.LiveView.Helpers
alias Phoenix.LiveView.JS alias Phoenix.LiveView.JS
@doc """ @doc """
@ -27,17 +27,17 @@ defmodule CanneryWeb.LiveHelpers do
""" """
def modal(assigns) do def modal(assigns) do
~H""" ~H"""
<.link <%= live_patch to: @return_to,
patch={@return_to} id: "modal-bg",
id="modal-bg" class:
class="fade-in fixed z-10 left-0 top-0 "fade-in fixed z-10 left-0 top-0
w-full h-full overflow-hidden w-full h-full overflow-hidden
p-8 flex flex-col justify-center items-center cursor-auto" p-8 flex flex-col justify-center items-center cursor-auto",
style="background-color: rgba(0,0,0,0.4);" style: "background-color: rgba(0,0,0,0.4);",
phx-remove={hide_modal()} phx_remove: hide_modal()
> do %>
<span class="hidden"></span> <span class="hidden"></span>
</.link> <% end %>
<div <div
id="modal" id="modal"
@ -48,21 +48,20 @@ defmodule CanneryWeb.LiveHelpers do
<div <div
id="modal-content" id="modal-content"
class="fade-in-scale w-full max-w-3xl relative class="fade-in-scale w-full max-w-3xl relative
pointer-events-auto overflow-hidden pointer-events-auto overflow-hidden
px-8 py-4 sm:py-8 px-8 py-4 sm:py-8 flex flex-col justify-center items-center
flex flex-col justify-start items-center flex flex-col justify-start items-center
bg-white border-2 rounded-lg" bg-white border-2 rounded-lg"
> >
<.link <%= live_patch to: @return_to,
patch={@return_to} id: "close",
id="close" class:
class="absolute top-8 right-10 "absolute top-8 right-10
text-gray-500 hover:text-gray-800 text-gray-500 hover:text-gray-800
transition-all duration-500 ease-in-out" transition-all duration-500 ease-in-out",
phx-remove={hide_modal()} phx_remove: hide_modal() do %>
>
<i class="fa-fw fa-lg fas fa-times"></i> <i class="fa-fw fa-lg fas fa-times"></i>
</.link> <% end %>
<div class="overflow-x-hidden overflow-y-auto w-full p-8 flex flex-col space-y-4 justify-start items-center"> <div class="overflow-x-hidden overflow-y-auto w-full p-8 flex flex-col space-y-4 justify-start items-center">
<%= render_slot(@inner_block) %> <%= render_slot(@inner_block) %>
@ -72,57 +71,10 @@ defmodule CanneryWeb.LiveHelpers do
""" """
end end
defp hide_modal(js \\ %JS{}) do def hide_modal(js \\ %JS{}) do
js js
|> JS.hide(to: "#modal", transition: "fade-out") |> JS.hide(to: "#modal", transition: "fade-out")
|> JS.hide(to: "#modal-bg", transition: "fade-out") |> JS.hide(to: "#modal-bg", transition: "fade-out")
|> JS.hide(to: "#modal-content", transition: "fade-out-scale") |> JS.hide(to: "#modal-content", transition: "fade-out-scale")
end end
@doc """
A toggle button element that can be directed to a liveview or a
live_component's `handle_event/3`.
## Examples
<.toggle_button action="my_liveview_action" value={@some_value}>
<span>Toggle me!</span>
</.toggle_button>
<.toggle_button action="my_live_component_action" target={@myself} value={@some_value}>
<span>Whatever you want</span>
</.toggle_button>
"""
def toggle_button(assigns) do
assigns = assigns |> assign_new(:id, fn -> assigns.action end)
~H"""
<label for={@id} class="inline-flex relative items-center cursor-pointer">
<input
id={@id}
type="checkbox"
value={@value}
checked={@value}
class="sr-only peer"
data-qa={@id}
{
if assigns |> Map.has_key?(:target),
do: %{"phx-click": @action, "phx-value-value": @value, "phx-target": @target},
else: %{"phx-click": @action, "phx-value-value": @value}
}
/>
<div class="w-11 h-6 bg-gray-300 rounded-full peer
peer-focus:ring-4 peer-focus:ring-teal-300 dark:peer-focus:ring-teal-800
peer-checked:bg-gray-600
peer-checked:after:translate-x-full peer-checked:after:border-white
after:content-[''] after:absolute after:top-1 after:left-[2px] after:bg-white after:border-gray-300
after:border after:rounded-full after:h-5 after:w-5
after:transition-all after:duration-250 after:ease-in-out
transition-colors duration-250 ease-in-out">
</div>
<span class="ml-3 text-sm font-medium text-gray-900 dark:text-gray-300">
<%= render_slot(@inner_block) %>
</span>
</label>
"""
end
end end

View File

@ -53,7 +53,7 @@ defmodule CanneryWeb.RangeLive.FormComponent do
case ActivityLog.update_shot_group(shot_group, shot_group_params, current_user) do case ActivityLog.update_shot_group(shot_group, shot_group_params, current_user) do
{:ok, _shot_group} -> {:ok, _shot_group} ->
prompt = dgettext("prompts", "Shot records updated successfully") prompt = dgettext("prompts", "Shot records updated successfully")
socket |> put_flash(:info, prompt) |> push_navigate(to: return_to) socket |> put_flash(:info, prompt) |> push_redirect(to: return_to)
{:error, %Ecto.Changeset{} = changeset} -> {:error, %Ecto.Changeset{} = changeset} ->
socket |> assign(:changeset, changeset) socket |> assign(:changeset, changeset)

View File

@ -4,7 +4,7 @@
</h2> </h2>
<.form <.form
:let={f} let={f}
for={@changeset} for={@changeset}
id="shot-group-form" id="shot-group-form"
class="flex flex-col space-y-4 sm:space-y-0 sm:grid sm:grid-cols-3 sm:gap-4 justify-center items-center" class="flex flex-col space-y-4 sm:space-y-0 sm:grid sm:grid-cols-3 sm:gap-4 justify-center items-center"
@ -12,12 +12,11 @@
phx-change="validate" phx-change="validate"
phx-submit="save" phx-submit="save"
> >
<div <%= if @changeset.action && not @changeset.valid? do %>
:if={@changeset.action && not @changeset.valid?()} <div class="invalid-feedback col-span-3 text-center">
class="invalid-feedback col-span-3 text-center" <%= changeset_errors(@changeset) %>
> </div>
<%= changeset_errors(@changeset) %> <% end %>
</div>
<%= label(f, :count, gettext("Shots fired"), class: "title text-lg text-primary-600") %> <%= label(f, :count, gettext("Shots fired"), class: "title text-lg text-primary-600") %>
<%= number_input(f, :count, <%= number_input(f, :count,
@ -34,7 +33,7 @@
) %> ) %>
<%= error_tag(f, :notes, "col-span-3") %> <%= error_tag(f, :notes, "col-span-3") %>
<%= label(f, :date, gettext("Date"), class: "title text-lg text-primary-600") %> <%= label(f, :date, gettext("Date (UTC)"), class: "title text-lg text-primary-600") %>
<%= date_input(f, :date, class: "input input-primary col-span-2") %> <%= date_input(f, :date, class: "input input-primary col-span-2") %>
<%= error_tag(f, :notes, "col-span-3") %> <%= error_tag(f, :notes, "col-span-3") %>

View File

@ -10,13 +10,7 @@ defmodule CanneryWeb.RangeLive.Index do
alias Phoenix.LiveView.Socket alias Phoenix.LiveView.Socket
@impl true @impl true
def mount(%{"search" => search}, _session, socket) do def mount(_params, _session, socket), do: {:ok, socket |> display_shot_groups()}
{:ok, socket |> assign(search: search) |> display_shot_groups()}
end
def mount(_params, _session, socket) do
{:ok, socket |> assign(search: nil) |> display_shot_groups()}
end
@impl true @impl true
def handle_params(params, _url, %{assigns: %{live_action: live_action}} = socket) do def handle_params(params, _url, %{assigns: %{live_action: live_action}} = socket) do
@ -29,46 +23,26 @@ defmodule CanneryWeb.RangeLive.Index do
%{"id" => id} %{"id" => id}
) do ) do
socket socket
|> assign( |> assign(:page_title, gettext("Record Shots"))
page_title: gettext("Record Shots"), |> assign(:ammo_group, Ammo.get_ammo_group!(id, current_user))
ammo_group: Ammo.get_ammo_group!(id, current_user)
)
end end
defp apply_action(%{assigns: %{current_user: current_user}} = socket, :edit, %{"id" => id}) do defp apply_action(%{assigns: %{current_user: current_user}} = socket, :edit, %{"id" => id}) do
socket socket
|> assign( |> assign(:page_title, gettext("Edit Shot Records"))
page_title: gettext("Edit Shot Records"), |> assign(:shot_group, ActivityLog.get_shot_group!(id, current_user))
shot_group: ActivityLog.get_shot_group!(id, current_user)
)
end end
defp apply_action(socket, :new, _params) do defp apply_action(socket, :new, _params) do
socket socket
|> assign( |> assign(:page_title, gettext("New Shot Records"))
page_title: gettext("New Shot Records"), |> assign(:shot_group, %ShotGroup{})
shot_group: %ShotGroup{}
)
end end
defp apply_action(socket, :index, _params) do defp apply_action(socket, :index, _params) do
socket socket
|> assign( |> assign(:page_title, gettext("Shot Records"))
page_title: gettext("Shot Records"), |> assign(:shot_group, nil)
search: nil,
shot_group: nil
)
|> display_shot_groups()
end
defp apply_action(socket, :search, %{"search" => search}) do
socket
|> assign(
page_title: gettext("Shot Records"),
search: search,
shot_group: nil
)
|> display_shot_groups()
end end
@impl true @impl true
@ -81,7 +55,6 @@ defmodule CanneryWeb.RangeLive.Index do
{:noreply, socket |> put_flash(:info, prompt) |> display_shot_groups()} {:noreply, socket |> put_flash(:info, prompt) |> display_shot_groups()}
end end
@impl true
def handle_event( def handle_event(
"toggle_staged", "toggle_staged",
%{"ammo_group_id" => ammo_group_id}, %{"ammo_group_id" => ammo_group_id},
@ -96,46 +69,75 @@ defmodule CanneryWeb.RangeLive.Index do
{:noreply, socket |> put_flash(:info, prompt) |> display_shot_groups()} {:noreply, socket |> put_flash(:info, prompt) |> display_shot_groups()}
end end
@impl true
def handle_event("search", %{"search" => %{"search_term" => ""}}, socket) do
{:noreply, socket |> push_patch(to: Routes.range_index_path(Endpoint, :index))}
end
def handle_event("search", %{"search" => %{"search_term" => search_term}}, socket) do
{:noreply, socket |> push_patch(to: Routes.range_index_path(Endpoint, :search, search_term))}
end
@spec display_shot_groups(Socket.t()) :: Socket.t() @spec display_shot_groups(Socket.t()) :: Socket.t()
defp display_shot_groups(%{assigns: %{search: search, current_user: current_user}} = socket) do defp display_shot_groups(%{assigns: %{current_user: current_user}} = socket) do
shot_groups = shot_groups =
ActivityLog.list_shot_groups(search, current_user) ActivityLog.list_shot_groups(current_user) |> Repo.preload(ammo_group: :ammo_type)
|> Repo.preload(ammo_group: :ammo_type)
ammo_groups = Ammo.list_staged_ammo_groups(current_user) ammo_groups = Ammo.list_staged_ammo_groups(current_user)
chart_data = shot_groups |> get_chart_data_for_shot_group()
columns = [
%{label: gettext("Ammo"), key: "name"},
%{label: gettext("Rounds shot"), key: "count"},
%{label: gettext("Notes"), key: "notes"},
%{label: gettext("Date"), key: "date"},
%{label: nil, key: "actions", sortable: false}
]
rows =
shot_groups
|> Enum.map(fn shot_group -> shot_group |> get_row_data_for_shot_group(columns) end)
socket socket
|> assign( |> assign(ammo_groups: ammo_groups, columns: columns, rows: rows, shot_groups: shot_groups)
ammo_groups: ammo_groups,
chart_data: chart_data,
shot_groups: shot_groups
)
end end
@spec get_chart_data_for_shot_group([ShotGroup.t()]) :: [map()] @spec get_row_data_for_shot_group(ShotGroup.t(), [map()]) :: [map()]
defp get_chart_data_for_shot_group(shot_groups) do defp get_row_data_for_shot_group(%{date: date} = shot_group, columns) do
shot_groups shot_group = shot_group |> Repo.preload(ammo_group: :ammo_type)
|> Repo.preload(ammo_group: :ammo_type) assigns = %{shot_group: shot_group}
|> Enum.group_by(fn %{date: date} -> date end, fn %{count: count} -> count end)
|> Enum.map(fn {date, rounds} ->
sum = Enum.sum(rounds)
%{ columns
date: date, |> Enum.into(%{}, fn %{key: key} ->
count: sum, value =
label: gettext("Rounds shot: %{count}", count: sum) case key do
} "name" ->
{shot_group.ammo_group.ammo_type.name,
live_patch(shot_group.ammo_group.ammo_type.name,
to: Routes.ammo_group_show_path(Endpoint, :show, shot_group.ammo_group),
class: "link"
)}
"date" ->
date |> display_date()
"actions" ->
~H"""
<div class="px-4 py-2 space-x-4 flex justify-center items-center">
<%= live_patch to: Routes.range_index_path(Endpoint, :edit, shot_group),
class: "text-primary-600 link",
data: [qa: "edit-#{shot_group.id}"] do %>
<i class="fa-fw fa-lg fas fa-edit"></i>
<% end %>
<%= link to: "#",
class: "text-primary-600 link",
phx_click: "delete",
phx_value_id: shot_group.id,
data: [
confirm: dgettext("prompts", "Are you sure you want to delete this shot record?"),
qa: "delete-#{shot_group.id}"
] do %>
<i class="fa-fw fa-lg fas fa-trash"></i>
<% end %>
</div>
"""
key ->
shot_group |> Map.get(key |> String.to_existing_atom())
end
{key, value}
end) end)
|> Enum.sort_by(fn %{date: date} -> date end, Date)
end end
end end

View File

@ -9,16 +9,18 @@
<%= display_emoji("😔") %> <%= display_emoji("😔") %>
</h1> </h1>
<.link navigate={Routes.ammo_group_index_path(Endpoint, :index)} class="btn btn-primary"> <%= live_patch(dgettext("actions", "Why not get some ready to shoot?"),
<%= dgettext("actions", "Why not get some ready to shoot?") %> to: Routes.ammo_group_index_path(Endpoint, :index),
</.link> class: "btn btn-primary"
) %>
<% else %> <% else %>
<.link navigate={Routes.ammo_group_index_path(Endpoint, :index)} class="btn btn-primary"> <%= live_patch(dgettext("actions", "Stage ammo"),
<%= dgettext("actions", "Stage ammo") %> to: Routes.ammo_group_index_path(Endpoint, :index),
</.link> class: "btn btn-primary"
) %>
<div class="w-full flex flex-row flex-wrap justify-center items-stretch"> <%= for ammo_group <- @ammo_groups do %>
<.ammo_group_card :for={ammo_group <- @ammo_groups} ammo_group={ammo_group}> <.ammo_group_card ammo_group={ammo_group}>
<button <button
type="button" type="button"
class="btn btn-primary" class="btn btn-primary"
@ -27,23 +29,21 @@
data-confirm={"#{dgettext("prompts", "Are you sure you want to unstage this ammo?")}"} data-confirm={"#{dgettext("prompts", "Are you sure you want to unstage this ammo?")}"}
> >
<%= if ammo_group.staged, <%= if ammo_group.staged,
do: dgettext("actions", "Unstage from range"), do: gettext("Unstage from range"),
else: dgettext("actions", "Stage for range") %> else: gettext("Stage for range") %>
</button> </button>
<.link <%= live_patch(dgettext("actions", "Record shots"),
patch={Routes.range_index_path(Endpoint, :add_shot_group, ammo_group)} to: Routes.range_index_path(Endpoint, :add_shot_group, ammo_group),
class="btn btn-primary" class: "btn btn-primary"
> ) %>
<%= dgettext("actions", "Record shots") %>
</.link>
</.ammo_group_card> </.ammo_group_card>
</div> <% end %>
<% end %> <% end %>
<hr class="hr" /> <hr class="hr" />
<%= if @shot_groups |> Enum.empty?() and @search |> is_nil() do %> <%= if @shot_groups |> Enum.empty?() do %>
<h1 class="title text-xl text-primary-600"> <h1 class="title text-xl text-primary-600">
<%= gettext("No shots recorded") %> <%= gettext("No shots recorded") %>
<%= display_emoji("😔") %> <%= display_emoji("😔") %>
@ -53,102 +53,39 @@
<%= gettext("Shot log") %> <%= gettext("Shot log") %>
</h1> </h1>
<canvas <.live_component
id="shot-log-chart" module={CanneryWeb.Components.TableComponent}
phx-hook="ShotLogChart" id="shot_groups_index_table"
phx-update="ignore" columns={@columns}
class="max-h-72" rows={@rows}
data-chart-data={Jason.encode!(@chart_data)} />
data-label={gettext("Rounds shot")}
data-color={random_color()}
aria-label={gettext("Rounds shot chart")}
role="img"
>
<%= dgettext("errors", "Your browser does not support the canvas element.") %>
</canvas>
<div class="w-full flex flex-col sm:flex-row justify-center items-center space-y-4 sm:space-y-0 sm:space-x-4 max-w-xl">
<.form
:let={f}
for={:search}
phx-change="search"
phx-submit="search"
class="grow self-stretch flex flex-col items-stretch"
data-qa="shot_group_search"
>
<%= text_input(f, :search_term,
class: "input input-primary",
value: @search,
phx_debounce: 300,
placeholder: gettext("Search shot records")
) %>
</.form>
</div>
<%= if @shot_groups |> Enum.empty?() do %>
<h1 class="title text-xl text-primary-600">
<%= gettext("No shots recorded") %>
<%= display_emoji("😔") %>
</h1>
<% else %>
<.live_component
module={CanneryWeb.Components.ShotGroupTableComponent}
id="shot_groups_index_table"
shot_groups={@shot_groups}
current_user={@current_user}
>
<:actions :let={shot_group}>
<div class="px-4 py-2 space-x-4 flex justify-center items-center">
<.link
patch={Routes.range_index_path(Endpoint, :edit, shot_group)}
class="text-primary-600 link"
data-qa={"edit-#{shot_group.id}"}
>
<i class="fa-fw fa-lg fas fa-edit"></i>
</.link>
<.link
href="#"
class="text-primary-600 link"
phx-click="delete"
phx-value-id={shot_group.id}
data-confirm={
dgettext("prompts", "Are you sure you want to delete this shot record?")
}
data-qa={"delete-#{shot_group.id}"}
>
<i class="fa-fw fa-lg fas fa-trash"></i>
</.link>
</div>
</:actions>
</.live_component>
<% end %>
<% end %> <% end %>
</div> </div>
<.modal :if={@live_action == :edit} return_to={Routes.range_index_path(Endpoint, :index)}> <%= if @live_action in [:edit] do %>
<.live_component <.modal return_to={Routes.range_index_path(Endpoint, :index)}>
module={CanneryWeb.RangeLive.FormComponent} <.live_component
id={@shot_group.id} module={CanneryWeb.RangeLive.FormComponent}
title={@page_title} id={@shot_group.id}
action={@live_action} title={@page_title}
shot_group={@shot_group} action={@live_action}
return_to={Routes.range_index_path(Endpoint, :index)} shot_group={@shot_group}
current_user={@current_user} return_to={Routes.range_index_path(Endpoint, :index)}
/> current_user={@current_user}
</.modal> />
</.modal>
<% end %>
<.modal <%= if @live_action in [:add_shot_group] do %>
:if={@live_action == :add_shot_group} <.modal return_to={Routes.range_index_path(Endpoint, :index)}>
return_to={Routes.range_index_path(Endpoint, :index)} <.live_component
> module={CanneryWeb.Components.AddShotGroupComponent}
<.live_component id={:new}
module={CanneryWeb.Components.AddShotGroupComponent} title={@page_title}
id={:new} action={@live_action}
title={@page_title} ammo_group={@ammo_group}
action={@live_action} return_to={Routes.range_index_path(Endpoint, :index)}
ammo_group={@ammo_group} current_user={@current_user}
return_to={Routes.range_index_path(Endpoint, :index)} />
current_user={@current_user} </.modal>
/> <% end %>
</.modal>

View File

@ -50,6 +50,53 @@ defmodule CanneryWeb.TagLive.FormComponent do
socket |> assign(:changeset, changeset) socket |> assign(:changeset, changeset)
end end
@impl true
def render(assigns) do
~H"""
<div>
<h2 class="mb-8 text-center title text-xl text-primary-600">
<%= @title %>
</h2>
<.form
let={f}
for={@changeset}
id="tag-form"
class="flex flex-col space-y-4 sm:space-y-0 sm:grid sm:grid-cols-3 sm:gap-4 justify-center items-center"
phx-target={@myself}
phx-change="validate"
phx-submit="save"
>
<%= if @changeset.action && not @changeset.valid? do %>
<div class="invalid-feedback col-span-3 text-center">
<%= changeset_errors(@changeset) %>
</div>
<% end %>
<%= label(f, :name, gettext("Name"), class: "title text-lg text-primary-600") %>
<%= text_input(f, :name, class: "input input-primary col-span-2") %>
<%= error_tag(f, :name, "col-span-3") %>
<%= label(f, :bg_color, gettext("Background color"), class: "title text-lg text-primary-600") %>
<span id="tag-bg-color-input" class="mx-auto col-span-2" phx-update="ignore">
<%= color_input(f, :bg_color) %>
</span>
<%= error_tag(f, :bg_color, "col-span-3") %>
<%= label(f, :text_color, gettext("Text color"), class: "title text-lg text-primary-600") %>
<span id="tag-text-color-input" class="mx-auto col-span-2" phx-update="ignore">
<%= color_input(f, :text_color) %>
</span>
<%= error_tag(f, :text_color, "col-span-3") %>
<%= submit(dgettext("actions", "Save"),
class: "mx-auto btn btn-primary col-span-3",
phx_disable_with: dgettext("prompts", "Saving...")
) %>
</.form>
</div>
"""
end
defp save_tag( defp save_tag(
%{assigns: %{tag: tag, current_user: current_user, return_to: return_to}} = socket, %{assigns: %{tag: tag, current_user: current_user, return_to: return_to}} = socket,
:edit, :edit,
@ -59,7 +106,7 @@ defmodule CanneryWeb.TagLive.FormComponent do
case Tags.update_tag(tag, tag_params, current_user) do case Tags.update_tag(tag, tag_params, current_user) do
{:ok, %{name: tag_name}} -> {:ok, %{name: tag_name}} ->
prompt = dgettext("prompts", "%{name} updated successfully", name: tag_name) prompt = dgettext("prompts", "%{name} updated successfully", name: tag_name)
socket |> put_flash(:info, prompt) |> push_navigate(to: return_to) socket |> put_flash(:info, prompt) |> push_redirect(to: return_to)
{:error, %Changeset{} = changeset} -> {:error, %Changeset{} = changeset} ->
socket |> assign(:changeset, changeset) socket |> assign(:changeset, changeset)
@ -77,7 +124,7 @@ defmodule CanneryWeb.TagLive.FormComponent do
case Tags.create_tag(tag_params, current_user) do case Tags.create_tag(tag_params, current_user) do
{:ok, %{name: tag_name}} -> {:ok, %{name: tag_name}} ->
prompt = dgettext("prompts", "%{name} created successfully", name: tag_name) prompt = dgettext("prompts", "%{name} created successfully", name: tag_name)
socket |> put_flash(:info, prompt) |> push_navigate(to: return_to) socket |> put_flash(:info, prompt) |> push_redirect(to: return_to)
{:error, %Changeset{} = changeset} -> {:error, %Changeset{} = changeset} ->
socket |> assign(changeset: changeset) socket |> assign(changeset: changeset)

View File

@ -1,42 +0,0 @@
<div>
<h2 class="mb-8 text-center title text-xl text-primary-600">
<%= @title %>
</h2>
<.form
:let={f}
for={@changeset}
id="tag-form"
class="flex flex-col space-y-4 sm:space-y-0 sm:grid sm:grid-cols-3 sm:gap-4 justify-center items-center"
phx-target={@myself}
phx-change="validate"
phx-submit="save"
>
<div
:if={@changeset.action && not @changeset.valid?()}
class="invalid-feedback col-span-3 text-center"
>
<%= changeset_errors(@changeset) %>
</div>
<%= label(f, :name, gettext("Name"), class: "title text-lg text-primary-600") %>
<%= text_input(f, :name, class: "input input-primary col-span-2") %>
<%= error_tag(f, :name, "col-span-3") %>
<%= label(f, :bg_color, gettext("Background color"), class: "title text-lg text-primary-600") %>
<span id="tag-bg-color-input" class="mx-auto col-span-2" phx-update="ignore">
<%= color_input(f, :bg_color) %>
</span>
<%= error_tag(f, :bg_color, "col-span-3") %>
<%= label(f, :text_color, gettext("Text color"), class: "title text-lg text-primary-600") %>
<span id="tag-text-color-input" class="mx-auto col-span-2" phx-update="ignore">
<%= color_input(f, :text_color) %>
</span>
<%= error_tag(f, :text_color, "col-span-3") %>
<%= submit(dgettext("actions", "Save"),
class: "mx-auto btn btn-primary col-span-3",
phx_disable_with: dgettext("prompts", "Saving...")
) %>
</.form>
</div>

View File

@ -6,56 +6,30 @@ defmodule CanneryWeb.TagLive.Index do
use CanneryWeb, :live_view use CanneryWeb, :live_view
import CanneryWeb.Components.TagCard import CanneryWeb.Components.TagCard
alias Cannery.{Tags, Tags.Tag} alias Cannery.{Tags, Tags.Tag}
alias CanneryWeb.ViewHelpers alias CanneryWeb.Endpoint
@impl true @impl true
def mount(%{"search" => search}, _session, socket) do def mount(_params, _session, socket), do: {:ok, socket |> display_tags()}
{:ok, socket |> assign(:search, search) |> display_tags()}
end
def mount(_params, _session, socket) do
{:ok, socket |> assign(:search, nil) |> display_tags()}
end
@impl true @impl true
def handle_params(params, _url, %{assigns: %{live_action: live_action}} = socket) do def handle_params(params, _url, %{assigns: %{live_action: live_action}} = socket) do
{:noreply, apply_action(socket, live_action, params)} {:noreply, apply_action(socket, live_action, params) |> display_tags}
end end
defp apply_action(%{assigns: %{current_user: current_user}} = socket, :edit, %{"id" => id}) do defp apply_action(%{assigns: %{current_user: current_user}} = socket, :edit, %{"id" => id}) do
socket socket
|> assign( |> assign(:page_title, gettext("Edit Tag"))
page_title: gettext("Edit Tag"), |> assign(:tag, Tags.get_tag!(id, current_user))
tag: Tags.get_tag!(id, current_user)
)
end end
defp apply_action(socket, :new, _params) do defp apply_action(socket, :new, _params) do
socket socket
|> assign( |> assign(:page_title, gettext("New Tag"))
page_title: gettext("New Tag"), |> assign(:tag, %Tag{bg_color: Tags.random_bg_color(), text_color: "#ffffff"})
tag: %Tag{bg_color: ViewHelpers.random_color(), text_color: "#ffffff"}
)
end end
defp apply_action(socket, :index, _params) do defp apply_action(socket, :index, _params) do
socket socket |> assign(:page_title, gettext("Tags")) |> assign(:tag, nil)
|> assign(
page_title: gettext("Tags"),
search: nil,
tag: nil
)
|> display_tags()
end
defp apply_action(socket, :search, %{"search" => search}) do
socket
|> assign(
page_title: gettext("Tags"),
search: search,
tag: nil
)
|> display_tags()
end end
@impl true @impl true
@ -65,16 +39,7 @@ defmodule CanneryWeb.TagLive.Index do
{:noreply, socket |> put_flash(:info, prompt) |> display_tags()} {:noreply, socket |> put_flash(:info, prompt) |> display_tags()}
end end
@impl true defp display_tags(%{assigns: %{current_user: current_user}} = socket) do
def handle_event("search", %{"search" => %{"search_term" => ""}}, socket) do socket |> assign(tags: Tags.list_tags(current_user))
{:noreply, socket |> push_patch(to: Routes.tag_index_path(Endpoint, :index))}
end
def handle_event("search", %{"search" => %{"search_term" => search_term}}, socket) do
{:noreply, socket |> push_patch(to: Routes.tag_index_path(Endpoint, :search, search_term))}
end
defp display_tags(%{assigns: %{search: search, current_user: current_user}} = socket) do
socket |> assign(tags: Tags.list_tags(search, current_user))
end end
end end

View File

@ -5,80 +5,56 @@
<p class="title text-md text-primary-600"> <p class="title text-md text-primary-600">
<%= gettext("Tags can be added to your containers to help you organize") %> <%= gettext("Tags can be added to your containers to help you organize") %>
</p> </p>
<%= if @tags |> Enum.empty?() and @search |> is_nil() do %>
<h2 class="title text-xl text-primary-600">
<%= gettext("No tags") %>
<%= display_emoji("😔") %>
</h2>
<.link patch={Routes.tag_index_path(Endpoint, :new)} class="btn btn-primary">
<%= dgettext("actions", "Make your first tag!") %>
</.link>
<% else %>
<.link patch={Routes.tag_index_path(Endpoint, :new)} class="btn btn-primary">
<%= dgettext("actions", "New Tag") %>
</.link>
<% end %>
<div class="w-full flex flex-col sm:flex-row justify-center items-center space-y-4 sm:space-y-0 sm:space-x-4 max-w-xl">
<.form
:let={f}
for={:search}
phx-change="search"
phx-submit="search"
class="grow self-stretch flex flex-col items-stretch"
data-qa="tag_search"
>
<%= text_input(f, :search_term,
class: "input input-primary",
value: @search,
phx_debounce: 300,
placeholder: gettext("Search tags")
) %>
</.form>
</div>
<%= if @tags |> Enum.empty?() do %> <%= if @tags |> Enum.empty?() do %>
<h2 class="title text-xl text-primary-600"> <h2 class="title text-xl text-primary-600">
<%= gettext("No tags") %> <%= gettext("No tags") %>
<%= display_emoji("😔") %> <%= display_emoji("😔") %>
</h2> </h2>
<% else %>
<div class="flex flex-row flex-wrap justify-center items-stretch">
<.tag_card :for={tag <- @tags} tag={tag}>
<.link
patch={Routes.tag_index_path(Endpoint, :edit, tag)}
class="text-primary-600 link"
data-qa={"edit-#{tag.id}"}
>
<i class="fa-fw fa-lg fas fa-edit"></i>
</.link>
<.link <%= live_patch(dgettext("actions", "Make your first tag!"),
href="#" to: Routes.tag_index_path(Endpoint, :new),
class="text-primary-600 link" class: "btn btn-primary"
phx-click="delete" ) %>
phx-value-id={tag.id} <% else %>
data-confirm={ <%= live_patch(dgettext("actions", "New Tag"),
dgettext("prompts", "Are you sure you want to delete %{name}?", name: tag.name) to: Routes.tag_index_path(Endpoint, :new),
} class: "btn btn-primary"
data-qa={"delete-#{tag.id}"} ) %>
>
<i class="fa-fw fa-lg fas fa-trash"></i>
</.link>
</.tag_card>
</div>
<% end %> <% end %>
<div class="flex flex-row flex-wrap justify-center items-center">
<%= for tag <- @tags do %>
<.tag_card tag={tag}>
<%= live_patch to: Routes.tag_index_path(Endpoint, :edit, tag),
class: "text-primary-600 link",
data: [qa: "edit-#{tag.id}"] do %>
<i class="fa-fw fa-lg fas fa-edit"></i>
<% end %>
<%= link to: "#",
class: "text-primary-600 link",
phx_click: "delete",
phx_value_id: tag.id,
data: [
confirm: dgettext("prompts", "Are you sure you want to delete %{name}?", name: tag.name),
qa: "delete-#{tag.id}"
] do %>
<i class="fa-fw fa-lg fas fa-trash"></i>
<% end %>
</.tag_card>
<% end %>
</div>
</div> </div>
<.modal :if={@live_action in [:new, :edit]} return_to={Routes.tag_index_path(Endpoint, :index)}> <%= if @live_action in [:new, :edit] do %>
<.live_component <.modal return_to={Routes.tag_index_path(Endpoint, :index)}>
module={CanneryWeb.TagLive.FormComponent} <.live_component
id={@tag.id || :new} module={CanneryWeb.TagLive.FormComponent}
title={@page_title} id={@tag.id || :new}
action={@live_action} title={@page_title}
tag={@tag} action={@live_action}
return_to={Routes.tag_index_path(Endpoint, :index)} tag={@tag}
current_user={@current_user} return_to={Routes.tag_index_path(Endpoint, :index)}
/> current_user={@current_user}
</.modal> />
</.modal>
<% end %>

View File

@ -11,17 +11,15 @@ defmodule CanneryWeb.Router do
plug :protect_from_forgery plug :protect_from_forgery
plug :put_secure_browser_headers plug :put_secure_browser_headers
plug :fetch_current_user plug :fetch_current_user
plug :put_user_locale plug :put_user_locale, default: Application.get_env(:gettext, :default_locale, "en_US")
end end
defp put_user_locale(%{assigns: %{current_user: %{locale: locale}}} = conn, _opts) do defp put_user_locale(%{assigns: %{current_user: %{locale: locale}}} = conn, default: default) do
default = Application.fetch_env!(:gettext, :default_locale)
Gettext.put_locale(locale || default) Gettext.put_locale(locale || default)
conn |> put_session(:locale, locale || default) conn |> put_session(:locale, locale || default)
end end
defp put_user_locale(conn, _opts) do defp put_user_locale(conn, default: default) do
default = Application.fetch_env!(:gettext, :default_locale)
Gettext.put_locale(default) Gettext.put_locale(default)
conn |> put_session(:locale, default) conn |> put_session(:locale, default)
end end
@ -62,52 +60,42 @@ defmodule CanneryWeb.Router do
put "/users/settings", UserSettingsController, :update put "/users/settings", UserSettingsController, :update
delete "/users/settings/:id", UserSettingsController, :delete delete "/users/settings/:id", UserSettingsController, :delete
get "/users/settings/confirm_email/:token", UserSettingsController, :confirm_email get "/users/settings/confirm_email/:token", UserSettingsController, :confirm_email
get "/export/:mode", ExportController, :export
live "/tags", TagLive.Index, :index live "/tags", TagLive.Index, :index
live "/tags/new", TagLive.Index, :new live "/tags/new", TagLive.Index, :new
live "/tags/edit/:id", TagLive.Index, :edit live "/tags/:id/edit", TagLive.Index, :edit
live "/tags/search/:search", TagLive.Index, :search
live "/catalog", AmmoTypeLive.Index, :index live "/catalog", AmmoTypeLive.Index, :index
live "/catalog/new", AmmoTypeLive.Index, :new live "/catalog/new", AmmoTypeLive.Index, :new
live "/catalog/clone/:id", AmmoTypeLive.Index, :clone live "/catalog/:id/edit", AmmoTypeLive.Index, :edit
live "/catalog/edit/:id", AmmoTypeLive.Index, :edit
live "/catalog/search/:search", AmmoTypeLive.Index, :search
live "/type/:id", AmmoTypeLive.Show, :show live "/catalog/:id", AmmoTypeLive.Show, :show
live "/type/:id/edit", AmmoTypeLive.Show, :edit live "/catalog/:id/show/edit", AmmoTypeLive.Show, :edit
live "/type/:id/table", AmmoTypeLive.Show, :table
live "/containers", ContainerLive.Index, :index live "/containers", ContainerLive.Index, :index
live "/containers/new", ContainerLive.Index, :new live "/containers/new", ContainerLive.Index, :new
live "/containers/edit/:id", ContainerLive.Index, :edit live "/containers/:id/edit", ContainerLive.Index, :edit
live "/containers/clone/:id", ContainerLive.Index, :clone live "/containers/:id/edit_tags", ContainerLive.Index, :edit_tags
live "/containers/edit_tags/:id", ContainerLive.Index, :edit_tags
live "/containers/search/:search", ContainerLive.Index, :search
live "/container/:id", ContainerLive.Show, :show live "/containers/:id", ContainerLive.Show, :show
live "/container/edit/:id", ContainerLive.Show, :edit live "/containers/:id/show/edit", ContainerLive.Show, :edit
live "/container/edit_tags/:id", ContainerLive.Show, :edit_tags live "/containers/:id/show/edit_tags", ContainerLive.Show, :edit_tags
live "/ammo", AmmoGroupLive.Index, :index live "/ammo", AmmoGroupLive.Index, :index
live "/ammo/new", AmmoGroupLive.Index, :new live "/ammo/new", AmmoGroupLive.Index, :new
live "/ammo/edit/:id", AmmoGroupLive.Index, :edit live "/ammo/:id/edit", AmmoGroupLive.Index, :edit
live "/ammo/clone/:id", AmmoGroupLive.Index, :clone live "/ammo/:id/add_shot_group", AmmoGroupLive.Index, :add_shot_group
live "/ammo/add_shot_group/:id", AmmoGroupLive.Index, :add_shot_group live "/ammo/:id/move", AmmoGroupLive.Index, :move
live "/ammo/move/:id", AmmoGroupLive.Index, :move
live "/ammo/search/:search", AmmoGroupLive.Index, :search
live "/ammo/show/:id", AmmoGroupLive.Show, :show live "/ammo/:id", AmmoGroupLive.Show, :show
live "/ammo/show/edit/:id", AmmoGroupLive.Show, :edit live "/ammo/:id/show/edit", AmmoGroupLive.Show, :edit
live "/ammo/show/add_shot_group/:id", AmmoGroupLive.Show, :add_shot_group live "/ammo/:id/show/add_shot_group", AmmoGroupLive.Show, :add_shot_group
live "/ammo/show/move/:id", AmmoGroupLive.Show, :move live "/ammo/:id/show/move", AmmoGroupLive.Show, :move
live "/ammo/show/:id/edit/:shot_group_id", AmmoGroupLive.Show, :edit_shot_group live "/ammo/:id/show/:shot_group_id/edit", AmmoGroupLive.Show, :edit_shot_group
live "/range", RangeLive.Index, :index live "/range", RangeLive.Index, :index
live "/range/edit/:id", RangeLive.Index, :edit live "/range/:id/edit", RangeLive.Index, :edit
live "/range/add_shot_group/:id", RangeLive.Index, :add_shot_group live "/range/:id/add_shot_group", RangeLive.Index, :add_shot_group
live "/range/search/:search", RangeLive.Index, :search
end end
scope "/", CanneryWeb do scope "/", CanneryWeb do

View File

@ -57,30 +57,6 @@ defmodule CanneryWeb.Telemetry do
"The time the connection spent waiting before being checked out for the query" "The time the connection spent waiting before being checked out for the query"
), ),
# Oban Metrics
counter("oban.job.exception",
tags: [:queue, :worker],
event_name: [:oban, :job, :exception],
measurement: :duration,
description: "Number of oban jobs that raised an exception"
),
counter("oban.job.start",
tags: [:queue, :worker],
event_name: [:oban, :job, :start],
measurement: :system_time,
description: "Number of oban jobs started"
),
summary("oban.job.stop.duration",
tags: [:queue, :worker],
unit: {:native, :millisecond},
description: "Length of time spent processing the oban job"
),
summary("oban.job.stop.queue_time",
tags: [:queue, :worker],
unit: {:native, :millisecond},
description: "Time the oban job spent waiting in milliseconds"
),
# VM Metrics # VM Metrics
summary("vm.memory.total", unit: {:byte, :kilobyte}), summary("vm.memory.total", unit: {:byte, :kilobyte}),
summary("vm.total_run_queue_lengths.total"), summary("vm.total_run_queue_lengths.total"),

View File

@ -3,21 +3,23 @@
<%= dgettext("emails", "Hi %{email},", email: @user.email) %> <%= dgettext("emails", "Hi %{email},", email: @user.email) %>
</span> </span>
<br /> <br/>
<span style="margin-bottom: 1em; font-size: 1.25em;"> <span style="margin-bottom: 1em; font-size: 1.25em;">
<%= dgettext("emails", "Welcome to Cannery") %> <%= dgettext("emails", "Welcome to %{name}!", name: "Cannery") %>
</span> </span>
<br /> <br/>
<%= dgettext("emails", "You can confirm your account by visiting the URL below:") %> <%= dgettext("emails", "You can confirm your account by visiting the URL below:") %>
<br /> <br/>
<a style="margin: 1em; color: rgb(31, 31, 31);" href={@url}><%= @url %></a> <a style="margin: 1em; color: rgb(31, 31, 31);" href="<%= @url %>"><%= @url %></a>
<br /> <br/>
<%= dgettext("emails", "If you didn't create an account at Cannery, please ignore this.") %> <%= dgettext("emails",
"If you didn't create an account at %{name}, please ignore this.",
name: "Cannery") %>
</div> </div>

View File

@ -1,7 +1,7 @@
<%= dgettext("emails", "Hi %{email},", email: @user.email) %> <%= dgettext("emails", "Hi %{email},", email: @user.email) %>
<%= dgettext("emails", "Welcome to Cannery") %> <%= dgettext("emails", "Welcome to %{name}%!", name: "Cannery") %>
<%= dgettext("emails", "You can confirm your account by visiting the URL below:") %> <%= dgettext("emails", "You can confirm your account by visiting the URL below:") %>

View File

@ -3,15 +3,17 @@
<%= dgettext("emails", "Hi %{email},", email: @user.email) %> <%= dgettext("emails", "Hi %{email},", email: @user.email) %>
</span> </span>
<br /> <br/>
<%= dgettext("emails", "You can reset your password by visiting the URL below:") %> <%= dgettext("emails", "You can reset your password by visiting the URL below:") %>
<br /> <br/>
<a style="margin: 1em; color: rgb(31, 31, 31);" href={@url}><%= @url %></a> <a style="margin: 1em; color: rgb(31, 31, 31);" href="<%= @url %>"><%= @url %></a>
<br /> <br/>
<%= dgettext("emails", "If you didn't request this change from Cannery, please ignore this.") %> <%= dgettext("emails",
"If you didn't request this change from %{name}, please ignore this.",
name: "Cannery") %>
</div> </div>

View File

@ -3,18 +3,17 @@
<%= dgettext("emails", "Hi %{email},", email: @user.email) %> <%= dgettext("emails", "Hi %{email},", email: @user.email) %>
</span> </span>
<br /> <br/>
<%= dgettext("emails", "You can change your email by visiting the URL below:") %> <%= dgettext("emails", "You can change your email by visiting the URL below:") %>
<br /> <br/>
<a style="margin: 1em; color: rgb(31, 31, 31);" href={@url}><%= @url %></a> <a style="margin: 1em; color: rgb(31, 31, 31);" href="<%= @url %>"><%= @url %></a>
<br /> <br/>
<%= dgettext( <%= dgettext("emails",
"emails", "If you didn't request this change from %{name}, please ignore this.",
"If you didn't request this change from Cannery, please ignore this." name: "Cannery") %>
) %>
</div> </div>

View File

@ -5,7 +5,7 @@
<meta http-equiv="X-UA-Compatible" content="IE=edge" /> <meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title> <title>
<%= dgettext("errors", "Error") %> | <%= gettext("Cannery") %> <%= dgettext("errors", "Error") %>| Cannery
</title> </title>
<link rel="stylesheet" href="/css/app.css" /> <link rel="stylesheet" href="/css/app.css" />
<script defer type="text/javascript" src="/js/app.js"> <script defer type="text/javascript" src="/js/app.js">
@ -13,7 +13,7 @@
</head> </head>
<body class="pb-8 m-0 p-0 w-full h-full"> <body class="pb-8 m-0 p-0 w-full h-full">
<header> <header>
<.topbar current_user={assigns[:current_user]} /> <.topbar current_user={assigns[:current_user]}></.topbar>
</header> </header>
<div class="pb-8 w-full flex flex-col justify-center items-center text-center"> <div class="pb-8 w-full flex flex-col justify-center items-center text-center">

Some files were not shown because too many files have changed in this diff Show More