From 1a0e99b5b763d1c83beb965cfef0bb30b256cf24 Mon Sep 17 00:00:00 2001 From: Justin Edmund Date: Sat, 26 Sep 2020 10:52:32 -0700 Subject: [PATCH] Fix saving weapons to existing grids --- src/components/WeaponGrid/index.tsx | 32 ++++++++++++++++++++++------- src/routes/New/index.tsx | 1 - 2 files changed, 25 insertions(+), 8 deletions(-) diff --git a/src/components/WeaponGrid/index.tsx b/src/components/WeaponGrid/index.tsx index 060ac0d3..3f9f9cb0 100644 --- a/src/components/WeaponGrid/index.tsx +++ b/src/components/WeaponGrid/index.tsx @@ -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() const [weapons, setWeapons] = useState({}) + 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 ( diff --git a/src/routes/New/index.tsx b/src/routes/New/index.tsx index 0e0d01e7..9fec15c8 100644 --- a/src/routes/New/index.tsx +++ b/src/routes/New/index.tsx @@ -12,7 +12,6 @@ const New: React.FC = (props: NewProps) => { function callback(path: string) { // This is scuffed, how do we do this natively? window.history.replaceState(null, `Grid Tool`, `${path}`) - } return (