Fetch and store the jobs and skills in app state
This commit is contained in:
parent
1d571f1a21
commit
c599a8352a
2 changed files with 52 additions and 2 deletions
|
|
@ -1,13 +1,17 @@
|
||||||
import React from "react"
|
import React, { useEffect } from "react"
|
||||||
import { getCookie } from "cookies-next"
|
import { getCookie } from "cookies-next"
|
||||||
import { serverSideTranslations } from "next-i18next/serverSideTranslations"
|
import { serverSideTranslations } from "next-i18next/serverSideTranslations"
|
||||||
|
|
||||||
import Party from "~components/Party"
|
import Party from "~components/Party"
|
||||||
|
|
||||||
|
import { appState } from "~utils/appState"
|
||||||
import api from "~utils/api"
|
import api from "~utils/api"
|
||||||
|
|
||||||
import type { NextApiRequest, NextApiResponse } from "next"
|
import type { NextApiRequest, NextApiResponse } from "next"
|
||||||
|
|
||||||
interface Props {
|
interface Props {
|
||||||
|
jobs: Job[]
|
||||||
|
jobSkills: JobSkill[]
|
||||||
raids: Raid[]
|
raids: Raid[]
|
||||||
sortedRaids: Raid[][]
|
sortedRaids: Raid[][]
|
||||||
}
|
}
|
||||||
|
|
@ -18,6 +22,17 @@ const NewRoute: React.FC<Props> = (props: Props) => {
|
||||||
window.history.replaceState(null, `Grid Tool`, `${path}`)
|
window.history.replaceState(null, `Grid Tool`, `${path}`)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
persistStaticData()
|
||||||
|
}, [persistStaticData])
|
||||||
|
|
||||||
|
function persistStaticData() {
|
||||||
|
console.log("Persisting static data...")
|
||||||
|
appState.raids = props.raids
|
||||||
|
appState.jobs = props.jobs
|
||||||
|
appState.jobSkills = props.jobSkills
|
||||||
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div id="Content">
|
<div id="Content">
|
||||||
<Party new={true} raids={props.sortedRaids} pushHistory={callback} />
|
<Party new={true} raids={props.sortedRaids} pushHistory={callback} />
|
||||||
|
|
@ -51,8 +66,17 @@ export const getServerSideProps = async ({ req, res, locale, query }: { req: Nex
|
||||||
.getAll({ params: headers })
|
.getAll({ params: headers })
|
||||||
.then((response) => organizeRaids(response.data.map((r: any) => r.raid)))
|
.then((response) => organizeRaids(response.data.map((r: any) => r.raid)))
|
||||||
|
|
||||||
|
let jobs = await api.endpoints.jobs
|
||||||
|
.getAll({ params: headers })
|
||||||
|
.then((response) => { return response.data })
|
||||||
|
|
||||||
|
let jobSkills = await api.allSkills(headers)
|
||||||
|
.then((response) => { return response.data })
|
||||||
|
|
||||||
return {
|
return {
|
||||||
props: {
|
props: {
|
||||||
|
jobs: jobs,
|
||||||
|
jobSkills: jobSkills,
|
||||||
raids: raids,
|
raids: raids,
|
||||||
sortedRaids: sortedRaids,
|
sortedRaids: sortedRaids,
|
||||||
...(await serverSideTranslations(locale, ["common"])),
|
...(await serverSideTranslations(locale, ["common"])),
|
||||||
|
|
|
||||||
|
|
@ -1,20 +1,34 @@
|
||||||
import React from "react"
|
import React, { useEffect } from "react"
|
||||||
import { getCookie } from "cookies-next"
|
import { getCookie } from "cookies-next"
|
||||||
import { serverSideTranslations } from "next-i18next/serverSideTranslations"
|
import { serverSideTranslations } from "next-i18next/serverSideTranslations"
|
||||||
|
|
||||||
import Party from "~components/Party"
|
import Party from "~components/Party"
|
||||||
|
|
||||||
|
import { appState } from "~utils/appState"
|
||||||
import api from "~utils/api"
|
import api from "~utils/api"
|
||||||
|
|
||||||
import type { NextApiRequest, NextApiResponse } from "next"
|
import type { NextApiRequest, NextApiResponse } from "next"
|
||||||
|
|
||||||
interface Props {
|
interface Props {
|
||||||
party: Party
|
party: Party
|
||||||
|
jobs: Job[]
|
||||||
|
jobSkills: JobSkill[]
|
||||||
raids: Raid[]
|
raids: Raid[]
|
||||||
sortedRaids: Raid[][]
|
sortedRaids: Raid[][]
|
||||||
}
|
}
|
||||||
|
|
||||||
const PartyRoute: React.FC<Props> = (props: Props) => {
|
const PartyRoute: React.FC<Props> = (props: Props) => {
|
||||||
|
useEffect(() => {
|
||||||
|
persistStaticData()
|
||||||
|
}, [persistStaticData])
|
||||||
|
|
||||||
|
function persistStaticData() {
|
||||||
|
console.log("Persisting static data...")
|
||||||
|
appState.raids = props.raids
|
||||||
|
appState.jobs = props.jobs
|
||||||
|
appState.jobSkills = props.jobSkills
|
||||||
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div id="Content">
|
<div id="Content">
|
||||||
<Party team={props.party} raids={props.sortedRaids} />
|
<Party team={props.party} raids={props.sortedRaids} />
|
||||||
|
|
@ -48,6 +62,16 @@ export const getServerSideProps = async ({ req, res, locale, query }: { req: Nex
|
||||||
.getAll()
|
.getAll()
|
||||||
.then((response) => organizeRaids(response.data.map((r: any) => r.raid)))
|
.then((response) => organizeRaids(response.data.map((r: any) => r.raid)))
|
||||||
|
|
||||||
|
let jobs = await api.endpoints.jobs
|
||||||
|
.getAll({ params: headers })
|
||||||
|
.then((response) => {
|
||||||
|
return response.data
|
||||||
|
})
|
||||||
|
|
||||||
|
let jobSkills = await api.allSkills(headers).then((response) => {
|
||||||
|
return response.data
|
||||||
|
})
|
||||||
|
|
||||||
let party: Party | null = null
|
let party: Party | null = null
|
||||||
if (query.party) {
|
if (query.party) {
|
||||||
let response = await api.endpoints.parties.getOne({ id: query.party, params: headers })
|
let response = await api.endpoints.parties.getOne({ id: query.party, params: headers })
|
||||||
|
|
@ -59,6 +83,8 @@ export const getServerSideProps = async ({ req, res, locale, query }: { req: Nex
|
||||||
return {
|
return {
|
||||||
props: {
|
props: {
|
||||||
party: party,
|
party: party,
|
||||||
|
job: jobs,
|
||||||
|
jobSkills: jobSkills,
|
||||||
raids: raids,
|
raids: raids,
|
||||||
sortedRaids: sortedRaids,
|
sortedRaids: sortedRaids,
|
||||||
...(await serverSideTranslations(locale, ["common"])),
|
...(await serverSideTranslations(locale, ["common"])),
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue