Merge pull request #139 from jedmund/fix-title

Refactor party creation
This commit is contained in:
Justin Edmund 2023-01-08 01:34:07 -08:00 committed by GitHub
commit c0890e6e96
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 95 additions and 101 deletions

View file

@ -11,7 +11,7 @@ import JobSection from '~components/JobSection'
import CharacterUnit from '~components/CharacterUnit' import CharacterUnit from '~components/CharacterUnit'
import CharacterConflictModal from '~components/CharacterConflictModal' import CharacterConflictModal from '~components/CharacterConflictModal'
import type { JobSkillObject, SearchableObject } from '~types' import type { DetailsObject, JobSkillObject, SearchableObject } from '~types'
import api from '~utils/api' import api from '~utils/api'
import { appState } from '~utils/appState' import { appState } from '~utils/appState'
@ -23,7 +23,7 @@ import './index.scss'
interface Props { interface Props {
new: boolean new: boolean
characters?: GridCharacter[] characters?: GridCharacter[]
createParty: () => Promise<AxiosResponse<any, any>> createParty: (details?: DetailsObject) => Promise<Party>
pushHistory?: (path: string) => void pushHistory?: (path: string) => void
} }
@ -95,13 +95,8 @@ const CharacterGrid = (props: Props) => {
const character = object as Character const character = object as Character
if (!party.id) { if (!party.id) {
props.createParty().then((response) => { props.createParty().then((team) => {
const party = response.data.party saveCharacter(team.id, character, position)
appState.party.id = party.id
setSlug(party.shortcode)
if (props.pushHistory) props.pushHistory(`/p/${party.shortcode}`)
saveCharacter(party.id, character, position)
.then((response) => storeGridCharacter(response.data)) .then((response) => storeGridCharacter(response.data))
.catch((error) => console.error(error)) .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({ const response = await api.updateJob({
partyId: party.id, partyId: appState.party.id,
params: payload, params: payload,
}) })
const newParty = response.data const team = response.data
setJob(newParty.job) setJob(team.job)
appState.party.job = newParty.job appState.party.job = team.job
setJobSkills(newParty.job_skills) setJobSkills(team.job_skills)
appState.party.jobSkills = newParty.job_skills appState.party.jobSkills = team.job_skills
} }
} }

View file

@ -26,7 +26,7 @@ const LabelledInput = React.forwardRef<HTMLInputElement, Props>(function Input(
// Classes // Classes
const classes = classNames({ Input: true }, props.className) const classes = classNames({ Input: true }, props.className)
const { defaultValue, ...inputProps } = props const { defaultValue, visible, ...inputProps } = props
// Change value when prop updates // Change value when prop updates
useEffect(() => { useEffect(() => {

View file

@ -11,7 +11,7 @@ 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 } from '~utils/enums'
import type { DetailsObject } from '~types' import type { DetailsObject } from '~types'
import './index.scss' import './index.scss'
@ -40,63 +40,59 @@ const Party = (props: Props) => {
}, []) }, [])
// Methods: Creating a new party // Methods: Creating a new party
async function createParty(extra: boolean = false) { async function createParty(details?: DetailsObject) {
return await api.endpoints.parties.create({ let payload = {}
party: { if (details) payload = formatDetailsObject(details)
extra: extra,
}, return await api.endpoints.parties
}) .create(payload)
.then((response) => storeParty(response.data.party))
} }
// Methods: Updating the party's details // Methods: Updating the party's details
function checkboxChanged(event: React.ChangeEvent<HTMLInputElement>) { async function updateDetails(details: DetailsObject) {
appState.party.extra = event.target.checked if (!appState.party.id) return await createParty(details)
else updateParty(details)
}
if (party.id) { function formatDetailsObject(details: DetailsObject) {
api.endpoints.parties.update(party.id, { const payload: { [key: string]: any } = {}
party: { extra: event.target.checked },
}) 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 updateDetails(details: DetailsObject) { function checkboxChanged(event: React.ChangeEvent<HTMLInputElement>) {
if ( appState.party.extra = event.target.checked
appState.party.name !== details.name ||
appState.party.description !== details.description ||
appState.party.raid?.id !== details.raid?.id
) {
if (appState.party.id)
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 // Only save if this is a saved party
appState.party.fullAuto = details.fullAuto if (appState.party.id) {
appState.party.autoGuard = details.autoGuard api.endpoints.parties.update(appState.party.id, {
party: { extra: event.target.checked },
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
})
} }
} }
@ -126,7 +122,7 @@ const Party = (props: Props) => {
// Methods: Storing party data // Methods: Storing party data
const storeParty = function (team: Party) { 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.name = team.name
appState.party.description = team.description appState.party.description = team.description
appState.party.raid = team.raid appState.party.raid = team.raid
@ -147,6 +143,11 @@ const Party = (props: Props) => {
storeCharacters(team.characters) storeCharacters(team.characters)
storeWeapons(team.weapons) storeWeapons(team.weapons)
storeSummons(team.summons) 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>) => { const storeCharacters = (list: Array<GridCharacter>) => {

View file

@ -252,11 +252,14 @@ const PartyDetails = (props: Props) => {
} }
function toggleDetails() { function toggleDetails() {
if (name !== party.name) { // Enabling this code will make live updates not work,
const resetName = party.name ? party.name : 'Untitled' // but I'm not sure why it's here, so we're not going to remove it.
setName(resetName)
if (nameInput.current) nameInput.current.value = resetName // if (name !== party.name) {
} // const resetName = party.name ? party.name : ''
// setName(resetName)
// if (nameInput.current) nameInput.current.value = resetName
// }
setOpen(!open) setOpen(!open)
} }
@ -270,7 +273,6 @@ const PartyDetails = (props: Props) => {
} }
function updateDetails(event: React.MouseEvent) { function updateDetails(event: React.MouseEvent) {
const nameValue = nameInput.current?.value
const descriptionValue = descriptionInput.current?.value const descriptionValue = descriptionInput.current?.value
const raid = raids.find((raid) => raid.slug === raidSlug) const raid = raids.find((raid) => raid.slug === raidSlug)
@ -282,7 +284,7 @@ const PartyDetails = (props: Props) => {
buttonCount: buttonCount, buttonCount: buttonCount,
turnCount: turnCount, turnCount: turnCount,
chainCount: chainCount, chainCount: chainCount,
name: nameValue, name: name,
description: descriptionValue, description: descriptionValue,
raid: raid, raid: raid,
} }

View file

@ -13,7 +13,7 @@ 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 { accountState } from '~utils/accountState' import { accountState } from '~utils/accountState'
import type { SearchableObject } from '~types' import type { DetailsObject, SearchableObject } from '~types'
import './index.scss' import './index.scss'
@ -21,7 +21,7 @@ import './index.scss'
interface Props { interface Props {
new: boolean new: boolean
summons?: GridSummon[] summons?: GridSummon[]
createParty: () => Promise<AxiosResponse<any, any>> createParty: (details?: DetailsObject) => Promise<Party>
pushHistory?: (path: string) => void pushHistory?: (path: string) => void
} }
@ -86,14 +86,8 @@ const SummonGrid = (props: Props) => {
const summon = object as Summon const summon = object as Summon
if (!party.id) { if (!party.id) {
props.createParty().then((response) => { props.createParty().then((team) => {
const party = response.data.party saveSummon(team.id, summon, position).then((response) =>
appState.party.id = party.id
setSlug(party.shortcode)
if (props.pushHistory) props.pushHistory(`/p/${party.shortcode}`)
saveSummon(party.id, summon, position).then((response) =>
storeGridSummon(response.data) storeGridSummon(response.data)
) )
}) })

View file

@ -91,7 +91,7 @@
img { img {
position: relative; position: relative;
width: 100%; width: 100%;
z-index: 0; z-index: 2;
&.Placeholder { &.Placeholder {
opacity: 0; opacity: 0;

View file

@ -13,7 +13,7 @@ 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 type { SearchableObject } from '~types' import type { DetailsObject, SearchableObject } from '~types'
import './index.scss' import './index.scss'
import WeaponConflictModal from '~components/WeaponConflictModal' import WeaponConflictModal from '~components/WeaponConflictModal'
@ -24,7 +24,7 @@ import { accountState } from '~utils/accountState'
interface Props { interface Props {
new: boolean new: boolean
weapons?: GridWeapon[] weapons?: GridWeapon[]
createParty: (extra: boolean) => Promise<AxiosResponse<any, any>> createParty: (details: DetailsObject) => Promise<Party>
pushHistory?: (path: string) => void pushHistory?: (path: string) => void
} }
@ -89,16 +89,11 @@ const WeaponGrid = (props: Props) => {
if (position == 1) appState.party.element = weapon.element if (position == 1) appState.party.element = weapon.element
if (!party.id) { if (!party.id) {
props.createParty(party.extra).then((response) => { const payload: DetailsObject = { extra: party.extra }
const party = response.data.party props.createParty(payload).then((team) => {
appState.party.id = party.id saveWeapon(team.id, weapon, position).then((response) => {
setSlug(party.shortcode)
if (props.pushHistory) props.pushHistory(`/p/${party.shortcode}`)
saveWeapon(party.id, weapon, position).then((response) =>
storeGridWeapon(response.data.grid_weapon) storeGridWeapon(response.data.grid_weapon)
) })
}) })
} else { } else {
if (party.editable) if (party.editable)

10
types/index.d.ts vendored
View file

@ -22,14 +22,16 @@ export type PaginationObject = {
export type DetailsObject = { export type DetailsObject = {
[key: string]: boolean | number | string | Raid | undefined [key: string]: boolean | number | string | Raid | undefined
fullAuto: boolean fullAuto?: boolean
autoGuard: boolean autoGuard?: boolean
chargeAttack: boolean chargeAttack?: boolean
clearTime: number clearTime?: number
buttonCount?: number buttonCount?: number
turnCount?: number turnCount?: number
chainCount?: number chainCount?: number
name?: string name?: string
description?: string description?: string
raid?: Raid raid?: Raid
job?: Job
extra?: boolean
} }