Compare commits
161 Commits
Author | SHA1 | Date | |
---|---|---|---|
2af9c7a4f9 | |||
7e7d6258d5 | |||
f246b9db93 | |||
5836a82ff7 | |||
7464947497 | |||
3adb8c9aae | |||
dce04e4d7f | |||
ee6266be3f | |||
36f56528ee | |||
76bbab1de9 | |||
6c09261368 | |||
3593334c85 | |||
57b5cb432d | |||
5b5fd7173b | |||
41bcc2f456 | |||
947659b207 | |||
9ebca20dc6 | |||
3cbd62e84c | |||
67010640f0 | |||
5fd46c326f | |||
8c62a39c97 | |||
9a1a104c6d | |||
11b1ae9980 | |||
948fa929ec | |||
268085e761 | |||
a1efd7cc60 | |||
2db9ab968f | |||
7863b06215 | |||
eb8cae27c0 | |||
ecca9ee4a9 | |||
a0c4f20afd | |||
0e46a05360 | |||
1062d97f79 | |||
1b5df8c56c | |||
da5c136313 | |||
ef206d273f | |||
ef513a8108 | |||
ed1765c652 | |||
541e6a4612 | |||
43589a88c3 | |||
15a6e75e7d | |||
2681a37fad | |||
d086c4c813 | |||
f49fe21da5 | |||
717c898d48 | |||
085411132d | |||
c8a6a9d81e | |||
a0389db9ef | |||
e6e4db7410 | |||
78542a533a | |||
46665dce88 | |||
4b420f313c | |||
42b4d0758f | |||
901c2a948f | |||
6fe5a29ebd | |||
ce7223597c | |||
4791a2849e | |||
7edc9642d4 | |||
2ae0c3133c | |||
f284773f68 | |||
d35a81ef39 | |||
4c3ebaec53 | |||
a13a57c6ee | |||
8c7b9f9a9b | |||
7b7604f48f | |||
b1b52b9edc | |||
3fcea8e679 | |||
98ec324a2c | |||
0868789811 | |||
87c62f9ef8 | |||
0919ee2a72 | |||
0b910af077 | |||
cb81eb4116 | |||
b1d7cfe591 | |||
1431b92e42 | |||
c555022ea7 | |||
0917aff37b | |||
7f9e6f9eff | |||
9e754fe630 | |||
45d905b384 | |||
3297130890 | |||
d27b54386d | |||
ebf32c49bf | |||
119f2af6bb | |||
9db6b2c316 | |||
22281486e0 | |||
1b7546aede | |||
e153893a5b | |||
2f7c17aad3 | |||
664c65d136 | |||
ef76eb002d | |||
5b40ac4137 | |||
ed20cdd858 | |||
1e0ec82f3e | |||
d07ac801aa | |||
420e7c2d71 | |||
40877d1ac0 | |||
1dd30e6a5b | |||
eedaf33e25 | |||
b0a100cd6c | |||
6455e2710d | |||
6523b28aa2 | |||
bad1a23dfe | |||
e0ddefe1d7 | |||
5d6ecba9f7 | |||
a2d1ff9b89 | |||
34288a0070 | |||
f9b08222e1 | |||
c0179f48bd | |||
8bb4aab49c | |||
9f2cc54738 | |||
0309e9d714 | |||
af4af84515 | |||
ec6946068e | |||
f120e54c3e | |||
3d115c6383 | |||
1d4622a285 | |||
d9e7948bb0 | |||
da8c788992 | |||
bd20820361 | |||
d0ee81093a | |||
6080fdbe64 | |||
f42aaf9099 | |||
a6aa6f3386 | |||
8405513337 | |||
d0857eccc1 | |||
4c9e707181 | |||
e6a4fbcfb5 | |||
a6b2c6181e | |||
d79d0fa179 | |||
46387e8d7a | |||
dccea1b9de | |||
5ca21c64fd | |||
9773ccc6ff | |||
9cd2bc574b | |||
ee28de1178 | |||
91cf9d0eb5 | |||
3ce8eda712 | |||
1a78e88b34 | |||
968abd04ee | |||
dc209fa192 | |||
a80df49fdd | |||
92d1d21d00 | |||
917f627933 | |||
4946a6b119 | |||
9f784c3190 | |||
aa08e212ee | |||
80ad939aab | |||
08c9cddc78 | |||
e6285c282b | |||
61829fc042 | |||
e2f8fd3ee5 | |||
763c9e521e | |||
98747be7c2 | |||
91288a9ffa | |||
a19ec682e6 | |||
dba53106fb | |||
91ff0c14e4 | |||
bf27511caa | |||
4ff2f64a22 | |||
146c8e7ab3 |
14
.credo.exs
14
.credo.exs
@ -157,17 +157,17 @@
|
||||
#
|
||||
# Controversial and experimental checks (opt-in, just replace `false` with `[]`)
|
||||
#
|
||||
{Credo.Check.Consistency.MultiAliasImportRequireUse, false},
|
||||
{Credo.Check.Consistency.UnusedVariableNames, false},
|
||||
{Credo.Check.Consistency.MultiAliasImportRequireUse, []},
|
||||
{Credo.Check.Consistency.UnusedVariableNames, [force: :meaningful]},
|
||||
{Credo.Check.Design.DuplicatedCode, false},
|
||||
{Credo.Check.Readability.AliasAs, false},
|
||||
{Credo.Check.Readability.BlockPipe, false},
|
||||
{Credo.Check.Readability.ImplTrue, false},
|
||||
{Credo.Check.Readability.MultiAlias, false},
|
||||
{Credo.Check.Readability.SeparateAliasRequire, false},
|
||||
{Credo.Check.Readability.SeparateAliasRequire, []},
|
||||
{Credo.Check.Readability.SinglePipe, false},
|
||||
{Credo.Check.Readability.Specs, false},
|
||||
{Credo.Check.Readability.StrictModuleLayout, false},
|
||||
{Credo.Check.Readability.StrictModuleLayout, []},
|
||||
{Credo.Check.Readability.WithCustomTaggedTuple, false},
|
||||
{Credo.Check.Refactor.ABCSize, false},
|
||||
{Credo.Check.Refactor.AppendSingleItem, false},
|
||||
@ -176,9 +176,9 @@
|
||||
{Credo.Check.Refactor.NegatedIsNil, false},
|
||||
{Credo.Check.Refactor.PipeChainStart, false},
|
||||
{Credo.Check.Refactor.VariableRebinding, false},
|
||||
{Credo.Check.Warning.LeakyEnvironment, false},
|
||||
{Credo.Check.Warning.MapGetUnsafePass, false},
|
||||
{Credo.Check.Warning.UnsafeToAtom, false}
|
||||
{Credo.Check.Warning.LeakyEnvironment, []},
|
||||
{Credo.Check.Warning.MapGetUnsafePass, []},
|
||||
{Credo.Check.Warning.UnsafeToAtom, []}
|
||||
|
||||
#
|
||||
# Custom checks can be created using `mix credo.gen.check`.
|
||||
|
28
.drone.yml
28
.drone.yml
@ -16,30 +16,48 @@ steps:
|
||||
- assets/node_modules/
|
||||
|
||||
- name: test
|
||||
image: bitwalker/alpine-elixir-phoenix:1.13
|
||||
image: elixir:1.13.4-alpine
|
||||
environment:
|
||||
TEST_DATABASE_URL: ecto://postgres:postgres@database/cannery_test
|
||||
HOST: testing.example.tld
|
||||
commands:
|
||||
- apk add --no-cache build-base npm git python3
|
||||
- mix local.rebar --force
|
||||
- mix local.hex --force
|
||||
- mix deps.get
|
||||
- npm install --prefix assets
|
||||
- mix deps.compile
|
||||
- npm --prefix ./assets ci --progress=false --no-audit --loglevel=error
|
||||
- npm run --prefix ./assets deploy
|
||||
- mix do phx.digest, gettext.extract
|
||||
- mix test
|
||||
|
||||
- name: build and publish
|
||||
- name: build and publish stable
|
||||
image: plugins/docker
|
||||
settings:
|
||||
repo: shibaobun/cannery
|
||||
tags: latest
|
||||
username:
|
||||
from_secret: docker_username
|
||||
password:
|
||||
from_secret: docker_password
|
||||
tags: latest
|
||||
when:
|
||||
branch:
|
||||
- stable
|
||||
|
||||
- name: build and publish tagged version
|
||||
image: plugins/docker
|
||||
settings:
|
||||
repo: shibaobun/cannery
|
||||
username:
|
||||
from_secret: docker_username
|
||||
password:
|
||||
from_secret: docker_password
|
||||
tags:
|
||||
- ${DRONE_TAG}
|
||||
when:
|
||||
event:
|
||||
- tag
|
||||
|
||||
- name: rebuild-cache
|
||||
image: drillster/drone-volume-cache
|
||||
volumes:
|
||||
@ -63,7 +81,7 @@ services:
|
||||
volumes:
|
||||
- name: cache
|
||||
host:
|
||||
path: /tmp/drone-cache
|
||||
path: /run/media/default/ssdsrv/gitea/drone-cache
|
||||
- name: docker_sock
|
||||
host:
|
||||
path: /var/run/docker.sock
|
||||
|
@ -2,5 +2,5 @@
|
||||
import_deps: [:ecto, :phoenix],
|
||||
inputs: ["*.{heex,ex,exs}", "priv/*/seeds.exs", "{config,lib,test}/**/*.{heex,ex,exs}"],
|
||||
subdirectories: ["priv/*/migrations"],
|
||||
plugins: [HeexFormatter]
|
||||
plugins: [Phoenix.LiveView.HTMLFormatter]
|
||||
]
|
||||
|
@ -1,3 +1,3 @@
|
||||
elixir 1.13.2-otp-24
|
||||
elixir 1.13.4-otp-24
|
||||
erlang 24.2
|
||||
nodejs 16.13.2
|
||||
|
80
CHANGELOG.md
Normal file
80
CHANGELOG.md
Normal file
@ -0,0 +1,80 @@
|
||||
# v0.5.4
|
||||
- Rename "Ammo" tab to "Catalog", and "Manage" tab is now "Ammo"
|
||||
- Ammo groups are now just referred to as Ammo or "Packs"
|
||||
- URL paths now reflect new names
|
||||
- Add pack and round count to container information
|
||||
- Add cute logo >:3 Thank you [kalli](https://twitter.com/t0kkuro)!
|
||||
- Add note about deleting an ammo type deleting all ammo of that type as well
|
||||
- Prompt to create first ammo type before trying to create first ammo
|
||||
- Add note about creating unlimited invites
|
||||
- Update screenshot lol
|
||||
|
||||
# v0.5.3
|
||||
- Update French translation: Thank you [duponin](https://udongein.xyz/users/duponin)!
|
||||
- Update German translation: Thank you [Kaia](https://shitposter.club/users/kaia)!
|
||||
|
||||
# v0.5.2
|
||||
- Add "Added on" date to ammo groups
|
||||
- Add "Added on" date to ammo types
|
||||
- Add "Registered on" date to user information
|
||||
- Add language in user settings. The `LOCALE` environment variable will continue
|
||||
to set the default locale for the application.
|
||||
- Add involvement links to home page
|
||||
- Fix button text-wrapping
|
||||
- Update dependencies
|
||||
|
||||
# v0.5.1
|
||||
- Add French translation: Thank you [duponin](https://udongein.xyz/users/duponin)!
|
||||
|
||||
# v0.5.0
|
||||
- Add German translation: Thank you [Kaia](https://shitposter.club/users/kaia)!
|
||||
- Fix not being able to edit ammo group when fully used up
|
||||
- Fix bug with average price per round calculation
|
||||
- Show average price per round on ammo type table
|
||||
- Use Elixir v1.13.4
|
||||
|
||||
# v0.4.1
|
||||
- Fix button and tag text wrapping
|
||||
- Code quality fixes
|
||||
|
||||
# v0.4.0
|
||||
- Make tables sortable
|
||||
- Add link to changelog from version number
|
||||
- Fix some elements flashing with black background
|
||||
- Fix bug with moving ammo group to new container
|
||||
- Fix bug with no error showing up for create ammo group form
|
||||
|
||||
# v0.3.0
|
||||
- Fix ammo type counts not showing when count is 0
|
||||
- Add prompt to create first container before first ammo group
|
||||
- Edit and delete shot groups from ammo group show page
|
||||
- Use today's date when adding new shot groups
|
||||
- Create multiple ammo groups at one time
|
||||
|
||||
# v0.2.3
|
||||
- Fix modals with overflowing forms
|
||||
- Fix grids having uneven margins in phone mode
|
||||
- Add page titles to registration and setting pages
|
||||
|
||||
# v0.2.2
|
||||
- Fix loading and reconnecting pages not being fixed
|
||||
- Fix closing modal in some cases not triggering a page reload
|
||||
- Fix error when display container and tag edit routes from a fresh reload
|
||||
|
||||
# v0.2.1
|
||||
- Fix checkbox spacing for mobile view
|
||||
- Fix spacing with form elements in mobile view
|
||||
- Fix user card spacing
|
||||
|
||||
# v0.2.0
|
||||
- Add or remove tags from containers list and details page
|
||||
- Show tags on containers
|
||||
- Add "Cannery" to page titles
|
||||
- Don't show true/false column for ammo types if all values are false
|
||||
- Fix ammo type firing type display
|
||||
- Show original count, current value, and percentage remaining for ammo groups
|
||||
- Show shot history for an ammo group
|
||||
- Show ammo round totals and total shot for ammo types
|
||||
|
||||
# v0.1.0
|
||||
- Initial release!
|
@ -63,8 +63,7 @@ And as always, thank you!
|
||||
[`phx_gen_auth`](https://hexdocs.pm/phx_gen_auth/).
|
||||
- `Dockerfile` and example `docker-compose.yml`
|
||||
- Automatic migrations in `MIX_ENV=prod` or Docker image
|
||||
- JS linting with [standard.js](https://standardjs.com), HEEx linting with
|
||||
[heex_formatter](https://github.com/feliperenan/heex_formatter)
|
||||
- JS linting with [standard.js](https://standardjs.com)
|
||||
|
||||
## Docs
|
||||
|
||||
@ -109,7 +108,8 @@ In `dev` mode, Cannery will listen for these environment variables at runtime.
|
||||
Defaults to `false`.
|
||||
- `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`.
|
||||
- `LOCALE`: Sets a custom locale. Defaults to `en_US`.
|
||||
- `LOCALE`: Sets a custom default locale. Defaults to `en_US`.
|
||||
- Available options: `en_US`, `de`, and `fr`
|
||||
|
||||
## `MIX_ENV=test`
|
||||
|
||||
@ -141,3 +141,6 @@ In `prod` mode (or in the Docker container), Cannery will listen for the same en
|
||||
Thank you so much for your contributions!
|
||||
|
||||
- shibao (https://misskey.bubbletea.dev/@shibao)
|
||||
- kaia (https://shitposter.club/users/kaia)
|
||||
- duponin (https://udongein.xyz/users/duponin)
|
||||
- kalli (https://twitter.com/t0kkuro)
|
||||
|
@ -1,4 +1,4 @@
|
||||
FROM elixir:1.13-alpine AS build
|
||||
FROM elixir:1.13.4-alpine AS build
|
||||
|
||||
# install build dependencies
|
||||
RUN apk add --no-cache build-base npm git python3
|
||||
|
@ -1,5 +1,8 @@
|
||||
# Cannery
|
||||
|
||||

|
||||

|
||||
|
||||
The self-hosted firearm tracker website.
|
||||
|
||||
* Easy to Use: Cannery lets you easily keep an eye on your ammo levels before
|
||||
@ -61,7 +64,8 @@ You can use the following environment variables to configure Cannery in
|
||||
with `docker run -it shibaobun/cannery mix phx.gen.secret` and set for server to start.
|
||||
- `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 locale. Defaults to `en_US`.
|
||||
- `LOCALE`: Sets a custom default locale. Defaults to `en_US`
|
||||
- Available options: `en_US`, `de`, and `fr`
|
||||
- `SMTP_HOST`: The url for your SMTP email provider. Must be set
|
||||
- `SMTP_PORT`: The port for your SMTP relay. Defaults to `587`.
|
||||
- `SMTP_USERNAME`: The username for your SMTP relay. Must be set!
|
||||
|
@ -17,8 +17,7 @@
|
||||
-o-transform: scale(1.5);
|
||||
transform: scale(1.5);
|
||||
padding: 10px;
|
||||
margin-left: auto;
|
||||
margin-right: auto;
|
||||
margin: 1em auto;
|
||||
}
|
||||
|
||||
.title {
|
||||
@ -26,6 +25,7 @@
|
||||
}
|
||||
|
||||
.btn {
|
||||
@apply inline-block break-words min-w-4;
|
||||
@apply focus:outline-none px-4 py-2 rounded-lg;
|
||||
@apply shadow-sm focus:shadow-lg;
|
||||
@apply transition-all duration-300 ease-in-out;
|
||||
@ -52,6 +52,7 @@
|
||||
}
|
||||
|
||||
.link {
|
||||
@apply inline-block break-all min-w-4;
|
||||
@apply hover:underline;
|
||||
@apply transition-colors duration-500 ease-in-out;
|
||||
}
|
||||
|
@ -2,7 +2,10 @@
|
||||
// update. https://github.com/phoenixframework/phoenix_live_view/issues/1011
|
||||
|
||||
export default {
|
||||
attrs () { return this.el.getAttribute('data-attrs').split(', ') },
|
||||
attrs () {
|
||||
const attrs = this.el.getAttribute('data-attrs')
|
||||
if (attrs) { return attrs.split(', ') } else { return [] }
|
||||
},
|
||||
beforeUpdate () { this.prevAttrs = this.attrs().map(name => [name, this.el.getAttribute(name)]) },
|
||||
updated () { this.prevAttrs.forEach(([name, val]) => this.el.setAttribute(name, val)) }
|
||||
}
|
||||
|
11731
assets/package-lock.json
generated
11731
assets/package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@ -9,36 +9,33 @@
|
||||
"test": "standard"
|
||||
},
|
||||
"dependencies": {
|
||||
"@fortawesome/fontawesome-free": "^5.15.4",
|
||||
"alpinejs": "^3.9.0",
|
||||
"@fortawesome/fontawesome-free": "^6.1.1",
|
||||
"alpinejs": "^3.10.2",
|
||||
"phoenix": "file:../deps/phoenix",
|
||||
"phoenix_html": "file:../deps/phoenix_html",
|
||||
"phoenix_live_view": "file:../deps/phoenix_live_view",
|
||||
"topbar": "^0.1.4"
|
||||
"topbar": "^1.0.1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@babel/core": "^7.15.0",
|
||||
"@babel/preset-env": "^7.15.0",
|
||||
"autoprefixer": "^10.2.6",
|
||||
"babel-loader": "^8.2.2",
|
||||
"copy-webpack-plugin": "^9.0.0",
|
||||
"css-loader": "^5.2.7",
|
||||
"css-minimizer-webpack-plugin": "^3.0.1",
|
||||
"@babel/core": "^7.17.10",
|
||||
"@babel/preset-env": "^7.17.10",
|
||||
"autoprefixer": "^10.4.7",
|
||||
"babel-loader": "^8.2.5",
|
||||
"copy-webpack-plugin": "^10.2.4",
|
||||
"css-loader": "^6.7.1",
|
||||
"css-minimizer-webpack-plugin": "^3.4.1",
|
||||
"file-loader": "^6.2.0",
|
||||
"hard-source-webpack-plugin": "^0.13.1",
|
||||
"mini-css-extract-plugin": "^1.6.0",
|
||||
"node-sass": "^7.0.1",
|
||||
"postcss": "^8.3.6",
|
||||
"postcss-import": "^14.0.2",
|
||||
"postcss-loader": "^6.1.1",
|
||||
"postcss-preset-env": "^7.3.1",
|
||||
"sass-loader": "^12.1.0",
|
||||
"standard": "^16.0.4",
|
||||
"style-loader": "^3.2.1",
|
||||
"tailwindcss": "^2.2.7",
|
||||
"terser-webpack-plugin": "^5.1.3",
|
||||
"webpack": "^5.67.0",
|
||||
"webpack-cli": "^4.8.0",
|
||||
"webpack-dev-server": "^4.7.4"
|
||||
"mini-css-extract-plugin": "^2.6.0",
|
||||
"postcss": "^8.4.13",
|
||||
"postcss-import": "^14.1.0",
|
||||
"postcss-loader": "^6.2.1",
|
||||
"postcss-preset-env": "^7.5.0",
|
||||
"sass-loader": "^12.6.0",
|
||||
"standard": "^17.0.0",
|
||||
"tailwindcss": "^3.0.24",
|
||||
"terser-webpack-plugin": "^5.3.1",
|
||||
"webpack": "^5.72.0",
|
||||
"webpack-cli": "^4.9.2",
|
||||
"webpack-dev-server": "^4.9.0"
|
||||
}
|
||||
}
|
||||
|
BIN
assets/static/images/cannery.png
Normal file
BIN
assets/static/images/cannery.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 84 KiB |
38
assets/static/images/cannery.svg
Normal file
38
assets/static/images/cannery.svg
Normal file
File diff suppressed because one or more lines are too long
After Width: | Height: | Size: 516 KiB |
@ -1,24 +1,18 @@
|
||||
const colors = require('tailwindcss/colors')
|
||||
|
||||
module.exports = {
|
||||
purge: [
|
||||
'../lib/**/*.ex',
|
||||
'../lib/**/*.heex',
|
||||
'../lib/**/*.leex',
|
||||
'../lib/**/*.eex',
|
||||
content: [
|
||||
'../lib/**/*.{ex,heex,leex,eex}',
|
||||
'./js/**/*.js'
|
||||
],
|
||||
darkMode: 'media',
|
||||
theme: {
|
||||
colors: {
|
||||
transparent: 'transparent',
|
||||
current: 'currentColor',
|
||||
|
||||
primary: colors.gray,
|
||||
|
||||
black: colors.black,
|
||||
white: colors.white,
|
||||
gray: colors.trueGray,
|
||||
gray: colors.neutral,
|
||||
indigo: colors.indigo,
|
||||
red: colors.rose,
|
||||
yellow: colors.amber
|
||||
@ -28,13 +22,21 @@ module.exports = {
|
||||
128: '32rem',
|
||||
192: '48rem',
|
||||
256: '64rem'
|
||||
}
|
||||
}
|
||||
},
|
||||
variants: {
|
||||
extend: {
|
||||
backgroundColor: ['active'],
|
||||
borderColor: ['active']
|
||||
minWidth: {
|
||||
4: '1rem',
|
||||
8: '2rem',
|
||||
12: '3rem',
|
||||
16: '4rem',
|
||||
20: '8rem'
|
||||
},
|
||||
maxWidth: {
|
||||
4: '1rem',
|
||||
8: '2rem',
|
||||
12: '3rem',
|
||||
16: '4rem',
|
||||
20: '8rem'
|
||||
}
|
||||
}
|
||||
},
|
||||
plugins: []
|
||||
|
@ -44,19 +44,14 @@ module.exports = (env, options) => {
|
||||
},
|
||||
{
|
||||
test: /\.(woff(2)?|ttf|eot|svg|otf)(\?v=[0-9]\.[0-9]\.[0-9])?$/,
|
||||
loader: 'file-loader',
|
||||
options: {
|
||||
name: '[name].[ext]?[hash]',
|
||||
outputPath: '../fonts'
|
||||
}
|
||||
type: 'asset/resource',
|
||||
generator: { filename: 'fonts/[name][ext]' }
|
||||
}
|
||||
]
|
||||
},
|
||||
plugins: [
|
||||
new MiniCssExtractPlugin({ filename: '../css/app.css' }),
|
||||
new CopyWebpackPlugin({
|
||||
patterns: [{ from: 'static/', to: '../' }]
|
||||
})
|
||||
new CopyWebpackPlugin({ patterns: [{ from: 'static/', to: '../' }] })
|
||||
]
|
||||
}
|
||||
}
|
||||
|
@ -14,8 +14,8 @@ end
|
||||
|
||||
config :cannery, CanneryWeb.ViewHelpers, shibao_mode: System.get_env("SHIBAO_MODE") == "true"
|
||||
|
||||
# Set locale
|
||||
Gettext.put_locale(System.get_env("LOCALE") || "en_US")
|
||||
# Set default locale
|
||||
config :gettext, :default_locale, System.get_env("LOCALE") || "en_US"
|
||||
|
||||
maybe_ipv6 = if System.get_env("ECTO_IPV6") == "true", do: [:inet6], else: []
|
||||
|
||||
|
@ -196,7 +196,7 @@ defmodule Cannery.Accounts do
|
||||
{:ok, _} <- Repo.transaction(user_email_multi(user, email, context)) do
|
||||
:ok
|
||||
else
|
||||
_ -> :error
|
||||
_error_tuple -> :error
|
||||
end
|
||||
end
|
||||
|
||||
@ -265,10 +265,39 @@ defmodule Cannery.Accounts do
|
||||
|> Repo.transaction()
|
||||
|> case do
|
||||
{:ok, %{user: user}} -> {:ok, user}
|
||||
{:error, :user, changeset, _} -> {:error, changeset}
|
||||
{:error, :user, changeset, _changes_so_far} -> {:error, changeset}
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns an `%Changeset{}` for changing the user locale.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> change_user_locale(user)
|
||||
%Changeset{data: %User{}}
|
||||
|
||||
"""
|
||||
@spec change_user_locale(User.t()) :: Changeset.t(User.t())
|
||||
def change_user_locale(%{locale: locale} = user), do: User.locale_changeset(user, locale)
|
||||
|
||||
@doc """
|
||||
Updates the user locale.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> update_user_locale(user, "valid locale")
|
||||
{:ok, %User{}}
|
||||
|
||||
iex> update_user_password(user, "invalid locale")
|
||||
{:error, %Changeset{}}
|
||||
|
||||
"""
|
||||
@spec update_user_locale(User.t(), locale :: String.t()) ::
|
||||
{:ok, User.t()} | {:error, Changeset.t(User.t())}
|
||||
def update_user_locale(user, locale),
|
||||
do: user |> User.locale_changeset(locale) |> Repo.update()
|
||||
|
||||
@doc """
|
||||
Deletes a user. must be performed by an admin or the same user!
|
||||
|
||||
@ -372,7 +401,7 @@ defmodule Cannery.Accounts do
|
||||
{:ok, %{user: user}} <- Repo.transaction(confirm_user_multi(user)) do
|
||||
{:ok, user}
|
||||
else
|
||||
_ -> :error
|
||||
_error_tuple -> :error
|
||||
end
|
||||
end
|
||||
|
||||
@ -420,7 +449,7 @@ defmodule Cannery.Accounts do
|
||||
%User{} = user <- Repo.one(query) do
|
||||
user
|
||||
else
|
||||
_ -> nil
|
||||
_error_tuple -> nil
|
||||
end
|
||||
end
|
||||
|
||||
@ -444,7 +473,7 @@ defmodule Cannery.Accounts do
|
||||
|> Repo.transaction()
|
||||
|> case do
|
||||
{:ok, %{user: user}} -> {:ok, user}
|
||||
{:error, :user, changeset, _} -> {:error, changeset}
|
||||
{:error, :user, changeset, _changes_so_far} -> {:error, changeset}
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -18,6 +18,7 @@ defmodule Cannery.Accounts.User do
|
||||
field :hashed_password, :string
|
||||
field :confirmed_at, :naive_datetime
|
||||
field :role, Ecto.Enum, values: [:admin, :user], default: :user
|
||||
field :locale, :string
|
||||
|
||||
has_many :invites, Invite, on_delete: :delete_all
|
||||
|
||||
@ -31,6 +32,7 @@ defmodule Cannery.Accounts.User do
|
||||
hashed_password: String.t(),
|
||||
confirmed_at: NaiveDateTime.t(),
|
||||
role: atom(),
|
||||
locale: String.t() | nil,
|
||||
invites: [Invite.t()],
|
||||
inserted_at: NaiveDateTime.t(),
|
||||
updated_at: NaiveDateTime.t()
|
||||
@ -60,7 +62,7 @@ defmodule Cannery.Accounts.User do
|
||||
Changeset.t(t() | new_user())
|
||||
def registration_changeset(user, attrs, opts \\ []) do
|
||||
user
|
||||
|> cast(attrs, [:email, :password, :role])
|
||||
|> cast(attrs, [:email, :password, :role, :locale])
|
||||
|> validate_email()
|
||||
|> validate_password(opts)
|
||||
end
|
||||
@ -171,7 +173,7 @@ defmodule Cannery.Accounts.User do
|
||||
Bcrypt.verify_pass(password, hashed_password)
|
||||
end
|
||||
|
||||
def valid_password?(_, _) do
|
||||
def valid_password?(_invalid_user, _invalid_password) do
|
||||
Bcrypt.no_user_verify()
|
||||
false
|
||||
end
|
||||
@ -185,4 +187,14 @@ defmodule Cannery.Accounts.User do
|
||||
do: changeset,
|
||||
else: changeset |> add_error(:current_password, dgettext("errors", "is not valid"))
|
||||
end
|
||||
|
||||
@doc """
|
||||
A changeset for changing the user's locale
|
||||
"""
|
||||
@spec locale_changeset(t() | Changeset.t(t()), locale :: String.t() | nil) :: Changeset.t(t())
|
||||
def locale_changeset(user_or_changeset, locale) do
|
||||
user_or_changeset
|
||||
|> cast(%{"locale" => locale}, [:locale])
|
||||
|> validate_required(:locale)
|
||||
end
|
||||
end
|
||||
|
@ -4,8 +4,7 @@ defmodule Cannery.ActivityLog do
|
||||
"""
|
||||
|
||||
import Ecto.Query, warn: false
|
||||
import CanneryWeb.Gettext
|
||||
alias Cannery.{Accounts.User, ActivityLog.ShotGroup, Ammo, Ammo.AmmoGroup, Repo}
|
||||
alias Cannery.{Accounts.User, ActivityLog.ShotGroup, Ammo.AmmoGroup, Repo}
|
||||
alias Ecto.{Changeset, Multi}
|
||||
|
||||
@doc """
|
||||
@ -60,25 +59,24 @@ defmodule Cannery.ActivityLog do
|
||||
"""
|
||||
@spec create_shot_group(attrs :: map(), User.t(), AmmoGroup.t()) ::
|
||||
{:ok, ShotGroup.t()} | {:error, Changeset.t(ShotGroup.t()) | nil}
|
||||
def create_shot_group(
|
||||
attrs,
|
||||
%User{id: user_id},
|
||||
%AmmoGroup{id: ammo_group_id, count: ammo_group_count, user_id: user_id} = ammo_group
|
||||
) do
|
||||
attrs = attrs |> Map.merge(%{"user_id" => user_id, "ammo_group_id" => ammo_group_id})
|
||||
changeset = %ShotGroup{} |> ShotGroup.create_changeset(attrs)
|
||||
shot_group_count = changeset |> Changeset.get_field(:count)
|
||||
|
||||
if shot_group_count > ammo_group_count do
|
||||
error = dgettext("errors", "Count must be less than %{count}", count: ammo_group_count)
|
||||
changeset = changeset |> Changeset.add_error(:count, error)
|
||||
{:error, changeset}
|
||||
else
|
||||
def create_shot_group(attrs, user, ammo_group) do
|
||||
Multi.new()
|
||||
|> Multi.insert(:create_shot_group, changeset)
|
||||
|> Multi.insert(
|
||||
:create_shot_group,
|
||||
%ShotGroup{} |> ShotGroup.create_changeset(user, ammo_group, attrs)
|
||||
)
|
||||
|> Multi.run(
|
||||
:ammo_group,
|
||||
fn repo, %{create_shot_group: %{ammo_group_id: ammo_group_id, user_id: user_id}} ->
|
||||
{:ok,
|
||||
repo.one(from ag in AmmoGroup, where: ag.id == ^ammo_group_id and ag.user_id == ^user_id)}
|
||||
end
|
||||
)
|
||||
|> Multi.update(
|
||||
:update_ammo_group,
|
||||
fn %{create_shot_group: %{count: shot_group_count}, ammo_group: %{count: ammo_group_count}} ->
|
||||
ammo_group |> AmmoGroup.range_changeset(%{"count" => ammo_group_count - shot_group_count})
|
||||
end
|
||||
)
|
||||
|> Repo.transaction()
|
||||
|> case do
|
||||
@ -87,7 +85,6 @@ defmodule Cannery.ActivityLog do
|
||||
{:error, _other_transaction, _value, _changes_so_far} -> {:error, nil}
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Updates a shot_group.
|
||||
@ -104,35 +101,32 @@ defmodule Cannery.ActivityLog do
|
||||
@spec update_shot_group(ShotGroup.t(), attrs :: map(), User.t()) ::
|
||||
{:ok, ShotGroup.t()} | {:error, Changeset.t(ShotGroup.t()) | nil}
|
||||
def update_shot_group(
|
||||
%ShotGroup{count: count, user_id: user_id, ammo_group_id: ammo_group_id} = shot_group,
|
||||
%ShotGroup{count: count, user_id: user_id} = shot_group,
|
||||
attrs,
|
||||
%User{id: user_id} = user
|
||||
) do
|
||||
%{count: ammo_group_count, user_id: ^user_id} =
|
||||
ammo_group = ammo_group_id |> Ammo.get_ammo_group!(user)
|
||||
|
||||
changeset = shot_group |> ShotGroup.update_changeset(attrs)
|
||||
new_shot_group_count = changeset |> Changeset.get_field(:count)
|
||||
shot_diff_to_add = new_shot_group_count - count
|
||||
|
||||
cond do
|
||||
shot_diff_to_add > ammo_group_count ->
|
||||
error = dgettext("errors", "Count must be less than %{count}", count: ammo_group_count)
|
||||
changeset = changeset |> Changeset.add_error(:count, error)
|
||||
{:error, changeset}
|
||||
|
||||
new_shot_group_count <= 0 ->
|
||||
error = dgettext("errors", "Count must be at least 1")
|
||||
changeset = changeset |> Changeset.add_error(:count, error)
|
||||
{:error, changeset}
|
||||
|
||||
true ->
|
||||
Multi.new()
|
||||
|> Multi.update(:update_shot_group, changeset)
|
||||
|> Multi.update(
|
||||
:update_shot_group,
|
||||
shot_group |> ShotGroup.update_changeset(user, attrs)
|
||||
)
|
||||
|> Multi.run(
|
||||
:ammo_group,
|
||||
fn repo, %{update_shot_group: %{ammo_group_id: ammo_group_id, user_id: user_id}} ->
|
||||
{:ok,
|
||||
repo.one(from ag in AmmoGroup, where: ag.id == ^ammo_group_id and ag.user_id == ^user_id)}
|
||||
end
|
||||
)
|
||||
|> Multi.update(
|
||||
:update_ammo_group,
|
||||
ammo_group
|
||||
|> AmmoGroup.range_changeset(%{"count" => ammo_group_count - shot_diff_to_add})
|
||||
fn %{
|
||||
update_shot_group: %{count: new_count},
|
||||
ammo_group: %{count: ammo_group_count} = ammo_group
|
||||
} ->
|
||||
shot_diff_to_add = new_count - count
|
||||
new_ammo_group_count = ammo_group_count - shot_diff_to_add
|
||||
ammo_group |> AmmoGroup.range_changeset(%{"count" => new_ammo_group_count})
|
||||
end
|
||||
)
|
||||
|> Repo.transaction()
|
||||
|> case do
|
||||
@ -141,7 +135,6 @@ defmodule Cannery.ActivityLog do
|
||||
{:error, _other_transaction, _value, _changes_so_far} -> {:error, nil}
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Deletes a shot_group.
|
||||
@ -158,18 +151,27 @@ defmodule Cannery.ActivityLog do
|
||||
@spec delete_shot_group(ShotGroup.t(), User.t()) ::
|
||||
{:ok, ShotGroup.t()} | {:error, Changeset.t(ShotGroup.t())}
|
||||
def delete_shot_group(
|
||||
%ShotGroup{count: count, user_id: user_id, ammo_group_id: ammo_group_id} = shot_group,
|
||||
%User{id: user_id} = user
|
||||
%ShotGroup{user_id: user_id} = shot_group,
|
||||
%User{id: user_id}
|
||||
) do
|
||||
%{count: ammo_group_count, user_id: ^user_id} =
|
||||
ammo_group = ammo_group_id |> Ammo.get_ammo_group!(user)
|
||||
|
||||
Multi.new()
|
||||
|> Multi.delete(:delete_shot_group, shot_group)
|
||||
|> Multi.run(
|
||||
:ammo_group,
|
||||
fn repo, %{delete_shot_group: %{ammo_group_id: ammo_group_id, user_id: user_id}} ->
|
||||
{:ok,
|
||||
repo.one(from ag in AmmoGroup, where: ag.id == ^ammo_group_id and ag.user_id == ^user_id)}
|
||||
end
|
||||
)
|
||||
|> Multi.update(
|
||||
:update_ammo_group,
|
||||
ammo_group
|
||||
|> AmmoGroup.range_changeset(%{"count" => ammo_group_count + count})
|
||||
fn %{
|
||||
delete_shot_group: %{count: count},
|
||||
ammo_group: %{count: ammo_group_count} = ammo_group
|
||||
} ->
|
||||
new_ammo_group_count = ammo_group_count + count
|
||||
ammo_group |> AmmoGroup.range_changeset(%{"count" => new_ammo_group_count})
|
||||
end
|
||||
)
|
||||
|> Repo.transaction()
|
||||
|> case do
|
||||
@ -178,21 +180,4 @@ defmodule Cannery.ActivityLog do
|
||||
{:error, _other_transaction, _value, _changes_so_far} -> {:error, nil}
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns an `%Ecto.Changeset{}` for tracking shot_group changes.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> change_shot_group(shot_group)
|
||||
%Ecto.Changeset{data: %ShotGroup{}}
|
||||
|
||||
"""
|
||||
@spec change_shot_group(ShotGroup.t() | ShotGroup.new_shot_group()) ::
|
||||
Changeset.t(ShotGroup.t() | ShotGroup.new_shot_group())
|
||||
@spec change_shot_group(ShotGroup.t() | ShotGroup.new_shot_group(), attrs :: map()) ::
|
||||
Changeset.t(ShotGroup.t() | ShotGroup.new_shot_group())
|
||||
def change_shot_group(%ShotGroup{} = shot_group, attrs \\ %{}) do
|
||||
shot_group |> ShotGroup.update_changeset(attrs)
|
||||
end
|
||||
end
|
||||
|
@ -4,8 +4,9 @@ defmodule Cannery.ActivityLog.ShotGroup do
|
||||
"""
|
||||
|
||||
use Ecto.Schema
|
||||
import CanneryWeb.Gettext
|
||||
import Ecto.Changeset
|
||||
alias Cannery.{Accounts.User, ActivityLog.ShotGroup, Ammo.AmmoGroup}
|
||||
alias Cannery.{Accounts.User, ActivityLog.ShotGroup, Ammo.AmmoGroup, Repo}
|
||||
alias Ecto.{Changeset, UUID}
|
||||
|
||||
@primary_key {:id, :binary_id, autogenerate: true}
|
||||
@ -37,21 +38,84 @@ defmodule Cannery.ActivityLog.ShotGroup do
|
||||
@type id :: UUID.t()
|
||||
|
||||
@doc false
|
||||
@spec create_changeset(new_shot_group(), attrs :: map()) :: Changeset.t(new_shot_group())
|
||||
def create_changeset(shot_group, attrs) do
|
||||
@spec create_changeset(
|
||||
new_shot_group(),
|
||||
User.t() | any(),
|
||||
AmmoGroup.t() | any(),
|
||||
attrs :: map()
|
||||
) ::
|
||||
Changeset.t(new_shot_group())
|
||||
def create_changeset(
|
||||
shot_group,
|
||||
%User{id: user_id},
|
||||
%AmmoGroup{id: ammo_group_id, user_id: user_id} = ammo_group,
|
||||
attrs
|
||||
)
|
||||
when not (user_id |> is_nil()) and not (ammo_group_id |> is_nil()) do
|
||||
shot_group
|
||||
|> cast(attrs, [:count, :notes, :date, :ammo_group_id, :user_id])
|
||||
|> change(user_id: user_id)
|
||||
|> change(ammo_group_id: ammo_group_id)
|
||||
|> cast(attrs, [:count, :notes, :date])
|
||||
|> validate_number(:count, greater_than: 0)
|
||||
|> validate_create_shot_group_count(ammo_group)
|
||||
|> validate_required([:count, :ammo_group_id, :user_id])
|
||||
end
|
||||
|
||||
def create_changeset(shot_group, _invalid_user, _invalid_ammo_group, attrs) do
|
||||
shot_group
|
||||
|> cast(attrs, [:count, :notes, :date])
|
||||
|> validate_number(:count, greater_than: 0)
|
||||
|> validate_required([:count, :ammo_group_id, :user_id])
|
||||
|> add_error(:invalid, dgettext("errors", "Please select a valid user and ammo group"))
|
||||
end
|
||||
|
||||
defp validate_create_shot_group_count(changeset, %AmmoGroup{count: ammo_group_count}) do
|
||||
if changeset |> Changeset.get_field(:count) > ammo_group_count do
|
||||
error = dgettext("errors", "Count must be less than %{count}", count: ammo_group_count)
|
||||
changeset |> Changeset.add_error(:count, error)
|
||||
else
|
||||
changeset
|
||||
end
|
||||
end
|
||||
|
||||
@doc false
|
||||
@spec update_changeset(t() | new_shot_group(), attrs :: map()) ::
|
||||
@spec update_changeset(t() | new_shot_group(), User.t(), attrs :: map()) ::
|
||||
Changeset.t(t() | new_shot_group())
|
||||
def update_changeset(shot_group, attrs) do
|
||||
def update_changeset(
|
||||
%ShotGroup{user_id: user_id} = shot_group,
|
||||
%User{id: user_id} = user,
|
||||
attrs
|
||||
)
|
||||
when not (user_id |> is_nil()) do
|
||||
shot_group
|
||||
|> cast(attrs, [:count, :notes, :date])
|
||||
|> validate_number(:count, greater_than: 0)
|
||||
|> validate_required([:count])
|
||||
|> validate_update_shot_group_count(shot_group, user)
|
||||
end
|
||||
|
||||
defp validate_update_shot_group_count(
|
||||
changeset,
|
||||
%ShotGroup{count: count} = shot_group,
|
||||
%User{id: user_id}
|
||||
)
|
||||
when not (user_id |> is_nil()) do
|
||||
%{ammo_group: %AmmoGroup{count: ammo_group_count, user_id: ^user_id}} =
|
||||
shot_group |> Repo.preload(:ammo_group)
|
||||
|
||||
new_shot_group_count = changeset |> Changeset.get_field(:count)
|
||||
shot_diff_to_add = new_shot_group_count - count
|
||||
|
||||
cond do
|
||||
shot_diff_to_add > ammo_group_count ->
|
||||
error = dgettext("errors", "Count must be less than %{count}", count: ammo_group_count)
|
||||
changeset |> Changeset.add_error(:count, error)
|
||||
|
||||
new_shot_group_count <= 0 ->
|
||||
changeset |> Changeset.add_error(:count, dgettext("errors", "Count must be at least 1"))
|
||||
|
||||
true ->
|
||||
changeset
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -3,11 +3,15 @@ defmodule Cannery.Ammo do
|
||||
The Ammo context.
|
||||
"""
|
||||
|
||||
import CanneryWeb.Gettext
|
||||
import Ecto.Query, warn: false
|
||||
alias Cannery.{Accounts.User, Containers, Repo}
|
||||
alias Cannery.ActivityLog.ShotGroup
|
||||
alias Cannery.Ammo.{AmmoGroup, AmmoType}
|
||||
alias Ecto.Changeset
|
||||
|
||||
@ammo_group_create_limit 10_000
|
||||
|
||||
@doc """
|
||||
Returns the list of ammo_types.
|
||||
|
||||
@ -21,6 +25,25 @@ defmodule Cannery.Ammo do
|
||||
def list_ammo_types(%User{id: user_id}),
|
||||
do: Repo.all(from at in AmmoType, where: at.user_id == ^user_id, order_by: at.name)
|
||||
|
||||
@doc """
|
||||
Returns a count of ammo_types.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> get_ammo_types_count!(%User{id: 123})
|
||||
3
|
||||
|
||||
"""
|
||||
@spec get_ammo_types_count!(User.t()) :: integer()
|
||||
def get_ammo_types_count!(%User{id: user_id}) do
|
||||
Repo.one(
|
||||
from at in AmmoType,
|
||||
where: at.user_id == ^user_id,
|
||||
select: count(at.id),
|
||||
distinct: true
|
||||
)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Gets a single ammo_type.
|
||||
|
||||
@ -42,19 +65,76 @@ defmodule Cannery.Ammo do
|
||||
@doc """
|
||||
Gets the average cost of a single ammo type
|
||||
|
||||
## Examples
|
||||
|
||||
iex> get_average_cost_for_ammo_type!(%AmmoType{id: 123}, %User{id: 123})
|
||||
1.50
|
||||
|
||||
"""
|
||||
@spec get_average_cost_for_ammo_type!(AmmoType.t(), User.t()) :: float() | nil
|
||||
def get_average_cost_for_ammo_type!(
|
||||
%AmmoType{id: ammo_type_id, user_id: user_id},
|
||||
%User{id: user_id}
|
||||
) do
|
||||
sg_total_query =
|
||||
from sg in ShotGroup,
|
||||
where: not (sg.count |> is_nil()),
|
||||
group_by: sg.ammo_group_id,
|
||||
select: %{ammo_group_id: sg.ammo_group_id, total: sum(sg.count)}
|
||||
|
||||
Repo.one!(
|
||||
from ag in AmmoGroup,
|
||||
as: :ammo_group,
|
||||
left_join: sg_query in subquery(sg_total_query),
|
||||
on: ag.id == sg_query.ammo_group_id,
|
||||
where: ag.ammo_type_id == ^ammo_type_id,
|
||||
where: not (ag.price_paid |> is_nil()),
|
||||
select: sum(ag.price_paid) / sum(ag.count + coalesce(sg_query.total, 0))
|
||||
)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Gets the total number of rounds for an ammo type
|
||||
|
||||
Raises `Ecto.NoResultsError` if the Ammo type does not exist.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> get_ammo_type!(123, %User{id: 123})
|
||||
iex> get_round_count_for_ammo_type(123, %User{id: 123})
|
||||
%AmmoType{}
|
||||
|
||||
iex> get_ammo_type!(456, %User{id: 123})
|
||||
iex> get_round_count_for_ammo_type(456, %User{id: 123})
|
||||
** (Ecto.NoResultsError)
|
||||
|
||||
"""
|
||||
@spec get_average_cost_for_ammo_type!(AmmoType.t(), User.t()) :: float()
|
||||
def get_average_cost_for_ammo_type!(
|
||||
@spec get_round_count_for_ammo_type(AmmoType.t(), User.t()) :: non_neg_integer()
|
||||
def get_round_count_for_ammo_type(
|
||||
%AmmoType{id: ammo_type_id, user_id: user_id},
|
||||
%User{id: user_id}
|
||||
) do
|
||||
Repo.one!(
|
||||
from ag in AmmoGroup,
|
||||
where: ag.ammo_type_id == ^ammo_type_id,
|
||||
select: sum(ag.count)
|
||||
) || 0
|
||||
end
|
||||
|
||||
@doc """
|
||||
Gets the total number of rounds shot for an ammo type
|
||||
|
||||
Raises `Ecto.NoResultsError` if the Ammo type does not exist.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> get_used_count_for_ammo_type(123, %User{id: 123})
|
||||
%AmmoType{}
|
||||
|
||||
iex> get_used_count_for_ammo_type(456, %User{id: 123})
|
||||
** (Ecto.NoResultsError)
|
||||
|
||||
"""
|
||||
@spec get_used_count_for_ammo_type(AmmoType.t(), User.t()) :: non_neg_integer()
|
||||
def get_used_count_for_ammo_type(
|
||||
%AmmoType{id: ammo_type_id, user_id: user_id},
|
||||
%User{id: user_id}
|
||||
) do
|
||||
@ -62,9 +142,8 @@ defmodule Cannery.Ammo do
|
||||
from ag in AmmoGroup,
|
||||
left_join: sg in assoc(ag, :shot_groups),
|
||||
where: ag.ammo_type_id == ^ammo_type_id,
|
||||
where: not (ag.price_paid |> is_nil()),
|
||||
select: sum(ag.price_paid) / (sum(ag.count) + sum(sg.count))
|
||||
)
|
||||
select: sum(sg.count)
|
||||
) || 0
|
||||
end
|
||||
|
||||
@doc """
|
||||
@ -81,11 +160,8 @@ defmodule Cannery.Ammo do
|
||||
"""
|
||||
@spec create_ammo_type(attrs :: map(), User.t()) ::
|
||||
{:ok, AmmoType.t()} | {:error, Changeset.t(AmmoType.new_ammo_type())}
|
||||
def create_ammo_type(attrs \\ %{}, %User{id: user_id}) do
|
||||
%AmmoType{}
|
||||
|> AmmoType.create_changeset(attrs |> Map.put("user_id", user_id))
|
||||
|> Repo.insert()
|
||||
end
|
||||
def create_ammo_type(attrs \\ %{}, %User{} = user),
|
||||
do: %AmmoType{} |> AmmoType.create_changeset(user, attrs) |> Repo.insert()
|
||||
|
||||
@doc """
|
||||
Updates a ammo_type.
|
||||
@ -134,22 +210,6 @@ defmodule Cannery.Ammo do
|
||||
def delete_ammo_type!(%AmmoType{user_id: user_id} = ammo_type, %User{id: user_id}),
|
||||
do: ammo_type |> Repo.delete!()
|
||||
|
||||
@doc """
|
||||
Returns an `%Changeset{}` for tracking ammo_type changes.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> change_ammo_type(ammo_type)
|
||||
%Changeset{data: %AmmoType{}}
|
||||
|
||||
"""
|
||||
@spec change_ammo_type(AmmoType.t() | AmmoType.new_ammo_type()) ::
|
||||
Changeset.t(AmmoType.t() | AmmoType.new_ammo_type())
|
||||
@spec change_ammo_type(AmmoType.t() | AmmoType.new_ammo_type(), attrs :: map()) ::
|
||||
Changeset.t(AmmoType.t() | AmmoType.new_ammo_type())
|
||||
def change_ammo_type(%AmmoType{} = ammo_type, attrs \\ %{}),
|
||||
do: AmmoType.update_changeset(ammo_type, attrs)
|
||||
|
||||
@doc """
|
||||
Returns the list of ammo_groups for a user and type.
|
||||
|
||||
@ -162,10 +222,12 @@ defmodule Cannery.Ammo do
|
||||
@spec list_ammo_groups_for_type(AmmoType.t(), User.t()) :: [AmmoGroup.t()]
|
||||
def list_ammo_groups_for_type(%AmmoType{id: ammo_type_id, user_id: user_id}, %User{id: user_id}) do
|
||||
Repo.all(
|
||||
from am in AmmoGroup,
|
||||
where: am.ammo_type_id == ^ammo_type_id,
|
||||
where: am.user_id == ^user_id,
|
||||
order_by: am.id
|
||||
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,
|
||||
preload: [shot_groups: sg],
|
||||
order_by: ag.id
|
||||
)
|
||||
end
|
||||
|
||||
@ -182,12 +244,18 @@ defmodule Cannery.Ammo do
|
||||
@spec list_ammo_groups(User.t(), include_empty :: boolean()) :: [AmmoGroup.t()]
|
||||
def list_ammo_groups(%User{id: user_id}, include_empty \\ false) do
|
||||
if include_empty do
|
||||
from am in AmmoGroup, where: am.user_id == ^user_id, order_by: am.id
|
||||
from ag in AmmoGroup,
|
||||
left_join: sg in assoc(ag, :shot_groups),
|
||||
where: ag.user_id == ^user_id,
|
||||
preload: [shot_groups: sg],
|
||||
order_by: ag.id
|
||||
else
|
||||
from am in AmmoGroup,
|
||||
where: am.user_id == ^user_id,
|
||||
where: not (am.count == 0),
|
||||
order_by: am.id
|
||||
from ag in AmmoGroup,
|
||||
left_join: sg in assoc(ag, :shot_groups),
|
||||
where: ag.user_id == ^user_id,
|
||||
where: not (ag.count == 0),
|
||||
preload: [shot_groups: sg],
|
||||
order_by: ag.id
|
||||
end
|
||||
|> Repo.all()
|
||||
end
|
||||
@ -204,10 +272,12 @@ defmodule Cannery.Ammo do
|
||||
@spec list_staged_ammo_groups(User.t()) :: [AmmoGroup.t()]
|
||||
def list_staged_ammo_groups(%User{id: user_id}) do
|
||||
Repo.all(
|
||||
from am in AmmoGroup,
|
||||
where: am.user_id == ^user_id,
|
||||
where: am.staged == true,
|
||||
order_by: am.id
|
||||
from ag in AmmoGroup,
|
||||
left_join: sg in assoc(ag, :shot_groups),
|
||||
where: ag.user_id == ^user_id,
|
||||
where: ag.staged == true,
|
||||
preload: [shot_groups: sg],
|
||||
order_by: ag.id
|
||||
)
|
||||
end
|
||||
|
||||
@ -226,40 +296,121 @@ defmodule Cannery.Ammo do
|
||||
|
||||
"""
|
||||
@spec get_ammo_group!(AmmoGroup.id(), User.t()) :: AmmoGroup.t()
|
||||
def get_ammo_group!(id, %User{id: user_id}),
|
||||
do: Repo.one!(from am in AmmoGroup, where: am.id == ^id and am.user_id == ^user_id)
|
||||
def get_ammo_group!(id, %User{id: user_id}) do
|
||||
Repo.one!(
|
||||
from ag in AmmoGroup,
|
||||
left_join: sg in assoc(ag, :shot_groups),
|
||||
where: ag.id == ^id,
|
||||
where: ag.user_id == ^user_id,
|
||||
preload: [shot_groups: sg]
|
||||
)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Creates a ammo_group.
|
||||
Returns the number of shot rounds for an ammo group
|
||||
"""
|
||||
@spec get_used_count(AmmoGroup.t()) :: non_neg_integer()
|
||||
def get_used_count(%AmmoGroup{} = ammo_group) do
|
||||
ammo_group
|
||||
|> Repo.preload(:shot_groups)
|
||||
|> Map.fetch!(:shot_groups)
|
||||
|> Enum.map(fn %{count: count} -> count end)
|
||||
|> Enum.sum()
|
||||
end
|
||||
|
||||
@doc """
|
||||
Calculates the percentage remaining of an ammo group out of 100
|
||||
"""
|
||||
@spec get_percentage_remaining(AmmoGroup.t()) :: non_neg_integer()
|
||||
def get_percentage_remaining(%AmmoGroup{count: 0}), do: 0
|
||||
|
||||
def get_percentage_remaining(%AmmoGroup{count: count} = ammo_group) do
|
||||
ammo_group = ammo_group |> Repo.preload(:shot_groups)
|
||||
|
||||
shot_group_sum =
|
||||
ammo_group.shot_groups |> Enum.map(fn %{count: count} -> count end) |> Enum.sum()
|
||||
|
||||
round(count / (count + shot_group_sum) * 100)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Creates multiple ammo_groups at once.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> create_ammo_group(%{field: value}, %User{id: 123})
|
||||
{:ok, %AmmoGroup{}}
|
||||
iex> create_ammo_groups(%{field: value}, 3, %User{id: 123})
|
||||
{:ok, {3, [%AmmoGroup{}]}}
|
||||
|
||||
iex> create_ammo_group(%{field: bad_value}, %User{id: 123})
|
||||
iex> create_ammo_groups(%{field: bad_value}, 3, %User{id: 123})
|
||||
{:error, %Changeset{}}
|
||||
|
||||
"""
|
||||
@spec create_ammo_group(attrs :: map(), User.t()) ::
|
||||
{:ok, AmmoGroup.t()} | {:error, Changeset.t(AmmoGroup.new_ammo_group())}
|
||||
def create_ammo_group(
|
||||
@spec create_ammo_groups(attrs :: map(), multiplier :: non_neg_integer(), User.t()) ::
|
||||
{:ok, {count :: non_neg_integer(), [AmmoGroup.t()] | nil}}
|
||||
| {:error, Changeset.t(AmmoGroup.new_ammo_group())}
|
||||
def create_ammo_groups(
|
||||
%{"ammo_type_id" => ammo_type_id, "container_id" => container_id} = attrs,
|
||||
%User{id: user_id} = user
|
||||
) do
|
||||
# validate ammo type and container ids belong to user
|
||||
_valid_ammo_type = get_ammo_type!(ammo_type_id, user)
|
||||
_valid_container = Containers.get_container!(container_id, user)
|
||||
multiplier,
|
||||
%User{} = user
|
||||
)
|
||||
when multiplier >= 1 and multiplier <= @ammo_group_create_limit and
|
||||
not (ammo_type_id |> is_nil()) and not (container_id |> is_nil()) do
|
||||
now = NaiveDateTime.utc_now() |> NaiveDateTime.truncate(:second)
|
||||
|
||||
changesets =
|
||||
Enum.map(1..multiplier, fn _count ->
|
||||
%AmmoGroup{}
|
||||
|> AmmoGroup.create_changeset(attrs |> Map.put("user_id", user_id))
|
||||
|> Repo.insert()
|
||||
|> AmmoGroup.create_changeset(
|
||||
get_ammo_type!(ammo_type_id, user),
|
||||
Containers.get_container!(container_id, user),
|
||||
user,
|
||||
attrs
|
||||
)
|
||||
end)
|
||||
|
||||
if changesets |> Enum.all?(fn %{valid?: valid} -> valid end) do
|
||||
{count, inserted_ammo_groups} =
|
||||
Repo.insert_all(
|
||||
AmmoGroup,
|
||||
changesets
|
||||
|> Enum.map(fn changeset ->
|
||||
changeset
|
||||
|> Map.get(:changes)
|
||||
|> Map.merge(%{inserted_at: now, updated_at: now})
|
||||
end),
|
||||
returning: true
|
||||
)
|
||||
|
||||
{:ok, {count, inserted_ammo_groups}}
|
||||
else
|
||||
changesets
|
||||
|> Enum.reject(fn %{valid?: valid} -> valid end)
|
||||
|> List.first()
|
||||
|> Changeset.apply_action(:insert)
|
||||
end
|
||||
end
|
||||
|
||||
def create_ammo_group(invalid_attrs, _user) do
|
||||
def create_ammo_groups(
|
||||
%{"ammo_type_id" => ammo_type_id, "container_id" => container_id} = attrs,
|
||||
_multiplier,
|
||||
user
|
||||
)
|
||||
when not (ammo_type_id |> is_nil()) and not (container_id |> is_nil()) do
|
||||
changeset =
|
||||
%AmmoGroup{}
|
||||
|> AmmoGroup.create_changeset(invalid_attrs |> Map.put("user_id", "-1"))
|
||||
|> Repo.insert()
|
||||
|> AmmoGroup.create_changeset(
|
||||
get_ammo_type!(ammo_type_id, user),
|
||||
Containers.get_container!(container_id, user),
|
||||
user,
|
||||
attrs
|
||||
)
|
||||
|> Changeset.add_error(:multiplier, dgettext("errors", "Invalid multiplier"))
|
||||
|
||||
{:error, changeset}
|
||||
end
|
||||
|
||||
def create_ammo_groups(invalid_attrs, _multiplier, user) do
|
||||
{:error, %AmmoGroup{} |> AmmoGroup.create_changeset(nil, nil, user, invalid_attrs)}
|
||||
end
|
||||
|
||||
@doc """
|
||||
@ -308,18 +459,4 @@ defmodule Cannery.Ammo do
|
||||
@spec delete_ammo_group!(AmmoGroup.t(), User.t()) :: AmmoGroup.t()
|
||||
def delete_ammo_group!(%AmmoGroup{user_id: user_id} = ammo_group, %User{id: user_id}),
|
||||
do: ammo_group |> Repo.delete!()
|
||||
|
||||
@doc """
|
||||
Returns an `%Changeset{}` for tracking ammo_group changes.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> change_ammo_group(ammo_group)
|
||||
%Changeset{data: %AmmoGroup{}}
|
||||
|
||||
"""
|
||||
@spec change_ammo_group(AmmoGroup.t()) :: Changeset.t(AmmoGroup.t())
|
||||
@spec change_ammo_group(AmmoGroup.t(), attrs :: map()) :: Changeset.t(AmmoGroup.t())
|
||||
def change_ammo_group(%AmmoGroup{} = ammo_group, attrs \\ %{}),
|
||||
do: AmmoGroup.update_changeset(ammo_group, attrs)
|
||||
end
|
||||
|
@ -7,6 +7,7 @@ defmodule Cannery.Ammo.AmmoGroup do
|
||||
"""
|
||||
|
||||
use Ecto.Schema
|
||||
import CanneryWeb.Gettext
|
||||
import Ecto.Changeset
|
||||
alias Cannery.Ammo.{AmmoGroup, AmmoType}
|
||||
alias Cannery.{Accounts.User, ActivityLog.ShotGroup, Containers.Container}
|
||||
@ -48,22 +49,49 @@ defmodule Cannery.Ammo.AmmoGroup do
|
||||
@type id :: UUID.t()
|
||||
|
||||
@doc false
|
||||
@spec create_changeset(new_ammo_group(), attrs :: map()) :: Changeset.t(new_ammo_group())
|
||||
def create_changeset(ammo_group, attrs) do
|
||||
@spec create_changeset(
|
||||
new_ammo_group(),
|
||||
AmmoType.t() | nil,
|
||||
Container.t() | nil,
|
||||
User.t(),
|
||||
attrs :: map()
|
||||
) :: Changeset.t(new_ammo_group())
|
||||
def create_changeset(
|
||||
ammo_group,
|
||||
%AmmoType{id: ammo_type_id},
|
||||
%Container{id: container_id, user_id: user_id},
|
||||
%User{id: user_id},
|
||||
attrs
|
||||
)
|
||||
when not (ammo_type_id |> is_nil()) and not (container_id |> is_nil()) and
|
||||
not (user_id |> is_nil()) do
|
||||
ammo_group
|
||||
|> cast(attrs, [:count, :price_paid, :notes, :staged, :ammo_type_id, :container_id, :user_id])
|
||||
|> change(ammo_type_id: ammo_type_id)
|
||||
|> change(user_id: user_id)
|
||||
|> change(container_id: container_id)
|
||||
|> cast(attrs, [:count, :price_paid, :notes, :staged])
|
||||
|> validate_number(:count, greater_than: 0)
|
||||
|> validate_required([:count, :staged, :ammo_type_id, :container_id, :user_id])
|
||||
end
|
||||
|
||||
@doc """
|
||||
Invalid changeset, used to prompt user to select ammo type and container
|
||||
"""
|
||||
def create_changeset(ammo_group, _invalid_ammo_type, _invalid_container, _invalid_user, attrs) do
|
||||
ammo_group
|
||||
|> cast(attrs, [:ammo_type_id, :container_id])
|
||||
|> validate_required([:ammo_type_id, :container_id])
|
||||
|> add_error(:invalid, dgettext("errors", "Please select an ammo type and container"))
|
||||
end
|
||||
|
||||
@doc false
|
||||
@spec update_changeset(t() | new_ammo_group(), attrs :: map()) ::
|
||||
Changeset.t(t() | new_ammo_group())
|
||||
def update_changeset(ammo_group, attrs) do
|
||||
ammo_group
|
||||
|> cast(attrs, [:count, :price_paid, :notes, :staged, :ammo_type_id, :container_id])
|
||||
|> validate_number(:count, greater_than: 0)
|
||||
|> validate_required([:count, :staged, :ammo_type_id, :container_id, :user_id])
|
||||
|> cast(attrs, [:count, :price_paid, :notes, :staged])
|
||||
|> validate_number(:count, greater_than_or_equal_to: 0)
|
||||
|> validate_required([:count, :staged])
|
||||
end
|
||||
|
||||
@doc """
|
||||
@ -75,6 +103,6 @@ defmodule Cannery.Ammo.AmmoGroup do
|
||||
def range_changeset(ammo_group, attrs) do
|
||||
ammo_group
|
||||
|> cast(attrs, [:count, :staged])
|
||||
|> validate_required([:count, :staged, :ammo_type_id, :container_id, :user_id])
|
||||
|> validate_required([:count, :staged])
|
||||
end
|
||||
end
|
||||
|
@ -31,10 +31,10 @@ defmodule Cannery.Ammo.AmmoType do
|
||||
field :pressure, :string
|
||||
field :primer_type, :string
|
||||
field :firing_type, :string
|
||||
field :tracer, :boolean, null: false, default: false
|
||||
field :incendiary, :boolean, null: false, default: false
|
||||
field :blank, :boolean, null: false, default: false
|
||||
field :corrosive, :boolean, null: false, default: false
|
||||
field :tracer, :boolean, default: false
|
||||
field :incendiary, :boolean, default: false
|
||||
field :blank, :boolean, default: false
|
||||
field :corrosive, :boolean, default: false
|
||||
|
||||
field :manufacturer, :string
|
||||
field :upc, :string
|
||||
@ -105,10 +105,12 @@ defmodule Cannery.Ammo.AmmoType do
|
||||
]
|
||||
|
||||
@doc false
|
||||
@spec create_changeset(new_ammo_type(), attrs :: map()) :: Changeset.t(new_ammo_type())
|
||||
def create_changeset(ammo_type, attrs) do
|
||||
@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
|
||||
ammo_type
|
||||
|> cast(attrs, [:user_id | changeset_fields()])
|
||||
|> change(user_id: user_id)
|
||||
|> cast(attrs, changeset_fields())
|
||||
|> validate_required([:name, :user_id])
|
||||
end
|
||||
|
||||
@ -118,6 +120,6 @@ defmodule Cannery.Ammo.AmmoType do
|
||||
def update_changeset(ammo_type, attrs) do
|
||||
ammo_type
|
||||
|> cast(attrs, changeset_fields())
|
||||
|> validate_required([:name, :user_id])
|
||||
|> validate_required(:name)
|
||||
end
|
||||
end
|
||||
|
@ -19,8 +19,35 @@ defmodule Cannery.Containers do
|
||||
|
||||
"""
|
||||
@spec list_containers(User.t()) :: [Container.t()]
|
||||
def list_containers(%User{id: user_id}),
|
||||
do: Repo.all(from c in Container, where: c.user_id == ^user_id, order_by: c.name)
|
||||
def list_containers(%User{id: user_id}) do
|
||||
Repo.all(
|
||||
from c in Container,
|
||||
left_join: t in assoc(c, :tags),
|
||||
left_join: ag in assoc(c, :ammo_groups),
|
||||
where: c.user_id == ^user_id,
|
||||
order_by: c.name,
|
||||
preload: [tags: t, ammo_groups: ag]
|
||||
)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns a count of containers.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> get_containers_count!(%User{id: 123})
|
||||
3
|
||||
|
||||
"""
|
||||
@spec get_containers_count!(User.t()) :: integer()
|
||||
def get_containers_count!(%User{id: user_id}) do
|
||||
Repo.one(
|
||||
from c in Container,
|
||||
where: c.user_id == ^user_id,
|
||||
select: count(c.id),
|
||||
distinct: true
|
||||
)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Gets a single container.
|
||||
@ -37,8 +64,17 @@ defmodule Cannery.Containers do
|
||||
|
||||
"""
|
||||
@spec get_container!(Container.id(), User.t()) :: Container.t()
|
||||
def get_container!(id, %User{id: user_id}),
|
||||
do: Repo.one!(from c in Container, where: c.id == ^id and c.user_id == ^user_id)
|
||||
def get_container!(id, %User{id: user_id}) do
|
||||
Repo.one!(
|
||||
from c in Container,
|
||||
left_join: t in assoc(c, :tags),
|
||||
left_join: ag in assoc(c, :ammo_groups),
|
||||
where: c.user_id == ^user_id,
|
||||
where: c.id == ^id,
|
||||
order_by: c.name,
|
||||
preload: [tags: t, ammo_groups: ag]
|
||||
)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Creates a container.
|
||||
@ -54,9 +90,8 @@ defmodule Cannery.Containers do
|
||||
"""
|
||||
@spec create_container(attrs :: map(), User.t()) ::
|
||||
{:ok, Container.t()} | {:error, Changeset.t(Container.new_container())}
|
||||
def create_container(attrs, %User{id: user_id}) do
|
||||
attrs = attrs |> Map.put("user_id", user_id)
|
||||
%Container{} |> Container.create_changeset(attrs) |> Repo.insert()
|
||||
def create_container(attrs, %User{} = user) do
|
||||
%Container{} |> Container.create_changeset(user, attrs) |> Repo.insert()
|
||||
end
|
||||
|
||||
@doc """
|
||||
@ -105,7 +140,7 @@ defmodule Cannery.Containers do
|
||||
error = dgettext("errors", "Container must be empty before deleting")
|
||||
|
||||
container
|
||||
|> change_container()
|
||||
|> Container.update_changeset(%{})
|
||||
|> Changeset.add_error(:ammo_groups, error)
|
||||
|> Changeset.apply_action(:delete)
|
||||
end
|
||||
@ -126,25 +161,6 @@ defmodule Cannery.Containers do
|
||||
container
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns an `%Changeset{}` for tracking container changes.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> change_container(container)
|
||||
%Changeset{data: %Container{}}
|
||||
|
||||
iex> change_container(%Changeset{})
|
||||
%Changeset{data: %Container{}}
|
||||
|
||||
"""
|
||||
@spec change_container(Container.t() | Container.new_container()) ::
|
||||
Changeset.t(Container.t() | Container.new_container())
|
||||
@spec change_container(Container.t() | Container.new_container(), attrs :: map()) ::
|
||||
Changeset.t(Container.t() | Container.new_container())
|
||||
def change_container(container, attrs \\ %{}),
|
||||
do: container |> Container.update_changeset(attrs)
|
||||
|
||||
@doc """
|
||||
Adds a tag to a container
|
||||
|
||||
@ -156,14 +172,11 @@ defmodule Cannery.Containers do
|
||||
"""
|
||||
@spec add_tag!(Container.t(), Tag.t(), User.t()) :: ContainerTag.t()
|
||||
def add_tag!(
|
||||
%Container{id: container_id, user_id: user_id},
|
||||
%Tag{id: tag_id, user_id: user_id},
|
||||
%Container{user_id: user_id} = container,
|
||||
%Tag{user_id: user_id} = tag,
|
||||
%User{id: user_id}
|
||||
) do
|
||||
%ContainerTag{}
|
||||
|> ContainerTag.changeset(%{"container_id" => container_id, "tag_id" => tag_id})
|
||||
|> Repo.insert!()
|
||||
end
|
||||
),
|
||||
do: %ContainerTag{} |> ContainerTag.create_changeset(tag, container) |> Repo.insert!()
|
||||
|
||||
@doc """
|
||||
Removes a tag from a container
|
||||
@ -189,4 +202,29 @@ defmodule Cannery.Containers do
|
||||
|
||||
if count == 0, do: raise("could not delete container tag"), else: count
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns number of rounds in container. If data is already preloaded, then
|
||||
there will be no db hit.
|
||||
"""
|
||||
@spec get_container_ammo_group_count!(Container.t()) :: non_neg_integer()
|
||||
def get_container_ammo_group_count!(%Container{} = container) do
|
||||
container
|
||||
|> Repo.preload(:ammo_groups)
|
||||
|> Map.fetch!(:ammo_groups)
|
||||
|> Enum.count()
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns number of rounds in container. If data is already preloaded, then
|
||||
there will be no db hit.
|
||||
"""
|
||||
@spec get_container_rounds!(Container.t()) :: non_neg_integer()
|
||||
def get_container_rounds!(%Container{} = container) do
|
||||
container
|
||||
|> Repo.preload(:ammo_groups)
|
||||
|> Map.fetch!(:ammo_groups)
|
||||
|> Enum.map(fn %{count: count} -> count end)
|
||||
|> Enum.sum()
|
||||
end
|
||||
end
|
||||
|
@ -42,10 +42,12 @@ defmodule Cannery.Containers.Container do
|
||||
@type id :: UUID.t()
|
||||
|
||||
@doc false
|
||||
@spec create_changeset(new_container(), attrs :: map()) :: Changeset.t(new_container())
|
||||
def create_changeset(container, attrs) do
|
||||
@spec create_changeset(new_container(), User.t(), attrs :: map()) ::
|
||||
Changeset.t(new_container())
|
||||
def create_changeset(container, %User{id: user_id}, attrs) do
|
||||
container
|
||||
|> cast(attrs, [:name, :desc, :type, :location, :user_id])
|
||||
|> change(user_id: user_id)
|
||||
|> cast(attrs, [:name, :desc, :type, :location])
|
||||
|> validate_required([:name, :type, :user_id])
|
||||
end
|
||||
|
||||
@ -55,6 +57,6 @@ defmodule Cannery.Containers.Container do
|
||||
def update_changeset(container, attrs) do
|
||||
container
|
||||
|> cast(attrs, [:name, :desc, :type, :location])
|
||||
|> validate_required([:name, :type, :user_id])
|
||||
|> validate_required([:name, :type])
|
||||
end
|
||||
end
|
||||
|
@ -31,10 +31,16 @@ defmodule Cannery.Containers.ContainerTag do
|
||||
@type id :: UUID.t()
|
||||
|
||||
@doc false
|
||||
@spec changeset(new_container_tag(), attrs :: map()) :: Changeset.t(new_container_tag())
|
||||
def changeset(container_tag, attrs) do
|
||||
@spec create_changeset(new_container_tag(), Tag.t(), Container.t()) ::
|
||||
Changeset.t(new_container_tag())
|
||||
def create_changeset(
|
||||
container_tag,
|
||||
%Tag{id: tag_id, user_id: user_id},
|
||||
%Container{id: container_id, user_id: user_id}
|
||||
) do
|
||||
container_tag
|
||||
|> cast(attrs, [:tag_id, :container_id])
|
||||
|> change(tag_id: tag_id)
|
||||
|> change(container_id: container_id)
|
||||
|> validate_required([:tag_id, :container_id])
|
||||
end
|
||||
end
|
||||
|
@ -100,15 +100,13 @@ defmodule Cannery.Invites do
|
||||
"""
|
||||
@spec create_invite(User.t(), attrs :: map()) ::
|
||||
{:ok, Invite.t()} | {:error, Changeset.t(Invite.new_invite())}
|
||||
def create_invite(%User{id: user_id, role: :admin}, attrs) do
|
||||
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)
|
||||
|
||||
attrs = attrs |> Map.merge(%{"user_id" => user_id, "token" => token})
|
||||
|
||||
%Invite{} |> Invite.create_changeset(attrs) |> Repo.insert()
|
||||
%Invite{} |> Invite.create_changeset(user, token, attrs) |> Repo.insert()
|
||||
end
|
||||
|
||||
@doc """
|
||||
@ -155,19 +153,4 @@ defmodule Cannery.Invites do
|
||||
"""
|
||||
@spec delete_invite!(Invite.t(), User.t()) :: Invite.t()
|
||||
def delete_invite!(invite, %User{role: :admin}), do: invite |> Repo.delete!()
|
||||
|
||||
@doc """
|
||||
Returns an `%Changeset{}` for tracking invite changes.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> change_invite(invite)
|
||||
%Changeset{data: %Invite{}}
|
||||
|
||||
"""
|
||||
@spec change_invite(Invite.t() | Invite.new_invite()) ::
|
||||
Changeset.t(Invite.t() | Invite.new_invite())
|
||||
@spec change_invite(Invite.t() | Invite.new_invite(), attrs :: map()) ::
|
||||
Changeset.t(Invite.t() | Invite.new_invite())
|
||||
def change_invite(invite, attrs \\ %{}), do: invite |> Invite.update_changeset(attrs)
|
||||
end
|
||||
|
@ -38,10 +38,12 @@ defmodule Cannery.Invites.Invite do
|
||||
@type id :: UUID.t()
|
||||
|
||||
@doc false
|
||||
@spec create_changeset(new_invite(), attrs :: map()) :: Changeset.t(new_invite())
|
||||
def create_changeset(invite, attrs) do
|
||||
@spec create_changeset(new_invite(), User.t(), token :: binary(), attrs :: map()) ::
|
||||
Changeset.t(new_invite())
|
||||
def create_changeset(invite, %User{id: user_id}, token, attrs) do
|
||||
invite
|
||||
|> cast(attrs, [:name, :token, :uses_left, :disabled_at, :user_id])
|
||||
|> change(token: token, user_id: user_id)
|
||||
|> cast(attrs, [:name, :uses_left, :disabled_at])
|
||||
|> validate_required([:name, :token, :user_id])
|
||||
|> validate_number(:uses_left, greater_than_or_equal_to: 0)
|
||||
end
|
||||
@ -51,7 +53,7 @@ defmodule Cannery.Invites.Invite do
|
||||
def update_changeset(invite, attrs) do
|
||||
invite
|
||||
|> cast(attrs, [:name, :uses_left, :disabled_at])
|
||||
|> validate_required([:name, :token, :user_id])
|
||||
|> validate_required([:name])
|
||||
|> validate_number(:uses_left, greater_than_or_equal_to: 0)
|
||||
end
|
||||
end
|
||||
|
@ -9,7 +9,7 @@ defmodule Cannery.Release do
|
||||
|
||||
def rollback(repo, version) do
|
||||
load_app()
|
||||
{:ok, _, _} = Ecto.Migrator.with_repo(repo, &Ecto.Migrator.run(&1, :down, to: version))
|
||||
{:ok, _fun, _opts} = Ecto.Migrator.with_repo(repo, &Ecto.Migrator.run(&1, :down, to: version))
|
||||
end
|
||||
|
||||
defp load_app do
|
||||
@ -20,7 +20,7 @@ defmodule Cannery.Release do
|
||||
load_app()
|
||||
|
||||
for repo <- Application.fetch_env!(@app, :ecto_repos) do
|
||||
{:ok, _, _} = Ecto.Migrator.with_repo(repo, &Ecto.Migrator.run(&1, :up, all: true))
|
||||
{:ok, _fun, _opts} = Ecto.Migrator.with_repo(repo, &Ecto.Migrator.run(&1, :up, all: true))
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -6,11 +6,11 @@ defmodule Cannery.Repo.Migrator do
|
||||
use GenServer
|
||||
require Logger
|
||||
|
||||
def start_link(_) do
|
||||
def start_link(_opts) do
|
||||
GenServer.start_link(__MODULE__, [], [])
|
||||
end
|
||||
|
||||
def init(_) do
|
||||
def init(_opts) do
|
||||
migrate!()
|
||||
{:ok, nil}
|
||||
end
|
||||
|
@ -74,8 +74,8 @@ defmodule Cannery.Tags do
|
||||
"""
|
||||
@spec create_tag(attrs :: map(), User.t()) ::
|
||||
{:ok, Tag.t()} | {:error, Changeset.t(Tag.new_tag())}
|
||||
def create_tag(attrs, %User{id: user_id}),
|
||||
do: %Tag{} |> Tag.create_changeset(attrs |> Map.put("user_id", user_id)) |> Repo.insert()
|
||||
def create_tag(attrs, %User{} = user),
|
||||
do: %Tag{} |> Tag.create_changeset(user, attrs) |> Repo.insert()
|
||||
|
||||
@doc """
|
||||
Updates a tag.
|
||||
@ -121,20 +121,6 @@ defmodule Cannery.Tags do
|
||||
@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!()
|
||||
|
||||
@doc """
|
||||
Returns an `%Changeset{}` for tracking tag changes.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> change_tag(tag)
|
||||
%Changeset{data: %Tag{}}
|
||||
|
||||
"""
|
||||
@spec change_tag(Tag.t() | Tag.new_tag()) :: Changeset.t(Tag.t() | Tag.new_tag())
|
||||
@spec change_tag(Tag.t() | Tag.new_tag(), attrs :: map()) ::
|
||||
Changeset.t(Tag.t() | Tag.new_tag())
|
||||
def change_tag(tag, attrs \\ %{}), do: Tag.update_changeset(tag, attrs)
|
||||
|
||||
@doc """
|
||||
Get a random tag bg_color in `#ffffff` hex format
|
||||
|
||||
|
@ -35,10 +35,11 @@ defmodule Cannery.Tags.Tag do
|
||||
@type id() :: UUID.t()
|
||||
|
||||
@doc false
|
||||
@spec create_changeset(new_tag(), attrs :: map()) :: Changeset.t(new_tag())
|
||||
def create_changeset(tag, attrs) do
|
||||
@spec create_changeset(new_tag(), User.t(), attrs :: map()) :: Changeset.t(new_tag())
|
||||
def create_changeset(tag, %User{id: user_id}, attrs) do
|
||||
tag
|
||||
|> cast(attrs, [:name, :bg_color, :text_color, :user_id])
|
||||
|> change(user_id: user_id)
|
||||
|> cast(attrs, [:name, :bg_color, :text_color])
|
||||
|> validate_required([:name, :bg_color, :text_color, :user_id])
|
||||
end
|
||||
|
||||
@ -47,6 +48,6 @@ defmodule Cannery.Tags.Tag do
|
||||
def update_changeset(tag, attrs) do
|
||||
tag
|
||||
|> cast(attrs, [:name, :bg_color, :text_color])
|
||||
|> validate_required([:name, :bg_color, :text_color, :user_id])
|
||||
|> validate_required([:name, :bg_color, :text_color])
|
||||
end
|
||||
end
|
||||
|
@ -47,6 +47,7 @@ defmodule CanneryWeb do
|
||||
use Phoenix.LiveView,
|
||||
layout: {CanneryWeb.LayoutView, "live.html"}
|
||||
|
||||
on_mount CanneryWeb.InitAssigns
|
||||
unquote(view_helpers())
|
||||
end
|
||||
end
|
||||
@ -71,6 +72,7 @@ defmodule CanneryWeb do
|
||||
quote do
|
||||
use Phoenix.Router
|
||||
|
||||
# credo:disable-for-next-line Credo.Check.Consistency.MultiAliasImportRequireUse
|
||||
import Plug.Conn
|
||||
import Phoenix.Controller
|
||||
import Phoenix.LiveView.Router
|
||||
@ -79,7 +81,9 @@ defmodule CanneryWeb do
|
||||
|
||||
def channel do
|
||||
quote do
|
||||
# credo:disable-for-next-line Credo.Check.Consistency.MultiAliasImportRequireUse
|
||||
use Phoenix.Channel
|
||||
# credo:disable-for-next-line Credo.Check.Consistency.MultiAliasImportRequireUse
|
||||
import CanneryWeb.Gettext
|
||||
end
|
||||
end
|
||||
@ -87,14 +91,18 @@ defmodule CanneryWeb do
|
||||
defp view_helpers do
|
||||
quote do
|
||||
# Use all HTML functionality (forms, tags, etc)
|
||||
# credo:disable-for-next-line Credo.Check.Consistency.MultiAliasImportRequireUse
|
||||
use Phoenix.HTML
|
||||
|
||||
# Import LiveView and .heex helpers (live_render, live_patch, <.form>, etc)
|
||||
# credo:disable-for-next-line Credo.Check.Consistency.MultiAliasImportRequireUse
|
||||
import Phoenix.LiveView.Helpers
|
||||
|
||||
# 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
|
||||
end
|
||||
|
@ -16,9 +16,10 @@ defmodule CanneryWeb.Components.AddShotGroupComponent do
|
||||
},
|
||||
Socket.t()
|
||||
) :: {:ok, Socket.t()}
|
||||
def update(%{ammo_group: _ammo_group, current_user: _current_user} = assigns, socket) do
|
||||
def update(%{ammo_group: ammo_group, current_user: current_user} = assigns, socket) do
|
||||
changeset =
|
||||
%ShotGroup{date: NaiveDateTime.utc_now(), count: 1} |> ActivityLog.change_shot_group()
|
||||
%ShotGroup{date: NaiveDateTime.utc_now(), count: 1}
|
||||
|> ShotGroup.create_changeset(current_user, ammo_group, %{})
|
||||
|
||||
{:ok, socket |> assign(assigns) |> assign(:changeset, changeset)}
|
||||
end
|
||||
@ -27,21 +28,13 @@ defmodule CanneryWeb.Components.AddShotGroupComponent do
|
||||
def handle_event(
|
||||
"validate",
|
||||
%{"shot_group" => shot_group_params},
|
||||
%{
|
||||
assigns: %{
|
||||
ammo_group: %AmmoGroup{id: ammo_group_id} = ammo_group,
|
||||
current_user: %User{id: user_id}
|
||||
}
|
||||
} = socket
|
||||
%{assigns: %{ammo_group: ammo_group, current_user: current_user}} = socket
|
||||
) do
|
||||
shot_group_params =
|
||||
shot_group_params
|
||||
|> process_params(ammo_group)
|
||||
|> Map.merge(%{"ammo_group_id" => ammo_group_id, "user_id" => user_id})
|
||||
params = shot_group_params |> process_params(ammo_group)
|
||||
|
||||
changeset =
|
||||
%ShotGroup{}
|
||||
|> ActivityLog.change_shot_group(shot_group_params)
|
||||
|> ShotGroup.create_changeset(current_user, ammo_group, params)
|
||||
|> Map.put(:action, :validate)
|
||||
|
||||
{:noreply, socket |> assign(:changeset, changeset)}
|
||||
@ -51,17 +44,12 @@ defmodule CanneryWeb.Components.AddShotGroupComponent do
|
||||
"save",
|
||||
%{"shot_group" => shot_group_params},
|
||||
%{
|
||||
assigns: %{
|
||||
ammo_group: %{id: ammo_group_id} = ammo_group,
|
||||
current_user: %{id: user_id} = current_user,
|
||||
return_to: return_to
|
||||
}
|
||||
assigns: %{ammo_group: ammo_group, current_user: current_user, return_to: return_to}
|
||||
} = socket
|
||||
) do
|
||||
socket =
|
||||
shot_group_params
|
||||
|> process_params(ammo_group)
|
||||
|> Map.merge(%{"ammo_group_id" => ammo_group_id, "user_id" => user_id})
|
||||
|> ActivityLog.create_shot_group(current_user, ammo_group)
|
||||
|> case do
|
||||
{:ok, _shot_group} ->
|
||||
|
@ -7,7 +7,7 @@
|
||||
let={f}
|
||||
for={@changeset}
|
||||
id="shot-group-form"
|
||||
class="flex flex-col 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"
|
||||
phx-target={@myself}
|
||||
phx-change="validate"
|
||||
phx-submit="save"
|
||||
@ -36,7 +36,11 @@
|
||||
<%= error_tag(f, :notes, "col-span-3") %>
|
||||
|
||||
<%= 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",
|
||||
phx_update: "ignore",
|
||||
value: Date.utc_today()
|
||||
) %>
|
||||
<%= error_tag(f, :notes, "col-span-3") %>
|
||||
|
||||
<%= submit(dgettext("actions", "Save"),
|
||||
|
@ -37,6 +37,11 @@ defmodule CanneryWeb.Components.AmmoGroupCard do
|
||||
</span>
|
||||
<% end %>
|
||||
|
||||
<span class="rounded-lg title text-lg">
|
||||
<%= gettext("Added on:") %>
|
||||
<%= @ammo_group.inserted_at |> display_datetime() %>
|
||||
</span>
|
||||
|
||||
<%= if @ammo_group.price_paid do %>
|
||||
<span class="rounded-lg title text-lg">
|
||||
<%= gettext("Price paid:") %>
|
||||
|
@ -4,17 +4,21 @@ defmodule CanneryWeb.Components.ContainerCard do
|
||||
"""
|
||||
|
||||
use CanneryWeb, :component
|
||||
import CanneryWeb.Components.TagCard
|
||||
alias Cannery.{Containers, Repo}
|
||||
alias CanneryWeb.Endpoint
|
||||
|
||||
def container_card(assigns) do
|
||||
def container_card(%{container: container} = assigns) do
|
||||
assigns = assigns |> Map.put(:container, container |> Repo.preload([:tags, :ammo_groups]))
|
||||
|
||||
~H"""
|
||||
<div
|
||||
id={"container-#{@container.id}"}
|
||||
class="mx-4 my-2 px-8 py-4 flex flex-col justify-center items-center
|
||||
class="overflow-hidden max-w-full 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
|
||||
transition-all duration-300 ease-in-out"
|
||||
>
|
||||
<div class="mb-4 flex flex-col justify-center items-center">
|
||||
<div class="max-w-full mb-4 flex flex-col justify-center items-center space-y-2">
|
||||
<%= live_redirect to: Routes.container_show_path(Endpoint, :show, @container),
|
||||
class: "link" do %>
|
||||
<h1 class="px-4 py-2 rounded-lg title text-xl">
|
||||
@ -40,6 +44,30 @@ defmodule CanneryWeb.Components.ContainerCard do
|
||||
<%= @container.location %>
|
||||
</span>
|
||||
<% end %>
|
||||
|
||||
<%= 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 class="rounded-lg title text-lg">
|
||||
<%= gettext("Rounds:") %>
|
||||
<%= @container |> Containers.get_container_rounds!() %>
|
||||
</span>
|
||||
<% end %>
|
||||
|
||||
<div class="flex flex-wrap justify-center items-center">
|
||||
<%= unless @container.tags |> Enum.empty?() do %>
|
||||
<%= for tag <- @container.tags do %>
|
||||
<.simple_tag_card tag={tag} />
|
||||
<% end %>
|
||||
<% end %>
|
||||
|
||||
<%= if assigns |> Map.has_key?(:tag_actions) do %>
|
||||
<%= render_slot(@tag_actions) %>
|
||||
<% end %>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<%= if assigns |> Map.has_key?(:inner_block) do %>
|
||||
|
@ -8,11 +8,9 @@ defmodule CanneryWeb.Components.InviteCard do
|
||||
|
||||
def invite_card(assigns) do
|
||||
~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
|
||||
transition-all duration-300 ease-in-out"
|
||||
>
|
||||
transition-all duration-300 ease-in-out">
|
||||
<h1 class="title text-xl">
|
||||
<%= @invite.name %>
|
||||
</h1>
|
||||
|
@ -4,7 +4,8 @@ defmodule CanneryWeb.Components.MoveAmmoGroupComponent do
|
||||
"""
|
||||
|
||||
use CanneryWeb, :live_component
|
||||
alias Cannery.{Accounts.User, Ammo, Ammo.AmmoGroup, Containers}
|
||||
alias Cannery.{Accounts.User, Ammo, Ammo.AmmoGroup, Containers, Containers.Container}
|
||||
alias CanneryWeb.Endpoint
|
||||
alias Phoenix.LiveView.Socket
|
||||
|
||||
@impl true
|
||||
@ -21,13 +22,18 @@ defmodule CanneryWeb.Components.MoveAmmoGroupComponent do
|
||||
assigns,
|
||||
socket
|
||||
) do
|
||||
changeset = Ammo.change_ammo_group(ammo_group)
|
||||
changeset = ammo_group |> AmmoGroup.update_changeset(%{})
|
||||
|
||||
containers =
|
||||
Containers.list_containers(current_user)
|
||||
|> Enum.reject(fn %{id: id} -> id == container_id end)
|
||||
|
||||
{:ok, socket |> assign(assigns) |> assign(changeset: changeset, containers: containers)}
|
||||
socket =
|
||||
socket
|
||||
|> assign(assigns)
|
||||
|> assign(changeset: changeset, containers: containers)
|
||||
|
||||
{:ok, socket}
|
||||
end
|
||||
|
||||
@impl true
|
||||
@ -54,4 +60,76 @@ defmodule CanneryWeb.Components.MoveAmmoGroupComponent do
|
||||
|
||||
{:noreply, socket}
|
||||
end
|
||||
|
||||
@impl true
|
||||
def render(%{containers: containers} = assigns) do
|
||||
columns = [
|
||||
%{label: gettext("Container"), key: "name"},
|
||||
%{label: gettext("Type"), key: "type"},
|
||||
%{label: gettext("Location"), key: "location"},
|
||||
%{label: nil, key: "actions", sortable: false}
|
||||
]
|
||||
|
||||
rows = containers |> get_rows_for_containers(assigns, columns)
|
||||
|
||||
assigns = assigns |> Map.merge(%{columns: columns, rows: rows})
|
||||
|
||||
~H"""
|
||||
<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">
|
||||
<%= gettext("Move ammo") %>
|
||||
</h2>
|
||||
|
||||
<%= if @containers |> Enum.empty?() do %>
|
||||
<h2 class="title text-xl text-primary-600">
|
||||
<%= gettext("No other containers") %>
|
||||
<%= display_emoji("😔") %>
|
||||
</h2>
|
||||
|
||||
<%= live_patch(dgettext("actions", "Add another container!"),
|
||||
to: Routes.container_index_path(Endpoint, :new),
|
||||
class: "btn btn-primary"
|
||||
) %>
|
||||
<% else %>
|
||||
<.live_component
|
||||
module={CanneryWeb.Components.TableComponent}
|
||||
id="move_ammo_group_table"
|
||||
columns={@columns}
|
||||
rows={@rows}
|
||||
/>
|
||||
<% end %>
|
||||
</div>
|
||||
"""
|
||||
end
|
||||
|
||||
@spec get_rows_for_containers([Container.t()], map(), [map()]) :: [map()]
|
||||
defp get_rows_for_containers(containers, assigns, columns) do
|
||||
containers
|
||||
|> Enum.map(fn container ->
|
||||
columns
|
||||
|> Enum.into(%{}, fn %{key: key} -> {key, get_row_value_by_key(key, container, assigns)} end)
|
||||
end)
|
||||
end
|
||||
|
||||
@spec get_row_value_by_key(String.t(), Container.t(), map()) :: any()
|
||||
defp get_row_value_by_key("actions", container, assigns) do
|
||||
assigns = assigns |> Map.put(:container, container)
|
||||
|
||||
~H"""
|
||||
<div class="px-4 py-2 space-x-4 flex justify-center items-center">
|
||||
<button
|
||||
type="button"
|
||||
class="btn btn-primary"
|
||||
phx-click="move"
|
||||
phx-target={@myself}
|
||||
phx-value-container_id={container.id}
|
||||
>
|
||||
<%= dgettext("actions", "Select") %>
|
||||
</button>
|
||||
</div>
|
||||
"""
|
||||
end
|
||||
|
||||
defp get_row_value_by_key(key, container, _assigns),
|
||||
do: container |> Map.get(key |> String.to_existing_atom())
|
||||
end
|
||||
|
@ -1,70 +0,0 @@
|
||||
<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">
|
||||
<%= gettext("Move ammo") %>
|
||||
</h2>
|
||||
|
||||
<%= if @containers |> Enum.empty?() do %>
|
||||
<h2 class="title text-xl text-primary-600">
|
||||
<%= gettext("No other containers") %>
|
||||
<%= display_emoji("😔") %>
|
||||
</h2>
|
||||
|
||||
<%= live_patch(dgettext("actions", "Add another container!"),
|
||||
to: Routes.container_index_path(Endpoint, :new),
|
||||
class: "btn btn-primary"
|
||||
) %>
|
||||
<% else %>
|
||||
<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">
|
||||
<thead class="border-b border-primary-600">
|
||||
<tr>
|
||||
<th class="p-2">
|
||||
<%= gettext("Container") %>
|
||||
</th>
|
||||
|
||||
<th class="p-2">
|
||||
<%= gettext("Type") %>
|
||||
</th>
|
||||
|
||||
<th class="p-2">
|
||||
<%= gettext("Location") %>
|
||||
</th>
|
||||
|
||||
<th class="p-2"></th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody id="containers">
|
||||
<%= for container <- @containers do %>
|
||||
<tr id={"container-#{container.id}"}>
|
||||
<td class="p-2">
|
||||
<%= container.name %>
|
||||
</td>
|
||||
|
||||
<td class="p-2">
|
||||
<%= container.type %>
|
||||
</td>
|
||||
|
||||
<td class="p-2">
|
||||
<%= container.location %>
|
||||
</td>
|
||||
|
||||
<td class="p-2">
|
||||
<div class="px-4 py-2 space-x-4 flex justify-center items-center">
|
||||
<button
|
||||
type="button"
|
||||
class="btn btn-primary"
|
||||
phx-click="move"
|
||||
phx-target={@myself}
|
||||
phx-value-container_id={container.id}
|
||||
>
|
||||
<%= dgettext("actions", "Select") %>
|
||||
</button>
|
||||
</div>
|
||||
</td>
|
||||
</tr>
|
||||
<% end %>
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
<% end %>
|
||||
</div>
|
87
lib/cannery_web/components/table_component.ex
Normal file
87
lib/cannery_web/components/table_component.ex
Normal file
@ -0,0 +1,87 @@
|
||||
defmodule CanneryWeb.Components.TableComponent do
|
||||
@moduledoc """
|
||||
Livecomponent that presents a resortable table
|
||||
|
||||
It takes the following required assigns:
|
||||
- `:columns`: An array of maps containing the following keys
|
||||
- `:label`: A gettext'd or otherwise user-facing string label for the
|
||||
column. Can be nil
|
||||
- `:key`: A string key used for sorting
|
||||
- `:class`: Extra classes to be applied to the column element, if desired.
|
||||
Optional
|
||||
- `:sortable`: If false, will prevent the user from sorting with it.
|
||||
Optional
|
||||
- `:values`: An array of maps containing data for each row. Each map is
|
||||
string-keyed with the associated column key to the following values:
|
||||
- A single element, like string, integer or Phoenix.LiveView.Rendered
|
||||
object, like returned from the ~H sigil
|
||||
- A tuple, containing a custom value used for sorting, and the displayed
|
||||
content.
|
||||
"""
|
||||
|
||||
use CanneryWeb, :live_component
|
||||
alias Phoenix.LiveView.Socket
|
||||
|
||||
@impl true
|
||||
@spec update(
|
||||
%{
|
||||
required(:columns) =>
|
||||
list(%{
|
||||
required(:label) => String.t() | nil,
|
||||
required(:key) => String.t() | nil,
|
||||
optional(:class) => String.t(),
|
||||
optional(:sortable) => false
|
||||
}),
|
||||
required(:rows) =>
|
||||
list(%{
|
||||
(key :: String.t()) => any() | {custom_sort_value :: String.t(), value :: any()}
|
||||
}),
|
||||
optional(any()) => any()
|
||||
},
|
||||
Socket.t()
|
||||
) :: {:ok, Socket.t()}
|
||||
def update(%{columns: columns, rows: rows} = assigns, socket) do
|
||||
initial_key = columns |> List.first() |> Map.get(:key)
|
||||
rows = rows |> Enum.sort_by(fn row -> row |> Map.get(initial_key) end, :asc)
|
||||
|
||||
socket =
|
||||
socket
|
||||
|> assign(assigns)
|
||||
|> assign(columns: columns, rows: rows, last_sort_key: initial_key, sort_mode: :asc)
|
||||
|
||||
{:ok, socket}
|
||||
end
|
||||
|
||||
@impl true
|
||||
def handle_event(
|
||||
"sort_by",
|
||||
%{"sort-key" => key},
|
||||
%{assigns: %{rows: rows, last_sort_key: key, sort_mode: sort_mode}} = socket
|
||||
) do
|
||||
sort_mode = if sort_mode == :asc, do: :desc, else: :asc
|
||||
rows = rows |> sort_by_custom_sort_value_or_value(key, sort_mode)
|
||||
{: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
|
||||
|
||||
defp sort_by_custom_sort_value_or_value(rows, key, sort_mode) do
|
||||
rows
|
||||
|> Enum.sort_by(
|
||||
fn row ->
|
||||
case row |> Map.get(key) do
|
||||
{custom_sort_key, _value} -> custom_sort_key
|
||||
value -> value
|
||||
end
|
||||
end,
|
||||
sort_mode
|
||||
)
|
||||
end
|
||||
end
|
52
lib/cannery_web/components/table_component.html.heex
Normal file
52
lib/cannery_web/components/table_component.html.heex
Normal file
@ -0,0 +1,52 @@
|
||||
<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">
|
||||
<thead class="border-b border-primary-600">
|
||||
<tr>
|
||||
<%= for %{key: key, label: label} = column <- @columns do %>
|
||||
<%= if column |> Map.get(:sortable, true) do %>
|
||||
<th class={"p-2 #{column[:class]}"}>
|
||||
<span
|
||||
class="cursor-pointer"
|
||||
phx-click="sort_by"
|
||||
phx-value-sort-key={key}
|
||||
phx-target={@myself}
|
||||
>
|
||||
<span class="underline"><%= label %></span>
|
||||
<%= if @last_sort_key == key do %>
|
||||
<%= case @sort_mode do %>
|
||||
<% :asc -> %>
|
||||
<i class="fas fa-sm fa-chevron-down"></i>
|
||||
<% :desc -> %>
|
||||
<i class="fas fa-sm fa-chevron-up"></i>
|
||||
<% end %>
|
||||
<% else %>
|
||||
<i class="fas fa-sm fa-chevron-up opacity-0"></i>
|
||||
<% end %>
|
||||
</span>
|
||||
</th>
|
||||
<% else %>
|
||||
<th class={"p-2 #{column[:class]}"}>
|
||||
<%= label %>
|
||||
</th>
|
||||
<% end %>
|
||||
<% end %>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<%= for values <- @rows do %>
|
||||
<tr>
|
||||
<%= for %{key: key} = value <- @columns do %>
|
||||
<td class={"p-2 #{value[:class]}"}>
|
||||
<%= case values |> Map.get(key) do %>
|
||||
<% {_custom_sort_value, value} -> %>
|
||||
<%= value %>
|
||||
<% value -> %>
|
||||
<%= value %>
|
||||
<% end %>
|
||||
</td>
|
||||
<% end %>
|
||||
</tr>
|
||||
<% end %>
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
@ -13,15 +13,20 @@ defmodule CanneryWeb.Components.TagCard do
|
||||
border border-gray-400 rounded-lg shadow-lg hover:shadow-md
|
||||
transition-all duration-300 ease-in-out"
|
||||
>
|
||||
<h1
|
||||
class="px-4 py-2 rounded-lg title text-xl"
|
||||
style={"color: #{@tag.text_color}; background-color: #{@tag.bg_color}"}
|
||||
>
|
||||
<%= @tag.name %>
|
||||
</h1>
|
||||
|
||||
<.simple_tag_card tag={@tag} />
|
||||
<%= render_slot(@inner_block) %>
|
||||
</div>
|
||||
"""
|
||||
end
|
||||
|
||||
def simple_tag_card(assigns) do
|
||||
~H"""
|
||||
<h1
|
||||
class="inline-block break-all mx-2 my-1 px-4 py-2 rounded-lg title text-xl"
|
||||
style={"color: #{@tag.text_color}; background-color: #{@tag.bg_color}"}
|
||||
>
|
||||
<%= @tag.name %>
|
||||
</h1>
|
||||
"""
|
||||
end
|
||||
end
|
||||
|
@ -16,10 +16,16 @@ defmodule CanneryWeb.Components.Topbar do
|
||||
<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="mb-4 sm:mb-0 sm:mr-8 flex flex-row justify-start items-center space-x-2">
|
||||
<%= live_redirect("Cannery",
|
||||
to: Routes.live_path(Endpoint, HomeLive),
|
||||
class: "mx-2 my-1 leading-5 text-xl text-white hover:underline"
|
||||
) %>
|
||||
<%= live_redirect to: Routes.live_path(Endpoint, HomeLive),
|
||||
class: "inline mx-2 my-1 leading-5 text-xl text-white"
|
||||
do %>
|
||||
<img
|
||||
src={Routes.static_path(Endpoint, "/images/cannery.svg")}
|
||||
alt={gettext("Cannery logo")}
|
||||
class="inline-block h-8 mx-1"
|
||||
/>
|
||||
<h1 class="inline hover:underline">Cannery</h1>
|
||||
<% end %>
|
||||
|
||||
<%= if @title_content do %>
|
||||
<span class="mx-2 my-1">
|
||||
@ -31,10 +37,8 @@ defmodule CanneryWeb.Components.Topbar do
|
||||
|
||||
<hr class="mb-2 sm:hidden hr-light" />
|
||||
|
||||
<ul
|
||||
class="flex flex-row flex-wrap justify-center items-center
|
||||
text-lg text-white text-ellipsis"
|
||||
>
|
||||
<ul class="flex flex-row flex-wrap justify-center items-center
|
||||
text-lg text-white text-ellipsis">
|
||||
<%= if @current_user do %>
|
||||
<li class="mx-2 my-1">
|
||||
<%= live_redirect(gettext("Tags"),
|
||||
@ -49,13 +53,13 @@ defmodule CanneryWeb.Components.Topbar do
|
||||
) %>
|
||||
</li>
|
||||
<li class="mx-2 my-1">
|
||||
<%= live_redirect(gettext("Ammo"),
|
||||
<%= live_redirect(gettext("Catalog"),
|
||||
to: Routes.ammo_type_index_path(Endpoint, :index),
|
||||
class: "text-primary-600 text-white hover:underline"
|
||||
) %>
|
||||
</li>
|
||||
<li class="mx-2 my-1">
|
||||
<%= live_redirect(gettext("Manage"),
|
||||
<%= live_redirect(gettext("Ammo"),
|
||||
to: Routes.ammo_group_index_path(Endpoint, :index),
|
||||
class: "text-primary-600 text-white hover:underline"
|
||||
) %>
|
||||
@ -91,7 +95,7 @@ defmodule CanneryWeb.Components.Topbar 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-tachometer-alt"></i>
|
||||
<i class="fas fa-gauge"></i>
|
||||
<% end %>
|
||||
</li>
|
||||
<% end %>
|
||||
|
@ -9,7 +9,7 @@ defmodule CanneryWeb.Components.UserCard do
|
||||
~H"""
|
||||
<div
|
||||
id={"user-#{@user.id}"}
|
||||
class="mx-4 my-2 px-8 py-4 flex flex-col justify-center items-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
|
||||
transition-all duration-300 ease-in-out"
|
||||
>
|
||||
@ -18,11 +18,18 @@ defmodule CanneryWeb.Components.UserCard do
|
||||
</h1>
|
||||
|
||||
<h3 class="px-4 py-2 rounded-lg title text-lg">
|
||||
<p>
|
||||
<%= if @user.confirmed_at |> is_nil() do %>
|
||||
Email unconfirmed
|
||||
<% else %>
|
||||
User was confirmed at<%= @user.confirmed_at |> display_datetime() %>
|
||||
User was confirmed at <%= @user.confirmed_at |> display_datetime() %>
|
||||
<% end %>
|
||||
</p>
|
||||
|
||||
<p>
|
||||
<%= gettext("User registered on") %>
|
||||
<%= @user.inserted_at |> display_datetime() %>
|
||||
</p>
|
||||
</h3>
|
||||
|
||||
<%= if @inner_block do %>
|
||||
|
@ -1,10 +1,11 @@
|
||||
defmodule CanneryWeb.UserConfirmationController do
|
||||
use CanneryWeb, :controller
|
||||
|
||||
import CanneryWeb.Gettext
|
||||
alias Cannery.Accounts
|
||||
|
||||
def new(conn, _params) do
|
||||
render(conn, "new.html")
|
||||
render(conn, "new.html", page_title: gettext("Confirm your account"))
|
||||
end
|
||||
|
||||
def create(conn, %{"user" => %{"email" => email}}) do
|
||||
|
@ -29,8 +29,11 @@ defmodule CanneryWeb.UserRegistrationController do
|
||||
|
||||
# renders new user registration page
|
||||
defp render_new(conn, invite \\ nil) do
|
||||
changeset = Accounts.change_user_registration(%User{})
|
||||
conn |> render("new.html", changeset: changeset, invite: invite)
|
||||
render(conn, "new.html",
|
||||
changeset: Accounts.change_user_registration(%User{}),
|
||||
invite: invite,
|
||||
page_title: gettext("Register")
|
||||
)
|
||||
end
|
||||
|
||||
def create(conn, %{"user" => %{"invite_token" => invite_token}} = attrs) do
|
||||
|
@ -6,7 +6,7 @@ defmodule CanneryWeb.UserResetPasswordController do
|
||||
plug :get_user_by_reset_password_token when action in [:edit, :update]
|
||||
|
||||
def new(conn, _params) do
|
||||
render(conn, "new.html")
|
||||
render(conn, "new.html", page_title: gettext("Forgot your password?"))
|
||||
end
|
||||
|
||||
def create(conn, %{"user" => %{"email" => email}}) do
|
||||
@ -31,7 +31,10 @@ defmodule CanneryWeb.UserResetPasswordController do
|
||||
end
|
||||
|
||||
def edit(conn, _params) do
|
||||
render(conn, "edit.html", changeset: Accounts.change_user_password(conn.assigns.user))
|
||||
render(conn, "edit.html",
|
||||
changeset: Accounts.change_user_password(conn.assigns.user),
|
||||
page_title: gettext("Reset your password")
|
||||
)
|
||||
end
|
||||
|
||||
# Do not log in the user after reset password to avoid a
|
||||
|
@ -5,7 +5,7 @@ defmodule CanneryWeb.UserSessionController do
|
||||
alias CanneryWeb.UserAuth
|
||||
|
||||
def new(conn, _params) do
|
||||
render(conn, "new.html", error_message: nil)
|
||||
render(conn, "new.html", error_message: nil, page_title: gettext("Log in"))
|
||||
end
|
||||
|
||||
def create(conn, %{"user" => user_params}) do
|
||||
|
@ -1,19 +1,20 @@
|
||||
defmodule CanneryWeb.UserSettingsController do
|
||||
use CanneryWeb, :controller
|
||||
|
||||
import CanneryWeb.Gettext
|
||||
alias Cannery.Accounts
|
||||
alias CanneryWeb.{HomeLive, UserAuth}
|
||||
|
||||
plug :assign_email_and_password_changesets
|
||||
|
||||
def edit(conn, _params) do
|
||||
render(conn, "edit.html")
|
||||
render(conn, "edit.html", page_title: gettext("Settings"))
|
||||
end
|
||||
|
||||
def update(conn, %{"action" => "update_email"} = params) do
|
||||
%{"current_password" => password, "user" => user_params} = params
|
||||
user = conn.assigns.current_user
|
||||
|
||||
def update(%{assigns: %{current_user: user}} = conn, %{
|
||||
"action" => "update_email",
|
||||
"current_password" => password,
|
||||
"user" => user_params
|
||||
}) do
|
||||
case Accounts.apply_user_email(user, password, user_params) do
|
||||
{:ok, applied_user} ->
|
||||
Accounts.deliver_update_email_instructions(
|
||||
@ -33,14 +34,15 @@ defmodule CanneryWeb.UserSettingsController do
|
||||
|> redirect(to: Routes.user_settings_path(conn, :edit))
|
||||
|
||||
{:error, changeset} ->
|
||||
render(conn, "edit.html", email_changeset: changeset)
|
||||
conn |> render("edit.html", email_changeset: changeset)
|
||||
end
|
||||
end
|
||||
|
||||
def update(conn, %{"action" => "update_password"} = params) do
|
||||
%{"current_password" => password, "user" => user_params} = params
|
||||
user = conn.assigns.current_user
|
||||
|
||||
def update(%{assigns: %{current_user: user}} = conn, %{
|
||||
"action" => "update_password",
|
||||
"current_password" => password,
|
||||
"user" => user_params
|
||||
}) do
|
||||
case Accounts.update_user_password(user, password, user_params) do
|
||||
{:ok, user} ->
|
||||
conn
|
||||
@ -49,12 +51,27 @@ defmodule CanneryWeb.UserSettingsController do
|
||||
|> UserAuth.log_in_user(user)
|
||||
|
||||
{:error, changeset} ->
|
||||
render(conn, "edit.html", password_changeset: changeset)
|
||||
conn |> render("edit.html", password_changeset: changeset)
|
||||
end
|
||||
end
|
||||
|
||||
def confirm_email(conn, %{"token" => token}) do
|
||||
case Accounts.update_user_email(conn.assigns.current_user, token) do
|
||||
def update(
|
||||
%{assigns: %{current_user: user}} = conn,
|
||||
%{"action" => "update_locale", "user" => %{"locale" => locale}}
|
||||
) do
|
||||
case Accounts.update_user_locale(user, locale) do
|
||||
{:ok, _user} ->
|
||||
conn
|
||||
|> put_flash(:info, dgettext("prompts", "Language updated successfully."))
|
||||
|> redirect(to: Routes.user_settings_path(conn, :edit))
|
||||
|
||||
{:error, changeset} ->
|
||||
conn |> render("edit.html", locale_changeset: changeset)
|
||||
end
|
||||
end
|
||||
|
||||
def confirm_email(%{assigns: %{current_user: user}} = conn, %{"token" => token}) do
|
||||
case Accounts.update_user_email(user, token) do
|
||||
:ok ->
|
||||
conn
|
||||
|> put_flash(:info, dgettext("prompts", "Email changed successfully."))
|
||||
@ -84,11 +101,10 @@ defmodule CanneryWeb.UserSettingsController do
|
||||
end
|
||||
end
|
||||
|
||||
defp assign_email_and_password_changesets(conn, _opts) do
|
||||
user = conn.assigns.current_user
|
||||
|
||||
defp assign_email_and_password_changesets(%{assigns: %{current_user: user}} = conn, _opts) do
|
||||
conn
|
||||
|> assign(:email_changeset, Accounts.change_user_email(user))
|
||||
|> assign(:password_changeset, Accounts.change_user_password(user))
|
||||
|> assign(:locale_changeset, Accounts.change_user_locale(user))
|
||||
end
|
||||
end
|
||||
|
@ -9,6 +9,8 @@ defmodule CanneryWeb.AmmoGroupLive.FormComponent do
|
||||
alias Ecto.Changeset
|
||||
alias Phoenix.LiveView.Socket
|
||||
|
||||
@ammo_group_create_limit 10_000
|
||||
|
||||
@impl true
|
||||
@spec update(
|
||||
%{:ammo_group => AmmoGroup.t(), :current_user => User.t(), optional(any) => any},
|
||||
@ -19,21 +21,30 @@ defmodule CanneryWeb.AmmoGroupLive.FormComponent do
|
||||
end
|
||||
|
||||
@spec update(Socket.t()) :: {:ok, Socket.t()}
|
||||
def update(%{assigns: %{ammo_group: ammo_group, current_user: current_user}} = socket) do
|
||||
changeset = Ammo.change_ammo_group(ammo_group)
|
||||
containers = Containers.list_containers(current_user)
|
||||
ammo_types = Ammo.list_ammo_types(current_user)
|
||||
{:ok, socket |> assign(changeset: changeset, containers: containers, ammo_types: ammo_types)}
|
||||
def update(%{assigns: %{current_user: current_user}} = socket) do
|
||||
%{assigns: %{ammo_types: ammo_types, containers: containers}} =
|
||||
socket =
|
||||
socket
|
||||
|> assign(:ammo_group_create_limit, @ammo_group_create_limit)
|
||||
|> assign(:ammo_types, Ammo.list_ammo_types(current_user))
|
||||
|> assign_new(:containers, fn -> Containers.list_containers(current_user) end)
|
||||
|
||||
params =
|
||||
if ammo_types |> List.first() |> is_nil(),
|
||||
do: %{},
|
||||
else: %{} |> Map.put("ammo_type_id", ammo_types |> List.first() |> Map.get(:id))
|
||||
|
||||
params =
|
||||
if containers |> List.first() |> is_nil(),
|
||||
do: params,
|
||||
else: params |> Map.put("container_id", containers |> List.first() |> Map.get(:id))
|
||||
|
||||
{:ok, socket |> assign_changeset(params)}
|
||||
end
|
||||
|
||||
@impl true
|
||||
def handle_event(
|
||||
"validate",
|
||||
%{"ammo_group" => ammo_group_params},
|
||||
%{assigns: %{ammo_group: ammo_group}} = socket
|
||||
) do
|
||||
socket = socket |> assign(:changeset, ammo_group |> Ammo.change_ammo_group(ammo_group_params))
|
||||
{:noreply, socket}
|
||||
def handle_event("validate", %{"ammo_group" => ammo_group_params}, socket) do
|
||||
{:noreply, socket |> assign_changeset(ammo_group_params)}
|
||||
end
|
||||
|
||||
def handle_event(
|
||||
@ -57,6 +68,44 @@ defmodule CanneryWeb.AmmoGroupLive.FormComponent do
|
||||
|
||||
# Save Helpers
|
||||
|
||||
defp assign_changeset(
|
||||
%{assigns: %{action: action, ammo_group: ammo_group, current_user: user}} = socket,
|
||||
ammo_group_params
|
||||
) do
|
||||
changeset_action =
|
||||
case action do
|
||||
:new -> :insert
|
||||
:edit -> :update
|
||||
end
|
||||
|
||||
changeset =
|
||||
case action do
|
||||
:new ->
|
||||
ammo_type =
|
||||
if ammo_group_params |> Map.has_key?("ammo_type_id"),
|
||||
do: ammo_group_params |> Map.get("ammo_type_id") |> Ammo.get_ammo_type!(user),
|
||||
else: nil
|
||||
|
||||
container =
|
||||
if ammo_group_params |> Map.has_key?("container_id"),
|
||||
do: ammo_group_params |> Map.get("container_id") |> Containers.get_container!(user),
|
||||
else: nil
|
||||
|
||||
ammo_group |> AmmoGroup.create_changeset(ammo_type, container, user, ammo_group_params)
|
||||
|
||||
:edit ->
|
||||
ammo_group |> AmmoGroup.update_changeset(ammo_group_params)
|
||||
end
|
||||
|
||||
changeset =
|
||||
case changeset |> Changeset.apply_action(changeset_action) do
|
||||
{:ok, _data} -> changeset
|
||||
{:error, changeset} -> changeset
|
||||
end
|
||||
|
||||
socket |> assign(:changeset, changeset)
|
||||
end
|
||||
|
||||
defp save_ammo_group(
|
||||
%{assigns: %{ammo_group: ammo_group, current_user: current_user, return_to: return_to}} =
|
||||
socket,
|
||||
@ -66,7 +115,7 @@ defmodule CanneryWeb.AmmoGroupLive.FormComponent do
|
||||
socket =
|
||||
case Ammo.update_ammo_group(ammo_group, ammo_group_params, current_user) do
|
||||
{:ok, _ammo_group} ->
|
||||
prompt = dgettext("prompts", "Ammo group updated successfully")
|
||||
prompt = dgettext("prompts", "Ammo updated successfully")
|
||||
socket |> put_flash(:info, prompt) |> push_redirect(to: return_to)
|
||||
|
||||
{:error, %Changeset{} = changeset} ->
|
||||
@ -77,20 +126,65 @@ defmodule CanneryWeb.AmmoGroupLive.FormComponent do
|
||||
end
|
||||
|
||||
defp save_ammo_group(
|
||||
%{assigns: %{current_user: current_user, return_to: return_to}} = socket,
|
||||
%{assigns: %{changeset: changeset}} = socket,
|
||||
:new,
|
||||
ammo_group_params
|
||||
%{"multiplier" => multiplier_str} = ammo_group_params
|
||||
) do
|
||||
socket =
|
||||
case Ammo.create_ammo_group(ammo_group_params, current_user) do
|
||||
{:ok, _ammo_group} ->
|
||||
prompt = dgettext("prompts", "Ammo group created successfully")
|
||||
case multiplier_str |> Integer.parse() do
|
||||
{multiplier, _remainder}
|
||||
when multiplier >= 1 and multiplier <= @ammo_group_create_limit ->
|
||||
socket |> create_multiple(ammo_group_params, multiplier)
|
||||
|
||||
{multiplier, _remainder} ->
|
||||
error_msg =
|
||||
dgettext(
|
||||
"errors",
|
||||
"Invalid number of copies, must be between 1 and %{max}. Was %{multiplier}",
|
||||
max: @ammo_group_create_limit,
|
||||
multiplier: multiplier
|
||||
)
|
||||
|
||||
{:error, changeset} =
|
||||
changeset
|
||||
|> Changeset.add_error(:multiplier, error_msg)
|
||||
|> Changeset.apply_action(:insert)
|
||||
|
||||
socket |> assign(:changeset, changeset)
|
||||
|
||||
:error ->
|
||||
error_msg = dgettext("errors", "Could not parse number of copies")
|
||||
|
||||
{:error, changeset} =
|
||||
changeset
|
||||
|> Changeset.add_error(:multiplier, error_msg)
|
||||
|> Changeset.apply_action(:insert)
|
||||
|
||||
socket |> assign(:changeset, changeset)
|
||||
end
|
||||
|
||||
{:noreply, socket}
|
||||
end
|
||||
|
||||
defp create_multiple(
|
||||
%{assigns: %{current_user: current_user, return_to: return_to}} = socket,
|
||||
ammo_group_params,
|
||||
multiplier
|
||||
) do
|
||||
case Ammo.create_ammo_groups(ammo_group_params, multiplier, current_user) do
|
||||
{:ok, {count, _ammo_groups}} ->
|
||||
prompt =
|
||||
dngettext(
|
||||
"prompts",
|
||||
"Ammo added successfully",
|
||||
"Ammo added successfully",
|
||||
count
|
||||
)
|
||||
|
||||
socket |> put_flash(:info, prompt) |> push_redirect(to: return_to)
|
||||
|
||||
{:error, %Changeset{} = changeset} ->
|
||||
socket |> assign(changeset: changeset)
|
||||
end
|
||||
|
||||
{:noreply, socket}
|
||||
end
|
||||
end
|
||||
|
@ -10,7 +10,7 @@
|
||||
phx-target={@myself}
|
||||
phx-change="validate"
|
||||
phx-submit="save"
|
||||
class="flex flex-col 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"
|
||||
>
|
||||
<%= if @changeset.action && not @changeset.valid? do %>
|
||||
<div class="invalid-feedback col-span-3 text-center">
|
||||
@ -18,42 +18,62 @@
|
||||
</div>
|
||||
<% end %>
|
||||
|
||||
<%= label(f, :ammo_type_id, gettext("Ammo type"), class: "mr-4 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),
|
||||
class: "text-center col-span-2 input input-primary"
|
||||
) %>
|
||||
<%= error_tag(f, :ammo_type_id, "col-span-3 text-center") %>
|
||||
|
||||
<%= label(f, :count, gettext("Count"), class: "mr-4 title text-lg text-primary-600") %>
|
||||
<%= label(f, :count, gettext("Count"), class: "title text-lg text-primary-600") %>
|
||||
<%= number_input(f, :count,
|
||||
class: "text-center col-span-2 input input-primary",
|
||||
min: 1
|
||||
min: 0
|
||||
) %>
|
||||
<%= error_tag(f, :count, "col-span-3 text-center") %>
|
||||
|
||||
<%= label(f, :price_paid, gettext("Price paid"), class: "mr-4 title text-lg text-primary-600") %>
|
||||
<%= label(f, :price_paid, gettext("Price paid"), class: "title text-lg text-primary-600") %>
|
||||
<%= number_input(f, :price_paid,
|
||||
step: "0.01",
|
||||
step: 0.01,
|
||||
class: "text-center col-span-2 input input-primary"
|
||||
) %>
|
||||
<%= error_tag(f, :price_paid, "col-span-3 text-center") %>
|
||||
|
||||
<%= label(f, :notes, gettext("Notes"), class: "mr-4 title text-lg text-primary-600") %>
|
||||
<%= label(f, :notes, gettext("Notes"), class: "title text-lg text-primary-600") %>
|
||||
<%= textarea(f, :notes,
|
||||
class: "text-center col-span-2 input input-primary",
|
||||
phx_hook: "MaintainAttrs"
|
||||
) %>
|
||||
<%= error_tag(f, :notes, "col-span-3 text-center") %>
|
||||
|
||||
<%= label(f, :container, gettext("Container"), class: "mr-4 title text-lg text-primary-600") %>
|
||||
<%= label(f, :container, gettext("Container"), class: "title text-lg text-primary-600") %>
|
||||
<%= select(f, :container_id, container_options(@containers),
|
||||
class: "text-center col-span-2 input input-primary"
|
||||
) %>
|
||||
<%= error_tag(f, :container_id, "col-span-3 text-center") %>
|
||||
|
||||
<%= case @action do %>
|
||||
<% :new -> %>
|
||||
<hr class="hr col-span-3" />
|
||||
|
||||
<%= label(f, :multiplier, gettext("Copies"), class: "title text-lg text-primary-600") %>
|
||||
<%= number_input(f, :multiplier,
|
||||
max: @ammo_group_create_limit,
|
||||
class: "text-center input input-primary",
|
||||
value: 1,
|
||||
phx_update: "ignore"
|
||||
) %>
|
||||
|
||||
<%= submit(dgettext("actions", "Create"),
|
||||
phx_disable_with: dgettext("prompts", "Creating..."),
|
||||
class: "mx-auto btn btn-primary"
|
||||
) %>
|
||||
|
||||
<%= error_tag(f, :multiplier, "col-span-3 text-center") %>
|
||||
<% :edit -> %>
|
||||
<%= submit(dgettext("actions", "Save"),
|
||||
phx_disable_with: dgettext("prompts", "Saving..."),
|
||||
class: "mx-auto col-span-3 btn btn-primary"
|
||||
) %>
|
||||
<% end %>
|
||||
</.form>
|
||||
</div>
|
||||
|
@ -4,12 +4,12 @@ defmodule CanneryWeb.AmmoGroupLive.Index do
|
||||
"""
|
||||
|
||||
use CanneryWeb, :live_view
|
||||
alias Cannery.{Ammo, Ammo.AmmoGroup, Repo}
|
||||
alias Cannery.{Ammo, Ammo.AmmoGroup, Containers, Repo}
|
||||
alias CanneryWeb.Endpoint
|
||||
|
||||
@impl true
|
||||
def mount(_params, session, socket) do
|
||||
{:ok, socket |> assign_defaults(session) |> display_ammo_groups()}
|
||||
def mount(_params, _session, socket) do
|
||||
{:ok, socket |> display_ammo_groups()}
|
||||
end
|
||||
|
||||
@impl true
|
||||
@ -17,9 +17,11 @@ defmodule CanneryWeb.AmmoGroupLive.Index do
|
||||
{:noreply, apply_action(socket, live_action, params)}
|
||||
end
|
||||
|
||||
defp apply_action(%{assigns: %{current_user: current_user}} = socket, :add_shot_group, %{
|
||||
"id" => id
|
||||
}) do
|
||||
defp apply_action(
|
||||
%{assigns: %{current_user: current_user}} = socket,
|
||||
:add_shot_group,
|
||||
%{"id" => id}
|
||||
) do
|
||||
socket
|
||||
|> assign(:page_title, gettext("Record shots"))
|
||||
|> assign(:ammo_group, Ammo.get_ammo_group!(id, current_user))
|
||||
@ -72,6 +74,138 @@ defmodule CanneryWeb.AmmoGroupLive.Index do
|
||||
|
||||
defp display_ammo_groups(%{assigns: %{current_user: current_user}} = socket) do
|
||||
ammo_groups = Ammo.list_ammo_groups(current_user) |> Repo.preload([:ammo_type, :container])
|
||||
socket |> assign(:ammo_groups, ammo_groups)
|
||||
ammo_types_count = Ammo.get_ammo_types_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
|
||||
|> assign(
|
||||
ammo_groups: ammo_groups,
|
||||
ammo_types_count: ammo_types_count,
|
||||
containers_count: containers_count,
|
||||
columns: columns,
|
||||
rows: rows
|
||||
)
|
||||
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
|
||||
|
@ -8,127 +8,51 @@
|
||||
<%= gettext("No Ammo") %>
|
||||
<%= display_emoji("😔") %>
|
||||
</h2>
|
||||
<% end %>
|
||||
|
||||
<%= cond do %>
|
||||
<% @containers_count == 0 -> %>
|
||||
<div class="flex justify-center items-center">
|
||||
<h2 class="m-2 title text-md text-primary-600">
|
||||
<%= dgettext("prompts", "You'll need to") %>
|
||||
</h2>
|
||||
|
||||
<%= live_patch(dgettext("actions", "add a container first"),
|
||||
to: Routes.container_index_path(Endpoint, :new),
|
||||
class: "btn btn-primary"
|
||||
) %>
|
||||
</div>
|
||||
<% @ammo_types_count == 0 -> %>
|
||||
<div class="flex justify-center items-center">
|
||||
<h2 class="m-2 title text-md text-primary-600">
|
||||
<%= dgettext("prompts", "You'll need to") %>
|
||||
</h2>
|
||||
|
||||
<%= live_patch(dgettext("actions", "add an ammo type first"),
|
||||
to: Routes.ammo_type_index_path(Endpoint, :new),
|
||||
class: "btn btn-primary"
|
||||
) %>
|
||||
</div>
|
||||
<% @ammo_groups |> Enum.empty?() -> %>
|
||||
<%= live_patch(dgettext("actions", "Add your first box!"),
|
||||
to: Routes.ammo_group_index_path(Endpoint, :new),
|
||||
class: "btn btn-primary"
|
||||
) %>
|
||||
<% else %>
|
||||
<%= live_patch(dgettext("actions", "New Ammo group"),
|
||||
<% true -> %>
|
||||
<%= live_patch(dgettext("actions", "Add Ammo"),
|
||||
to: Routes.ammo_group_index_path(Endpoint, :new),
|
||||
class: "btn btn-primary"
|
||||
) %>
|
||||
|
||||
<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">
|
||||
<thead class="border-b border-primary-600">
|
||||
<tr>
|
||||
<th class="p-2">
|
||||
<%= gettext("Ammo type") %>
|
||||
</th>
|
||||
<th class="p-2">
|
||||
<%= gettext("Count") %>
|
||||
</th>
|
||||
<th class="p-2">
|
||||
<%= gettext("Price paid") %>
|
||||
</th>
|
||||
<th class="p-2">
|
||||
<%= gettext("Notes") %>
|
||||
</th>
|
||||
<th class="p-2">
|
||||
<%= gettext("Range") %>
|
||||
</th>
|
||||
<th class="p-2">
|
||||
<%= gettext("Container") %>
|
||||
</th>
|
||||
|
||||
<th class="p-2"></th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody id="ammo_groups">
|
||||
<%= for ammo_group <- @ammo_groups do %>
|
||||
<tr id={"ammo_group-#{ammo_group.id}"}>
|
||||
<td class="p-2">
|
||||
<%= live_patch(ammo_group.ammo_type.name,
|
||||
to: Routes.ammo_type_show_path(Endpoint, :show, ammo_group.ammo_type),
|
||||
class: "link"
|
||||
) %>
|
||||
</td>
|
||||
|
||||
<td class="p-2">
|
||||
<%= ammo_group.count %>
|
||||
</td>
|
||||
|
||||
<td class="p-2">
|
||||
<%= if ammo_group.price_paid do %>
|
||||
<%= gettext("$%{amount}",
|
||||
amount: ammo_group.price_paid |> :erlang.float_to_binary(decimals: 2)
|
||||
) %>
|
||||
<% end %>
|
||||
</td>
|
||||
|
||||
<td class="p-2">
|
||||
<%= ammo_group.notes %>
|
||||
</td>
|
||||
|
||||
<td class="p-2">
|
||||
<div class="px-4 py-2 space-x-4 flex justify-center items-center">
|
||||
<button
|
||||
type="button"
|
||||
class="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: "btn btn-primary"
|
||||
) %>
|
||||
</div>
|
||||
</td>
|
||||
|
||||
<td class="p-2">
|
||||
<%= if ammo_group.container do %>
|
||||
<%= live_patch(ammo_group.container.name,
|
||||
to: Routes.ammo_group_index_path(Endpoint, :move, ammo_group),
|
||||
class: "btn btn-primary"
|
||||
) %>
|
||||
<% end %>
|
||||
</td>
|
||||
|
||||
<td class="p-2">
|
||||
<div class="px-4 py-2 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>
|
||||
</td>
|
||||
</tr>
|
||||
<% end %>
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
<%= unless @ammo_groups |> Enum.empty?() do %>
|
||||
<.live_component
|
||||
module={CanneryWeb.Components.TableComponent}
|
||||
id="ammo_groups_index_table"
|
||||
action={@live_action}
|
||||
columns={@columns}
|
||||
rows={@rows}
|
||||
/>
|
||||
<% end %>
|
||||
</div>
|
||||
|
||||
@ -170,4 +94,5 @@
|
||||
/>
|
||||
</.modal>
|
||||
<% true -> %>
|
||||
<% end %>
|
||||
<%= nil %>
|
||||
<% end %>
|
||||
|
@ -5,33 +5,49 @@ defmodule CanneryWeb.AmmoGroupLive.Show do
|
||||
|
||||
use CanneryWeb, :live_view
|
||||
import CanneryWeb.Components.ContainerCard
|
||||
alias Cannery.{Ammo, Repo}
|
||||
alias Cannery.{ActivityLog, ActivityLog.ShotGroup, Ammo, Ammo.AmmoGroup, Repo}
|
||||
alias CanneryWeb.Endpoint
|
||||
alias Phoenix.LiveView.Socket
|
||||
|
||||
@impl true
|
||||
def mount(_params, session, socket) do
|
||||
{:ok, socket |> assign_defaults(session)}
|
||||
end
|
||||
def mount(_params, _session, socket), do: {:ok, socket}
|
||||
|
||||
@impl true
|
||||
def handle_params(
|
||||
%{"id" => id},
|
||||
%{"id" => id, "shot_group_id" => shot_group_id},
|
||||
_url,
|
||||
%{assigns: %{live_action: live_action, current_user: current_user}} = socket
|
||||
) do
|
||||
ammo_group = Ammo.get_ammo_group!(id, current_user) |> Repo.preload([:container, :ammo_type])
|
||||
{:noreply, socket |> assign(page_title: page_title(live_action), ammo_group: ammo_group)}
|
||||
shot_group = ActivityLog.get_shot_group!(shot_group_id, current_user)
|
||||
|
||||
socket =
|
||||
socket
|
||||
|> assign(page_title: page_title(live_action), shot_group: shot_group)
|
||||
|> display_ammo_group(id)
|
||||
|
||||
{:noreply, socket}
|
||||
end
|
||||
|
||||
@impl true
|
||||
def handle_params(%{"id" => id}, _url, %{assigns: %{live_action: live_action}} = socket) do
|
||||
{:noreply, socket |> assign(page_title: page_title(live_action)) |> display_ammo_group(id)}
|
||||
end
|
||||
|
||||
defp page_title(:add_shot_group), do: gettext("Record Shots")
|
||||
defp page_title(:edit_shot_group), do: gettext("Edit Shot Records")
|
||||
defp page_title(:move), do: gettext("Move Ammo")
|
||||
defp page_title(:show), do: gettext("Show Ammo")
|
||||
defp page_title(:edit), do: gettext("Edit Ammo")
|
||||
|
||||
@impl true
|
||||
def handle_event(
|
||||
"delete",
|
||||
_,
|
||||
_params,
|
||||
%{assigns: %{ammo_group: ammo_group, current_user: current_user}} = socket
|
||||
) do
|
||||
ammo_group |> Ammo.delete_ammo_group!(current_user)
|
||||
|
||||
prompt = dgettext("prompts", "Ammo group deleted succesfully")
|
||||
prompt = dgettext("prompts", "Ammo deleted succesfully")
|
||||
redirect_to = Routes.ammo_group_index_path(socket, :index)
|
||||
|
||||
{:noreply, socket |> put_flash(:info, prompt) |> push_redirect(to: redirect_to)}
|
||||
@ -40,17 +56,90 @@ defmodule CanneryWeb.AmmoGroupLive.Show do
|
||||
@impl true
|
||||
def handle_event(
|
||||
"toggle_staged",
|
||||
_,
|
||||
_params,
|
||||
%{assigns: %{ammo_group: ammo_group, current_user: current_user}} = socket
|
||||
) do
|
||||
{:ok, ammo_group} =
|
||||
ammo_group |> Ammo.update_ammo_group(%{"staged" => !ammo_group.staged}, current_user)
|
||||
|
||||
{:noreply, socket |> assign(ammo_group: ammo_group)}
|
||||
{:noreply, socket |> display_ammo_group(ammo_group)}
|
||||
end
|
||||
|
||||
defp page_title(:add_shot_group), do: gettext("Add Shot group")
|
||||
defp page_title(:move), do: gettext("Move Ammo group")
|
||||
defp page_title(:show), do: gettext("Show Ammo group")
|
||||
defp page_title(:edit), do: gettext("Edit Ammo group")
|
||||
@impl true
|
||||
def handle_event(
|
||||
"delete_shot_group",
|
||||
%{"id" => id},
|
||||
%{assigns: %{ammo_group: ammo_group, current_user: current_user}} = socket
|
||||
) do
|
||||
{:ok, _} =
|
||||
ActivityLog.get_shot_group!(id, current_user)
|
||||
|> ActivityLog.delete_shot_group(current_user)
|
||||
|
||||
prompt = dgettext("prompts", "Shot records deleted succesfully")
|
||||
{:noreply, socket |> put_flash(:info, prompt) |> display_ammo_group(ammo_group)}
|
||||
end
|
||||
|
||||
@spec display_ammo_group(Socket.t(), AmmoGroup.t() | AmmoGroup.id()) :: Socket.t()
|
||||
defp display_ammo_group(socket, %AmmoGroup{} = ammo_group) do
|
||||
ammo_group = ammo_group |> Repo.preload([:container, :ammo_type, :shot_groups], force: true)
|
||||
|
||||
columns = [
|
||||
%{label: gettext("Rounds shot"), key: "count"},
|
||||
%{label: gettext("Notes"), key: "notes"},
|
||||
%{label: gettext("Date"), key: "date"},
|
||||
%{label: nil, key: "actions", sortable: false}
|
||||
]
|
||||
|
||||
rows =
|
||||
ammo_group.shot_groups
|
||||
|> Enum.map(fn shot_group ->
|
||||
ammo_group |> get_table_row_for_shot_group(shot_group, columns)
|
||||
end)
|
||||
|
||||
socket |> assign(ammo_group: ammo_group, columns: columns, rows: rows)
|
||||
end
|
||||
|
||||
defp display_ammo_group(%{assigns: %{current_user: current_user}} = socket, id),
|
||||
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()]
|
||||
defp get_table_row_for_shot_group(ammo_group, %{date: date} = shot_group, columns) do
|
||||
assigns = %{ammo_group: ammo_group, shot_group: shot_group}
|
||||
|
||||
columns
|
||||
|> Enum.into(%{}, fn %{key: key} ->
|
||||
value =
|
||||
case key do
|
||||
"date" ->
|
||||
{date, date |> display_date()}
|
||||
|
||||
"actions" ->
|
||||
~H"""
|
||||
<div class="px-4 py-2 space-x-4 flex justify-center items-center">
|
||||
<%= live_patch to: Routes.ammo_group_show_path(Endpoint, :edit_shot_group, @ammo_group, 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_shot_group",
|
||||
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
|
||||
end
|
||||
|
@ -9,6 +9,16 @@
|
||||
<%= @ammo_group.count %>
|
||||
</span>
|
||||
|
||||
<span class="rounded-lg title text-lg">
|
||||
<%= gettext("Original count:") %>
|
||||
<%= @ammo_group.count + Ammo.get_used_count(@ammo_group) %>
|
||||
</span>
|
||||
|
||||
<span class="rounded-lg title text-lg">
|
||||
<%= gettext("Percentage left:") %>
|
||||
<%= "#{@ammo_group |> Ammo.get_percentage_remaining()}%" %>
|
||||
</span>
|
||||
|
||||
<%= if @ammo_group.notes do %>
|
||||
<span class="rounded-lg title text-lg">
|
||||
<%= gettext("Notes:") %>
|
||||
@ -16,19 +26,33 @@
|
||||
</span>
|
||||
<% end %>
|
||||
|
||||
<span class="rounded-lg title text-lg">
|
||||
<%= gettext("Added on:") %>
|
||||
<%= @ammo_group.inserted_at |> display_datetime() %>
|
||||
</span>
|
||||
|
||||
<%= if @ammo_group.price_paid do %>
|
||||
<span class="rounded-lg title text-lg">
|
||||
<%= gettext("Price paid:") %>
|
||||
<%= gettext("Original cost:") %>
|
||||
<%= gettext("$%{amount}",
|
||||
amount: @ammo_group.price_paid |> :erlang.float_to_binary(decimals: 2)
|
||||
) %>
|
||||
</span>
|
||||
|
||||
<span class="rounded-lg title text-lg">
|
||||
<%= gettext("Current value:") %>
|
||||
<%= gettext("$%{amount}",
|
||||
amount:
|
||||
(@ammo_group.price_paid * Ammo.get_percentage_remaining(@ammo_group) / 100)
|
||||
|> :erlang.float_to_binary(decimals: 2)
|
||||
) %>
|
||||
</span>
|
||||
<% end %>
|
||||
</div>
|
||||
|
||||
<div class="flex flex-col justify-center items-center">
|
||||
<div class="flex flex-wrap justify-center items-center text-primary-600">
|
||||
<%= live_patch(dgettext("actions", "Ammo Details"),
|
||||
<%= live_patch(dgettext("actions", "View in Catalog"),
|
||||
to: Routes.ammo_type_show_path(Endpoint, :show, @ammo_group.ammo_type),
|
||||
class: "mx-4 my-2 btn btn-primary",
|
||||
data: [qa: "details"]
|
||||
@ -81,9 +105,24 @@
|
||||
|
||||
<.container_card container={@ammo_group.container} />
|
||||
<% else %>
|
||||
<%= gettext("This ammo group is not in a container") %>
|
||||
<%= gettext("This ammo is not in a container") %>
|
||||
<% end %>
|
||||
</div>
|
||||
|
||||
<%= unless @ammo_group.shot_groups |> Enum.empty?() do %>
|
||||
<hr class="mb-4 w-full" />
|
||||
|
||||
<h1 class="mb-4 px-4 py-2 text-center rounded-lg title text-xl">
|
||||
<%= gettext("Rounds used") %>
|
||||
</h1>
|
||||
|
||||
<.live_component
|
||||
module={CanneryWeb.Components.TableComponent}
|
||||
id="ammo_group_shot_groups_table"
|
||||
columns={@columns}
|
||||
rows={@rows}
|
||||
/>
|
||||
<% end %>
|
||||
</div>
|
||||
|
||||
<%= case @live_action do %>
|
||||
@ -99,6 +138,18 @@
|
||||
current_user={@current_user}
|
||||
/>
|
||||
</.modal>
|
||||
<% :edit_shot_group -> %>
|
||||
<.modal return_to={Routes.ammo_group_show_path(Endpoint, :show, @ammo_group)}>
|
||||
<.live_component
|
||||
module={CanneryWeb.RangeLive.FormComponent}
|
||||
id={@shot_group.id}
|
||||
title={@page_title}
|
||||
action={@live_action}
|
||||
shot_group={@shot_group}
|
||||
return_to={Routes.ammo_group_show_path(Endpoint, :show, @ammo_group)}
|
||||
current_user={@current_user}
|
||||
/>
|
||||
</.modal>
|
||||
<% :add_shot_group -> %>
|
||||
<.modal return_to={Routes.ammo_group_show_path(Endpoint, :show, @ammo_group)}>
|
||||
<.live_component
|
||||
@ -124,4 +175,4 @@
|
||||
/>
|
||||
</.modal>
|
||||
<% _show -> %>
|
||||
<% end %>
|
||||
<% end %>
|
||||
|
@ -13,17 +13,13 @@ defmodule CanneryWeb.AmmoTypeLive.FormComponent do
|
||||
%{:ammo_type => AmmoType.t(), :current_user => User.t(), optional(any) => any},
|
||||
Socket.t()
|
||||
) :: {:ok, Socket.t()}
|
||||
def update(%{ammo_type: ammo_type, current_user: _current_user} = assigns, socket) do
|
||||
{:ok, socket |> assign(assigns) |> assign(:changeset, Ammo.change_ammo_type(ammo_type))}
|
||||
def update(%{current_user: _current_user} = assigns, socket) do
|
||||
{:ok, socket |> assign(assigns) |> assign_changeset(%{})}
|
||||
end
|
||||
|
||||
@impl true
|
||||
def handle_event(
|
||||
"validate",
|
||||
%{"ammo_type" => ammo_type_params},
|
||||
%{assigns: %{ammo_type: ammo_type}} = socket
|
||||
) do
|
||||
{:noreply, socket |> assign(:changeset, ammo_type |> Ammo.change_ammo_type(ammo_type_params))}
|
||||
def handle_event("validate", %{"ammo_type" => ammo_type_params}, socket) do
|
||||
{:noreply, socket |> assign_changeset(ammo_type_params)}
|
||||
end
|
||||
|
||||
def handle_event(
|
||||
@ -34,6 +30,31 @@ defmodule CanneryWeb.AmmoTypeLive.FormComponent do
|
||||
save_ammo_type(socket, action, ammo_type_params)
|
||||
end
|
||||
|
||||
defp assign_changeset(
|
||||
%{assigns: %{action: action, ammo_type: ammo_type, current_user: user}} = socket,
|
||||
ammo_type_params
|
||||
) do
|
||||
changeset_action =
|
||||
case action do
|
||||
:new -> :insert
|
||||
:edit -> :update
|
||||
end
|
||||
|
||||
changeset =
|
||||
case action do
|
||||
:new -> ammo_type |> AmmoType.create_changeset(user, ammo_type_params)
|
||||
:edit -> ammo_type |> AmmoType.update_changeset(ammo_type_params)
|
||||
end
|
||||
|
||||
changeset =
|
||||
case changeset |> Changeset.apply_action(changeset_action) do
|
||||
{:ok, _data} -> changeset
|
||||
{:error, changeset} -> changeset
|
||||
end
|
||||
|
||||
socket |> assign(changeset: changeset)
|
||||
end
|
||||
|
||||
defp save_ammo_type(
|
||||
%{assigns: %{ammo_type: ammo_type, current_user: current_user, return_to: return_to}} =
|
||||
socket,
|
||||
|
@ -9,7 +9,7 @@
|
||||
phx-target={@myself}
|
||||
phx-change="validate"
|
||||
phx-submit="save"
|
||||
class="flex flex-col 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"
|
||||
>
|
||||
<%= if @changeset.action && not @changeset.valid? do %>
|
||||
<div class="invalid-feedback col-span-3 text-center">
|
||||
@ -17,11 +17,11 @@
|
||||
</div>
|
||||
<% end %>
|
||||
|
||||
<%= label(f, :name, gettext("Name"), class: "mr-4 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") %>
|
||||
<%= error_tag(f, :name, "col-span-3 text-center") %>
|
||||
|
||||
<%= label(f, :desc, gettext("Description"), class: "mr-4 title text-lg text-primary-600") %>
|
||||
<%= label(f, :desc, gettext("Description"), class: "title text-lg text-primary-600") %>
|
||||
<%= textarea(f, :desc,
|
||||
class: "text-center col-span-2 input input-primary",
|
||||
phx_hook: "MaintainAttrs"
|
||||
@ -34,50 +34,42 @@
|
||||
>
|
||||
<%= gettext("Example bullet type abbreviations") %>
|
||||
</a>
|
||||
<%= label(f, :bullet_type, gettext("Bullet type"),
|
||||
class: "mr-4 title text-lg text-primary-600"
|
||||
) %>
|
||||
<%= label(f, :bullet_type, gettext("Bullet type"), class: "title text-lg text-primary-600") %>
|
||||
<%= text_input(f, :bullet_type,
|
||||
class: "text-center col-span-2 input input-primary",
|
||||
placeholder: gettext("FMJ")
|
||||
) %>
|
||||
<%= error_tag(f, :bullet_type, "col-span-3 text-center") %>
|
||||
|
||||
<%= label(f, :bullet_core, gettext("Bullet core"),
|
||||
class: "mr-4 title text-lg text-primary-600"
|
||||
) %>
|
||||
<%= label(f, :bullet_core, gettext("Bullet core"), class: "title text-lg text-primary-600") %>
|
||||
<%= text_input(f, :bullet_core,
|
||||
class: "text-center col-span-2 input input-primary",
|
||||
placeholder: gettext("Steel")
|
||||
) %>
|
||||
<%= error_tag(f, :bullet_core, "col-span-3 text-center") %>
|
||||
|
||||
<%= label(f, :cartridge, gettext("Cartridge"), class: "mr-4 title text-lg text-primary-600") %>
|
||||
<%= label(f, :cartridge, gettext("Cartridge"), class: "title text-lg text-primary-600") %>
|
||||
<%= text_input(f, :cartridge,
|
||||
class: "text-center col-span-2 input input-primary",
|
||||
placeholder: "5.56x46mm NATO"
|
||||
) %>
|
||||
<%= error_tag(f, :cartridge, "col-span-3 text-center") %>
|
||||
|
||||
<%= label(f, :caliber, gettext("Caliber"), class: "mr-4 title text-lg text-primary-600") %>
|
||||
<%= label(f, :caliber, gettext("Caliber"), class: "title text-lg text-primary-600") %>
|
||||
<%= text_input(f, :caliber,
|
||||
class: "text-center col-span-2 input input-primary",
|
||||
placeholder: ".223"
|
||||
) %>
|
||||
<%= error_tag(f, :caliber, "col-span-3 text-center") %>
|
||||
|
||||
<%= label(f, :case_material, gettext("Case material"),
|
||||
class: "mr-4 title text-lg text-primary-600"
|
||||
) %>
|
||||
<%= label(f, :case_material, gettext("Case material"), class: "title text-lg text-primary-600") %>
|
||||
<%= text_input(f, :case_material,
|
||||
class: "text-center col-span-2 input input-primary",
|
||||
placeholder: gettext("Brass")
|
||||
) %>
|
||||
<%= error_tag(f, :case_material, "col-span-3 text-center") %>
|
||||
|
||||
<%= label(f, :jacket_type, gettext("Jacket type"),
|
||||
class: "mr-4 title text-lg text-primary-600"
|
||||
) %>
|
||||
<%= label(f, :jacket_type, gettext("Jacket type"), class: "title text-lg text-primary-600") %>
|
||||
<%= text_input(f, :jacket_type,
|
||||
class: "text-center col-span-2 input input-primary",
|
||||
placeholder: gettext("Bimetal")
|
||||
@ -85,7 +77,7 @@
|
||||
<%= error_tag(f, :case_material, "col-span-3 text-center") %>
|
||||
|
||||
<%= label(f, :muzzle_velocity, gettext("Muzzle velocity"),
|
||||
class: "mr-4 title text-lg text-primary-600"
|
||||
class: "title text-lg text-primary-600"
|
||||
) %>
|
||||
<%= number_input(f, :muzzle_velocity,
|
||||
step: "1",
|
||||
@ -94,14 +86,12 @@
|
||||
) %>
|
||||
<%= error_tag(f, :muzzle_velocity, "col-span-3 text-center") %>
|
||||
|
||||
<%= label(f, :powder_type, gettext("Powder type"),
|
||||
class: "mr-4 title text-lg text-primary-600"
|
||||
) %>
|
||||
<%= label(f, :powder_type, gettext("Powder type"), class: "title text-lg text-primary-600") %>
|
||||
<%= text_input(f, :powder_type, class: "text-center col-span-2 input input-primary") %>
|
||||
<%= error_tag(f, :powder_type, "col-span-3 text-center") %>
|
||||
|
||||
<%= label(f, :powder_grains_per_charge, gettext("Powder grains per charge"),
|
||||
class: "mr-4 title text-lg text-primary-600"
|
||||
class: "title text-lg text-primary-600"
|
||||
) %>
|
||||
<%= number_input(f, :powder_grains_per_charge,
|
||||
step: "1",
|
||||
@ -110,7 +100,7 @@
|
||||
) %>
|
||||
<%= error_tag(f, :powder_grains_per_charge, "col-span-3 text-center") %>
|
||||
|
||||
<%= label(f, :grains, gettext("Grains"), class: "mr-4 title text-lg text-primary-600") %>
|
||||
<%= label(f, :grains, gettext("Grains"), class: "title text-lg text-primary-600") %>
|
||||
<%= number_input(f, :grains,
|
||||
step: "1",
|
||||
class: "text-center col-span-2 input input-primary",
|
||||
@ -118,54 +108,48 @@
|
||||
) %>
|
||||
<%= error_tag(f, :grains, "col-span-3 text-center") %>
|
||||
|
||||
<%= label(f, :pressure, gettext("Pressure"), class: "mr-4 title text-lg text-primary-600") %>
|
||||
<%= label(f, :pressure, gettext("Pressure"), class: "title text-lg text-primary-600") %>
|
||||
<%= text_input(f, :pressure,
|
||||
class: "text-center col-span-2 input input-primary",
|
||||
placeholder: "+P"
|
||||
) %>
|
||||
<%= error_tag(f, :pressure, "col-span-3 text-center") %>
|
||||
|
||||
<%= label(f, :primer_type, gettext("Primer type"),
|
||||
class: "mr-4 title text-lg text-primary-600"
|
||||
) %>
|
||||
<%= label(f, :primer_type, gettext("Primer type"), class: "title text-lg text-primary-600") %>
|
||||
<%= text_input(f, :primer_type,
|
||||
class: "text-center col-span-2 input input-primary",
|
||||
placeholder: "Boxer"
|
||||
) %>
|
||||
<%= error_tag(f, :primer_type, "col-span-3 text-center") %>
|
||||
|
||||
<%= label(f, :firing_type, gettext("Firing type"),
|
||||
class: "mr-4 title text-lg text-primary-600"
|
||||
) %>
|
||||
<%= label(f, :firing_type, gettext("Firing type"), class: "title text-lg text-primary-600") %>
|
||||
<%= text_input(f, :firing_type,
|
||||
class: "text-center col-span-2 input input-primary",
|
||||
placeholder: "Centerfire"
|
||||
) %>
|
||||
<%= error_tag(f, :firing_type, "col-span-3 text-center") %>
|
||||
|
||||
<%= label(f, :tracer, gettext("Tracer"), class: "mr-4 title text-lg text-primary-600") %>
|
||||
<%= label(f, :tracer, gettext("Tracer"), class: "title text-lg text-primary-600") %>
|
||||
<%= checkbox(f, :tracer, class: "text-center col-span-2 checkbox") %>
|
||||
<%= error_tag(f, :tracer, "col-span-3 text-center") %>
|
||||
|
||||
<%= label(f, :incendiary, gettext("Incendiary"), class: "mr-4 title text-lg text-primary-600") %>
|
||||
<%= label(f, :incendiary, gettext("Incendiary"), class: "title text-lg text-primary-600") %>
|
||||
<%= checkbox(f, :incendiary, class: "text-center col-span-2 checkbox") %>
|
||||
<%= error_tag(f, :incendiary, "col-span-3 text-center") %>
|
||||
|
||||
<%= label(f, :blank, gettext("Blank"), class: "mr-4 title text-lg text-primary-600") %>
|
||||
<%= label(f, :blank, gettext("Blank"), class: "title text-lg text-primary-600") %>
|
||||
<%= checkbox(f, :blank, class: "text-center col-span-2 checkbox") %>
|
||||
<%= error_tag(f, :blank, "col-span-3 text-center") %>
|
||||
|
||||
<%= label(f, :corrosive, gettext("Corrosive"), class: "mr-4 title text-lg text-primary-600") %>
|
||||
<%= label(f, :corrosive, gettext("Corrosive"), class: "title text-lg text-primary-600") %>
|
||||
<%= checkbox(f, :corrosive, class: "text-center col-span-2 checkbox") %>
|
||||
<%= error_tag(f, :corrosive, "col-span-3 text-center") %>
|
||||
|
||||
<%= label(f, :manufacturer, gettext("Manufacturer"),
|
||||
class: "mr-4 title text-lg text-primary-600"
|
||||
) %>
|
||||
<%= label(f, :manufacturer, gettext("Manufacturer"), class: "title text-lg text-primary-600") %>
|
||||
<%= text_input(f, :manufacturer, class: "text-center col-span-2 input input-primary") %>
|
||||
<%= error_tag(f, :manufacturer, "col-span-3 text-center") %>
|
||||
|
||||
<%= label(f, :upc, gettext("UPC"), class: "mr-4 title text-lg text-primary-600") %>
|
||||
<%= label(f, :upc, gettext("UPC"), class: "title text-lg text-primary-600") %>
|
||||
<%= text_input(f, :upc, class: "text-center col-span-2 input input-primary") %>
|
||||
<%= error_tag(f, :upc, "col-span-3 text-center") %>
|
||||
|
||||
|
@ -9,8 +9,8 @@ defmodule CanneryWeb.AmmoTypeLive.Index do
|
||||
alias CanneryWeb.Endpoint
|
||||
|
||||
@impl true
|
||||
def mount(_params, session, socket) do
|
||||
{:ok, socket |> assign_defaults(session) |> list_ammo_types()}
|
||||
def mount(_params, _session, socket) do
|
||||
{:ok, socket |> list_ammo_types()}
|
||||
end
|
||||
|
||||
@impl true
|
||||
@ -31,7 +31,7 @@ defmodule CanneryWeb.AmmoTypeLive.Index do
|
||||
end
|
||||
|
||||
defp apply_action(socket, :index, _params) do
|
||||
socket |> assign(:page_title, gettext("Listing Ammo types")) |> assign(:ammo_type, nil)
|
||||
socket |> assign(:page_title, gettext("Ammo types")) |> assign(:ammo_type, nil)
|
||||
end
|
||||
|
||||
@impl true
|
||||
@ -46,34 +46,108 @@ defmodule CanneryWeb.AmmoTypeLive.Index do
|
||||
defp list_ammo_types(%{assigns: %{current_user: current_user}} = socket) do
|
||||
ammo_types = Ammo.list_ammo_types(current_user)
|
||||
|
||||
columns_to_display =
|
||||
columns =
|
||||
[
|
||||
{gettext("Name"), :name, :string},
|
||||
{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("Rimfire"), :rimfire, :boolean},
|
||||
{gettext("Tracer"), :tracer, :boolean},
|
||||
{gettext("Incendiary"), :incendiary, :boolean},
|
||||
{gettext("Blank"), :blank, :boolean},
|
||||
{gettext("Corrosive"), :corrosive, :boolean},
|
||||
{gettext("Manufacturer"), :manufacturer, :string},
|
||||
{gettext("UPC"), :upc, :string}
|
||||
%{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}
|
||||
]
|
||||
# filter columns to only used ones
|
||||
|> Enum.filter(fn {_label, field, _type} ->
|
||||
ammo_types |> Enum.any?(fn ammo_type -> not (ammo_type |> Map.get(field) |> is_nil()) end)
|
||||
end)
|
||||
|> Enum.filter(fn %{key: key, type: type} ->
|
||||
# remove columns if all values match defaults
|
||||
default_value = if type == :boolean, do: false, else: nil
|
||||
|
||||
socket |> assign(ammo_types: ammo_types, columns_to_display: columns_to_display)
|
||||
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
|
||||
|
||||
defp get_ammo_type_values(ammo_type, columns, current_user) do
|
||||
columns
|
||||
|> Enum.into(%{}, fn %{key: key, type: type} ->
|
||||
{key, get_ammo_type_value(type, key, ammo_type, current_user)}
|
||||
end)
|
||||
end
|
||||
|
||||
defp get_ammo_type_value(:boolean, key, ammo_type, _current_user),
|
||||
do: ammo_type |> Map.get(key |> String.to_existing_atom()) |> humanize()
|
||||
|
||||
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
|
||||
|
||||
defp get_ammo_type_value(:actions, _key, ammo_type, _current_user) do
|
||||
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
|
||||
|
||||
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
|
||||
|
@ -1,9 +1,9 @@
|
||||
<div class="flex flex-col space-y-8 justify-center items-center">
|
||||
<h1 class="title text-2xl title-primary-500">
|
||||
<%= gettext("Ammo Types") %>
|
||||
<%= gettext("Catalog") %>
|
||||
</h1>
|
||||
|
||||
<%= if @ammo_types |> Enum.empty?() do %>
|
||||
<%= if @rows |> Enum.empty?() do %>
|
||||
<h2 class="title text-xl text-primary-600">
|
||||
<%= gettext("No Ammo Types") %>
|
||||
<%= display_emoji("😔") %>
|
||||
@ -19,64 +19,13 @@
|
||||
class: "btn btn-primary"
|
||||
) %>
|
||||
|
||||
<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">
|
||||
<thead class="border-b border-primary-600">
|
||||
<tr>
|
||||
<%= for {field_name, _field, _type} <- @columns_to_display do %>
|
||||
<th class="p-2">
|
||||
<%= field_name %>
|
||||
</th>
|
||||
<% end %>
|
||||
|
||||
<th class="p-2"></th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<%= for ammo_type <- @ammo_types do %>
|
||||
<tr id={"ammo_type-#{ammo_type.id}"}>
|
||||
<%= for {_label, field, type} <- @columns_to_display do %>
|
||||
<td class="p-2">
|
||||
<%= case type do %>
|
||||
<% :boolean -> %>
|
||||
<%= ammo_type |> Map.get(field) |> humanize() %>
|
||||
<% _other -> %>
|
||||
<%= ammo_type |> Map.get(field) %>
|
||||
<% end %>
|
||||
</td>
|
||||
<% end %>
|
||||
|
||||
<td class="p-2">
|
||||
<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 this ammo?"),
|
||||
qa: "delete-#{ammo_type.id}"
|
||||
] do %>
|
||||
<i class="fa-lg fas fa-trash"></i>
|
||||
<% end %>
|
||||
</div>
|
||||
</td>
|
||||
</tr>
|
||||
<% end %>
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
<.live_component
|
||||
module={CanneryWeb.Components.TableComponent}
|
||||
id="ammo_types_index_table"
|
||||
action={@live_action}
|
||||
columns={@columns}
|
||||
rows={@rows}
|
||||
/>
|
||||
<% end %>
|
||||
</div>
|
||||
|
||||
|
@ -9,12 +9,10 @@ defmodule CanneryWeb.AmmoTypeLive.Show do
|
||||
alias CanneryWeb.Endpoint
|
||||
|
||||
@impl true
|
||||
def mount(_params, session, socket) do
|
||||
{:ok, socket |> assign_defaults(session)}
|
||||
end
|
||||
def mount(_params, _session, socket), do: {:ok, socket}
|
||||
|
||||
@impl true
|
||||
def handle_params(%{"id" => id}, _, %{assigns: %{current_user: current_user}} = socket) do
|
||||
def handle_params(%{"id" => id}, _params, %{assigns: %{current_user: current_user}} = socket) do
|
||||
ammo_type = Ammo.get_ammo_type!(id, current_user)
|
||||
|
||||
socket =
|
||||
@ -32,7 +30,7 @@ defmodule CanneryWeb.AmmoTypeLive.Show do
|
||||
@impl true
|
||||
def handle_event(
|
||||
"delete",
|
||||
_,
|
||||
_params,
|
||||
%{assigns: %{ammo_type: ammo_type, current_user: current_user}} = socket
|
||||
) do
|
||||
%{name: ammo_type_name} = ammo_type |> Ammo.delete_ammo_type!(current_user)
|
||||
|
@ -5,11 +5,9 @@
|
||||
</h1>
|
||||
|
||||
<%= if @ammo_type.desc do %>
|
||||
<span
|
||||
class="max-w-2xl w-full px-8 py-4 rounded-lg
|
||||
<span class="max-w-2xl w-full px-8 py-4 rounded-lg
|
||||
text-center title text-lg
|
||||
border border-primary-600"
|
||||
>
|
||||
border border-primary-600">
|
||||
<%= @ammo_type.desc %>
|
||||
</span>
|
||||
<% end %>
|
||||
@ -26,7 +24,7 @@
|
||||
phx_click: "delete",
|
||||
data: [
|
||||
confirm:
|
||||
dgettext("prompts", "Are you sure you want to delete %{name}?", name: @ammo_type.name),
|
||||
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"
|
||||
] do %>
|
||||
<i class="fa-fw fa-lg fas fa-trash"></i>
|
||||
@ -35,70 +33,82 @@
|
||||
|
||||
<hr class="hr" />
|
||||
|
||||
<div class="grid sm:grid-cols-2 text-center justify-center items-center">
|
||||
<%= for {field_name, field} <- [
|
||||
{gettext("Bullet type"), :bullet_type},
|
||||
{gettext("Bullet core"), :bullet_core},
|
||||
{gettext("Cartridge"), :cartridge},
|
||||
{gettext("Caliber"), :caliber},
|
||||
{gettext("Case material"), :case_material},
|
||||
{gettext("Jacket type"), :jacket_type},
|
||||
{gettext("Muzzle velocity"), :muzzle_velocity},
|
||||
{gettext("Powder type"), :powder_type},
|
||||
{gettext("Powder grains per charge"), :powder_grains_per_charge},
|
||||
{gettext("Grains"), :grains},
|
||||
{gettext("Pressure"), :pressure},
|
||||
{gettext("Primer type"), :primer_type}
|
||||
<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="mb-2 sm:mr-4 title text-lg">
|
||||
<h3 class="title text-lg">
|
||||
<%= field_name %>:
|
||||
</h3>
|
||||
|
||||
<span class="mb-4 sm:mb-2 text-primary-600">
|
||||
<%= @ammo_type |> Map.get(field) %>
|
||||
</span>
|
||||
<% end %>
|
||||
<% end %>
|
||||
|
||||
<%= for {field_name, field} <- [
|
||||
{"Rimfire", :rimfire},
|
||||
{"Tracer", :tracer},
|
||||
{"Incendiary", :incendiary},
|
||||
{"Blank", :blank},
|
||||
{"Corrosive", :corrosive}
|
||||
] do %>
|
||||
<h3 class="mb-2 sm:mr-4 title text-lg">
|
||||
<%= field_name %>:
|
||||
</h3>
|
||||
|
||||
<span class="mb-4 sm:mb-2 text-primary-600">
|
||||
<span class="text-primary-600">
|
||||
<%= case type do %>
|
||||
<% :boolean -> %>
|
||||
<%= @ammo_type |> Map.get(field) |> humanize() %>
|
||||
<% _ -> %>
|
||||
<%= @ammo_type |> Map.get(field) %>
|
||||
<% end %>
|
||||
</span>
|
||||
<% end %>
|
||||
<% end %>
|
||||
|
||||
<%= for {field_name, field} <- [{"Manufacturer", :manufacturer}, {"UPC", :upc}] do %>
|
||||
<%= if @ammo_type |> Map.get(field) do %>
|
||||
<h3 class="mb-2 sm:mr-4 title text-lg">
|
||||
<%= field_name %>:
|
||||
<h3 class="title text-lg">
|
||||
<%= gettext("Current # of rounds:") %>
|
||||
</h3>
|
||||
|
||||
<span class="mb-4 sm:mb-2 text-primary-600">
|
||||
<%= @ammo_type |> Map.get(field) %>
|
||||
<span class="text-primary-600">
|
||||
<%= @ammo_type |> Ammo.get_round_count_for_ammo_type(@current_user) %>
|
||||
</span>
|
||||
|
||||
<h3 class="title text-lg">
|
||||
<%= gettext("Total rounds shot:") %>
|
||||
</h3>
|
||||
|
||||
<span class="text-primary-600">
|
||||
<%= @ammo_type |> Ammo.get_used_count_for_ammo_type(@current_user) %>
|
||||
</span>
|
||||
|
||||
<h3 class="title text-lg">
|
||||
<%= gettext("Added on:") %>
|
||||
</h3>
|
||||
|
||||
<span class="text-primary-600">
|
||||
<%= @ammo_type.inserted_at |> display_datetime() %>
|
||||
</span>
|
||||
<% end %>
|
||||
<% end %>
|
||||
|
||||
<%= if @avg_cost_per_round do %>
|
||||
<h3 class="mb-2 sm:mr-4 title text-lg">
|
||||
<h3 class="title text-lg">
|
||||
<%= gettext("Average Price paid") %>:
|
||||
</h3>
|
||||
|
||||
<span class="mb-4 sm:mb-2 text-primary-600">
|
||||
<span class="text-primary-600">
|
||||
<%= gettext("$%{amount}",
|
||||
amount: @avg_cost_per_round |> :erlang.float_to_binary(decimals: 2)
|
||||
) %>
|
||||
</span>
|
||||
<% else %>
|
||||
<h3 class="mx-8 my-4 title text-lg text-primary-600 col-span-2">
|
||||
<%= gettext("No cost information") %>
|
||||
</h3>
|
||||
<% end %>
|
||||
</div>
|
||||
|
||||
@ -106,11 +116,16 @@
|
||||
|
||||
<div>
|
||||
<%= if @ammo_groups |> Enum.empty?() do %>
|
||||
<h2 class="mx-8 my-4 title text-lg text-primary-600">
|
||||
<%= gettext("No ammo for this type") %>
|
||||
<%= display_emoji("😔") %>
|
||||
</h2>
|
||||
<% else %>
|
||||
<div class="flex flex-wrap justify-center items-center">
|
||||
<%= for ammo_group <- @ammo_groups do %>
|
||||
<.ammo_group_card ammo_group={ammo_group} />
|
||||
<% end %>
|
||||
</div>
|
||||
<% end %>
|
||||
</div>
|
||||
</div>
|
||||
|
@ -1,51 +0,0 @@
|
||||
defmodule CanneryWeb.ContainerLive.AddTagComponent do
|
||||
@moduledoc """
|
||||
Livecomponent that can add a tag to a Container
|
||||
"""
|
||||
|
||||
use CanneryWeb, :live_component
|
||||
alias Cannery.{Accounts.User, Containers, Containers.Container, Tags, Tags.Tag}
|
||||
alias Phoenix.LiveView.Socket
|
||||
|
||||
@impl true
|
||||
@spec update(
|
||||
%{:container => Container.t(), :current_user => User.t(), optional(any) => any},
|
||||
Socket.t()
|
||||
) :: {:ok, Socket.t()}
|
||||
def update(%{container: _container, current_user: current_user} = assigns, socket) do
|
||||
{:ok, socket |> assign(assigns) |> assign(:tags, Tags.list_tags(current_user))}
|
||||
end
|
||||
|
||||
@impl true
|
||||
def handle_event(
|
||||
"save",
|
||||
%{"tag" => %{"tag_id" => tag_id}},
|
||||
%{
|
||||
assigns: %{
|
||||
tags: tags,
|
||||
container: container,
|
||||
current_user: current_user,
|
||||
return_to: return_to
|
||||
}
|
||||
} = socket
|
||||
) do
|
||||
socket =
|
||||
case tags |> Enum.find(fn %{id: id} -> tag_id == id end) do
|
||||
nil ->
|
||||
prompt = dgettext("errors", "Tag could not be added")
|
||||
socket |> put_flash(:error, prompt)
|
||||
|
||||
%{name: tag_name} = tag ->
|
||||
_container_tag = Containers.add_tag!(container, tag, current_user)
|
||||
prompt = dgettext("prompts", "%{name} added successfully", name: tag_name)
|
||||
socket |> put_flash(:info, prompt) |> push_redirect(to: return_to)
|
||||
end
|
||||
|
||||
{:noreply, socket}
|
||||
end
|
||||
|
||||
@spec tag_options([Tag.t()]) :: [{String.t(), Tag.id()}]
|
||||
defp tag_options(tags) do
|
||||
tags |> Enum.map(fn %{id: id, name: name} -> {name, id} end)
|
||||
end
|
||||
end
|
@ -1,22 +0,0 @@
|
||||
<div>
|
||||
<h2 class="mb-8 text-center title text-xl text-primary-600">
|
||||
<%= @title %>
|
||||
</h2>
|
||||
|
||||
<.form
|
||||
let={f}
|
||||
for={:tag}
|
||||
id="add-tag-to-container-form"
|
||||
class="flex flex-col sm:grid sm:grid-cols-3 sm:gap-4 justify-center items-center"
|
||||
phx-target={@myself}
|
||||
phx-submit="save"
|
||||
>
|
||||
<%= select(f, :tag_id, tag_options(@tags), class: "text-center col-span-2 input input-primary") %>
|
||||
<%= error_tag(f, :tag_id, "col-span-3 text-center") %>
|
||||
|
||||
<%= submit(dgettext("actions", "Add"),
|
||||
class: "mx-auto btn btn-primary",
|
||||
phx_disable_with: dgettext("prompts", "Adding...")
|
||||
) %>
|
||||
</.form>
|
||||
</div>
|
73
lib/cannery_web/live/container_live/edit_tags_component.ex
Normal file
73
lib/cannery_web/live/container_live/edit_tags_component.ex
Normal file
@ -0,0 +1,73 @@
|
||||
defmodule CanneryWeb.ContainerLive.EditTagsComponent do
|
||||
@moduledoc """
|
||||
Livecomponent that can add or remove a tag to a Container
|
||||
"""
|
||||
|
||||
use CanneryWeb, :live_component
|
||||
alias Cannery.{Accounts.User, Containers, Containers.Container, Repo, Tags, Tags.Tag}
|
||||
alias Phoenix.LiveView.Socket
|
||||
|
||||
@impl true
|
||||
@spec update(
|
||||
%{:container => Container.t(), :current_user => User.t(), optional(any) => any},
|
||||
Socket.t()
|
||||
) :: {:ok, Socket.t()}
|
||||
def update(%{container: container, current_user: current_user} = assigns, socket) do
|
||||
tags = Tags.list_tags(current_user)
|
||||
container = container |> Repo.preload(:tags)
|
||||
{:ok, socket |> assign(assigns) |> assign(tags: tags, container: container)}
|
||||
end
|
||||
|
||||
@impl true
|
||||
def handle_event(
|
||||
"save",
|
||||
%{"tag" => %{"tag_id" => tag_id}},
|
||||
%{assigns: %{tags: tags, container: container, current_user: current_user}} = socket
|
||||
) do
|
||||
socket =
|
||||
case tags |> Enum.find(fn %{id: id} -> tag_id == id end) do
|
||||
nil ->
|
||||
prompt = dgettext("errors", "Tag could not be added")
|
||||
socket |> put_flash(:error, prompt)
|
||||
|
||||
%{name: tag_name} = tag ->
|
||||
_container_tag = Containers.add_tag!(container, tag, current_user)
|
||||
container = container |> Repo.preload(:tags, force: true)
|
||||
prompt = dgettext("prompts", "%{name} added successfully", name: tag_name)
|
||||
socket |> put_flash(:info, prompt) |> assign(container: container)
|
||||
end
|
||||
|
||||
{:noreply, socket}
|
||||
end
|
||||
|
||||
@impl true
|
||||
def handle_event(
|
||||
"delete",
|
||||
%{"tag-id" => tag_id},
|
||||
%{assigns: %{tags: tags, container: container, current_user: current_user}} = socket
|
||||
) do
|
||||
socket =
|
||||
case tags |> Enum.find(fn %{id: id} -> tag_id == id end) do
|
||||
nil ->
|
||||
prompt = dgettext("errors", "Tag could not be removed")
|
||||
socket |> put_flash(:error, prompt)
|
||||
|
||||
%{name: tag_name} = tag ->
|
||||
_container_tag = Containers.remove_tag!(container, tag, current_user)
|
||||
container = container |> Repo.preload(:tags, force: true)
|
||||
prompt = dgettext("prompts", "%{name} removed successfully", name: tag_name)
|
||||
socket |> put_flash(:info, prompt) |> assign(container: container)
|
||||
end
|
||||
|
||||
{:noreply, socket}
|
||||
end
|
||||
|
||||
@spec tag_options([Tag.t()], Container.t()) :: [{String.t(), Tag.id()}]
|
||||
defp tag_options(tags, %Container{tags: container_tags}) do
|
||||
container_tags_map = container_tags |> Enum.map(fn %{id: id} -> id end) |> MapSet.new()
|
||||
|
||||
tags
|
||||
|> Enum.reject(fn %{id: id} -> container_tags_map |> MapSet.member?(id) end)
|
||||
|> Enum.map(fn %{id: id, name: name} -> {name, id} end)
|
||||
end
|
||||
end
|
@ -0,0 +1,58 @@
|
||||
<div class="flex flex-col justify-center items-center text-center space-y-8">
|
||||
<h2 class="title text-xl text-primary-600">
|
||||
<%= @title %>
|
||||
</h2>
|
||||
|
||||
<div class="flex flex-wrap justify-center items-center">
|
||||
<%= for tag <- @container.tags do %>
|
||||
<%= link to: "#",
|
||||
class: "mx-2 my-1 px-4 py-2 rounded-lg title text-xl",
|
||||
style: "color: #{tag.text_color}; background-color: #{tag.bg_color}",
|
||||
phx_click: "delete",
|
||||
phx_value_tag_id: tag.id,
|
||||
phx_target: @myself,
|
||||
data: [
|
||||
confirm:
|
||||
dgettext(
|
||||
"prompts",
|
||||
"Are you sure you want to remove the %{tag_name} tag from %{container_name}?",
|
||||
tag_name: tag.name,
|
||||
container_name: @container.name
|
||||
)
|
||||
] do %>
|
||||
<%= tag.name %>
|
||||
<i class="fa-fw fa-sm fas fa-trash"></i>
|
||||
<% end %>
|
||||
<% end %>
|
||||
|
||||
<%= if @container.tags |> Enum.empty?() do %>
|
||||
<h2 class="title text-xl text-primary-600">
|
||||
<%= gettext("No tags") %>
|
||||
<%= display_emoji("😔") %>
|
||||
</h2>
|
||||
<% end %>
|
||||
</div>
|
||||
|
||||
<%= unless tag_options(@tags, @container) |> Enum.empty?() do %>
|
||||
<hr class="hr" />
|
||||
|
||||
<.form
|
||||
let={f}
|
||||
for={:tag}
|
||||
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"
|
||||
phx-target={@myself}
|
||||
phx-submit="save"
|
||||
>
|
||||
<%= select(f, :tag_id, tag_options(@tags, @container),
|
||||
class: "text-center col-span-2 input input-primary"
|
||||
) %>
|
||||
<%= error_tag(f, :tag_id, "col-span-3 text-center") %>
|
||||
|
||||
<%= submit(dgettext("actions", "Add"),
|
||||
class: "mx-auto btn btn-primary",
|
||||
phx_disable_with: dgettext("prompts", "Adding...")
|
||||
) %>
|
||||
</.form>
|
||||
<% end %>
|
||||
</div>
|
@ -13,18 +13,13 @@ defmodule CanneryWeb.ContainerLive.FormComponent do
|
||||
%{:container => Container.t(), :current_user => User.t(), optional(any) => any},
|
||||
Socket.t()
|
||||
) :: {:ok, Socket.t()}
|
||||
def update(%{container: container} = assigns, socket) do
|
||||
{:ok, socket |> assign(assigns) |> assign(:changeset, Containers.change_container(container))}
|
||||
def update(%{container: _container} = assigns, socket) do
|
||||
{:ok, socket |> assign(assigns) |> assign_changeset(%{})}
|
||||
end
|
||||
|
||||
@impl true
|
||||
def handle_event(
|
||||
"validate",
|
||||
%{"container" => container_params},
|
||||
%{assigns: %{container: container}} = socket
|
||||
) do
|
||||
changeset = container |> Containers.change_container(container_params)
|
||||
{:noreply, socket |> assign(:changeset, changeset)}
|
||||
def handle_event("validate", %{"container" => container_params}, socket) do
|
||||
{:noreply, socket |> assign_changeset(container_params)}
|
||||
end
|
||||
|
||||
def handle_event(
|
||||
@ -35,6 +30,31 @@ defmodule CanneryWeb.ContainerLive.FormComponent do
|
||||
save_container(socket, action, container_params)
|
||||
end
|
||||
|
||||
defp assign_changeset(
|
||||
%{assigns: %{action: action, container: container, current_user: user}} = socket,
|
||||
container_params
|
||||
) do
|
||||
changeset_action =
|
||||
case action do
|
||||
:new -> :insert
|
||||
:edit -> :update
|
||||
end
|
||||
|
||||
changeset =
|
||||
case action do
|
||||
:new -> container |> Container.create_changeset(user, container_params)
|
||||
:edit -> container |> Container.update_changeset(container_params)
|
||||
end
|
||||
|
||||
changeset =
|
||||
case changeset |> Changeset.apply_action(changeset_action) do
|
||||
{:ok, _data} -> changeset
|
||||
{:error, changeset} -> changeset
|
||||
end
|
||||
|
||||
socket |> assign(:changeset, changeset)
|
||||
end
|
||||
|
||||
defp save_container(
|
||||
%{assigns: %{container: container, current_user: current_user, return_to: return_to}} =
|
||||
socket,
|
||||
|
@ -6,7 +6,7 @@
|
||||
let={f}
|
||||
for={@changeset}
|
||||
id="container-form"
|
||||
class="flex flex-col 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"
|
||||
phx-target={@myself}
|
||||
phx-change="validate"
|
||||
phx-submit="save"
|
||||
|
@ -5,24 +5,26 @@ defmodule CanneryWeb.ContainerLive.Index do
|
||||
|
||||
use CanneryWeb, :live_view
|
||||
import CanneryWeb.Components.ContainerCard
|
||||
alias Cannery.{Containers, Containers.Container}
|
||||
alias Cannery.{Containers, Containers.Container, Repo}
|
||||
alias CanneryWeb.Endpoint
|
||||
alias Ecto.Changeset
|
||||
|
||||
@impl true
|
||||
def mount(_params, session, socket) do
|
||||
{:ok, socket |> assign_defaults(session) |> display_containers()}
|
||||
end
|
||||
def mount(_params, _session, socket), do: {:ok, socket}
|
||||
|
||||
@impl true
|
||||
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_containers()}
|
||||
end
|
||||
|
||||
defp apply_action(%{assigns: %{current_user: current_user}} = socket, :edit, %{"id" => id}) do
|
||||
%{name: container_name} =
|
||||
container =
|
||||
Containers.get_container!(id, current_user)
|
||||
|> Repo.preload([:tags, :ammo_groups], force: true)
|
||||
|
||||
socket
|
||||
|> assign(:page_title, gettext("Edit Container"))
|
||||
|> assign(:container, Containers.get_container!(id, current_user))
|
||||
|> assign(page_title: gettext("Edit %{name}", name: container_name), container: container)
|
||||
end
|
||||
|
||||
defp apply_action(socket, :new, _params) do
|
||||
@ -30,7 +32,19 @@ defmodule CanneryWeb.ContainerLive.Index do
|
||||
end
|
||||
|
||||
defp apply_action(socket, :index, _params) do
|
||||
socket |> assign(:page_title, gettext("Listing Containers")) |> assign(:container, nil)
|
||||
socket
|
||||
|> assign(:page_title, gettext("Containers"))
|
||||
|> assign(:container, nil)
|
||||
|> display_containers()
|
||||
end
|
||||
|
||||
defp apply_action(%{assigns: %{current_user: current_user}} = socket, :edit_tags, %{"id" => id}) do
|
||||
%{name: container_name} =
|
||||
container =
|
||||
Containers.get_container!(id, current_user) |> Repo.preload([:tags, :ammo_groups])
|
||||
|
||||
page_title = gettext("Edit %{name} tags", name: container_name)
|
||||
socket |> assign(page_title: page_title, container: container)
|
||||
end
|
||||
|
||||
@impl true
|
||||
@ -70,6 +84,9 @@ defmodule CanneryWeb.ContainerLive.Index do
|
||||
end
|
||||
|
||||
defp display_containers(%{assigns: %{current_user: current_user}} = socket) do
|
||||
socket |> assign(containers: Containers.list_containers(current_user))
|
||||
containers =
|
||||
Containers.list_containers(current_user) |> Repo.preload([:tags, :ammo_groups], force: true)
|
||||
|
||||
socket |> assign(containers: containers)
|
||||
end
|
||||
end
|
||||
|
@ -20,9 +20,17 @@
|
||||
) %>
|
||||
<% end %>
|
||||
|
||||
<div class="flex flex-row flex-wrap justify-center items-center">
|
||||
<div class="max-w-full flex flex-row flex-wrap justify-center items-center">
|
||||
<%= for container <- @containers do %>
|
||||
<.container_card container={container}>
|
||||
<:tag_actions>
|
||||
<div class="mx-4 my-2">
|
||||
<%= live_patch to: Routes.container_index_path(Endpoint, :edit_tags, container),
|
||||
class: "text-primary-600 link" do %>
|
||||
<i class="fa-fw fa-lg fas fa-tags"></i>
|
||||
<% end %>
|
||||
</div>
|
||||
</:tag_actions>
|
||||
<%= live_patch to: Routes.container_index_path(Endpoint, :edit, container),
|
||||
class: "text-primary-600 link",
|
||||
data: [qa: "edit-#{container.id}"] do %>
|
||||
@ -58,3 +66,16 @@
|
||||
/>
|
||||
</.modal>
|
||||
<% end %>
|
||||
|
||||
<%= if @live_action == :edit_tags do %>
|
||||
<.modal return_to={Routes.container_index_path(Endpoint, :index)}>
|
||||
<.live_component
|
||||
module={CanneryWeb.ContainerLive.EditTagsComponent}
|
||||
id={@container.id}
|
||||
title={@page_title}
|
||||
action={@live_action}
|
||||
container={@container}
|
||||
current_user={@current_user}
|
||||
/>
|
||||
</.modal>
|
||||
<% end %>
|
||||
|
@ -11,18 +11,15 @@ defmodule CanneryWeb.ContainerLive.Show do
|
||||
alias Phoenix.LiveView.Socket
|
||||
|
||||
@impl true
|
||||
def mount(_params, session, socket) do
|
||||
{:ok, socket |> assign_defaults(session)}
|
||||
end
|
||||
def mount(_params, _session, socket), do: {:ok, socket}
|
||||
|
||||
@impl true
|
||||
def handle_params(
|
||||
%{"id" => id},
|
||||
_,
|
||||
%{assigns: %{current_user: current_user, live_action: live_action}} = socket
|
||||
_session,
|
||||
%{assigns: %{current_user: current_user}} = socket
|
||||
) do
|
||||
{:noreply,
|
||||
socket |> assign(page_title: page_title(live_action)) |> render_container(id, current_user)}
|
||||
{:noreply, socket |> render_container(id, current_user)}
|
||||
end
|
||||
|
||||
@impl true
|
||||
@ -54,7 +51,7 @@ defmodule CanneryWeb.ContainerLive.Show do
|
||||
@impl true
|
||||
def handle_event(
|
||||
"delete_container",
|
||||
_,
|
||||
_params,
|
||||
%{assigns: %{container: container, current_user: current_user}} = socket
|
||||
) do
|
||||
socket =
|
||||
@ -85,16 +82,20 @@ defmodule CanneryWeb.ContainerLive.Show do
|
||||
{:noreply, socket}
|
||||
end
|
||||
|
||||
defp page_title(:show), do: gettext("Show Container")
|
||||
defp page_title(:edit), do: gettext("Edit Container")
|
||||
defp page_title(:add_tag), do: gettext("Add Tag to Container")
|
||||
|
||||
@spec render_container(Socket.t(), Container.id(), User.t()) :: Socket.t()
|
||||
defp render_container(socket, id, current_user) do
|
||||
defp render_container(%{assigns: %{live_action: live_action}} = socket, id, current_user) do
|
||||
%{name: container_name} =
|
||||
container =
|
||||
Containers.get_container!(id, current_user)
|
||||
|> Repo.preload([:ammo_groups, :tags], force: true)
|
||||
|
||||
socket |> assign(container: container)
|
||||
page_title =
|
||||
case live_action do
|
||||
:show -> gettext("Show %{name}", name: container_name)
|
||||
:edit -> gettext("Edit %{name}", name: container_name)
|
||||
:edit_tags -> gettext("Edit %{name} tags", name: container_name)
|
||||
end
|
||||
|
||||
socket |> assign(container: container, page_title: page_title)
|
||||
end
|
||||
end
|
||||
|
@ -22,6 +22,18 @@
|
||||
</span>
|
||||
<% end %>
|
||||
|
||||
<%= 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 class="rounded-lg title text-lg">
|
||||
<%= gettext("Rounds:") %>
|
||||
<%= @container |> Containers.get_container_rounds!() %>
|
||||
</span>
|
||||
<% end %>
|
||||
|
||||
<div class="flex space-x-4 justify-center items-center text-primary-600">
|
||||
<%= live_patch to: Routes.container_show_path(Endpoint, :edit, @container),
|
||||
class: "text-primary-600 link",
|
||||
@ -51,47 +63,40 @@
|
||||
</h2>
|
||||
|
||||
<%= live_patch(dgettext("actions", "Why not add one?"),
|
||||
to: Routes.container_show_path(Endpoint, :add_tag, @container),
|
||||
to: Routes.container_show_path(Endpoint, :edit_tags, @container),
|
||||
class: "btn btn-primary"
|
||||
) %>
|
||||
</div>
|
||||
<% else %>
|
||||
<h2 class="mb-4 title text-xl text-primary-600">
|
||||
<%= gettext("Tags") %>
|
||||
</h2>
|
||||
|
||||
<div class="flex flex-wrap justify-center items-center">
|
||||
<%= for tag <- @container.tags do %>
|
||||
<.tag_card tag={tag}>
|
||||
<%= link to: "#",
|
||||
class: "text-primary-600 link",
|
||||
phx_click: "delete_tag",
|
||||
phx_value_tag_id: tag.id,
|
||||
data: [
|
||||
confirm:
|
||||
dgettext(
|
||||
"prompts",
|
||||
"Are you sure you want to remove the %{tag_name} tag from %{container_name}?",
|
||||
tag_name: tag.name,
|
||||
container_name: @container.name
|
||||
)
|
||||
] do %>
|
||||
<i class="fa-fw fa-lg fas fa-trash"></i>
|
||||
<.simple_tag_card tag={tag} />
|
||||
<% end %>
|
||||
</.tag_card>
|
||||
|
||||
<div class="mx-4 my-2">
|
||||
<%= live_patch to: Routes.container_show_path(Endpoint, :edit_tags, @container),
|
||||
class: "text-primary-600 link" do %>
|
||||
<i class="fa-fw fa-lg fas fa-tags"></i>
|
||||
<% end %>
|
||||
</div>
|
||||
</div>
|
||||
<% end %>
|
||||
|
||||
<hr class="mb-4 hr" />
|
||||
|
||||
<p>
|
||||
<div>
|
||||
<%= if @container.ammo_groups |> Enum.empty?() do %>
|
||||
<%= gettext("No ammo groups in this container") %>
|
||||
<h2 class="mx-8 my-4 title text-lg text-primary-600">
|
||||
<%= gettext("No ammo in this container") %>
|
||||
</h2>
|
||||
<% else %>
|
||||
<div class="flex flex-wrap justify-center items-center">
|
||||
<%= for ammo_group <- @container.ammo_groups do %>
|
||||
<.ammo_group_card ammo_group={ammo_group} />
|
||||
<% end %>
|
||||
</div>
|
||||
<% end %>
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<%= if @live_action in [:edit] do %>
|
||||
@ -108,10 +113,10 @@
|
||||
</.modal>
|
||||
<% end %>
|
||||
|
||||
<%= if @live_action == :add_tag do %>
|
||||
<%= if @live_action == :edit_tags do %>
|
||||
<.modal return_to={Routes.container_show_path(Endpoint, :show, @container)}>
|
||||
<.live_component
|
||||
module={CanneryWeb.ContainerLive.AddTagComponent}
|
||||
module={CanneryWeb.ContainerLive.EditTagsComponent}
|
||||
id={@container.id}
|
||||
title={@page_title}
|
||||
action={@live_action}
|
||||
|
@ -5,11 +5,13 @@ defmodule CanneryWeb.HomeLive do
|
||||
|
||||
use CanneryWeb, :live_view
|
||||
alias Cannery.Accounts
|
||||
alias CanneryWeb.Endpoint
|
||||
|
||||
@impl true
|
||||
def mount(_params, session, socket) do
|
||||
def mount(_params, _session, socket) do
|
||||
admins = Accounts.list_users_by_role(:admin)
|
||||
{:ok, socket |> assign_defaults(session) |> assign(query: "", results: %{}, admins: admins)}
|
||||
socket = socket |> assign(page_title: "Home", query: "", results: %{}, admins: admins)
|
||||
{:ok, socket}
|
||||
end
|
||||
|
||||
@impl true
|
||||
@ -23,7 +25,7 @@ defmodule CanneryWeb.HomeLive do
|
||||
%{^query => vsn} ->
|
||||
{:noreply, redirect(socket, external: "https://hexdocs.pm/#{query}/#{vsn}")}
|
||||
|
||||
_ ->
|
||||
_no_query ->
|
||||
{:noreply,
|
||||
socket
|
||||
|> put_flash(:error, "No dependencies found matching \"#{query}\"")
|
||||
@ -34,9 +36,14 @@ defmodule CanneryWeb.HomeLive do
|
||||
@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"
|
||||
>
|
||||
<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>
|
||||
@ -121,11 +128,51 @@ defmodule CanneryWeb.HomeLive do
|
||||
</p>
|
||||
</li>
|
||||
|
||||
<li class="flex flex-row justify-center space-x-2">
|
||||
<li class="flex flex-row justify-center items-center space-x-2">
|
||||
<b>Version:</b>
|
||||
<p>
|
||||
0.1.0
|
||||
</p>
|
||||
<%= 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.4</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>
|
||||
|
19
lib/cannery_web/live/init_assigns.ex
Normal file
19
lib/cannery_web/live/init_assigns.ex
Normal file
@ -0,0 +1,19 @@
|
||||
defmodule CanneryWeb.InitAssigns do
|
||||
@moduledoc """
|
||||
Ensures common `assigns` are applied to all LiveViews attaching this hook.
|
||||
"""
|
||||
import Phoenix.LiveView
|
||||
alias Cannery.Accounts
|
||||
|
||||
def on_mount(:default, _params, %{"locale" => locale, "user_token" => user_token}, socket) do
|
||||
Gettext.put_locale(locale)
|
||||
|
||||
socket =
|
||||
socket
|
||||
|> assign_new(:current_user, fn -> Accounts.get_user_by_session_token(user_token) end)
|
||||
|
||||
{:cont, socket}
|
||||
end
|
||||
|
||||
def on_mount(:default, _params, _session, socket), do: {:cont, socket}
|
||||
end
|
@ -13,23 +13,44 @@ defmodule CanneryWeb.InviteLive.FormComponent do
|
||||
%{:invite => Invite.t(), :current_user => User.t(), optional(any) => any},
|
||||
Socket.t()
|
||||
) :: {:ok, Socket.t()}
|
||||
def update(%{invite: invite} = assigns, socket) do
|
||||
{:ok, socket |> assign(assigns) |> assign(:changeset, Invites.change_invite(invite))}
|
||||
def update(%{invite: _invite} = assigns, socket) do
|
||||
{:ok, socket |> assign(assigns) |> assign_changeset(%{})}
|
||||
end
|
||||
|
||||
@impl true
|
||||
def handle_event(
|
||||
"validate",
|
||||
%{"invite" => invite_params},
|
||||
%{assigns: %{invite: invite}} = socket
|
||||
) do
|
||||
{:noreply, socket |> assign(:changeset, invite |> Invites.change_invite(invite_params))}
|
||||
def handle_event("validate", %{"invite" => invite_params}, socket) do
|
||||
{:noreply, socket |> assign_changeset(invite_params)}
|
||||
end
|
||||
|
||||
def handle_event("save", %{"invite" => invite_params}, %{assigns: %{action: action}} = socket) do
|
||||
save_invite(socket, action, invite_params)
|
||||
end
|
||||
|
||||
defp assign_changeset(
|
||||
%{assigns: %{action: action, current_user: user, invite: invite}} = socket,
|
||||
invite_params
|
||||
) do
|
||||
changeset_action =
|
||||
case action do
|
||||
:new -> :insert
|
||||
:edit -> :update
|
||||
end
|
||||
|
||||
changeset =
|
||||
case action do
|
||||
:new -> invite |> Invite.create_changeset(user, "example_token", invite_params)
|
||||
:edit -> invite |> Invite.update_changeset(invite_params)
|
||||
end
|
||||
|
||||
changeset =
|
||||
case changeset |> Changeset.apply_action(changeset_action) do
|
||||
{:ok, _data} -> changeset
|
||||
{:error, changeset} -> changeset
|
||||
end
|
||||
|
||||
socket |> assign(:changeset, changeset)
|
||||
end
|
||||
|
||||
defp save_invite(
|
||||
%{assigns: %{current_user: current_user, invite: invite, return_to: return_to}} = socket,
|
||||
:edit,
|
||||
|
@ -6,7 +6,7 @@
|
||||
let={f}
|
||||
for={@changeset}
|
||||
id="invite-form"
|
||||
class="flex flex-col 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"
|
||||
phx-target={@myself}
|
||||
phx-change="validate"
|
||||
phx-submit="save"
|
||||
@ -24,6 +24,9 @@
|
||||
<%= label(f, :uses_left, gettext("Uses left"), class: "title text-lg text-primary-600") %>
|
||||
<%= number_input(f, :uses_left, min: 0, class: "input input-primary col-span-2") %>
|
||||
<%= error_tag(f, :uses_left, "col-span-3") %>
|
||||
<span class="col-span-3 text-primary-400 italic text-center">
|
||||
<%= gettext("Leave \"Uses left\" blank to make invite unlimited") %>
|
||||
</span>
|
||||
|
||||
<%= submit(dgettext("actions", "Save"),
|
||||
class: "mx-auto btn btn-primary col-span-3",
|
||||
|
@ -10,9 +10,7 @@ defmodule CanneryWeb.InviteLive.Index do
|
||||
alias Phoenix.LiveView.JS
|
||||
|
||||
@impl true
|
||||
def mount(_params, session, socket) do
|
||||
%{assigns: %{current_user: current_user}} = socket = socket |> assign_defaults(session)
|
||||
|
||||
def mount(_params, _session, %{assigns: %{current_user: current_user}} = socket) do
|
||||
socket =
|
||||
if current_user |> Map.get(:role) == :admin do
|
||||
socket |> display_invites()
|
||||
@ -40,7 +38,7 @@ defmodule CanneryWeb.InviteLive.Index do
|
||||
end
|
||||
|
||||
defp apply_action(socket, :index, _params) do
|
||||
socket |> assign(page_title: gettext("Listing Invites"), invite: nil)
|
||||
socket |> assign(page_title: gettext("Invites"), invite: nil)
|
||||
end
|
||||
|
||||
@impl true
|
||||
@ -119,7 +117,7 @@ defmodule CanneryWeb.InviteLive.Index do
|
||||
end
|
||||
|
||||
@impl true
|
||||
def handle_event("copy_to_clipboard", _, socket) do
|
||||
def handle_event("copy_to_clipboard", _params, socket) do
|
||||
prompt = dgettext("prompts", "Copied to clipboard")
|
||||
{:noreply, socket |> put_flash(:info, prompt)}
|
||||
end
|
||||
|
@ -3,20 +3,9 @@ defmodule CanneryWeb.LiveHelpers do
|
||||
Contains common helper functions for liveviews
|
||||
"""
|
||||
|
||||
import Phoenix.LiveView
|
||||
import Phoenix.LiveView.Helpers
|
||||
alias Cannery.Accounts
|
||||
alias Phoenix.LiveView.JS
|
||||
|
||||
def assign_defaults(socket, %{"user_token" => user_token} = _session) do
|
||||
socket
|
||||
|> assign_new(:current_user, fn -> Accounts.get_user_by_session_token(user_token) end)
|
||||
end
|
||||
|
||||
def assign_defaults(socket, _session) do
|
||||
socket
|
||||
end
|
||||
|
||||
@doc """
|
||||
Renders a live component inside a modal.
|
||||
|
||||
@ -38,32 +27,43 @@ defmodule CanneryWeb.LiveHelpers do
|
||||
"""
|
||||
def modal(assigns) do
|
||||
~H"""
|
||||
<%= live_patch to: @return_to,
|
||||
id: "modal-bg",
|
||||
class:
|
||||
"fade-in fixed z-10 left-0 top-0
|
||||
w-full h-full overflow-hidden
|
||||
p-8 flex flex-col justify-center items-center cursor-auto",
|
||||
style: "background-color: rgba(0,0,0,0.4);",
|
||||
phx_remove: hide_modal()
|
||||
do %>
|
||||
<span class="hidden"></span>
|
||||
<% end %>
|
||||
|
||||
<div
|
||||
id="modal"
|
||||
class="fade-in fixed z-10 left-0 top-0
|
||||
class="fixed z-10 left-0 top-0 pointer-events-none
|
||||
w-full h-full overflow-hidden
|
||||
p-8 flex flex-col justify-center items-center"
|
||||
style="opacity: 1 !important; background-color: rgba(0,0,0,0.4);"
|
||||
phx-remove={hide_modal()}
|
||||
p-4 sm:p-8 flex flex-col justify-center items-center"
|
||||
>
|
||||
<div
|
||||
id="modal-content"
|
||||
class="fade-in-scale w-full max-w-3xl max-h-128 relative overflow-y-auto
|
||||
class="fade-in-scale w-full max-w-3xl relative
|
||||
pointer-events-auto overflow-hidden
|
||||
px-8 py-4 sm:py-8 flex flex-col justify-center items-center
|
||||
flex flex-col justify-start items-center
|
||||
bg-white border-2 rounded-lg"
|
||||
phx-click-away={hide_modal()}
|
||||
phx-window-keydown={hide_modal()}
|
||||
phx-key="escape"
|
||||
>
|
||||
<%= live_patch to: @return_to,
|
||||
id: "close",
|
||||
class:
|
||||
"absolute top-8 right-10 text-gray-500 hover:text-gray-800 transition-all duration-500 ease-in-out",
|
||||
phx_click: hide_modal() do %>
|
||||
"absolute top-8 right-10
|
||||
text-gray-500 hover:text-gray-800
|
||||
transition-all duration-500 ease-in-out",
|
||||
phx_remove: hide_modal() do %>
|
||||
<i class="fa-fw fa-lg fas fa-times"></i>
|
||||
<% end %>
|
||||
|
||||
<div class="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) %>
|
||||
</div>
|
||||
</div>
|
||||
@ -74,6 +74,7 @@ defmodule CanneryWeb.LiveHelpers do
|
||||
def hide_modal(js \\ %JS{}) do
|
||||
js
|
||||
|> JS.hide(to: "#modal", transition: "fade-out")
|
||||
|> JS.hide(to: "#modal-bg", transition: "fade-out")
|
||||
|> JS.hide(to: "#modal-content", transition: "fade-out-scale")
|
||||
end
|
||||
end
|
||||
|
@ -1,6 +1,6 @@
|
||||
defmodule CanneryWeb.RangeLive.FormComponent do
|
||||
@moduledoc """
|
||||
Livecomponent that can update or create a ShotGroup
|
||||
Livecomponent that can update a ShotGroup
|
||||
"""
|
||||
|
||||
use CanneryWeb, :live_component
|
||||
@ -24,7 +24,7 @@ defmodule CanneryWeb.RangeLive.FormComponent do
|
||||
} = assigns,
|
||||
socket
|
||||
) do
|
||||
changeset = shot_group |> ActivityLog.change_shot_group()
|
||||
changeset = shot_group |> ShotGroup.update_changeset(current_user, %{})
|
||||
ammo_group = Ammo.get_ammo_group!(ammo_group_id, current_user)
|
||||
{:ok, socket |> assign(assigns) |> assign(ammo_group: ammo_group, changeset: changeset)}
|
||||
end
|
||||
@ -33,11 +33,11 @@ defmodule CanneryWeb.RangeLive.FormComponent do
|
||||
def handle_event(
|
||||
"validate",
|
||||
%{"shot_group" => shot_group_params},
|
||||
%{assigns: %{shot_group: shot_group}} = socket
|
||||
%{assigns: %{current_user: current_user, shot_group: shot_group}} = socket
|
||||
) do
|
||||
changeset =
|
||||
shot_group
|
||||
|> ActivityLog.change_shot_group(shot_group_params)
|
||||
|> ShotGroup.update_changeset(current_user, shot_group_params)
|
||||
|> Map.put(:action, :validate)
|
||||
|
||||
{:noreply, assign(socket, :changeset, changeset)}
|
||||
|
@ -7,7 +7,7 @@
|
||||
let={f}
|
||||
for={@changeset}
|
||||
id="shot-group-form"
|
||||
class="flex flex-col 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"
|
||||
phx-target={@myself}
|
||||
phx-change="validate"
|
||||
phx-submit="save"
|
||||
|
@ -10,9 +10,7 @@ defmodule CanneryWeb.RangeLive.Index do
|
||||
alias Phoenix.LiveView.Socket
|
||||
|
||||
@impl true
|
||||
def mount(_params, session, socket) do
|
||||
{:ok, socket |> assign_defaults(session) |> display_shot_groups()}
|
||||
end
|
||||
def mount(_params, _session, socket), do: {:ok, socket |> display_shot_groups()}
|
||||
|
||||
@impl true
|
||||
def handle_params(params, _url, %{assigns: %{live_action: live_action}} = socket) do
|
||||
@ -25,7 +23,7 @@ defmodule CanneryWeb.RangeLive.Index do
|
||||
%{"id" => id}
|
||||
) do
|
||||
socket
|
||||
|> assign(:page_title, gettext("Record shots"))
|
||||
|> assign(:page_title, gettext("Record Shots"))
|
||||
|> assign(:ammo_group, Ammo.get_ammo_group!(id, current_user))
|
||||
end
|
||||
|
||||
@ -67,7 +65,7 @@ defmodule CanneryWeb.RangeLive.Index do
|
||||
{:ok, _ammo_group} =
|
||||
ammo_group |> Ammo.update_ammo_group(%{"staged" => !ammo_group.staged}, current_user)
|
||||
|
||||
prompt = dgettext("prompts", "Ammo group unstaged succesfully")
|
||||
prompt = dgettext("prompts", "Ammo unstaged succesfully")
|
||||
{:noreply, socket |> put_flash(:info, prompt) |> display_shot_groups()}
|
||||
end
|
||||
|
||||
@ -77,6 +75,69 @@ defmodule CanneryWeb.RangeLive.Index do
|
||||
ActivityLog.list_shot_groups(current_user) |> Repo.preload(ammo_group: :ammo_type)
|
||||
|
||||
ammo_groups = Ammo.list_staged_ammo_groups(current_user)
|
||||
socket |> assign(shot_groups: shot_groups, ammo_groups: ammo_groups)
|
||||
|
||||
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
|
||||
|> assign(ammo_groups: ammo_groups, columns: columns, rows: rows, shot_groups: shot_groups)
|
||||
end
|
||||
|
||||
@spec get_row_data_for_shot_group(ShotGroup.t(), [map()]) :: [map()]
|
||||
defp get_row_data_for_shot_group(%{date: date} = shot_group, columns) do
|
||||
shot_group = shot_group |> Repo.preload(ammo_group: :ammo_type)
|
||||
assigns = %{shot_group: shot_group}
|
||||
|
||||
columns
|
||||
|> Enum.into(%{}, fn %{key: key} ->
|
||||
value =
|
||||
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
|
||||
end
|
||||
|
@ -53,70 +53,12 @@
|
||||
<%= gettext("Shot log") %>
|
||||
</h1>
|
||||
|
||||
<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">
|
||||
<thead class="border-b border-primary-600">
|
||||
<tr>
|
||||
<th class="p-2">
|
||||
<%= gettext("Ammo") %>
|
||||
</th>
|
||||
<th class="p-2">
|
||||
<%= gettext("Rounds shot") %>
|
||||
</th>
|
||||
<th class="p-2">
|
||||
<%= gettext("Notes") %>
|
||||
</th>
|
||||
<th class="p-2">
|
||||
<%= gettext("Date") %>
|
||||
</th>
|
||||
|
||||
<th class="p-2"></th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody id="shot_groups">
|
||||
<%= for shot_group <- @shot_groups do %>
|
||||
<tr id={"shot_group-#{shot_group.id}"}>
|
||||
<td class="p-2">
|
||||
<%= live_patch(shot_group.ammo_group.ammo_type.name,
|
||||
to: Routes.ammo_group_show_path(Endpoint, :show, shot_group.ammo_group),
|
||||
class: "link"
|
||||
) %>
|
||||
</td>
|
||||
<td class="p-2">
|
||||
<%= shot_group.count %>
|
||||
</td>
|
||||
<td class="p-2">
|
||||
<%= shot_group.notes %>
|
||||
</td>
|
||||
<td class="p-2">
|
||||
<%= shot_group.date |> display_date() %>
|
||||
</td>
|
||||
|
||||
<td class="p-2 w-full h-full space-x-2 flex justify-center items-center">
|
||||
<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>
|
||||
</td>
|
||||
</tr>
|
||||
<% end %>
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
<.live_component
|
||||
module={CanneryWeb.Components.TableComponent}
|
||||
id="shot_groups_index_table"
|
||||
columns={@columns}
|
||||
rows={@rows}
|
||||
/>
|
||||
<% end %>
|
||||
</div>
|
||||
|
||||
|
@ -12,19 +12,44 @@ defmodule CanneryWeb.TagLive.FormComponent do
|
||||
@impl true
|
||||
@spec update(%{:tag => Tag.t(), :current_user => User.t(), optional(any) => any}, Socket.t()) ::
|
||||
{:ok, Socket.t()}
|
||||
def update(%{tag: tag} = assigns, socket) do
|
||||
{:ok, socket |> assign(assigns) |> assign(:changeset, Tags.change_tag(tag))}
|
||||
def update(%{tag: _tag} = assigns, socket) do
|
||||
{:ok, socket |> assign(assigns) |> assign_changeset(%{})}
|
||||
end
|
||||
|
||||
@impl true
|
||||
def handle_event("validate", %{"tag" => tag_params}, %{assigns: %{tag: tag}} = socket) do
|
||||
{:noreply, socket |> assign(:changeset, tag |> Tags.change_tag(tag_params))}
|
||||
def handle_event("validate", %{"tag" => tag_params}, socket) do
|
||||
{:noreply, socket |> assign_changeset(tag_params)}
|
||||
end
|
||||
|
||||
def handle_event("save", %{"tag" => tag_params}, %{assigns: %{action: action}} = socket) do
|
||||
save_tag(socket, action, tag_params)
|
||||
end
|
||||
|
||||
defp assign_changeset(
|
||||
%{assigns: %{action: action, current_user: user, tag: tag}} = socket,
|
||||
tag_params
|
||||
) do
|
||||
changeset_action =
|
||||
case action do
|
||||
:new -> :insert
|
||||
:edit -> :update
|
||||
end
|
||||
|
||||
changeset =
|
||||
case action do
|
||||
:new -> tag |> Tag.create_changeset(user, tag_params)
|
||||
:edit -> tag |> Tag.update_changeset(tag_params)
|
||||
end
|
||||
|
||||
changeset =
|
||||
case changeset |> Changeset.apply_action(changeset_action) do
|
||||
{:ok, _data} -> changeset
|
||||
{:error, changeset} -> changeset
|
||||
end
|
||||
|
||||
socket |> assign(:changeset, changeset)
|
||||
end
|
||||
|
||||
@impl true
|
||||
def render(assigns) do
|
||||
~H"""
|
||||
@ -36,7 +61,7 @@ defmodule CanneryWeb.TagLive.FormComponent do
|
||||
let={f}
|
||||
for={@changeset}
|
||||
id="tag-form"
|
||||
class="flex flex-col 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"
|
||||
phx-target={@myself}
|
||||
phx-change="validate"
|
||||
phx-submit="save"
|
||||
|
@ -9,13 +9,11 @@ defmodule CanneryWeb.TagLive.Index do
|
||||
alias CanneryWeb.Endpoint
|
||||
|
||||
@impl true
|
||||
def mount(_params, session, socket) do
|
||||
{:ok, socket |> assign_defaults(session) |> display_tags()}
|
||||
end
|
||||
def mount(_params, _session, socket), do: {:ok, socket |> display_tags()}
|
||||
|
||||
@impl true
|
||||
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
|
||||
|
||||
defp apply_action(%{assigns: %{current_user: current_user}} = socket, :edit, %{"id" => id}) do
|
||||
@ -31,7 +29,7 @@ defmodule CanneryWeb.TagLive.Index do
|
||||
end
|
||||
|
||||
defp apply_action(socket, :index, _params) do
|
||||
socket |> assign(:page_title, gettext("Listing Tags")) |> assign(:tag, nil)
|
||||
socket |> assign(:page_title, gettext("Tags")) |> assign(:tag, nil)
|
||||
end
|
||||
|
||||
@impl true
|
||||
|
@ -11,6 +11,17 @@ defmodule CanneryWeb.Router do
|
||||
plug :protect_from_forgery
|
||||
plug :put_secure_browser_headers
|
||||
plug :fetch_current_user
|
||||
plug :put_user_locale, default: Application.get_env(:gettext, :default_locale, "en_US")
|
||||
end
|
||||
|
||||
defp put_user_locale(%{assigns: %{current_user: %{locale: locale}}} = conn, default: default) do
|
||||
Gettext.put_locale(locale || default)
|
||||
conn |> put_session(:locale, locale || default)
|
||||
end
|
||||
|
||||
defp put_user_locale(conn, default: default) do
|
||||
Gettext.put_locale(default)
|
||||
conn |> put_session(:locale, default)
|
||||
end
|
||||
|
||||
pipeline :require_admin do
|
||||
@ -54,31 +65,33 @@ defmodule CanneryWeb.Router do
|
||||
live "/tags/new", TagLive.Index, :new
|
||||
live "/tags/:id/edit", TagLive.Index, :edit
|
||||
|
||||
live "/ammo_types", AmmoTypeLive.Index, :index
|
||||
live "/ammo_types/new", AmmoTypeLive.Index, :new
|
||||
live "/ammo_types/:id/edit", AmmoTypeLive.Index, :edit
|
||||
live "/catalog", AmmoTypeLive.Index, :index
|
||||
live "/catalog/new", AmmoTypeLive.Index, :new
|
||||
live "/catalog/:id/edit", AmmoTypeLive.Index, :edit
|
||||
|
||||
live "/ammo_types/:id", AmmoTypeLive.Show, :show
|
||||
live "/ammo_types/:id/show/edit", AmmoTypeLive.Show, :edit
|
||||
live "/catalog/:id", AmmoTypeLive.Show, :show
|
||||
live "/catalog/:id/show/edit", AmmoTypeLive.Show, :edit
|
||||
|
||||
live "/containers", ContainerLive.Index, :index
|
||||
live "/containers/new", ContainerLive.Index, :new
|
||||
live "/containers/:id/edit", ContainerLive.Index, :edit
|
||||
live "/containers/:id/edit_tags", ContainerLive.Index, :edit_tags
|
||||
|
||||
live "/containers/:id", ContainerLive.Show, :show
|
||||
live "/containers/:id/show/edit", ContainerLive.Show, :edit
|
||||
live "/containers/:id/show/add_tag", ContainerLive.Show, :add_tag
|
||||
live "/containers/:id/show/edit_tags", ContainerLive.Show, :edit_tags
|
||||
|
||||
live "/ammo_groups", AmmoGroupLive.Index, :index
|
||||
live "/ammo_groups/new", AmmoGroupLive.Index, :new
|
||||
live "/ammo_groups/:id/edit", AmmoGroupLive.Index, :edit
|
||||
live "/ammo_groups/:id/add_shot_group", AmmoGroupLive.Index, :add_shot_group
|
||||
live "/ammo_groups/:id/move", AmmoGroupLive.Index, :move
|
||||
live "/ammo", AmmoGroupLive.Index, :index
|
||||
live "/ammo/new", AmmoGroupLive.Index, :new
|
||||
live "/ammo/:id/edit", AmmoGroupLive.Index, :edit
|
||||
live "/ammo/:id/add_shot_group", AmmoGroupLive.Index, :add_shot_group
|
||||
live "/ammo/:id/move", AmmoGroupLive.Index, :move
|
||||
|
||||
live "/ammo_groups/:id", AmmoGroupLive.Show, :show
|
||||
live "/ammo_groups/:id/show/edit", AmmoGroupLive.Show, :edit
|
||||
live "/ammo_groups/:id/show/add_shot_group", AmmoGroupLive.Show, :add_shot_group
|
||||
live "/ammo_groups/:id/show/move", AmmoGroupLive.Show, :move
|
||||
live "/ammo/:id", AmmoGroupLive.Show, :show
|
||||
live "/ammo/:id/show/edit", AmmoGroupLive.Show, :edit
|
||||
live "/ammo/:id/show/add_shot_group", AmmoGroupLive.Show, :add_shot_group
|
||||
live "/ammo/:id/show/move", AmmoGroupLive.Show, :move
|
||||
live "/ammo/:id/show/:shot_group_id/edit", AmmoGroupLive.Show, :edit_shot_group
|
||||
|
||||
live "/range", RangeLive.Index, :index
|
||||
live "/range/:id/edit", RangeLive.Index, :edit
|
||||
|
@ -8,7 +8,8 @@
|
||||
<%= dgettext("errors", "Error") %>| Cannery
|
||||
</title>
|
||||
<link rel="stylesheet" href="/css/app.css" />
|
||||
<script defer type="text/javascript" src="/js/app.js"></script>
|
||||
<script defer type="text/javascript" src="/js/app.js">
|
||||
</script>
|
||||
</head>
|
||||
<body class="pb-8 m-0 p-0 w-full h-full">
|
||||
<header>
|
||||
@ -16,9 +17,7 @@
|
||||
</header>
|
||||
|
||||
<div class="pb-8 w-full flex flex-col justify-center items-center text-center">
|
||||
<div
|
||||
class="p-8 sm:p-16 w-full flex flex-col justify-center items-center space-y-4 max-w-3xl"
|
||||
>
|
||||
<div class="p-8 sm:p-16 w-full flex flex-col justify-center items-center space-y-4 max-w-3xl">
|
||||
<h1 class="title text-primary-600 text-3xl">
|
||||
<%= @error_string %>
|
||||
</h1>
|
||||
|
@ -4,14 +4,10 @@
|
||||
<%= @email.subject %>
|
||||
</title>
|
||||
</head>
|
||||
<body
|
||||
style="padding: 2em; color: rgb(31, 31, 31); background-color: rgb(220, 220, 228); font-family: 'Lucida Sans', 'Lucida Sans Regular', 'Lucida Grande', 'Lucida Sans Unicode', Geneva, Verdana, sans-serif; text-align: center;"
|
||||
>
|
||||
<body style="padding: 2em; color: rgb(31, 31, 31); background-color: rgb(220, 220, 228); font-family: 'Lucida Sans', 'Lucida Sans Regular', 'Lucida Grande', 'Lucida Sans Unicode', Geneva, Verdana, sans-serif; text-align: center;">
|
||||
<%= @inner_content %>
|
||||
|
||||
<hr
|
||||
style="margin: 2em auto; border-width: 1px; border-color: rgb(212, 212, 216); width: 100%; max-width: 42rem;"
|
||||
/>
|
||||
<hr style="margin: 2em auto; border-width: 1px; border-color: rgb(212, 212, 216); width: 100%; max-width: 42rem;" />
|
||||
|
||||
<a style="color: rgb(31, 31, 31);" href={Routes.live_url(Endpoint, HomeLive)}>
|
||||
<%= dgettext(
|
||||
|
@ -29,7 +29,7 @@
|
||||
|
||||
<div
|
||||
id="loading"
|
||||
class="absolute opacity-0 top-0 left-0 w-screen h-screen bg-white z-50
|
||||
class="fixed opacity-0 top-0 left-0 w-screen h-screen bg-white z-50
|
||||
flex flex-col justify-center items-center space-y-4
|
||||
transition-opacity ease-in-out duration-500"
|
||||
>
|
||||
@ -37,12 +37,12 @@
|
||||
<%= gettext("Loading...") %>
|
||||
</h1>
|
||||
|
||||
<i class="fas fa-3x fa-spin fa-cog"></i>
|
||||
<i class="fas fa-3x fa-spin fa-gear"></i>
|
||||
</div>
|
||||
|
||||
<div
|
||||
id="disconnect"
|
||||
class="absolute opacity-0 top-0 left-0 w-screen h-screen bg-white z-50
|
||||
class="fixed opacity-0 top-0 left-0 w-screen h-screen bg-white z-50
|
||||
flex flex-col justify-center items-center space-y-4
|
||||
transition-opacity ease-in-out duration-500"
|
||||
>
|
||||
|
@ -1,11 +1,12 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en" class="m-0 p-0 w-full h-full">
|
||||
<html lang="en" class="m-0 p-0 w-full h-full bg-white">
|
||||
<head>
|
||||
<meta charset="utf-8" />
|
||||
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<%= csrf_meta_tag() %>
|
||||
<%= live_title_tag(assigns[:page_title] || "Cannery", suffix: "") %>
|
||||
<%= if(assigns |> Map.has_key?(:page_title), do: @page_title, else: "Cannery")
|
||||
|> live_title_tag(suffix: " | Cannery") %>
|
||||
<link phx-track-static rel="stylesheet" href={Routes.static_path(@conn, "/css/app.css")} />
|
||||
<script
|
||||
defer
|
||||
@ -16,7 +17,7 @@
|
||||
</script>
|
||||
</head>
|
||||
|
||||
<body class="m-0 p-0 w-full h-full">
|
||||
<body class="m-0 p-0 w-full h-full bg-white">
|
||||
<%= @inner_content %>
|
||||
</body>
|
||||
</html>
|
||||
|
@ -7,7 +7,7 @@
|
||||
Routes.user_confirmation_path(@conn, :create),
|
||||
[
|
||||
class:
|
||||
"flex flex-col sm:grid sm:grid-cols-3 sm:gap-4 justify-center items-center"
|
||||
"flex flex-col space-y-4 sm:space-y-0 sm:grid sm:grid-cols-3 sm:gap-4 justify-center items-center"
|
||||
],
|
||||
fn f -> %>
|
||||
<%= label(f, :email, class: "title text-lg text-primary-600") %>
|
||||
|
@ -7,7 +7,7 @@
|
||||
Routes.user_registration_path(@conn, :create),
|
||||
[
|
||||
class:
|
||||
"flex flex-col sm:grid sm:grid-cols-3 sm:gap-4 justify-center items-center"
|
||||
"flex flex-col space-y-4 sm:space-y-0 sm:grid sm:grid-cols-3 sm:gap-4 justify-center items-center"
|
||||
],
|
||||
fn f -> %>
|
||||
<%= if @changeset.action && not @changeset.valid? do %>
|
||||
@ -30,6 +30,15 @@
|
||||
<%= password_input(f, :password, required: true, class: "input input-primary col-span-2") %>
|
||||
<%= error_tag(f, :password, "col-span-3") %>
|
||||
|
||||
<%= label(f, :locale, gettext("Language"), class: "title text-lg text-primary-600") %>
|
||||
<%= select(
|
||||
f,
|
||||
:locale,
|
||||
[{gettext("English"), "en_US"}, {gettext("German"), "de"}, {gettext("French"), "fr"}],
|
||||
class: "input input-primary col-span-2"
|
||||
) %>
|
||||
<%= error_tag(f, :locale) %>
|
||||
|
||||
<%= submit(dgettext("actions", "Register"), class: "mx-auto btn btn-primary col-span-3") %>
|
||||
<% end %>
|
||||
|
||||
|
@ -7,7 +7,7 @@
|
||||
Routes.user_reset_password_path(@conn, :update, @token),
|
||||
[
|
||||
class:
|
||||
"flex flex-col sm:grid sm:grid-cols-3 sm:gap-4 justify-center items-center"
|
||||
"flex flex-col space-y-4 sm:space-y-0 sm:grid sm:grid-cols-3 sm:gap-4 justify-center items-center"
|
||||
],
|
||||
fn f -> %>
|
||||
<%= if @changeset.action && not @changeset.valid? do %>
|
||||
|
@ -7,7 +7,7 @@
|
||||
Routes.user_reset_password_path(@conn, :create),
|
||||
[
|
||||
class:
|
||||
"flex flex-col sm:grid sm:grid-cols-3 sm:gap-4 justify-center items-center"
|
||||
"flex flex-col space-y-4 sm:space-y-0 sm:grid sm:grid-cols-3 sm:gap-4 justify-center items-center"
|
||||
],
|
||||
fn f -> %>
|
||||
<%= label(f, :email, class: "title text-lg text-primary-600") %>
|
||||
|
@ -8,7 +8,7 @@
|
||||
[
|
||||
as: :user,
|
||||
class:
|
||||
"flex flex-col sm:grid sm:grid-cols-3 sm:gap-4 justify-center items-center"
|
||||
"flex flex-col space-y-4 sm:space-y-0 sm:grid sm:grid-cols-3 sm:gap-4 justify-center items-center"
|
||||
],
|
||||
fn f -> %>
|
||||
<%= if @error_message do %>
|
||||
|
@ -1,17 +1,16 @@
|
||||
<div class="mx-auto mb-8 max-w-2xl flex flex-col justify-center items-center space-y-4">
|
||||
<div class="mx-auto mb-8 max-w-2xl flex flex-col justify-center items-center text-center space-y-4">
|
||||
<h1 class="pb-4 title text-primary-600 text-xl">
|
||||
<%= gettext("Settings") %>
|
||||
</h1>
|
||||
|
||||
<hr class="hr" />
|
||||
|
||||
<%= form_for @email_changeset,
|
||||
Routes.user_settings_path(@conn, :update),
|
||||
[
|
||||
class:
|
||||
"pb-4 text-center flex flex-col sm:grid sm:grid-cols-3 sm:gap-4 justify-center items-center"
|
||||
],
|
||||
fn f -> %>
|
||||
<.form
|
||||
let={f}
|
||||
for={@email_changeset}
|
||||
action={Routes.user_settings_path(@conn, :update)}
|
||||
class="flex flex-col space-y-4 sm:space-y-0 sm:grid sm:grid-cols-3 sm:gap-4 justify-center items-center"
|
||||
>
|
||||
<h3 class="title text-primary-600 text-lg col-span-3">
|
||||
<%= dgettext("actions", "Change email") %>
|
||||
</h3>
|
||||
@ -45,17 +44,16 @@
|
||||
<%= submit(dgettext("actions", "Change email"),
|
||||
class: "mx-auto btn btn-primary col-span-3"
|
||||
) %>
|
||||
<% end %>
|
||||
</.form>
|
||||
|
||||
<hr class="hr" />
|
||||
|
||||
<%= form_for @password_changeset,
|
||||
Routes.user_settings_path(@conn, :update),
|
||||
[
|
||||
class:
|
||||
"pb-4 text-center flex flex-col sm:grid sm:grid-cols-3 sm:gap-4 justify-center items-center"
|
||||
],
|
||||
fn f -> %>
|
||||
<.form
|
||||
let={f}
|
||||
for={@password_changeset}
|
||||
action={Routes.user_settings_path(@conn, :update)}
|
||||
class="flex flex-col space-y-4 sm:space-y-0 sm:grid sm:grid-cols-3 sm:gap-4 justify-center items-center"
|
||||
>
|
||||
<h3 class="title text-primary-600 text-lg col-span-3">
|
||||
<%= dgettext("actions", "Change password") %>
|
||||
</h3>
|
||||
@ -101,8 +99,44 @@
|
||||
<%= submit(dgettext("actions", "Change password"),
|
||||
class: "mx-auto btn btn-primary col-span-3"
|
||||
) %>
|
||||
</.form>
|
||||
|
||||
<hr class="hr" />
|
||||
|
||||
<.form
|
||||
let={f}
|
||||
for={@locale_changeset}
|
||||
action={Routes.user_settings_path(@conn, :update)}
|
||||
class="flex flex-col space-y-4 justify-center items-center"
|
||||
>
|
||||
<h3 class="title text-primary-600 text-lg">
|
||||
<%= dgettext("actions", "Change Language") %>
|
||||
</h3>
|
||||
|
||||
<%= if @locale_changeset.action && not @locale_changeset.valid? do %>
|
||||
<div class="alert alert-danger">
|
||||
<p>
|
||||
<%= dgettext("errors", "Oops, something went wrong! Please check the errors below.") %>
|
||||
</p>
|
||||
</div>
|
||||
<% end %>
|
||||
|
||||
<%= hidden_input(f, :action, name: "action", value: "update_locale") %>
|
||||
|
||||
<%= select(
|
||||
f,
|
||||
:locale,
|
||||
[{gettext("English"), "en_US"}, {gettext("German"), "de"}, {gettext("French"), "fr"}],
|
||||
class: "mx-2 my-1 min-w-md input input-primary"
|
||||
) %>
|
||||
<%= error_tag(f, :locale) %>
|
||||
|
||||
<%= submit(dgettext("actions", "Change language"),
|
||||
class: "whitespace-nowrap mx-auto btn btn-primary",
|
||||
data: [qa: dgettext("prompts", "Are you sure you want to change your language?")]
|
||||
) %>
|
||||
</.form>
|
||||
|
||||
<hr class="hr" />
|
||||
|
||||
<%= link(dgettext("actions", "Delete User"),
|
||||
|
@ -2,7 +2,6 @@ defmodule CanneryWeb.EmailView do
|
||||
@moduledoc """
|
||||
A view for email-related helper functions
|
||||
"""
|
||||
alias CanneryWeb.{Endpoint, HomeLive}
|
||||
|
||||
use CanneryWeb, :view
|
||||
alias CanneryWeb.{Endpoint, HomeLive}
|
||||
end
|
||||
|
@ -8,7 +8,7 @@ defmodule CanneryWeb.ErrorView do
|
||||
case error_path do
|
||||
"404.html" -> dgettext("errors", "Not found")
|
||||
"401.html" -> dgettext("errors", "Unauthorized")
|
||||
_ -> dgettext("errors", "Internal Server Error")
|
||||
_other_template -> dgettext("errors", "Internal Server Error")
|
||||
end
|
||||
|
||||
render("error.html", %{error_string: error_string})
|
||||
|
@ -7,6 +7,8 @@ defmodule CanneryWeb.ViewHelpers do
|
||||
|
||||
import Phoenix.LiveView.Helpers
|
||||
|
||||
@id_length 16
|
||||
|
||||
@doc """
|
||||
Returns a <time> element that renders the naivedatetime in the user's local
|
||||
timezone with Alpine.js
|
||||
@ -16,11 +18,12 @@ defmodule CanneryWeb.ViewHelpers do
|
||||
|
||||
def display_datetime(datetime) do
|
||||
assigns = %{
|
||||
id: :crypto.strong_rand_bytes(@id_length) |> Base.url_encode64(),
|
||||
datetime: datetime |> DateTime.from_naive!("Etc/UTC") |> DateTime.to_iso8601(:extended)
|
||||
}
|
||||
|
||||
~H"""
|
||||
<time datetime={@datetime} x-data={"{
|
||||
<time id={@id} datetime={@datetime} x-data={"{
|
||||
date:
|
||||
Intl.DateTimeFormat([], {dateStyle: 'short', timeStyle: 'long'})
|
||||
.format(new Date(\"#{@datetime}\"))
|
||||
@ -38,10 +41,13 @@ defmodule CanneryWeb.ViewHelpers do
|
||||
def display_date(nil), do: ""
|
||||
|
||||
def display_date(date) do
|
||||
assigns = %{date: date |> Date.to_iso8601(:extended)}
|
||||
assigns = %{
|
||||
id: :crypto.strong_rand_bytes(@id_length) |> Base.url_encode64(),
|
||||
date: date |> Date.to_iso8601(:extended)
|
||||
}
|
||||
|
||||
~H"""
|
||||
<time datetime={@date} x-data={"{
|
||||
<time id={@id} datetime={@date} x-data={"{
|
||||
date:
|
||||
Intl.DateTimeFormat([], {timeZone: 'Etc/UTC', dateStyle: 'short'}).format(new Date(\"#{@date}\"))
|
||||
}"} x-text="date">
|
||||
|
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user