Skip to content

Commit a4a92de

Browse files
committed
REDLINE-20 falta só o editar
1 parent dbc18ad commit a4a92de

File tree

3 files changed

+66
-15
lines changed

3 files changed

+66
-15
lines changed
Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,43 @@
11
<template>
22
<ul>
3-
<li v-if="!cState.store.categorias || !cState.store.categorias.length">Não há categorias para exibir</li>
4-
<li v-for="cat in cState.store.categorias" :key="cat.id">{{cat.descricao}}</li>
3+
<li>
4+
<form @submit.prevent.stop="salvarNova">
5+
<input v-model="novaCategoria.descricao" required placeholder="Nova categoria" />
6+
<button type="submit">Salvar</button>
7+
</form>
8+
</li>
9+
<li v-if="!cState.store.categorias || !cState.store.categorias.length">
10+
Não há categorias para exibir
11+
</li>
12+
<li v-for="cat in cState.store.categorias" :key="cat.id">
13+
{{ cat.descricao }}
14+
<button @click="removeCategoria(cat)">&#128686;</button>
15+
</li>
516
</ul>
617
</template>
718
<script setup>
8-
9-
import { useCategoriaStore } from "@/stores/categoriaStore";
10-
import { onMounted } from "vue";
19+
import { useCategoriaStore } from '@/stores/categoriaStore'
20+
import { onMounted, reactive } from 'vue'
1121
1222
const cState = useCategoriaStore()
1323
24+
const novaCategoria = reactive({
25+
descricao: ''
26+
})
27+
28+
const salvarNova = async () => {
29+
await cState.salvarCategoria({ ...novaCategoria })
30+
novaCategoria.descricao = ''
31+
await cState.sincronizarCategorias()
32+
}
33+
34+
const removeCategoria = async (categoria) => {
35+
if (!confirm('deseja realmente excluir esta categoria?')) return
36+
await cState.excluirCategoria(categoria)
37+
await cState.sincronizarCategorias()
38+
}
1439
onMounted(() => {
1540
cState.sincronizarCategorias()
1641
})
1742
</script>
18-
<style scoped></style>
43+
<style scoped></style>

web-app-vue/src/services/api.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ const req = async ({ method = 'POST', uri, payload }) => {
1010
}
1111
if (token) headers['Authorization'] = `Bearer ${token}`
1212
const result = await fetch(url, {
13-
method,
13+
body: JSON.stringify(payload),
1414
headers,
15-
body: JSON.stringify(payload)
15+
method
1616
})
1717
return result.json()
1818
}
@@ -47,3 +47,12 @@ export const deleteConta = async ({ id, conta }) => await del({ uri: `/${id}/con
4747

4848
export const listCategorias = async ({ id, q = '', limit = 50, offset = 0 }) =>
4949
await get({ uri: `/${id}/categoria?q=${q}&limit=${limit}&offset=${offset}` })
50+
51+
export const updateCategoria = async ({ id, categoria }) =>
52+
await put({ uri: `/${id}/categoria/${categoria.id}`, payload: categoria })
53+
54+
export const insertCategoria = async ({ id, categoria }) =>
55+
await post({ uri: `/${id}/categoria`, payload: categoria })
56+
57+
export const delCategoria = async ({ id, categoria }) =>
58+
await del({ uri: `/${id}/categoria/${categoria.id}` })
Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,38 @@
11
import { defineStore } from 'pinia'
22
import { reactive } from 'vue'
3-
import { getRedLine } from "@/services/redLine";
4-
import { listCategorias } from "@/services/api";
5-
import { useUserStore } from "@/stores/userStore";
3+
import { getRedLine, setRedLine } from '@/services/redLine'
4+
import { delCategoria, insertCategoria, listCategorias, updateCategoria } from '@/services/api'
5+
import { useUserStore } from '@/stores/userStore'
66

77
export const useCategoriaStore = defineStore('categoria-store', () => {
88
const uState = useUserStore()
99

10-
1110
const store = reactive({
1211
categorias: getRedLine()?.categorias || []
1312
})
1413

1514
const sincronizarCategorias = async () => {
16-
const {id} = uState.userData
17-
store.categorias = await listCategorias({id})
15+
const { id } = uState.userData
16+
const categorias = await listCategorias({ id })
17+
store.categorias = categorias
18+
setRedLine({ ...getRedLine(), categorias })
19+
}
20+
21+
const salvarCategoria = async (categoria) => {
22+
const { id } = uState.userData
23+
categoria.usuario_id = id
24+
if (categoria.id) {
25+
await updateCategoria({ id, categoria })
26+
} else {
27+
await insertCategoria({ id, categoria })
28+
}
29+
}
30+
31+
const excluirCategoria = async (categoria) => {
32+
const { id } = uState.userData
33+
categoria.usuario_id = id
34+
await delCategoria({ id, categoria })
1835
}
1936

20-
return { store, sincronizarCategorias }
37+
return { store, sincronizarCategorias, salvarCategoria, excluirCategoria }
2138
})

0 commit comments

Comments
 (0)