Update weapon should happen in WeaponUnit

Previously, this happened in WeaponModal. It happens in CharacterUnit on that end, so this change brings us in line with how we're doing things elsewhere
This commit is contained in:
Justin Edmund 2023-07-03 19:06:11 -07:00
parent fab8ab260c
commit c60b9887e3

View file

@ -1,7 +1,12 @@
import React, { useEffect, useState, MouseEvent } from 'react'
import React, { useEffect, useState } from 'react'
import { useRouter } from 'next/router'
import { Trans, useTranslation } from 'next-i18next'
import { AxiosResponse } from 'axios'
import classNames from 'classnames'
import clonedeep from 'lodash.clonedeep'
import api from '~utils/api'
import { appState } from '~utils/appState'
import Alert from '~components/common/Alert'
import SearchModal from '~components/search/SearchModal'
@ -16,7 +21,7 @@ import WeaponHovercard from '~components/weapon/WeaponHovercard'
import UncapIndicator from '~components/uncap/UncapIndicator'
import Button from '~components/common/Button'
import type { SearchableObject } from '~types'
import type { GridWeaponObject, SearchableObject } from '~types'
import ax from '~data/ax'
@ -130,6 +135,35 @@ const WeaponUnit = ({
setAlertOpen(false)
}
// Methods: Data fetching and manipulation
async function updateWeapon(object: GridWeaponObject) {
if (gridWeapon) {
return await api.endpoints.grid_weapons
.update(gridWeapon.id, object)
.then((response) => processResult(response))
.catch((error) => processError(error))
}
}
function processResult(response: AxiosResponse) {
const gridWeapon: GridWeapon = response.data
if (gridWeapon.mainhand) {
appState.grid.weapons.mainWeapon = gridWeapon
appState.party.element = gridWeapon.object.element
} else if (!gridWeapon.mainhand && gridWeapon.position !== null) {
let weapon = clonedeep(gridWeapon)
weapon.element = gridWeapon.object.element
appState.grid.weapons.allWeapons[gridWeapon.position] = weapon
}
}
function processError(error: any) {
console.error(error)
}
// Methods: Data fetching and manipulation
function getCanonicalAxSkill(index: number) {
if (
@ -430,6 +464,7 @@ const WeaponUnit = ({
gridWeapon={gridWeapon}
open={detailsModalOpen}
onOpenChange={handleWeaponModalOpenChange}
updateWeapon={updateWeapon}
/>
)
}