|
2 | 2 | q-card(flat :style="{ flex: 1 }") |
3 | 3 | .fit.flex |
4 | 4 | .flex.justify-center.items-center.column.q-ma-md(:style='{flex: 1}') |
5 | | - q-select.full-width(filled) |
| 5 | + q-select.full-width(filled v-model='from' label='Expéditeur' :options='fromList' option-label='name' option-value='name' |
| 6 | + hint) |
6 | 7 | .flex.justify-center.items-center.column.q-ma-md(:style='{flex: 1}') |
7 | 8 | q-input.full-width( |
8 | 9 | filled |
| 10 | + v-model='amount' |
| 11 | + label='Montant' |
| 12 | + type='number' |
| 13 | + :rules='[(val) => val > 0 || "Le montant doit être supérieur à 0", (val) => val <= getCurrency || "Le montant doit être inférieur ou égal à votre solde"]' |
| 14 | + :min='0' |
| 15 | + :max='getCurrency' |
| 16 | + hint |
9 | 17 | ) |
10 | 18 | template(#append) |
11 | 19 | q-icon(name='mdi-cash') |
12 | 20 | .flex.justify-center.items-center.column.q-ma-md(:style='{flex: 1}') |
13 | | - q-select.full-width(filled) |
| 21 | + q-select.full-width( |
| 22 | + v-model='to' |
| 23 | + filled :options='options' label='Destinataire' option-label='name' option-value='name' |
| 24 | + use-input |
| 25 | + fill-input |
| 26 | + emit-value |
| 27 | + hide-selected |
| 28 | + clearable |
| 29 | + @filter="filterFn" |
| 30 | + @filter-abort="abortFilterFn" |
| 31 | + input-debounce="0" |
| 32 | + hint |
| 33 | + ) |
| 34 | + template(#append) |
| 35 | + q-icon(name='mdi-account') |
| 36 | + .fit.flex |
| 37 | + .flex.justify-center.items-center.column.q-ma-md(:style='{flex: 1}') |
| 38 | + q-btn.text-black( |
| 39 | + :disable='amount <= 0 || to === ""' |
| 40 | + label='Echanger' |
| 41 | + color='primary' |
| 42 | + @click='exchange' |
| 43 | + ) |
14 | 44 | </template> |
15 | 45 |
|
16 | | -<script lang="ts"></script> |
| 46 | +<script lang="ts"> |
| 47 | +import { cp } from 'node:fs' |
| 48 | +
|
| 49 | +export default { |
| 50 | + data() { |
| 51 | + return { |
| 52 | + amount: 0, |
| 53 | + to: '', |
| 54 | + } |
| 55 | + }, |
| 56 | + async setup() { |
| 57 | + const options = ref([]) |
| 58 | + const getUsername = computed(() => { |
| 59 | + const $auth = useAuth() |
| 60 | +
|
| 61 | + return $auth.user?.name |
| 62 | + }) |
| 63 | +
|
| 64 | + const from = ref(getUsername.value) |
| 65 | +
|
| 66 | + const { |
| 67 | + data: users, |
| 68 | + refresh, |
| 69 | + error, |
| 70 | + } = await useHttp<any>('/core/users/', { |
| 71 | + transform: (result) => { |
| 72 | + return ( |
| 73 | + result?.data.filter((user: any) => { |
| 74 | + return user.name !== getUsername.value |
| 75 | + }) || [] |
| 76 | + ) |
| 77 | + }, |
| 78 | + }) |
| 79 | +
|
| 80 | + options.value = users.value |
| 81 | +
|
| 82 | + return { |
| 83 | + from, |
| 84 | + getUsername, |
| 85 | + users, |
| 86 | + options, |
| 87 | + } |
| 88 | + }, |
| 89 | + computed: { |
| 90 | + fromList() { |
| 91 | + const list = ref<any[]>([]) |
| 92 | +
|
| 93 | + list.value.push({ |
| 94 | + name: this.getUsername + ' (vous)', |
| 95 | + value: this.getUsername, |
| 96 | + }) |
| 97 | +
|
| 98 | + list.value.push({ |
| 99 | + name: 'Mairie', |
| 100 | + value: 'Mairie', |
| 101 | + }) |
| 102 | +
|
| 103 | + return list.value |
| 104 | + }, |
| 105 | + getCurrency() { |
| 106 | + const $auth = useAuth() |
| 107 | +
|
| 108 | + return $auth.user?.currency |
| 109 | + }, |
| 110 | + }, |
| 111 | + methods: { |
| 112 | + filterFn(val: string, update: any) { |
| 113 | + if (val === '') { |
| 114 | + update(() => { |
| 115 | + this.options = this.users |
| 116 | + }) |
| 117 | + return |
| 118 | + } |
| 119 | +
|
| 120 | + const needle = val.toLowerCase() |
| 121 | + const filtered = this.users.filter((user: any) => { |
| 122 | + return user.name?.toLowerCase().indexOf(needle) > -1 |
| 123 | + }) |
| 124 | +
|
| 125 | + update(() => { |
| 126 | + this.options = filtered |
| 127 | + }) |
| 128 | + }, |
| 129 | + abortFilterFn() { |
| 130 | + this.options = this.users |
| 131 | + }, |
| 132 | + exchange() { |
| 133 | + this.$q |
| 134 | + .dialog({ |
| 135 | + title: 'Echange', |
| 136 | + message: 'Etes-vous sûr de vouloir échanger ' + this.amount + ' € avec ' + this.to + ' ?', |
| 137 | + persistent: true, |
| 138 | + ok: { label: 'Oui', color: 'primary' }, |
| 139 | + cancel: { label: 'Annuler', color: 'negative' }, |
| 140 | + }) |
| 141 | + .onOk(() => { |
| 142 | + this.$http |
| 143 | + .post('/core/bank/exchange/', { |
| 144 | + from: this.from, |
| 145 | + to: this.to, |
| 146 | + amount: this.amount, |
| 147 | + }) |
| 148 | + .then(() => { |
| 149 | + this.$q.notify({ |
| 150 | + type: 'positive', |
| 151 | + message: 'Echange effectué avec succès', |
| 152 | + }) |
| 153 | + this.$router.push('/bank') |
| 154 | + }) |
| 155 | + .catch((err) => { |
| 156 | + this.$q.notify({ |
| 157 | + type: 'negative', |
| 158 | + message: err.message, |
| 159 | + }) |
| 160 | + }) |
| 161 | + }) |
| 162 | + }, |
| 163 | + }, |
| 164 | +} |
| 165 | +</script> |
0 commit comments