Fix saving weapons to existing grids
This commit is contained in:
parent
b2e62ae89e
commit
1a0e99b5b7
2 changed files with 25 additions and 8 deletions
|
|
@ -1,4 +1,5 @@
|
||||||
import React, { useEffect, useState } from 'react'
|
import React, { useEffect, useState } from 'react'
|
||||||
|
import { useCookies } from 'react-cookie'
|
||||||
import api from '~utils/api'
|
import api from '~utils/api'
|
||||||
|
|
||||||
import WeaponUnit from '~components/WeaponUnit'
|
import WeaponUnit from '~components/WeaponUnit'
|
||||||
|
|
@ -24,25 +25,35 @@ const WeaponGrid = (props: Props) => {
|
||||||
|
|
||||||
const [mainhand, setMainhand] = useState<Weapon>()
|
const [mainhand, setMainhand] = useState<Weapon>()
|
||||||
const [weapons, setWeapons] = useState<GridArray>({})
|
const [weapons, setWeapons] = useState<GridArray>({})
|
||||||
|
const [partyId, setPartyId] = useState('')
|
||||||
|
|
||||||
|
const [cookies, setCookie] = useCookies(['user'])
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (props.exists && props.found)
|
if (props.exists && props.found)
|
||||||
configure()
|
configure()
|
||||||
}, [props.mainhand, props.grid])
|
}, [props.mainhand, props.grid, props.partyId])
|
||||||
|
|
||||||
function configure() {
|
function configure() {
|
||||||
setMainhand(props.mainhand)
|
setMainhand(props.mainhand)
|
||||||
setWeapons(props.grid || {})
|
setWeapons(props.grid || {})
|
||||||
|
setPartyId(props.partyId || '')
|
||||||
}
|
}
|
||||||
|
|
||||||
function createParty() {
|
function createParty() {
|
||||||
|
const headers = (cookies.user != null) ? {
|
||||||
|
headers: {
|
||||||
|
'Authorization': `Bearer ${cookies.user.access_token}`
|
||||||
|
}
|
||||||
|
} : {}
|
||||||
|
|
||||||
const body = (props.userId === undefined) ? {} : {
|
const body = (props.userId === undefined) ? {} : {
|
||||||
party: {
|
party: {
|
||||||
user_id: props.userId
|
user_id: props.userId
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return api.endpoints.parties.create(body)
|
return api.endpoints.parties.create(body, headers)
|
||||||
}
|
}
|
||||||
|
|
||||||
function receiveWeapon(weapon: Weapon, position: number) {
|
function receiveWeapon(weapon: Weapon, position: number) {
|
||||||
|
|
@ -56,8 +67,10 @@ const WeaponGrid = (props: Props) => {
|
||||||
newWeapons[position] = weapon
|
newWeapons[position] = weapon
|
||||||
setWeapons(newWeapons)
|
setWeapons(newWeapons)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (props.partyId == undefined) {
|
if (partyId) {
|
||||||
|
saveWeapon(partyId, weapon, position)
|
||||||
|
} else {
|
||||||
createParty()
|
createParty()
|
||||||
.then(response => {
|
.then(response => {
|
||||||
return response.data.party
|
return response.data.party
|
||||||
|
|
@ -71,13 +84,18 @@ const WeaponGrid = (props: Props) => {
|
||||||
})
|
})
|
||||||
.then(partyId => {
|
.then(partyId => {
|
||||||
saveWeapon(partyId, weapon, position)
|
saveWeapon(partyId, weapon, position)
|
||||||
|
setPartyId(partyId)
|
||||||
})
|
})
|
||||||
} else {
|
|
||||||
saveWeapon(props.partyId, weapon, position)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function saveWeapon(pid: string, weapon: Weapon, position: number) {
|
function saveWeapon(pid: string, weapon: Weapon, position: number) {
|
||||||
|
const headers = (cookies.user != null) ? {
|
||||||
|
headers: {
|
||||||
|
'Authorization': `Bearer ${cookies.user.access_token}`
|
||||||
|
}
|
||||||
|
} : {}
|
||||||
|
|
||||||
const body = {
|
const body = {
|
||||||
'weapon': {
|
'weapon': {
|
||||||
'party_id': pid,
|
'party_id': pid,
|
||||||
|
|
@ -87,7 +105,7 @@ const WeaponGrid = (props: Props) => {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
api.endpoints.weapons.create(body)
|
api.endpoints.weapons.create(body, headers)
|
||||||
}
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
|
|
||||||
|
|
@ -12,7 +12,6 @@ const New: React.FC<NewProps> = (props: NewProps) => {
|
||||||
function callback(path: string) {
|
function callback(path: string) {
|
||||||
// This is scuffed, how do we do this natively?
|
// This is scuffed, how do we do this natively?
|
||||||
window.history.replaceState(null, `Grid Tool`, `${path}`)
|
window.history.replaceState(null, `Grid Tool`, `${path}`)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue