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 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) {
@ -57,7 +68,9 @@ const WeaponGrid = (props: Props) => {
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 (

View file

@ -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 (