diff --git a/components/CharacterGrid/index.tsx b/components/CharacterGrid/index.tsx index b97218da..bed247ca 100644 --- a/components/CharacterGrid/index.tsx +++ b/components/CharacterGrid/index.tsx @@ -23,6 +23,7 @@ import './index.scss' // Props interface Props { new: boolean + editable: boolean characters?: GridCharacter[] createParty: (details?: DetailsObject) => Promise pushHistory?: (path: string) => void @@ -75,17 +76,6 @@ const CharacterGrid = (props: Props) => { [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(() => { setJob(appState.party.job) setJobSkills(appState.party.jobSkills) @@ -115,7 +105,7 @@ const CharacterGrid = (props: Props) => { .catch((error) => console.error(error)) }) } else { - if (party.editable) + if (props.editable) saveCharacter(party.id, character, position) .then((response) => handleCharacterResponse(response.data)) .catch((error) => { @@ -232,7 +222,7 @@ const CharacterGrid = (props: Props) => { } function saveJobSkill(skill: JobSkill, position: number) { - if (party.id && appState.party.editable) { + if (party.id && props.editable) { const positionedKey = `skill${position}_id` let skillObject: { @@ -522,7 +512,7 @@ const CharacterGrid = (props: Props) => { job={job} jobSkills={jobSkills} jobAccessory={jobAccessory} - editable={party.editable} + editable={props.editable} saveJob={saveJob} saveSkill={saveJobSkill} saveAccessory={saveAccessory} @@ -541,7 +531,7 @@ const CharacterGrid = (props: Props) => {
  • { // Set up states const { party } = useSnapshot(appState) + const [editable, setEditable] = useState(false) const [currentTab, setCurrentTab] = useState(GridType.Weapon) // Retrieve cookies @@ -48,6 +52,36 @@ const Party = (props: Props) => { 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 useEffect(() => { setCurrentTab(props.selectedTab) @@ -267,6 +301,7 @@ const Party = (props: Props) => { const weaponGrid = ( { const summonGrid = ( { const characterGrid = ( Promise pushHistory?: (path: string) => void @@ -55,17 +56,6 @@ const SummonGrid = (props: Props) => { [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 useEffect(() => { let initialPreviousUncapValues: { [key: number]: number } = {} @@ -100,7 +90,7 @@ const SummonGrid = (props: Props) => { ) }) } else { - if (party.editable) + if (props.editable) saveSummon(party.id, summon, position) .then((response) => handleSummonResponse(response.data)) .catch((error) => { @@ -401,7 +391,7 @@ const SummonGrid = (props: Props) => {
    {t('summons.main')}
    {
    {t('summons.friend')}
    {
  • { const subAuraSummonElement = ( Promise pushHistory?: (path: string) => void @@ -60,17 +61,6 @@ const WeaponGrid = (props: Props) => { [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 useEffect(() => { let initialPreviousUncapValues: { [key: number]: number } = {} @@ -99,7 +89,7 @@ const WeaponGrid = (props: Props) => { }) }) } else { - if (party.editable) + if (props.editable) saveWeapon(party.id, weapon, position) .then((response) => { if (response) handleWeaponResponse(response.data) @@ -337,7 +327,7 @@ const WeaponGrid = (props: Props) => { const mainhandElement = ( {
  • { const extraGridElement = (