Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/decal redeem #309

Merged
merged 6 commits into from
Feb 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,22 @@ For detailed rules of this file, see [Changelog Rules](#changelog-rules)
### ⚙ Tasks
*


[#303]: https://github.com/fuelRats/fuelrats.com/pull/303
[Unreleased]: https://github.com/FuelRats/fuelrats.com/compare/v2.12.6...HEAD
[Unreleased]: https://github.com/FuelRats/fuelrats.com/compare/v2.12.8...HEAD





## [2.12.8][] - 2021-02-01

### 🐛 Fixed
* Fix multiple issues with redeeming decals - [#309][]


[#309]: https://github.com/fuelRats/fuelrats.com/pull/309
[2.12.8]: https://github.com/FuelRats/fuelrats.com/compare/v2.12.7...v2.12.8



Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "fuelrats.com",
"version": "2.12.7",
"version": "2.12.8",
"description": "The primary website of The Fuel Rats!",
"main": "dist/server.js",
"license": "BSD-3-Clause",
Expand Down
2 changes: 1 addition & 1 deletion src/components/UserDecalPanel/UserDecalPanel.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { useCallback, useState } from 'react'
import { useDispatch, useSelector } from 'react-redux'

import { redeemDecal } from '~/store/actions/decals'
import {
selectCurrentUserId,
selectDecalsByUserId,
Expand All @@ -24,7 +25,6 @@ function UserDetailsPanel () {
const handleRedeemDecal = useCallback(async () => {
setRedeeming(true)

// eslint-disable-next-line no-undef
await dispatch(redeemDecal(userId))

setRedeeming(false)
Expand Down
8 changes: 8 additions & 0 deletions src/store/actions/decals.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { defineRelationship } from '@fuelrats/web-util/redux-json-api'

import actionTypes from '../actionTypes'
import { createsRelationship, RESOURCE } from '../reducers/frAPIResources'
import { decrementsEligibleDecals } from '../reducers/users'
import { frApiRequest } from './services'


Expand All @@ -12,5 +16,9 @@ export const redeemDecal = (id) => {
url: `/users/${id}/decals`,
method: 'post',
},
createsRelationship(
defineRelationship({ type: 'users', id }, { decals: [RESOURCE] }),
),
decrementsEligibleDecals(id),
)
}
2 changes: 2 additions & 0 deletions src/store/reducers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import images from './images'
import leaderboard from './leaderboard'
import pageViews from './pageViews'
import session from './session'
import users from './users'
import wordpress from './wordpress'


Expand All @@ -28,6 +29,7 @@ export default chainReducers(
leaderboard,
pageViews,
session,
users,
wordpress,
}),
],
Expand Down
40 changes: 40 additions & 0 deletions src/store/reducers/users.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { isError } from 'flux-standard-action'
import { produce } from 'immer'

import safeParseInt from '~/helpers/safeParseInt'

import actionTypes from '../actionTypes'
import initialState from '../initialState'

const metaKey = '__decals/decrement'

const usersReducer = produce((draftState, action) => {
if (isError(action)) {
return
}

switch (action.type) {
case actionTypes.decals.redeem:
if (action.meta[metaKey]) {
const user = draftState[action.meta[metaKey]]
if (user?.meta) {
user.meta.redeemable = Math.max(safeParseInt(user.meta.redeemable) - 1, 0)
}
}
break

default:
break
}
}, initialState.users)



export function decrementsEligibleDecals (id) {
return {
[metaKey]: id,
}
}


export default usersReducer