Fix saving weapons to existing grids

This commit is contained in:
Justin Edmund 2020-09-26 10:52:32 -07:00
parent b2e62ae89e
commit 1a0e99b5b7
2 changed files with 25 additions and 8 deletions

View file

@ -1,4 +1,5 @@
import React, { useEffect, useState } from 'react'
import { useCookies } from 'react-cookie'
import api from '~utils/api'
import WeaponUnit from '~components/WeaponUnit'
@ -24,25 +25,35 @@ const WeaponGrid = (props: Props) => {
const [mainhand, setMainhand] = useState<Weapon>()
const [weapons, setWeapons] = useState<GridArray>({})
const [partyId, setPartyId] = useState('')
const [cookies, setCookie] = useCookies(['user'])
useEffect(() => {
if (props.exists && props.found)
configure()
}, [props.mainhand, props.grid])
}, [props.mainhand, props.grid, props.partyId])
function configure() {
setMainhand(props.mainhand)
setWeapons(props.grid || {})
setPartyId(props.partyId || '')
}
function createParty() {
const headers = (cookies.user != null) ? {
headers: {
'Authorization': `Bearer ${cookies.user.access_token}`
}
} : {}
const body = (props.userId === undefined) ? {} : {
party: {
user_id: props.userId
}
}
return api.endpoints.parties.create(body)
return api.endpoints.parties.create(body, headers)
}
function receiveWeapon(weapon: Weapon, position: number) {
@ -56,8 +67,10 @@ const WeaponGrid = (props: Props) => {
newWeapons[position] = weapon
setWeapons(newWeapons)
}
if (props.partyId == undefined) {
if (partyId) {
saveWeapon(partyId, weapon, position)
} else {
createParty()
.then(response => {
return response.data.party
@ -71,13 +84,18 @@ const WeaponGrid = (props: Props) => {
})
.then(partyId => {
saveWeapon(partyId, weapon, position)
setPartyId(partyId)
})
} else {
saveWeapon(props.partyId, weapon, position)
}
}
function saveWeapon(pid: string, weapon: Weapon, position: number) {
const headers = (cookies.user != null) ? {
headers: {
'Authorization': `Bearer ${cookies.user.access_token}`
}
} : {}
const body = {
'weapon': {
'party_id': pid,
@ -87,7 +105,7 @@ const WeaponGrid = (props: Props) => {
}
}
api.endpoints.weapons.create(body)
api.endpoints.weapons.create(body, headers)
}
return (

View file

@ -12,7 +12,6 @@ const New: React.FC<NewProps> = (props: NewProps) => {
function callback(path: string) {
// This is scuffed, how do we do this natively?
window.history.replaceState(null, `Grid Tool`, `${path}`)
}
return (