Determine editable at Party level, pass down to grids

This commit is contained in:
Justin Edmund 2023-01-31 01:25:02 -08:00
parent c0df78758b
commit 608b744a71
4 changed files with 53 additions and 46 deletions

View file

@ -23,6 +23,7 @@ import './index.scss'
// Props // Props
interface Props { interface Props {
new: boolean new: boolean
editable: boolean
characters?: GridCharacter[] characters?: GridCharacter[]
createParty: (details?: DetailsObject) => Promise<Party> createParty: (details?: DetailsObject) => Promise<Party>
pushHistory?: (path: string) => void pushHistory?: (path: string) => void
@ -75,17 +76,6 @@ const CharacterGrid = (props: Props) => {
[key: number]: number | undefined [key: number]: number | undefined
}>({}) }>({})
// Set the editable flag only on first load
useEffect(() => {
// If user is logged in and matches
if (
(accountData && party.user && accountData.userId === party.user.id) ||
props.new
)
appState.party.editable = true
else appState.party.editable = false
}, [props.new, accountData, party])
useEffect(() => { useEffect(() => {
setJob(appState.party.job) setJob(appState.party.job)
setJobSkills(appState.party.jobSkills) setJobSkills(appState.party.jobSkills)
@ -115,7 +105,7 @@ const CharacterGrid = (props: Props) => {
.catch((error) => console.error(error)) .catch((error) => console.error(error))
}) })
} else { } else {
if (party.editable) if (props.editable)
saveCharacter(party.id, character, position) saveCharacter(party.id, character, position)
.then((response) => handleCharacterResponse(response.data)) .then((response) => handleCharacterResponse(response.data))
.catch((error) => { .catch((error) => {
@ -232,7 +222,7 @@ const CharacterGrid = (props: Props) => {
} }
function saveJobSkill(skill: JobSkill, position: number) { function saveJobSkill(skill: JobSkill, position: number) {
if (party.id && appState.party.editable) { if (party.id && props.editable) {
const positionedKey = `skill${position}_id` const positionedKey = `skill${position}_id`
let skillObject: { let skillObject: {
@ -522,7 +512,7 @@ const CharacterGrid = (props: Props) => {
job={job} job={job}
jobSkills={jobSkills} jobSkills={jobSkills}
jobAccessory={jobAccessory} jobAccessory={jobAccessory}
editable={party.editable} editable={props.editable}
saveJob={saveJob} saveJob={saveJob}
saveSkill={saveJobSkill} saveSkill={saveJobSkill}
saveAccessory={saveAccessory} saveAccessory={saveAccessory}
@ -541,7 +531,7 @@ const CharacterGrid = (props: Props) => {
<li key={`grid_unit_${i}`}> <li key={`grid_unit_${i}`}>
<CharacterUnit <CharacterUnit
gridCharacter={grid.characters[i]} gridCharacter={grid.characters[i]}
editable={party.editable} editable={props.editable}
position={i} position={i}
updateObject={receiveCharacterFromSearch} updateObject={receiveCharacterFromSearch}
updateUncap={initiateUncapUpdate} updateUncap={initiateUncapUpdate}

View file

@ -2,6 +2,7 @@ import React, { useEffect, useState } from 'react'
import { useRouter } from 'next/router' import { useRouter } from 'next/router'
import { useSnapshot } from 'valtio' import { useSnapshot } from 'valtio'
import clonedeep from 'lodash.clonedeep' import clonedeep from 'lodash.clonedeep'
import ls from 'local-storage'
import PartySegmentedControl from '~components/PartySegmentedControl' import PartySegmentedControl from '~components/PartySegmentedControl'
import PartyDetails from '~components/PartyDetails' import PartyDetails from '~components/PartyDetails'
@ -16,6 +17,8 @@ import { retrieveCookies } from '~utils/retrieveCookies'
import type { DetailsObject } from '~types' import type { DetailsObject } from '~types'
import './index.scss' import './index.scss'
import { accountCookie } from '~utils/userToken'
import { getCookie } from 'cookies-next'
// Props // Props
interface Props { interface Props {
@ -36,6 +39,7 @@ const Party = (props: Props) => {
// Set up states // Set up states
const { party } = useSnapshot(appState) const { party } = useSnapshot(appState)
const [editable, setEditable] = useState(false)
const [currentTab, setCurrentTab] = useState<GridType>(GridType.Weapon) const [currentTab, setCurrentTab] = useState<GridType>(GridType.Weapon)
// Retrieve cookies // Retrieve cookies
@ -48,6 +52,36 @@ const Party = (props: Props) => {
if (props.team) storeParty(props.team) if (props.team) storeParty(props.team)
}, []) }, [])
// Set editable on first load
useEffect(() => {
// Get cookie
const cookie = getCookie('account')
const accountData: AccountCookie = cookie
? JSON.parse(cookie as string)
: null
let editable = false
if (props.new) editable = true
if (accountData && props.team && !props.new) {
if (accountData.token) {
// Authenticated
if (props.team.user && accountData.userId === props.team.user.id) {
editable = true
}
} else {
// Not authenticated
if (!props.team.user && accountData.userId === props.team.local_id) {
editable = true
}
}
}
appState.party.editable = editable
setEditable(editable)
})
// Set selected tab from props // Set selected tab from props
useEffect(() => { useEffect(() => {
setCurrentTab(props.selectedTab) setCurrentTab(props.selectedTab)
@ -267,6 +301,7 @@ const Party = (props: Props) => {
const weaponGrid = ( const weaponGrid = (
<WeaponGrid <WeaponGrid
new={props.new || false} new={props.new || false}
editable={editable}
weapons={props.team?.weapons} weapons={props.team?.weapons}
createParty={createParty} createParty={createParty}
pushHistory={props.pushHistory} pushHistory={props.pushHistory}
@ -276,6 +311,7 @@ const Party = (props: Props) => {
const summonGrid = ( const summonGrid = (
<SummonGrid <SummonGrid
new={props.new || false} new={props.new || false}
editable={editable}
summons={props.team?.summons} summons={props.team?.summons}
createParty={createParty} createParty={createParty}
pushHistory={props.pushHistory} pushHistory={props.pushHistory}
@ -285,6 +321,7 @@ const Party = (props: Props) => {
const characterGrid = ( const characterGrid = (
<CharacterGrid <CharacterGrid
new={props.new || false} new={props.new || false}
editable={editable}
characters={props.team?.characters} characters={props.team?.characters}
createParty={createParty} createParty={createParty}
pushHistory={props.pushHistory} pushHistory={props.pushHistory}

View file

@ -21,6 +21,7 @@ import './index.scss'
// Props // Props
interface Props { interface Props {
new: boolean new: boolean
editable: boolean
summons?: GridSummon[] summons?: GridSummon[]
createParty: (details?: DetailsObject) => Promise<Party> createParty: (details?: DetailsObject) => Promise<Party>
pushHistory?: (path: string) => void pushHistory?: (path: string) => void
@ -55,17 +56,6 @@ const SummonGrid = (props: Props) => {
[key: number]: number [key: number]: number
}>({}) }>({})
// Set the editable flag only on first load
useEffect(() => {
// If user is logged in and matches
if (
(accountData && party.user && accountData.userId === party.user.id) ||
props.new
)
appState.party.editable = true
else appState.party.editable = false
}, [props.new, accountData, party])
// Initialize an array of current uncap values for each summon // Initialize an array of current uncap values for each summon
useEffect(() => { useEffect(() => {
let initialPreviousUncapValues: { [key: number]: number } = {} let initialPreviousUncapValues: { [key: number]: number } = {}
@ -100,7 +90,7 @@ const SummonGrid = (props: Props) => {
) )
}) })
} else { } else {
if (party.editable) if (props.editable)
saveSummon(party.id, summon, position) saveSummon(party.id, summon, position)
.then((response) => handleSummonResponse(response.data)) .then((response) => handleSummonResponse(response.data))
.catch((error) => { .catch((error) => {
@ -401,7 +391,7 @@ const SummonGrid = (props: Props) => {
<div className="Label">{t('summons.main')}</div> <div className="Label">{t('summons.main')}</div>
<SummonUnit <SummonUnit
gridSummon={grid.summons.mainSummon} gridSummon={grid.summons.mainSummon}
editable={party.editable} editable={props.editable}
key="grid_main_summon" key="grid_main_summon"
position={-1} position={-1}
unitType={0} unitType={0}
@ -418,7 +408,7 @@ const SummonGrid = (props: Props) => {
<div className="Label Friend">{t('summons.friend')}</div> <div className="Label Friend">{t('summons.friend')}</div>
<SummonUnit <SummonUnit
gridSummon={grid.summons.friendSummon} gridSummon={grid.summons.friendSummon}
editable={party.editable} editable={props.editable}
key="grid_friend_summon" key="grid_friend_summon"
position={6} position={6}
unitType={2} unitType={2}
@ -439,7 +429,7 @@ const SummonGrid = (props: Props) => {
<li key={`grid_unit_${i}`}> <li key={`grid_unit_${i}`}>
<SummonUnit <SummonUnit
gridSummon={grid.summons.allSummons[i]} gridSummon={grid.summons.allSummons[i]}
editable={party.editable} editable={props.editable}
position={i} position={i}
unitType={1} unitType={1}
removeSummon={removeSummon} removeSummon={removeSummon}
@ -457,7 +447,7 @@ const SummonGrid = (props: Props) => {
const subAuraSummonElement = ( const subAuraSummonElement = (
<ExtraSummons <ExtraSummons
grid={grid.summons.allSummons} grid={grid.summons.allSummons}
editable={party.editable} editable={props.editable}
exists={false} exists={false}
offset={numSummons} offset={numSummons}
removeSummon={removeSummon} removeSummon={removeSummon}

View file

@ -23,6 +23,7 @@ import './index.scss'
// Props // Props
interface Props { interface Props {
new: boolean new: boolean
editable: boolean
weapons?: GridWeapon[] weapons?: GridWeapon[]
createParty: (details: DetailsObject) => Promise<Party> createParty: (details: DetailsObject) => Promise<Party>
pushHistory?: (path: string) => void pushHistory?: (path: string) => void
@ -60,17 +61,6 @@ const WeaponGrid = (props: Props) => {
[key: number]: number [key: number]: number
}>({}) }>({})
// Set the editable flag only on first load
useEffect(() => {
// If user is logged in and matches
if (
(accountData && party.user && accountData.userId === party.user.id) ||
props.new
)
appState.party.editable = true
else appState.party.editable = false
}, [props.new, accountData, party])
// Initialize an array of current uncap values for each weapon // Initialize an array of current uncap values for each weapon
useEffect(() => { useEffect(() => {
let initialPreviousUncapValues: { [key: number]: number } = {} let initialPreviousUncapValues: { [key: number]: number } = {}
@ -99,7 +89,7 @@ const WeaponGrid = (props: Props) => {
}) })
}) })
} else { } else {
if (party.editable) if (props.editable)
saveWeapon(party.id, weapon, position) saveWeapon(party.id, weapon, position)
.then((response) => { .then((response) => {
if (response) handleWeaponResponse(response.data) if (response) handleWeaponResponse(response.data)
@ -337,7 +327,7 @@ const WeaponGrid = (props: Props) => {
const mainhandElement = ( const mainhandElement = (
<WeaponUnit <WeaponUnit
gridWeapon={appState.grid.weapons.mainWeapon} gridWeapon={appState.grid.weapons.mainWeapon}
editable={party.editable} editable={props.editable}
key="grid_mainhand" key="grid_mainhand"
position={-1} position={-1}
unitType={0} unitType={0}
@ -352,7 +342,7 @@ const WeaponGrid = (props: Props) => {
<li key={`grid_unit_${i}`}> <li key={`grid_unit_${i}`}>
<WeaponUnit <WeaponUnit
gridWeapon={appState.grid.weapons.allWeapons[i]} gridWeapon={appState.grid.weapons.allWeapons[i]}
editable={party.editable} editable={props.editable}
position={i} position={i}
unitType={1} unitType={1}
removeWeapon={removeWeapon} removeWeapon={removeWeapon}
@ -366,7 +356,7 @@ const WeaponGrid = (props: Props) => {
const extraGridElement = ( const extraGridElement = (
<ExtraWeapons <ExtraWeapons
grid={appState.grid.weapons.allWeapons} grid={appState.grid.weapons.allWeapons}
editable={party.editable} editable={props.editable}
offset={numWeapons} offset={numWeapons}
removeWeapon={removeWeapon} removeWeapon={removeWeapon}
updateObject={receiveWeaponFromSearch} updateObject={receiveWeaponFromSearch}