Skip to content

Commit 11f8a4c

Browse files
committed
feat(exchange): implement exchange functionality with validation and user selection
1 parent 78a7f4e commit 11f8a4c

File tree

2 files changed

+153
-4
lines changed

2 files changed

+153
-4
lines changed

apps/web/src/pages/bank.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
q-card(flat :style="{ flex: 1 }")
33
q-tabs(v-model='tab' inline-label)
44
q-tab(name='upload', label='Déposer')
5-
//- q-tab(name='exchange', label='Echanger')
5+
q-tab(name='exchange', label='Echanger')
66
q-card-section
77
nuxt-page
88
</template>

apps/web/src/pages/bank/exchange.vue

Lines changed: 152 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,164 @@
22
q-card(flat :style="{ flex: 1 }")
33
.fit.flex
44
.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)
67
.flex.justify-center.items-center.column.q-ma-md(:style='{flex: 1}')
78
q-input.full-width(
89
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
917
)
1018
template(#append)
1119
q-icon(name='mdi-cash')
1220
.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+
)
1444
</template>
1545

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

Comments
 (0)