Fix cookie usage in Party and grid tabs
This commit is contained in:
parent
cdf25a42bf
commit
a3d1c1ee56
4 changed files with 875 additions and 947 deletions
|
|
@ -1,23 +1,23 @@
|
||||||
/* eslint-disable react-hooks/exhaustive-deps */
|
/* eslint-disable react-hooks/exhaustive-deps */
|
||||||
import React, { useCallback, useEffect, useMemo, useState } from 'react'
|
import React, { useCallback, useEffect, useMemo, useState } from "react"
|
||||||
import { useCookies } from 'react-cookie'
|
import { getCookie } from "cookies-next"
|
||||||
import { useSnapshot } from 'valtio'
|
import { useSnapshot } from "valtio"
|
||||||
|
|
||||||
import { AxiosResponse } from 'axios'
|
import { AxiosResponse } from "axios"
|
||||||
import debounce from 'lodash.debounce'
|
import debounce from "lodash.debounce"
|
||||||
|
|
||||||
import JobSection from '~components/JobSection'
|
import JobSection from "~components/JobSection"
|
||||||
import CharacterUnit from '~components/CharacterUnit'
|
import CharacterUnit from "~components/CharacterUnit"
|
||||||
|
|
||||||
import api from '~utils/api'
|
import api from "~utils/api"
|
||||||
import { appState } from '~utils/appState'
|
import { appState } from "~utils/appState"
|
||||||
|
|
||||||
import './index.scss'
|
import "./index.scss"
|
||||||
|
|
||||||
// Props
|
// Props
|
||||||
interface Props {
|
interface Props {
|
||||||
new: boolean
|
new: boolean
|
||||||
slug?: string
|
characters?: GridCharacter[]
|
||||||
createParty: () => Promise<AxiosResponse<any, any>>
|
createParty: () => Promise<AxiosResponse<any, any>>
|
||||||
pushHistory?: (path: string) => void
|
pushHistory?: (path: string) => void
|
||||||
}
|
}
|
||||||
|
|
@ -27,127 +27,85 @@ const CharacterGrid = (props: Props) => {
|
||||||
const numCharacters: number = 5
|
const numCharacters: number = 5
|
||||||
|
|
||||||
// Cookies
|
// Cookies
|
||||||
const [cookies] = useCookies(['account'])
|
const cookie = getCookie("account")
|
||||||
const headers = (cookies.account != null) ? {
|
const accountData: AccountCookie = cookie
|
||||||
headers: {
|
? JSON.parse(cookie as string)
|
||||||
'Authorization': `Bearer ${cookies.account.access_token}`
|
: null
|
||||||
}
|
const headers = accountData
|
||||||
} : {}
|
? { headers: { Authorization: `Bearer ${accountData.token}` } }
|
||||||
|
: {}
|
||||||
|
|
||||||
// Set up state for view management
|
// Set up state for view management
|
||||||
const { party, grid } = useSnapshot(appState)
|
const { party, grid } = useSnapshot(appState)
|
||||||
|
|
||||||
const [slug, setSlug] = useState()
|
const [slug, setSlug] = useState()
|
||||||
const [found, setFound] = useState(false)
|
|
||||||
const [loading, setLoading] = useState(true)
|
|
||||||
const [firstLoadComplete, setFirstLoadComplete] = useState(false)
|
|
||||||
|
|
||||||
// Create a temporary state to store previous character uncap values
|
// Create a temporary state to store previous character uncap values
|
||||||
const [previousUncapValues, setPreviousUncapValues] = useState<{[key: number]: number}>({})
|
const [previousUncapValues, setPreviousUncapValues] = useState<{
|
||||||
|
[key: number]: number
|
||||||
// Fetch data from the server
|
}>({})
|
||||||
useEffect(() => {
|
|
||||||
const shortcode = (props.slug) ? props.slug : slug
|
|
||||||
if (shortcode) fetchGrid(shortcode)
|
|
||||||
else appState.party.editable = true
|
|
||||||
}, [slug, props.slug])
|
|
||||||
|
|
||||||
// Set the editable flag only on first load
|
// Set the editable flag only on first load
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!loading && !firstLoadComplete) {
|
|
||||||
// If user is logged in and matches
|
// If user is logged in and matches
|
||||||
if ((cookies.account && party.user && cookies.account.user_id === party.user.id) || props.new)
|
if (
|
||||||
|
(accountData && party.user && accountData.userId === party.user.id) ||
|
||||||
|
props.new
|
||||||
|
)
|
||||||
appState.party.editable = true
|
appState.party.editable = true
|
||||||
else
|
else appState.party.editable = false
|
||||||
appState.party.editable = false
|
}, [props.new, accountData, party])
|
||||||
|
|
||||||
setFirstLoadComplete(true)
|
|
||||||
}
|
|
||||||
}, [props.new, cookies, party, loading, firstLoadComplete])
|
|
||||||
|
|
||||||
// Initialize an array of current uncap values for each characters
|
// Initialize an array of current uncap values for each characters
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
let initialPreviousUncapValues: { [key: number]: number } = {}
|
let initialPreviousUncapValues: { [key: number]: number } = {}
|
||||||
Object.values(appState.grid.characters).map(o => initialPreviousUncapValues[o.position] = o.uncap_level)
|
Object.values(appState.grid.characters).map(
|
||||||
|
(o) => (initialPreviousUncapValues[o.position] = o.uncap_level)
|
||||||
|
)
|
||||||
setPreviousUncapValues(initialPreviousUncapValues)
|
setPreviousUncapValues(initialPreviousUncapValues)
|
||||||
}, [appState.grid.characters])
|
}, [appState.grid.characters])
|
||||||
|
|
||||||
// Methods: Fetching an object from the server
|
|
||||||
async function fetchGrid(shortcode: string) {
|
|
||||||
return api.endpoints.parties.getOneWithObject({ id: shortcode, object: 'characters', params: headers })
|
|
||||||
.then(response => processResult(response))
|
|
||||||
.catch(error => processError(error))
|
|
||||||
}
|
|
||||||
|
|
||||||
function processResult(response: AxiosResponse) {
|
|
||||||
// Store the response
|
|
||||||
const party: Party = response.data.party
|
|
||||||
|
|
||||||
// Store the important party and state-keeping values
|
|
||||||
appState.party.id = party.id
|
|
||||||
appState.party.user = party.user
|
|
||||||
appState.party.favorited = party.favorited
|
|
||||||
appState.party.created_at = party.created_at
|
|
||||||
appState.party.updated_at = party.updated_at
|
|
||||||
|
|
||||||
setFound(true)
|
|
||||||
setLoading(false)
|
|
||||||
|
|
||||||
// Populate the weapons in state
|
|
||||||
populateCharacters(party.characters)
|
|
||||||
}
|
|
||||||
|
|
||||||
function processError(error: any) {
|
|
||||||
if (error.response != null) {
|
|
||||||
if (error.response.status == 404) {
|
|
||||||
setFound(false)
|
|
||||||
setLoading(false)
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
console.error(error)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function populateCharacters(list: Array<GridCharacter>) {
|
|
||||||
list.forEach((object: GridCharacter) => {
|
|
||||||
if (object.position != null)
|
|
||||||
appState.grid.characters[object.position] = object
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
// Methods: Adding an object from search
|
// Methods: Adding an object from search
|
||||||
function receiveCharacterFromSearch(object: Character | Weapon | Summon, position: number) {
|
function receiveCharacterFromSearch(
|
||||||
|
object: Character | Weapon | Summon,
|
||||||
|
position: number
|
||||||
|
) {
|
||||||
const character = object as Character
|
const character = object as Character
|
||||||
|
|
||||||
if (!party.id) {
|
if (!party.id) {
|
||||||
props.createParty()
|
props.createParty().then((response) => {
|
||||||
.then(response => {
|
|
||||||
const party = response.data.party
|
const party = response.data.party
|
||||||
appState.party.id = party.id
|
appState.party.id = party.id
|
||||||
setSlug(party.shortcode)
|
setSlug(party.shortcode)
|
||||||
|
|
||||||
if (props.pushHistory) props.pushHistory(`/p/${party.shortcode}`)
|
if (props.pushHistory) props.pushHistory(`/p/${party.shortcode}`)
|
||||||
saveCharacter(party.id, character, position)
|
saveCharacter(party.id, character, position)
|
||||||
.then(response => storeGridCharacter(response.data.grid_character))
|
.then((response) => storeGridCharacter(response.data.grid_character))
|
||||||
.catch(error => console.error(error))
|
.catch((error) => console.error(error))
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
if (party.editable)
|
if (party.editable)
|
||||||
saveCharacter(party.id, character, position)
|
saveCharacter(party.id, character, position)
|
||||||
.then(response => storeGridCharacter(response.data.grid_character))
|
.then((response) => storeGridCharacter(response.data.grid_character))
|
||||||
.catch(error => console.error(error))
|
.catch((error) => console.error(error))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function saveCharacter(partyId: string, character: Character, position: number) {
|
async function saveCharacter(
|
||||||
return await api.endpoints.characters.create({
|
partyId: string,
|
||||||
'character': {
|
character: Character,
|
||||||
'party_id': partyId,
|
position: number
|
||||||
'character_id': character.id,
|
) {
|
||||||
'position': position,
|
return await api.endpoints.characters.create(
|
||||||
'uncap_level': characterUncapLevel(character)
|
{
|
||||||
}
|
character: {
|
||||||
}, headers)
|
party_id: partyId,
|
||||||
|
character_id: character.id,
|
||||||
|
position: position,
|
||||||
|
uncap_level: characterUncapLevel(character),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
headers
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
function storeGridCharacter(gridCharacter: GridCharacter) {
|
function storeGridCharacter(gridCharacter: GridCharacter) {
|
||||||
|
|
@ -178,8 +136,9 @@ const CharacterGrid = (props: Props) => {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if (uncapLevel != previousUncapValues[position])
|
if (uncapLevel != previousUncapValues[position])
|
||||||
await api.updateUncap('character', id, uncapLevel)
|
await api.updateUncap("character", id, uncapLevel).then((response) => {
|
||||||
.then(response => { storeGridCharacter(response.data.grid_character) })
|
storeGridCharacter(response.data.grid_character)
|
||||||
|
})
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(error)
|
console.error(error)
|
||||||
|
|
||||||
|
|
@ -193,7 +152,11 @@ const CharacterGrid = (props: Props) => {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function initiateUncapUpdate(id: string, position: number, uncapLevel: number) {
|
function initiateUncapUpdate(
|
||||||
|
id: string,
|
||||||
|
position: number,
|
||||||
|
uncapLevel: number
|
||||||
|
) {
|
||||||
memoizeAction(id, position, uncapLevel)
|
memoizeAction(id, position, uncapLevel)
|
||||||
|
|
||||||
// Optimistically update UI
|
// Optimistically update UI
|
||||||
|
|
@ -203,13 +166,16 @@ const CharacterGrid = (props: Props) => {
|
||||||
const memoizeAction = useCallback(
|
const memoizeAction = useCallback(
|
||||||
(id: string, position: number, uncapLevel: number) => {
|
(id: string, position: number, uncapLevel: number) => {
|
||||||
debouncedAction(id, position, uncapLevel)
|
debouncedAction(id, position, uncapLevel)
|
||||||
}, [props, previousUncapValues]
|
},
|
||||||
|
[props, previousUncapValues]
|
||||||
)
|
)
|
||||||
|
|
||||||
const debouncedAction = useMemo(() =>
|
const debouncedAction = useMemo(
|
||||||
|
() =>
|
||||||
debounce((id, position, number) => {
|
debounce((id, position, number) => {
|
||||||
saveUncap(id, position, number)
|
saveUncap(id, position, number)
|
||||||
}, 500), [props, saveUncap]
|
}, 500),
|
||||||
|
[props, saveUncap]
|
||||||
)
|
)
|
||||||
|
|
||||||
const updateUncapLevel = (position: number, uncapLevel: number) => {
|
const updateUncapLevel = (position: number, uncapLevel: number) => {
|
||||||
|
|
|
||||||
|
|
@ -1,38 +1,41 @@
|
||||||
import React, { useCallback, useEffect, useMemo, useState } from 'react'
|
import React, { useCallback, useEffect, useMemo, useState } from "react"
|
||||||
import { useRouter } from 'next/router'
|
import { useRouter } from "next/router"
|
||||||
import { useSnapshot } from 'valtio'
|
import { useSnapshot } from "valtio"
|
||||||
import { useCookies } from 'react-cookie'
|
import { getCookie } from "cookies-next"
|
||||||
import clonedeep from 'lodash.clonedeep'
|
import clonedeep from "lodash.clonedeep"
|
||||||
import { subscribeKey } from 'valtio/utils'
|
|
||||||
|
|
||||||
import PartySegmentedControl from '~components/PartySegmentedControl'
|
import PartySegmentedControl from "~components/PartySegmentedControl"
|
||||||
import PartyDetails from '~components/PartyDetails'
|
import PartyDetails from "~components/PartyDetails"
|
||||||
import WeaponGrid from '~components/WeaponGrid'
|
import WeaponGrid from "~components/WeaponGrid"
|
||||||
import SummonGrid from '~components/SummonGrid'
|
import SummonGrid from "~components/SummonGrid"
|
||||||
import CharacterGrid from '~components/CharacterGrid'
|
import CharacterGrid from "~components/CharacterGrid"
|
||||||
|
|
||||||
import api from '~utils/api'
|
import api from "~utils/api"
|
||||||
import { appState, initialAppState } from '~utils/appState'
|
import { appState, initialAppState } from "~utils/appState"
|
||||||
import { GridType, TeamElement } from '~utils/enums'
|
import { GridType, TeamElement } from "~utils/enums"
|
||||||
|
|
||||||
import './index.scss'
|
import "./index.scss"
|
||||||
import { AxiosResponse } from 'axios'
|
|
||||||
|
|
||||||
// Props
|
// Props
|
||||||
interface Props {
|
interface Props {
|
||||||
new?: boolean
|
new?: boolean
|
||||||
slug?: string
|
team?: Party
|
||||||
|
raids: Raid[][]
|
||||||
pushHistory?: (path: string) => void
|
pushHistory?: (path: string) => void
|
||||||
}
|
}
|
||||||
|
|
||||||
const Party = (props: Props) => {
|
const Party = (props: Props) => {
|
||||||
// Cookies
|
// Cookies
|
||||||
const [cookies] = useCookies(['account'])
|
const cookie = getCookie("account")
|
||||||
|
const accountData: AccountCookie = cookie
|
||||||
|
? JSON.parse(cookie as string)
|
||||||
|
: null
|
||||||
|
|
||||||
const headers = useMemo(() => {
|
const headers = useMemo(() => {
|
||||||
return (cookies.account != null) ? {
|
return accountData
|
||||||
headers: { 'Authorization': `Bearer ${cookies.account.access_token}` }
|
? { headers: { Authorization: `Bearer ${accountData.token}` } }
|
||||||
} : {}
|
: {}
|
||||||
}, [cookies.account])
|
}, [accountData])
|
||||||
|
|
||||||
// Set up router
|
// Set up router
|
||||||
const router = useRouter()
|
const router = useRouter()
|
||||||
|
|
@ -48,6 +51,7 @@ const Party = (props: Props) => {
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const resetState = clonedeep(initialAppState)
|
const resetState = clonedeep(initialAppState)
|
||||||
appState.grid = resetState.grid
|
appState.grid = resetState.grid
|
||||||
|
if (props.team) storeParty(props.team)
|
||||||
}, [])
|
}, [])
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
|
@ -62,9 +66,9 @@ const Party = (props: Props) => {
|
||||||
async function createParty(extra: boolean = false) {
|
async function createParty(extra: boolean = false) {
|
||||||
let body = {
|
let body = {
|
||||||
party: {
|
party: {
|
||||||
...(cookies.account) && { user_id: cookies.account.user_id },
|
...(accountData && { user_id: accountData.userId }),
|
||||||
extra: extra
|
extra: extra,
|
||||||
}
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
return await api.endpoints.parties.create(body, headers)
|
return await api.endpoints.parties.create(body, headers)
|
||||||
|
|
@ -75,32 +79,47 @@ const Party = (props: Props) => {
|
||||||
appState.party.extra = event.target.checked
|
appState.party.extra = event.target.checked
|
||||||
|
|
||||||
if (party.id) {
|
if (party.id) {
|
||||||
api.endpoints.parties.update(party.id, {
|
api.endpoints.parties.update(
|
||||||
'party': { 'extra': event.target.checked }
|
party.id,
|
||||||
}, headers)
|
{
|
||||||
|
party: { extra: event.target.checked },
|
||||||
|
},
|
||||||
|
headers
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function jobChanged() {
|
function jobChanged() {
|
||||||
if (party.id) {
|
if (party.id) {
|
||||||
api.endpoints.parties.update(party.id, {
|
api.endpoints.parties.update(
|
||||||
'party': { 'job_id': (job) ? job.id : '' }
|
party.id,
|
||||||
}, headers)
|
{
|
||||||
|
party: { job_id: job ? job.id : "" },
|
||||||
|
},
|
||||||
|
headers
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function updateDetails(name?: string, description?: string, raid?: Raid) {
|
function updateDetails(name?: string, description?: string, raid?: Raid) {
|
||||||
if (appState.party.name !== name ||
|
if (
|
||||||
|
appState.party.name !== name ||
|
||||||
appState.party.description !== description ||
|
appState.party.description !== description ||
|
||||||
appState.party.raid?.id !== raid?.id) {
|
appState.party.raid?.id !== raid?.id
|
||||||
|
) {
|
||||||
if (appState.party.id)
|
if (appState.party.id)
|
||||||
api.endpoints.parties.update(appState.party.id, {
|
api.endpoints.parties
|
||||||
'party': {
|
.update(
|
||||||
'name': name,
|
appState.party.id,
|
||||||
'description': description,
|
{
|
||||||
'raid_id': raid?.id
|
party: {
|
||||||
}
|
name: name,
|
||||||
}, headers)
|
description: description,
|
||||||
|
raid_id: raid?.id,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
headers
|
||||||
|
)
|
||||||
.then(() => {
|
.then(() => {
|
||||||
appState.party.name = name
|
appState.party.name = name
|
||||||
appState.party.description = description
|
appState.party.description = description
|
||||||
|
|
@ -113,10 +132,11 @@ const Party = (props: Props) => {
|
||||||
// Deleting the party
|
// Deleting the party
|
||||||
function deleteTeam(event: React.MouseEvent<HTMLButtonElement, MouseEvent>) {
|
function deleteTeam(event: React.MouseEvent<HTMLButtonElement, MouseEvent>) {
|
||||||
if (appState.party.editable && appState.party.id) {
|
if (appState.party.editable && appState.party.id) {
|
||||||
api.endpoints.parties.destroy({ id: appState.party.id, params: headers })
|
api.endpoints.parties
|
||||||
|
.destroy({ id: appState.party.id, params: headers })
|
||||||
.then(() => {
|
.then(() => {
|
||||||
// Push to route
|
// Push to route
|
||||||
router.push('/')
|
router.push("/")
|
||||||
|
|
||||||
// Clean state
|
// Clean state
|
||||||
const resetState = clonedeep(initialAppState)
|
const resetState = clonedeep(initialAppState)
|
||||||
|
|
@ -133,19 +153,67 @@ const Party = (props: Props) => {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Methods: Storing party data
|
||||||
|
const storeParty = function (party: Party) {
|
||||||
|
// Store the important party and state-keeping values
|
||||||
|
appState.party.id = party.id
|
||||||
|
appState.party.extra = party.extra
|
||||||
|
appState.party.user = party.user
|
||||||
|
appState.party.favorited = party.favorited
|
||||||
|
appState.party.created_at = party.created_at
|
||||||
|
appState.party.updated_at = party.updated_at
|
||||||
|
|
||||||
|
// Populate state
|
||||||
|
storeCharacters(party.characters)
|
||||||
|
storeWeapons(party.weapons)
|
||||||
|
storeSummons(party.summons)
|
||||||
|
}
|
||||||
|
|
||||||
|
const storeCharacters = (list: Array<GridCharacter>) => {
|
||||||
|
list.forEach((object: GridCharacter) => {
|
||||||
|
if (object.position != null)
|
||||||
|
appState.grid.characters[object.position] = object
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
const storeWeapons = (list: Array<GridWeapon>) => {
|
||||||
|
list.forEach((gridObject: GridWeapon) => {
|
||||||
|
if (gridObject.mainhand) {
|
||||||
|
appState.grid.weapons.mainWeapon = gridObject
|
||||||
|
appState.party.element = gridObject.object.element
|
||||||
|
} else if (!gridObject.mainhand && gridObject.position != null) {
|
||||||
|
appState.grid.weapons.allWeapons[gridObject.position] = gridObject
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
const storeSummons = (list: Array<GridSummon>) => {
|
||||||
|
list.forEach((gridObject: GridSummon) => {
|
||||||
|
if (gridObject.main) appState.grid.summons.mainSummon = gridObject
|
||||||
|
else if (gridObject.friend)
|
||||||
|
appState.grid.summons.friendSummon = gridObject
|
||||||
|
else if (
|
||||||
|
!gridObject.main &&
|
||||||
|
!gridObject.friend &&
|
||||||
|
gridObject.position != null
|
||||||
|
)
|
||||||
|
appState.grid.summons.allSummons[gridObject.position] = gridObject
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
// Methods: Navigating with segmented control
|
// Methods: Navigating with segmented control
|
||||||
function segmentClicked(event: React.ChangeEvent<HTMLInputElement>) {
|
function segmentClicked(event: React.ChangeEvent<HTMLInputElement>) {
|
||||||
switch (event.target.value) {
|
switch (event.target.value) {
|
||||||
case 'class':
|
case "class":
|
||||||
setCurrentTab(GridType.Class)
|
setCurrentTab(GridType.Class)
|
||||||
break
|
break
|
||||||
case 'characters':
|
case "characters":
|
||||||
setCurrentTab(GridType.Character)
|
setCurrentTab(GridType.Character)
|
||||||
break
|
break
|
||||||
case 'weapons':
|
case "weapons":
|
||||||
setCurrentTab(GridType.Weapon)
|
setCurrentTab(GridType.Weapon)
|
||||||
break
|
break
|
||||||
case 'summons':
|
case "summons":
|
||||||
setCurrentTab(GridType.Summon)
|
setCurrentTab(GridType.Summon)
|
||||||
break
|
break
|
||||||
default:
|
default:
|
||||||
|
|
@ -153,42 +221,6 @@ const Party = (props: Props) => {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Methods: Fetch party details
|
|
||||||
const processResult = useCallback((response: AxiosResponse) => {
|
|
||||||
appState.party.id = response.data.party.id
|
|
||||||
appState.party.user = response.data.party.user
|
|
||||||
appState.party.favorited = response.data.party.favorited
|
|
||||||
appState.party.created_at = response.data.party.created_at
|
|
||||||
appState.party.updated_at = response.data.party.updated_at
|
|
||||||
|
|
||||||
// Store the party's user-generated details
|
|
||||||
appState.party.name = response.data.party.name
|
|
||||||
appState.party.description = response.data.party.description
|
|
||||||
appState.party.raid = response.data.party.raid
|
|
||||||
appState.party.job = response.data.party.job
|
|
||||||
}, [])
|
|
||||||
|
|
||||||
const handleError = useCallback((error: any) => {
|
|
||||||
if (error.response != null && error.response.status == 404) {
|
|
||||||
// setFound(false)
|
|
||||||
} else if (error.response != null) {
|
|
||||||
console.error(error)
|
|
||||||
} else {
|
|
||||||
console.error("There was an error.")
|
|
||||||
}
|
|
||||||
}, [])
|
|
||||||
|
|
||||||
const fetchDetails = useCallback((shortcode: string) => {
|
|
||||||
return api.endpoints.parties.getOne({ id: shortcode, params: headers })
|
|
||||||
.then(response => processResult(response))
|
|
||||||
.catch(error => handleError(error))
|
|
||||||
}, [headers, processResult, handleError])
|
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
const shortcode = (props.slug) ? props.slug : undefined
|
|
||||||
if (shortcode) fetchDetails(shortcode)
|
|
||||||
}, [props.slug, fetchDetails])
|
|
||||||
|
|
||||||
// Render: JSX components
|
// Render: JSX components
|
||||||
const navigation = (
|
const navigation = (
|
||||||
<PartySegmentedControl
|
<PartySegmentedControl
|
||||||
|
|
@ -201,7 +233,7 @@ const Party = (props: Props) => {
|
||||||
const weaponGrid = (
|
const weaponGrid = (
|
||||||
<WeaponGrid
|
<WeaponGrid
|
||||||
new={props.new || false}
|
new={props.new || false}
|
||||||
slug={props.slug}
|
weapons={props.team?.weapons}
|
||||||
createParty={createParty}
|
createParty={createParty}
|
||||||
pushHistory={props.pushHistory}
|
pushHistory={props.pushHistory}
|
||||||
/>
|
/>
|
||||||
|
|
@ -210,7 +242,7 @@ const Party = (props: Props) => {
|
||||||
const summonGrid = (
|
const summonGrid = (
|
||||||
<SummonGrid
|
<SummonGrid
|
||||||
new={props.new || false}
|
new={props.new || false}
|
||||||
slug={props.slug}
|
summons={props.team?.summons}
|
||||||
createParty={createParty}
|
createParty={createParty}
|
||||||
pushHistory={props.pushHistory}
|
pushHistory={props.pushHistory}
|
||||||
/>
|
/>
|
||||||
|
|
@ -219,7 +251,7 @@ const Party = (props: Props) => {
|
||||||
const characterGrid = (
|
const characterGrid = (
|
||||||
<CharacterGrid
|
<CharacterGrid
|
||||||
new={props.new || false}
|
new={props.new || false}
|
||||||
slug={props.slug}
|
characters={props.team?.characters}
|
||||||
createParty={createParty}
|
createParty={createParty}
|
||||||
pushHistory={props.pushHistory}
|
pushHistory={props.pushHistory}
|
||||||
/>
|
/>
|
||||||
|
|
@ -239,14 +271,14 @@ const Party = (props: Props) => {
|
||||||
return (
|
return (
|
||||||
<div>
|
<div>
|
||||||
{navigation}
|
{navigation}
|
||||||
<section id="Party">
|
<section id="Party">{currentGrid()}</section>
|
||||||
{ currentGrid() }
|
{
|
||||||
</section>
|
<PartyDetails
|
||||||
{ <PartyDetails
|
|
||||||
editable={party.editable}
|
editable={party.editable}
|
||||||
updateCallback={updateDetails}
|
updateCallback={updateDetails}
|
||||||
deleteCallback={deleteTeam}
|
deleteCallback={deleteTeam}
|
||||||
/>}
|
/>
|
||||||
|
}
|
||||||
</div>
|
</div>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,24 +1,24 @@
|
||||||
/* eslint-disable react-hooks/exhaustive-deps */
|
/* eslint-disable react-hooks/exhaustive-deps */
|
||||||
import React, { useCallback, useEffect, useMemo, useState } from 'react'
|
import React, { useCallback, useEffect, useMemo, useState } from "react"
|
||||||
import { useCookies } from 'react-cookie'
|
import { getCookie } from "cookies-next"
|
||||||
import { useSnapshot } from 'valtio'
|
import { useSnapshot } from "valtio"
|
||||||
import { useTranslation } from 'next-i18next'
|
import { useTranslation } from "next-i18next"
|
||||||
|
|
||||||
import { AxiosResponse } from 'axios'
|
import { AxiosResponse } from "axios"
|
||||||
import debounce from 'lodash.debounce'
|
import debounce from "lodash.debounce"
|
||||||
|
|
||||||
import SummonUnit from '~components/SummonUnit'
|
import SummonUnit from "~components/SummonUnit"
|
||||||
import ExtraSummons from '~components/ExtraSummons'
|
import ExtraSummons from "~components/ExtraSummons"
|
||||||
|
|
||||||
import api from '~utils/api'
|
import api from "~utils/api"
|
||||||
import { appState } from '~utils/appState'
|
import { appState } from "~utils/appState"
|
||||||
|
|
||||||
import './index.scss'
|
import "./index.scss"
|
||||||
|
|
||||||
// Props
|
// Props
|
||||||
interface Props {
|
interface Props {
|
||||||
new: boolean
|
new: boolean
|
||||||
slug?: string
|
summons?: GridSummon[]
|
||||||
createParty: () => Promise<AxiosResponse<any, any>>
|
createParty: () => Promise<AxiosResponse<any, any>>
|
||||||
pushHistory?: (path: string) => void
|
pushHistory?: (path: string) => void
|
||||||
}
|
}
|
||||||
|
|
@ -27,130 +27,85 @@ const SummonGrid = (props: Props) => {
|
||||||
// Constants
|
// Constants
|
||||||
const numSummons: number = 4
|
const numSummons: number = 4
|
||||||
|
|
||||||
const { t } = useTranslation('common')
|
|
||||||
|
|
||||||
// Cookies
|
// Cookies
|
||||||
const [cookies, _] = useCookies(['account'])
|
const cookie = getCookie("account")
|
||||||
const headers = (cookies.account != null) ? {
|
const accountData: AccountCookie = cookie
|
||||||
headers: {
|
? JSON.parse(cookie as string)
|
||||||
'Authorization': `Bearer ${cookies.account.access_token}`
|
: null
|
||||||
}
|
const headers = accountData
|
||||||
} : {}
|
? { headers: { Authorization: `Bearer ${accountData.token}` } }
|
||||||
|
: {}
|
||||||
|
|
||||||
|
// Localization
|
||||||
|
const { t } = useTranslation("common")
|
||||||
|
|
||||||
// Set up state for view management
|
// Set up state for view management
|
||||||
const { party, grid } = useSnapshot(appState)
|
const { party, grid } = useSnapshot(appState)
|
||||||
|
|
||||||
const [slug, setSlug] = useState()
|
const [slug, setSlug] = useState()
|
||||||
const [found, setFound] = useState(false)
|
|
||||||
const [loading, setLoading] = useState(true)
|
|
||||||
const [firstLoadComplete, setFirstLoadComplete] = useState(false)
|
|
||||||
|
|
||||||
// Create a temporary state to store previous weapon uncap value
|
// Create a temporary state to store previous weapon uncap value
|
||||||
const [previousUncapValues, setPreviousUncapValues] = useState<{[key: number]: number}>({})
|
const [previousUncapValues, setPreviousUncapValues] = useState<{
|
||||||
|
[key: number]: number
|
||||||
// Fetch data from the server
|
}>({})
|
||||||
useEffect(() => {
|
|
||||||
const shortcode = (props.slug) ? props.slug : slug
|
|
||||||
if (shortcode) fetchGrid(shortcode)
|
|
||||||
else appState.party.editable = true
|
|
||||||
}, [slug, props.slug])
|
|
||||||
|
|
||||||
// Set the editable flag only on first load
|
// Set the editable flag only on first load
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!loading && !firstLoadComplete) {
|
|
||||||
// If user is logged in and matches
|
// If user is logged in and matches
|
||||||
if ((cookies.account && party.user && cookies.account.user_id === party.user.id) || props.new)
|
if (
|
||||||
|
(accountData && party.user && accountData.userId === party.user.id) ||
|
||||||
|
props.new
|
||||||
|
)
|
||||||
appState.party.editable = true
|
appState.party.editable = true
|
||||||
else
|
else appState.party.editable = false
|
||||||
appState.party.editable = false
|
}, [props.new, accountData, party])
|
||||||
|
|
||||||
setFirstLoadComplete(true)
|
|
||||||
}
|
|
||||||
}, [props.new, cookies, party, loading, firstLoadComplete])
|
|
||||||
|
|
||||||
// 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 } = {}
|
||||||
|
|
||||||
if (appState.grid.summons.mainSummon)
|
if (appState.grid.summons.mainSummon)
|
||||||
initialPreviousUncapValues[-1] = appState.grid.summons.mainSummon.uncap_level
|
initialPreviousUncapValues[-1] =
|
||||||
|
appState.grid.summons.mainSummon.uncap_level
|
||||||
|
|
||||||
if (appState.grid.summons.friendSummon)
|
if (appState.grid.summons.friendSummon)
|
||||||
initialPreviousUncapValues[6] = appState.grid.summons.friendSummon.uncap_level
|
initialPreviousUncapValues[6] =
|
||||||
|
appState.grid.summons.friendSummon.uncap_level
|
||||||
|
|
||||||
Object.values(appState.grid.summons.allSummons).map(o => initialPreviousUncapValues[o.position] = o.uncap_level)
|
Object.values(appState.grid.summons.allSummons).map(
|
||||||
|
(o) => (initialPreviousUncapValues[o.position] = o.uncap_level)
|
||||||
|
)
|
||||||
|
|
||||||
setPreviousUncapValues(initialPreviousUncapValues)
|
setPreviousUncapValues(initialPreviousUncapValues)
|
||||||
}, [appState.grid.summons.mainSummon, appState.grid.summons.friendSummon, appState.grid.summons.allSummons])
|
}, [
|
||||||
|
appState.grid.summons.mainSummon,
|
||||||
|
appState.grid.summons.friendSummon,
|
||||||
// Methods: Fetching an object from the server
|
appState.grid.summons.allSummons,
|
||||||
async function fetchGrid(shortcode: string) {
|
])
|
||||||
return api.endpoints.parties.getOneWithObject({ id: shortcode, object: 'summons', params: headers })
|
|
||||||
.then(response => processResult(response))
|
|
||||||
.catch(error => processError(error))
|
|
||||||
}
|
|
||||||
|
|
||||||
function processResult(response: AxiosResponse) {
|
|
||||||
// Store the response
|
|
||||||
const party: Party = response.data.party
|
|
||||||
|
|
||||||
// Store the important party and state-keeping values
|
|
||||||
appState.party.id = party.id
|
|
||||||
appState.party.user = party.user
|
|
||||||
appState.party.favorited = party.favorited
|
|
||||||
appState.party.created_at = party.created_at
|
|
||||||
appState.party.updated_at = party.updated_at
|
|
||||||
|
|
||||||
setFound(true)
|
|
||||||
setLoading(false)
|
|
||||||
|
|
||||||
// Populate the weapons in state
|
|
||||||
populateSummons(party.summons)
|
|
||||||
}
|
|
||||||
|
|
||||||
function processError(error: any) {
|
|
||||||
if (error.response != null) {
|
|
||||||
if (error.response.status == 404) {
|
|
||||||
setFound(false)
|
|
||||||
setLoading(false)
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
console.error(error)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function populateSummons(list: Array<GridSummon>) {
|
|
||||||
list.forEach((gridObject: GridSummon) => {
|
|
||||||
if (gridObject.main)
|
|
||||||
appState.grid.summons.mainSummon = gridObject
|
|
||||||
else if (gridObject.friend)
|
|
||||||
appState.grid.summons.friendSummon = gridObject
|
|
||||||
else if (!gridObject.main && !gridObject.friend && gridObject.position != null)
|
|
||||||
appState.grid.summons.allSummons[gridObject.position] = gridObject
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
// Methods: Adding an object from search
|
// Methods: Adding an object from search
|
||||||
function receiveSummonFromSearch(object: Character | Weapon | Summon, position: number) {
|
function receiveSummonFromSearch(
|
||||||
|
object: Character | Weapon | Summon,
|
||||||
|
position: number
|
||||||
|
) {
|
||||||
const summon = object as Summon
|
const summon = object as Summon
|
||||||
|
|
||||||
if (!party.id) {
|
if (!party.id) {
|
||||||
props.createParty()
|
props.createParty().then((response) => {
|
||||||
.then(response => {
|
|
||||||
const party = response.data.party
|
const party = response.data.party
|
||||||
appState.party.id = party.id
|
appState.party.id = party.id
|
||||||
setSlug(party.shortcode)
|
setSlug(party.shortcode)
|
||||||
|
|
||||||
if (props.pushHistory) props.pushHistory(`/p/${party.shortcode}`)
|
if (props.pushHistory) props.pushHistory(`/p/${party.shortcode}`)
|
||||||
|
|
||||||
saveSummon(party.id, summon, position)
|
saveSummon(party.id, summon, position).then((response) =>
|
||||||
.then(response => storeGridSummon(response.data.grid_summon))
|
storeGridSummon(response.data.grid_summon)
|
||||||
|
)
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
if (party.editable)
|
if (party.editable)
|
||||||
saveSummon(party.id, summon, position)
|
saveSummon(party.id, summon, position).then((response) =>
|
||||||
.then(response => storeGridSummon(response.data.grid_summon))
|
storeGridSummon(response.data.grid_summon)
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -159,25 +114,26 @@ const SummonGrid = (props: Props) => {
|
||||||
if (summon.uncap.ulb) uncapLevel = 5
|
if (summon.uncap.ulb) uncapLevel = 5
|
||||||
else if (summon.uncap.flb) uncapLevel = 4
|
else if (summon.uncap.flb) uncapLevel = 4
|
||||||
|
|
||||||
return await api.endpoints.summons.create({
|
return await api.endpoints.summons.create(
|
||||||
'summon': {
|
{
|
||||||
'party_id': partyId,
|
summon: {
|
||||||
'summon_id': summon.id,
|
party_id: partyId,
|
||||||
'position': position,
|
summon_id: summon.id,
|
||||||
'main': (position == -1),
|
position: position,
|
||||||
'friend': (position == 6),
|
main: position == -1,
|
||||||
'uncap_level': uncapLevel
|
friend: position == 6,
|
||||||
}
|
uncap_level: uncapLevel,
|
||||||
}, headers)
|
},
|
||||||
|
},
|
||||||
|
headers
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
function storeGridSummon(gridSummon: GridSummon) {
|
function storeGridSummon(gridSummon: GridSummon) {
|
||||||
if (gridSummon.position == -1)
|
if (gridSummon.position == -1) appState.grid.summons.mainSummon = gridSummon
|
||||||
appState.grid.summons.mainSummon = gridSummon
|
|
||||||
else if (gridSummon.position == 6)
|
else if (gridSummon.position == 6)
|
||||||
appState.grid.summons.friendSummon = gridSummon
|
appState.grid.summons.friendSummon = gridSummon
|
||||||
else
|
else appState.grid.summons.allSummons[gridSummon.position] = gridSummon
|
||||||
appState.grid.summons.allSummons[gridSummon.position] = gridSummon
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Methods: Updating uncap level
|
// Methods: Updating uncap level
|
||||||
|
|
@ -187,8 +143,9 @@ const SummonGrid = (props: Props) => {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if (uncapLevel != previousUncapValues[position])
|
if (uncapLevel != previousUncapValues[position])
|
||||||
await api.updateUncap('summon', id, uncapLevel)
|
await api.updateUncap("summon", id, uncapLevel).then((response) => {
|
||||||
.then(response => { storeGridSummon(response.data.grid_summon) })
|
storeGridSummon(response.data.grid_summon)
|
||||||
|
})
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(error)
|
console.error(error)
|
||||||
|
|
||||||
|
|
@ -202,7 +159,11 @@ const SummonGrid = (props: Props) => {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function initiateUncapUpdate(id: string, position: number, uncapLevel: number) {
|
function initiateUncapUpdate(
|
||||||
|
id: string,
|
||||||
|
position: number,
|
||||||
|
uncapLevel: number
|
||||||
|
) {
|
||||||
memoizeAction(id, position, uncapLevel)
|
memoizeAction(id, position, uncapLevel)
|
||||||
|
|
||||||
// Optimistically update UI
|
// Optimistically update UI
|
||||||
|
|
@ -212,13 +173,16 @@ const SummonGrid = (props: Props) => {
|
||||||
const memoizeAction = useCallback(
|
const memoizeAction = useCallback(
|
||||||
(id: string, position: number, uncapLevel: number) => {
|
(id: string, position: number, uncapLevel: number) => {
|
||||||
debouncedAction(id, position, uncapLevel)
|
debouncedAction(id, position, uncapLevel)
|
||||||
}, [props, previousUncapValues]
|
},
|
||||||
|
[props, previousUncapValues]
|
||||||
)
|
)
|
||||||
|
|
||||||
const debouncedAction = useMemo(() =>
|
const debouncedAction = useMemo(
|
||||||
|
() =>
|
||||||
debounce((id, position, number) => {
|
debounce((id, position, number) => {
|
||||||
saveUncap(id, position, number)
|
saveUncap(id, position, number)
|
||||||
}, 500), [props, saveUncap]
|
}, 500),
|
||||||
|
[props, saveUncap]
|
||||||
)
|
)
|
||||||
|
|
||||||
const updateUncapLevel = (position: number, uncapLevel: number) => {
|
const updateUncapLevel = (position: number, uncapLevel: number) => {
|
||||||
|
|
@ -226,17 +190,21 @@ const SummonGrid = (props: Props) => {
|
||||||
appState.grid.summons.mainSummon.uncap_level = uncapLevel
|
appState.grid.summons.mainSummon.uncap_level = uncapLevel
|
||||||
else if (appState.grid.summons.friendSummon && position == 6)
|
else if (appState.grid.summons.friendSummon && position == 6)
|
||||||
appState.grid.summons.friendSummon.uncap_level = uncapLevel
|
appState.grid.summons.friendSummon.uncap_level = uncapLevel
|
||||||
else
|
else appState.grid.summons.allSummons[position].uncap_level = uncapLevel
|
||||||
appState.grid.summons.allSummons[position].uncap_level = uncapLevel
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function storePreviousUncapValue(position: number) {
|
function storePreviousUncapValue(position: number) {
|
||||||
// Save the current value in case of an unexpected result
|
// Save the current value in case of an unexpected result
|
||||||
let newPreviousValues = { ...previousUncapValues }
|
let newPreviousValues = { ...previousUncapValues }
|
||||||
|
|
||||||
if (appState.grid.summons.mainSummon && position == -1) newPreviousValues[position] = appState.grid.summons.mainSummon.uncap_level
|
if (appState.grid.summons.mainSummon && position == -1)
|
||||||
else if (appState.grid.summons.friendSummon && position == 6) newPreviousValues[position] = appState.grid.summons.friendSummon.uncap_level
|
newPreviousValues[position] = appState.grid.summons.mainSummon.uncap_level
|
||||||
else newPreviousValues[position] = appState.grid.summons.allSummons[position].uncap_level
|
else if (appState.grid.summons.friendSummon && position == 6)
|
||||||
|
newPreviousValues[position] =
|
||||||
|
appState.grid.summons.friendSummon.uncap_level
|
||||||
|
else
|
||||||
|
newPreviousValues[position] =
|
||||||
|
appState.grid.summons.allSummons[position].uncap_level
|
||||||
|
|
||||||
setPreviousUncapValues(newPreviousValues)
|
setPreviousUncapValues(newPreviousValues)
|
||||||
}
|
}
|
||||||
|
|
@ -244,7 +212,7 @@ const SummonGrid = (props: Props) => {
|
||||||
// Render: JSX components
|
// Render: JSX components
|
||||||
const mainSummonElement = (
|
const mainSummonElement = (
|
||||||
<div className="LabeledUnit">
|
<div className="LabeledUnit">
|
||||||
<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={party.editable}
|
||||||
|
|
@ -259,7 +227,7 @@ const SummonGrid = (props: Props) => {
|
||||||
|
|
||||||
const friendSummonElement = (
|
const friendSummonElement = (
|
||||||
<div className="LabeledUnit">
|
<div className="LabeledUnit">
|
||||||
<div className="Label">{t('summons.friend')}</div>
|
<div className="Label">{t("summons.friend")}</div>
|
||||||
<SummonUnit
|
<SummonUnit
|
||||||
gridSummon={grid.summons.friendSummon}
|
gridSummon={grid.summons.friendSummon}
|
||||||
editable={party.editable}
|
editable={party.editable}
|
||||||
|
|
@ -273,10 +241,11 @@ const SummonGrid = (props: Props) => {
|
||||||
)
|
)
|
||||||
const summonGridElement = (
|
const summonGridElement = (
|
||||||
<div id="LabeledGrid">
|
<div id="LabeledGrid">
|
||||||
<div className="Label">{t('summons.summons')}</div>
|
<div className="Label">{t("summons.summons")}</div>
|
||||||
<ul id="grid_summons">
|
<ul id="grid_summons">
|
||||||
{Array.from(Array(numSummons)).map((x, i) => {
|
{Array.from(Array(numSummons)).map((x, i) => {
|
||||||
return (<li key={`grid_unit_${i}`} >
|
return (
|
||||||
|
<li key={`grid_unit_${i}`}>
|
||||||
<SummonUnit
|
<SummonUnit
|
||||||
gridSummon={grid.summons.allSummons[i]}
|
gridSummon={grid.summons.allSummons[i]}
|
||||||
editable={party.editable}
|
editable={party.editable}
|
||||||
|
|
@ -285,7 +254,8 @@ const SummonGrid = (props: Props) => {
|
||||||
updateObject={receiveSummonFromSearch}
|
updateObject={receiveSummonFromSearch}
|
||||||
updateUncap={initiateUncapUpdate}
|
updateUncap={initiateUncapUpdate}
|
||||||
/>
|
/>
|
||||||
</li>)
|
</li>
|
||||||
|
)
|
||||||
})}
|
})}
|
||||||
</ul>
|
</ul>
|
||||||
</div>
|
</div>
|
||||||
|
|
|
||||||
|
|
@ -1,23 +1,23 @@
|
||||||
/* eslint-disable react-hooks/exhaustive-deps */
|
/* eslint-disable react-hooks/exhaustive-deps */
|
||||||
import React, { useCallback, useEffect, useMemo, useState } from 'react'
|
import React, { useCallback, useEffect, useMemo, useState } from "react"
|
||||||
import { useCookies } from 'react-cookie'
|
import { getCookie } from "cookies-next"
|
||||||
import { useSnapshot } from 'valtio'
|
import { useSnapshot } from "valtio"
|
||||||
|
|
||||||
import { AxiosResponse } from 'axios'
|
import { AxiosResponse } from "axios"
|
||||||
import debounce from 'lodash.debounce'
|
import debounce from "lodash.debounce"
|
||||||
|
|
||||||
import WeaponUnit from '~components/WeaponUnit'
|
import WeaponUnit from "~components/WeaponUnit"
|
||||||
import ExtraWeapons from '~components/ExtraWeapons'
|
import ExtraWeapons from "~components/ExtraWeapons"
|
||||||
|
|
||||||
import api from '~utils/api'
|
import api from "~utils/api"
|
||||||
import { appState } from '~utils/appState'
|
import { appState } from "~utils/appState"
|
||||||
|
|
||||||
import './index.scss'
|
import "./index.scss"
|
||||||
|
|
||||||
// Props
|
// Props
|
||||||
interface Props {
|
interface Props {
|
||||||
new: boolean
|
new: boolean
|
||||||
slug?: string
|
weapons?: GridWeapon[]
|
||||||
createParty: (extra: boolean) => Promise<AxiosResponse<any, any>>
|
createParty: (extra: boolean) => Promise<AxiosResponse<any, any>>
|
||||||
pushHistory?: (path: string) => void
|
pushHistory?: (path: string) => void
|
||||||
}
|
}
|
||||||
|
|
@ -27,125 +27,73 @@ const WeaponGrid = (props: Props) => {
|
||||||
const numWeapons: number = 9
|
const numWeapons: number = 9
|
||||||
|
|
||||||
// Cookies
|
// Cookies
|
||||||
const [cookies] = useCookies(['account'])
|
const cookie = getCookie("account")
|
||||||
const headers = (cookies.account != null) ? {
|
const accountData: AccountCookie = cookie
|
||||||
headers: {
|
? JSON.parse(cookie as string)
|
||||||
'Authorization': `Bearer ${cookies.account.access_token}`
|
: null
|
||||||
}
|
const headers = accountData
|
||||||
} : {}
|
? { headers: { Authorization: `Bearer ${accountData.token}` } }
|
||||||
|
: {}
|
||||||
|
|
||||||
// Set up state for view management
|
// Set up state for view management
|
||||||
const { party, grid } = useSnapshot(appState)
|
const { party, grid } = useSnapshot(appState)
|
||||||
|
|
||||||
const [slug, setSlug] = useState()
|
const [slug, setSlug] = useState()
|
||||||
const [found, setFound] = useState(false)
|
|
||||||
const [loading, setLoading] = useState(true)
|
|
||||||
const [firstLoadComplete, setFirstLoadComplete] = useState(false)
|
|
||||||
|
|
||||||
// Create a temporary state to store previous weapon uncap values
|
// Create a temporary state to store previous weapon uncap values
|
||||||
const [previousUncapValues, setPreviousUncapValues] = useState<{[key: number]: number}>({})
|
const [previousUncapValues, setPreviousUncapValues] = useState<{
|
||||||
|
[key: number]: number
|
||||||
// Fetch data from the server
|
}>({})
|
||||||
useEffect(() => {
|
|
||||||
const shortcode = (props.slug) ? props.slug : slug
|
|
||||||
if (shortcode) fetchGrid(shortcode)
|
|
||||||
else appState.party.editable = true
|
|
||||||
}, [slug, props.slug])
|
|
||||||
|
|
||||||
// Set the editable flag only on first load
|
// Set the editable flag only on first load
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!loading && !firstLoadComplete) {
|
|
||||||
// If user is logged in and matches
|
// If user is logged in and matches
|
||||||
if ((cookies.account && party.user && cookies.account.user_id === party.user.id) || props.new)
|
if (
|
||||||
|
(accountData && party.user && accountData.userId === party.user.id) ||
|
||||||
|
props.new
|
||||||
|
)
|
||||||
appState.party.editable = true
|
appState.party.editable = true
|
||||||
else
|
else appState.party.editable = false
|
||||||
appState.party.editable = false
|
}, [props.new, accountData, party])
|
||||||
|
|
||||||
setFirstLoadComplete(true)
|
|
||||||
}
|
|
||||||
}, [props.new, cookies, party, loading, firstLoadComplete])
|
|
||||||
|
|
||||||
// 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 } = {}
|
||||||
|
|
||||||
if (appState.grid.weapons.mainWeapon)
|
if (appState.grid.weapons.mainWeapon)
|
||||||
initialPreviousUncapValues[-1] = appState.grid.weapons.mainWeapon.uncap_level
|
initialPreviousUncapValues[-1] =
|
||||||
|
appState.grid.weapons.mainWeapon.uncap_level
|
||||||
|
|
||||||
Object.values(appState.grid.weapons.allWeapons).map(o => initialPreviousUncapValues[o.position] = o.uncap_level)
|
Object.values(appState.grid.weapons.allWeapons).map(
|
||||||
|
(o) => (initialPreviousUncapValues[o.position] = o.uncap_level)
|
||||||
|
)
|
||||||
|
|
||||||
setPreviousUncapValues(initialPreviousUncapValues)
|
setPreviousUncapValues(initialPreviousUncapValues)
|
||||||
}, [appState.grid.weapons.mainWeapon, appState.grid.weapons.allWeapons])
|
}, [appState.grid.weapons.mainWeapon, appState.grid.weapons.allWeapons])
|
||||||
|
|
||||||
// Methods: Fetching an object from the server
|
|
||||||
async function fetchGrid(shortcode: string) {
|
|
||||||
return api.endpoints.parties.getOneWithObject({ id: shortcode, object: 'weapons', params: headers })
|
|
||||||
.then(response => processResult(response))
|
|
||||||
.catch(error => processError(error))
|
|
||||||
}
|
|
||||||
|
|
||||||
function processResult(response: AxiosResponse) {
|
|
||||||
// Store the response
|
|
||||||
const party: Party = response.data.party
|
|
||||||
|
|
||||||
// Store the important party and state-keeping values
|
|
||||||
appState.party.id = party.id
|
|
||||||
appState.party.extra = party.extra
|
|
||||||
appState.party.user = party.user
|
|
||||||
appState.party.favorited = party.favorited
|
|
||||||
appState.party.created_at = party.created_at
|
|
||||||
appState.party.updated_at = party.updated_at
|
|
||||||
|
|
||||||
setFound(true)
|
|
||||||
setLoading(false)
|
|
||||||
|
|
||||||
// Populate the weapons in state
|
|
||||||
populateWeapons(party.weapons)
|
|
||||||
}
|
|
||||||
|
|
||||||
function processError(error: any) {
|
|
||||||
if (error.response != null) {
|
|
||||||
if (error.response.status == 404) {
|
|
||||||
setFound(false)
|
|
||||||
setLoading(false)
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
console.error(error)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function populateWeapons(list: Array<GridWeapon>) {
|
|
||||||
list.forEach((gridObject: GridWeapon) => {
|
|
||||||
if (gridObject.mainhand) {
|
|
||||||
appState.grid.weapons.mainWeapon = gridObject
|
|
||||||
appState.party.element = gridObject.object.element
|
|
||||||
} else if (!gridObject.mainhand && gridObject.position != null) {
|
|
||||||
appState.grid.weapons.allWeapons[gridObject.position] = gridObject
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
// Methods: Adding an object from search
|
// Methods: Adding an object from search
|
||||||
function receiveWeaponFromSearch(object: Character | Weapon | Summon, position: number) {
|
function receiveWeaponFromSearch(
|
||||||
|
object: Character | Weapon | Summon,
|
||||||
|
position: number
|
||||||
|
) {
|
||||||
const weapon = object as Weapon
|
const weapon = object as Weapon
|
||||||
if (position == 1)
|
if (position == 1) appState.party.element = weapon.element
|
||||||
appState.party.element = weapon.element
|
|
||||||
|
|
||||||
if (!party.id) {
|
if (!party.id) {
|
||||||
props.createParty(party.extra)
|
props.createParty(party.extra).then((response) => {
|
||||||
.then(response => {
|
|
||||||
const party = response.data.party
|
const party = response.data.party
|
||||||
appState.party.id = party.id
|
appState.party.id = party.id
|
||||||
setSlug(party.shortcode)
|
setSlug(party.shortcode)
|
||||||
|
|
||||||
if (props.pushHistory) props.pushHistory(`/p/${party.shortcode}`)
|
if (props.pushHistory) props.pushHistory(`/p/${party.shortcode}`)
|
||||||
|
|
||||||
saveWeapon(party.id, weapon, position)
|
saveWeapon(party.id, weapon, position).then((response) =>
|
||||||
.then(response => storeGridWeapon(response.data.grid_weapon))
|
storeGridWeapon(response.data.grid_weapon)
|
||||||
|
)
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
saveWeapon(party.id, weapon, position)
|
saveWeapon(party.id, weapon, position).then((response) =>
|
||||||
.then(response => storeGridWeapon(response.data.grid_weapon))
|
storeGridWeapon(response.data.grid_weapon)
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -154,15 +102,18 @@ const WeaponGrid = (props: Props) => {
|
||||||
if (weapon.uncap.ulb) uncapLevel = 5
|
if (weapon.uncap.ulb) uncapLevel = 5
|
||||||
else if (weapon.uncap.flb) uncapLevel = 4
|
else if (weapon.uncap.flb) uncapLevel = 4
|
||||||
|
|
||||||
return await api.endpoints.weapons.create({
|
return await api.endpoints.weapons.create(
|
||||||
'weapon': {
|
{
|
||||||
'party_id': partyId,
|
weapon: {
|
||||||
'weapon_id': weapon.id,
|
party_id: partyId,
|
||||||
'position': position,
|
weapon_id: weapon.id,
|
||||||
'mainhand': (position == -1),
|
position: position,
|
||||||
'uncap_level': uncapLevel
|
mainhand: position == -1,
|
||||||
}
|
uncap_level: uncapLevel,
|
||||||
}, headers)
|
},
|
||||||
|
},
|
||||||
|
headers
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
function storeGridWeapon(gridWeapon: GridWeapon) {
|
function storeGridWeapon(gridWeapon: GridWeapon) {
|
||||||
|
|
@ -182,8 +133,9 @@ const WeaponGrid = (props: Props) => {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if (uncapLevel != previousUncapValues[position])
|
if (uncapLevel != previousUncapValues[position])
|
||||||
await api.updateUncap('weapon', id, uncapLevel)
|
await api.updateUncap("weapon", id, uncapLevel).then((response) => {
|
||||||
.then(response => { storeGridWeapon(response.data.grid_weapon) })
|
storeGridWeapon(response.data.grid_weapon)
|
||||||
|
})
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(error)
|
console.error(error)
|
||||||
|
|
||||||
|
|
@ -197,7 +149,11 @@ const WeaponGrid = (props: Props) => {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function initiateUncapUpdate(id: string, position: number, uncapLevel: number) {
|
function initiateUncapUpdate(
|
||||||
|
id: string,
|
||||||
|
position: number,
|
||||||
|
uncapLevel: number
|
||||||
|
) {
|
||||||
memoizeAction(id, position, uncapLevel)
|
memoizeAction(id, position, uncapLevel)
|
||||||
|
|
||||||
// Optimistically update UI
|
// Optimistically update UI
|
||||||
|
|
@ -207,27 +163,31 @@ const WeaponGrid = (props: Props) => {
|
||||||
const memoizeAction = useCallback(
|
const memoizeAction = useCallback(
|
||||||
(id: string, position: number, uncapLevel: number) => {
|
(id: string, position: number, uncapLevel: number) => {
|
||||||
debouncedAction(id, position, uncapLevel)
|
debouncedAction(id, position, uncapLevel)
|
||||||
}, [props, previousUncapValues]
|
},
|
||||||
|
[props, previousUncapValues]
|
||||||
)
|
)
|
||||||
|
|
||||||
const debouncedAction = useMemo(() =>
|
const debouncedAction = useMemo(
|
||||||
|
() =>
|
||||||
debounce((id, position, number) => {
|
debounce((id, position, number) => {
|
||||||
saveUncap(id, position, number)
|
saveUncap(id, position, number)
|
||||||
}, 500), [props, saveUncap]
|
}, 500),
|
||||||
|
[props, saveUncap]
|
||||||
)
|
)
|
||||||
|
|
||||||
const updateUncapLevel = (position: number, uncapLevel: number) => {
|
const updateUncapLevel = (position: number, uncapLevel: number) => {
|
||||||
if (appState.grid.weapons.mainWeapon && position == -1)
|
if (appState.grid.weapons.mainWeapon && position == -1)
|
||||||
appState.grid.weapons.mainWeapon.uncap_level = uncapLevel
|
appState.grid.weapons.mainWeapon.uncap_level = uncapLevel
|
||||||
else
|
else appState.grid.weapons.allWeapons[position].uncap_level = uncapLevel
|
||||||
appState.grid.weapons.allWeapons[position].uncap_level = uncapLevel
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function storePreviousUncapValue(position: number) {
|
function storePreviousUncapValue(position: number) {
|
||||||
// Save the current value in case of an unexpected result
|
// Save the current value in case of an unexpected result
|
||||||
let newPreviousValues = { ...previousUncapValues }
|
let newPreviousValues = { ...previousUncapValues }
|
||||||
newPreviousValues[position] = (appState.grid.weapons.mainWeapon && position == -1) ?
|
newPreviousValues[position] =
|
||||||
appState.grid.weapons.mainWeapon.uncap_level : appState.grid.weapons.allWeapons[position].uncap_level
|
appState.grid.weapons.mainWeapon && position == -1
|
||||||
|
? appState.grid.weapons.mainWeapon.uncap_level
|
||||||
|
: appState.grid.weapons.allWeapons[position].uncap_level
|
||||||
setPreviousUncapValues(newPreviousValues)
|
setPreviousUncapValues(newPreviousValues)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -244,8 +204,7 @@ const WeaponGrid = (props: Props) => {
|
||||||
/>
|
/>
|
||||||
)
|
)
|
||||||
|
|
||||||
const weaponGridElement = (
|
const weaponGridElement = Array.from(Array(numWeapons)).map((x, i) => {
|
||||||
Array.from(Array(numWeapons)).map((x, i) => {
|
|
||||||
return (
|
return (
|
||||||
<li key={`grid_unit_${i}`}>
|
<li key={`grid_unit_${i}`}>
|
||||||
<WeaponUnit
|
<WeaponUnit
|
||||||
|
|
@ -259,7 +218,6 @@ const WeaponGrid = (props: Props) => {
|
||||||
</li>
|
</li>
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
)
|
|
||||||
|
|
||||||
const extraGridElement = (
|
const extraGridElement = (
|
||||||
<ExtraWeapons
|
<ExtraWeapons
|
||||||
|
|
@ -278,7 +236,9 @@ const WeaponGrid = (props: Props) => {
|
||||||
<ul className="grid_weapons">{weaponGridElement}</ul>
|
<ul className="grid_weapons">{weaponGridElement}</ul>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
{ (() => { return (party.extra) ? extraGridElement : '' })() }
|
{(() => {
|
||||||
|
return party.extra ? extraGridElement : ""
|
||||||
|
})()}
|
||||||
</div>
|
</div>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue