Skip to content

Commit

Permalink
feat: keep the current filter after delete the record
Browse files Browse the repository at this point in the history
  • Loading branch information
JHIH-LEI committed Aug 23, 2021
1 parent a573d80 commit bb33245
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 9 deletions.
8 changes: 4 additions & 4 deletions routes/modules/home.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,13 @@ router.get('/', (req, res) => {
})
})

router.post('/filter/record', (req, res) => {
router.get('/filter/record', (req, res) => {
const userId = req.user._id
// 使用者選擇的篩選條件
// req.body = { year: '2020', month: '08', category: 'music' }
const year = req.body.year === '全部年份' ? { $ne: '' } : Number(req.body.year)
const month = req.body.month === '全部月份' ? { $ne: '' } : Number(req.body.month)
const category = req.body.category === '全部類別' ? { $ne: '' } : req.body.category
const year = req.query.year === '全部年份' ? { $ne: '' } : Number(req.query.year)
const month = req.query.month === '全部月份' ? { $ne: '' } : Number(req.query.month)
const category = req.query.category === '全部類別' ? { $ne: '' } : req.query.category
// 要傳到前端的資料,有使用者可以篩選的條件以及總金額
let totalAmount = 0
let categoryNameList = []
Expand Down
7 changes: 3 additions & 4 deletions routes/modules/record.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,10 @@ router.delete('/:id', (req, res) => {
const userId = req.user._id
const _id = req.params.id
const url = req.headers.referer
const pathname = new URL(url).pathname
Record.findOne({ _id, userId })
.then(record => record.remove())
// 返回上一頁,這樣才能避免篩選類別後,再刪除資料後,又回到全部類別
.then(() => res.redirect(pathname))
// 返回上一頁,這樣才能避免篩選後,再刪除資料後,又回到全部類別,換句話說就是刪除後要保留當前篩選
.then(() => res.redirect(url))
.catch(error => console.log(error))
})

Expand All @@ -75,7 +74,7 @@ router.put('/:type/:id', (req, res) => {
const { name, date, category, amount, merchant } = req.body
let money = 0
// 根據類別類型來調整金額正負(type為收入或支出)
money = type === '收入' ? amount : 0 - amount
money = type === '收入' ? amount : - amount
const _id = req.params.id
Record.findOne({ _id, userId })
.then(record => {
Expand Down
2 changes: 1 addition & 1 deletion views/index.handlebars
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<div class="d-flex justify-content-center align-items-center m-5">
篩選器:
{{!-- 篩選器 --}}
<form action="/filter/record" method="POST" id="get-sort" class="d-flex justify-content-between">
<form action="/filter/record" id="get-sort" class="d-flex justify-content-between">
{{!-- 年篩選 --}}
<select class="form-select" name="year" id="sort-year">
<option>全部年份</option>
Expand Down

0 comments on commit bb33245

Please sign in to comment.