Refactor createParty to allow creating anything first
This commit is contained in:
parent
c0cdb91178
commit
bd1f6ccb98
4 changed files with 78 additions and 104 deletions
|
|
@ -11,7 +11,7 @@ import JobSection from '~components/JobSection'
|
|||
import CharacterUnit from '~components/CharacterUnit'
|
||||
import CharacterConflictModal from '~components/CharacterConflictModal'
|
||||
|
||||
import type { JobSkillObject, SearchableObject } from '~types'
|
||||
import type { DetailsObject, JobSkillObject, SearchableObject } from '~types'
|
||||
|
||||
import api from '~utils/api'
|
||||
import { appState } from '~utils/appState'
|
||||
|
|
@ -23,7 +23,7 @@ import './index.scss'
|
|||
interface Props {
|
||||
new: boolean
|
||||
characters?: GridCharacter[]
|
||||
createParty: () => Promise<AxiosResponse<any, any>>
|
||||
createParty: (details?: DetailsObject) => Promise<Party>
|
||||
pushHistory?: (path: string) => void
|
||||
}
|
||||
|
||||
|
|
@ -95,13 +95,8 @@ const CharacterGrid = (props: Props) => {
|
|||
const character = object as Character
|
||||
|
||||
if (!party.id) {
|
||||
props.createParty().then((response) => {
|
||||
const party = response.data.party
|
||||
appState.party.id = party.id
|
||||
setSlug(party.shortcode)
|
||||
|
||||
if (props.pushHistory) props.pushHistory(`/p/${party.shortcode}`)
|
||||
saveCharacter(party.id, character, position)
|
||||
props.createParty().then((team) => {
|
||||
saveCharacter(team.id, character, position)
|
||||
.then((response) => storeGridCharacter(response.data))
|
||||
.catch((error) => console.error(error))
|
||||
})
|
||||
|
|
@ -184,19 +179,24 @@ const CharacterGrid = (props: Props) => {
|
|||
},
|
||||
}
|
||||
|
||||
if (party.id && appState.party.editable) {
|
||||
if (!party.id) {
|
||||
// If the party has no ID, create a new party
|
||||
await props.createParty()
|
||||
}
|
||||
|
||||
if (appState.party.id) {
|
||||
const response = await api.updateJob({
|
||||
partyId: party.id,
|
||||
partyId: appState.party.id,
|
||||
params: payload,
|
||||
})
|
||||
|
||||
const newParty = response.data
|
||||
const team = response.data
|
||||
|
||||
setJob(newParty.job)
|
||||
appState.party.job = newParty.job
|
||||
setJob(team.job)
|
||||
appState.party.job = team.job
|
||||
|
||||
setJobSkills(newParty.job_skills)
|
||||
appState.party.jobSkills = newParty.job_skills
|
||||
setJobSkills(team.job_skills)
|
||||
appState.party.jobSkills = team.job_skills
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -11,7 +11,7 @@ import CharacterGrid from '~components/CharacterGrid'
|
|||
|
||||
import api from '~utils/api'
|
||||
import { appState, initialAppState } from '~utils/appState'
|
||||
import { GridType, TeamElement } from '~utils/enums'
|
||||
import { GridType } from '~utils/enums'
|
||||
import type { DetailsObject } from '~types'
|
||||
|
||||
import './index.scss'
|
||||
|
|
@ -40,82 +40,62 @@ const Party = (props: Props) => {
|
|||
}, [])
|
||||
|
||||
// Methods: Creating a new party
|
||||
async function createParty(extra: boolean = false) {
|
||||
return await api.endpoints.parties.create({
|
||||
party: {
|
||||
extra: extra,
|
||||
},
|
||||
})
|
||||
async function createParty(details?: DetailsObject) {
|
||||
let payload = {}
|
||||
if (details) payload = formatDetailsObject(details)
|
||||
|
||||
return await api.endpoints.parties
|
||||
.create(payload)
|
||||
.then((response) => storeParty(response.data.party))
|
||||
}
|
||||
|
||||
// Methods: Updating the party's details
|
||||
async function updateDetails(details: DetailsObject) {
|
||||
if (!appState.party.id) return await createParty(details)
|
||||
else updateParty(details)
|
||||
}
|
||||
|
||||
function formatDetailsObject(details: DetailsObject) {
|
||||
const payload: { [key: string]: any } = {}
|
||||
|
||||
if (details.name) payload.name = details.name
|
||||
if (details.description) payload.description = details.description
|
||||
if (details.raid) payload.raid_id = details.raid.id
|
||||
if (details.chargeAttack) payload.charge_attack = details.chargeAttack
|
||||
if (details.fullAuto) payload.full_auto = details.fullAuto
|
||||
if (details.autoGuard) payload.auto_guard = details.autoGuard
|
||||
if (details.clearTime) payload.clear_time = details.clearTime
|
||||
if (details.buttonCount) payload.button_count = details.buttonCount
|
||||
if (details.chainCount) payload.chain_count = details.chainCount
|
||||
if (details.turnCount) payload.turn_count = details.turnCount
|
||||
if (details.extra) payload.extra = details.extra
|
||||
if (details.job) payload.job_id = details.job.id
|
||||
|
||||
if (Object.keys(payload).length > 1) return { party: payload }
|
||||
else return {}
|
||||
}
|
||||
|
||||
async function updateParty(details: DetailsObject) {
|
||||
const payload = formatDetailsObject(details)
|
||||
|
||||
if (appState.party.id) {
|
||||
return await api.endpoints.parties
|
||||
.update(appState.party.id, payload)
|
||||
.then((response) => storeParty(response.data.party))
|
||||
}
|
||||
}
|
||||
|
||||
function checkboxChanged(event: React.ChangeEvent<HTMLInputElement>) {
|
||||
appState.party.extra = event.target.checked
|
||||
|
||||
if (party.id) {
|
||||
api.endpoints.parties.update(party.id, {
|
||||
// Only save if this is a saved party
|
||||
if (appState.party.id) {
|
||||
api.endpoints.parties.update(appState.party.id, {
|
||||
party: { extra: event.target.checked },
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
async function updateDetails(details: DetailsObject) {
|
||||
if (
|
||||
appState.party.name !== details.name ||
|
||||
appState.party.description !== details.description ||
|
||||
appState.party.raid?.id !== details.raid?.id
|
||||
) {
|
||||
if (!appState.party.id)
|
||||
await createParty().then((response) => {
|
||||
// If the party has no ID, create a new party
|
||||
const party = response.data.party
|
||||
storeParty(party)
|
||||
|
||||
// Then, push the browser history to the new party's URL
|
||||
if (props.pushHistory) props.pushHistory(`/p/${party.shortcode}`)
|
||||
})
|
||||
|
||||
// Update the party
|
||||
await sendUpdate(details)
|
||||
}
|
||||
}
|
||||
|
||||
async function sendUpdate(details: DetailsObject) {
|
||||
if (appState.party.id) {
|
||||
return await api.endpoints.parties
|
||||
.update(appState.party.id, {
|
||||
party: {
|
||||
name: details.name,
|
||||
description: details.description,
|
||||
raid_id: details.raid?.id,
|
||||
charge_attack: details.chargeAttack,
|
||||
full_auto: details.fullAuto,
|
||||
auto_guard: details.autoGuard,
|
||||
clear_time: details.clearTime,
|
||||
button_count: details.buttonCount,
|
||||
chain_count: details.chainCount,
|
||||
turn_count: details.turnCount,
|
||||
},
|
||||
})
|
||||
.then(() => {
|
||||
appState.party.name = details.name
|
||||
appState.party.description = details.description
|
||||
appState.party.raid = details.raid
|
||||
|
||||
appState.party.chargeAttack = details.chargeAttack
|
||||
appState.party.fullAuto = details.fullAuto
|
||||
appState.party.autoGuard = details.autoGuard
|
||||
|
||||
appState.party.clearTime = details.clearTime
|
||||
appState.party.buttonCount = details.buttonCount
|
||||
appState.party.chainCount = details.chainCount
|
||||
appState.party.turnCount = details.turnCount
|
||||
|
||||
appState.party.updated_at = party.updated_at
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// Deleting the party
|
||||
function deleteTeam(event: React.MouseEvent<HTMLButtonElement, MouseEvent>) {
|
||||
if (appState.party.editable && appState.party.id) {
|
||||
|
|
@ -142,7 +122,7 @@ const Party = (props: Props) => {
|
|||
|
||||
// Methods: Storing party data
|
||||
const storeParty = function (team: Party) {
|
||||
// Store the important party and state-keeping values
|
||||
// Store the important party and state-keeping values in global state
|
||||
appState.party.name = team.name
|
||||
appState.party.description = team.description
|
||||
appState.party.raid = team.raid
|
||||
|
|
@ -163,6 +143,11 @@ const Party = (props: Props) => {
|
|||
storeCharacters(team.characters)
|
||||
storeWeapons(team.weapons)
|
||||
storeSummons(team.summons)
|
||||
|
||||
// Then, push the browser history to the new party's URL
|
||||
if (props.pushHistory) props.pushHistory(`/p/${team.shortcode}`)
|
||||
|
||||
return team
|
||||
}
|
||||
|
||||
const storeCharacters = (list: Array<GridCharacter>) => {
|
||||
|
|
|
|||
|
|
@ -13,7 +13,7 @@ import ExtraSummons from '~components/ExtraSummons'
|
|||
import api from '~utils/api'
|
||||
import { appState } from '~utils/appState'
|
||||
import { accountState } from '~utils/accountState'
|
||||
import type { SearchableObject } from '~types'
|
||||
import type { DetailsObject, SearchableObject } from '~types'
|
||||
|
||||
import './index.scss'
|
||||
|
||||
|
|
@ -21,7 +21,7 @@ import './index.scss'
|
|||
interface Props {
|
||||
new: boolean
|
||||
summons?: GridSummon[]
|
||||
createParty: () => Promise<AxiosResponse<any, any>>
|
||||
createParty: (details?: DetailsObject) => Promise<Party>
|
||||
pushHistory?: (path: string) => void
|
||||
}
|
||||
|
||||
|
|
@ -86,14 +86,8 @@ const SummonGrid = (props: Props) => {
|
|||
const summon = object as Summon
|
||||
|
||||
if (!party.id) {
|
||||
props.createParty().then((response) => {
|
||||
const party = response.data.party
|
||||
appState.party.id = party.id
|
||||
setSlug(party.shortcode)
|
||||
|
||||
if (props.pushHistory) props.pushHistory(`/p/${party.shortcode}`)
|
||||
|
||||
saveSummon(party.id, summon, position).then((response) =>
|
||||
props.createParty().then((team) => {
|
||||
saveSummon(team.id, summon, position).then((response) =>
|
||||
storeGridSummon(response.data)
|
||||
)
|
||||
})
|
||||
|
|
|
|||
|
|
@ -13,7 +13,7 @@ import ExtraWeapons from '~components/ExtraWeapons'
|
|||
import api from '~utils/api'
|
||||
import { appState } from '~utils/appState'
|
||||
|
||||
import type { SearchableObject } from '~types'
|
||||
import type { DetailsObject, SearchableObject } from '~types'
|
||||
|
||||
import './index.scss'
|
||||
import WeaponConflictModal from '~components/WeaponConflictModal'
|
||||
|
|
@ -24,7 +24,7 @@ import { accountState } from '~utils/accountState'
|
|||
interface Props {
|
||||
new: boolean
|
||||
weapons?: GridWeapon[]
|
||||
createParty: (extra: boolean) => Promise<AxiosResponse<any, any>>
|
||||
createParty: (details: DetailsObject) => Promise<Party>
|
||||
pushHistory?: (path: string) => void
|
||||
}
|
||||
|
||||
|
|
@ -89,16 +89,11 @@ const WeaponGrid = (props: Props) => {
|
|||
if (position == 1) appState.party.element = weapon.element
|
||||
|
||||
if (!party.id) {
|
||||
props.createParty(party.extra).then((response) => {
|
||||
const party = response.data.party
|
||||
appState.party.id = party.id
|
||||
setSlug(party.shortcode)
|
||||
|
||||
if (props.pushHistory) props.pushHistory(`/p/${party.shortcode}`)
|
||||
|
||||
saveWeapon(party.id, weapon, position).then((response) =>
|
||||
const payload: DetailsObject = { extra: party.extra }
|
||||
props.createParty(payload).then((team) => {
|
||||
saveWeapon(team.id, weapon, position).then((response) => {
|
||||
storeGridWeapon(response.data.grid_weapon)
|
||||
)
|
||||
})
|
||||
})
|
||||
} else {
|
||||
if (party.editable)
|
||||
|
|
|
|||
Loading…
Reference in a new issue