-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #28 from xyng/stationen
Stationen
- Loading branch information
Showing
38 changed files
with
1,381 additions
and
146 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -257,3 +257,5 @@ dist | |
yuoshi.zip | ||
|
||
.webpack.env | ||
|
||
.vscode |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
# copy this file to .webpack.env and change settings there | ||
# copy this file to .webpack.env and change settings there. | ||
STUDIP_URL=http://localhost:8123 | ||
PLUGIN_PATH=/plugins_packages/xyng/Yuoshi | ||
API_PATH=/jsonapi.php/v1 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import React, { createContext, useContext } from "react" | ||
|
||
import useGetModelFromListOrFetch from "../helpers/useGetModelFromListOrFetch" | ||
import Station from "../models/Station" | ||
|
||
import { useStationContext } from "./StationContext" | ||
|
||
interface CurrentStationContextInterface { | ||
station: Station | ||
updateStation: (station: Station, reload?: boolean) => Promise<void> | ||
} | ||
const CurrentStationContext = createContext<CurrentStationContextInterface | null>( | ||
null | ||
) | ||
|
||
export const useCurrentStationContext = () => { | ||
const ctx = useContext(CurrentStationContext) | ||
|
||
if (ctx === null) { | ||
throw new Error("No CurrentStationContext available.") | ||
} | ||
|
||
return ctx | ||
} | ||
|
||
const fetchStation = async (stationId: string) => { | ||
const station = ( | ||
await Station.with("station").find(stationId) | ||
).getData() as Station | null | ||
|
||
if (!station) { | ||
throw new Error("Station not found") | ||
} | ||
|
||
return station | ||
} | ||
|
||
export const CurrentStationContextProvider: React.FC<{ | ||
stationId?: string | ||
}> = ({ children, stationId }) => { | ||
const { station, updateStation, reloadStations } = useStationContext() | ||
const { entityData, updateEntity } = useGetModelFromListOrFetch( | ||
stationId, | ||
station, | ||
stationId ? [stationId, "stationId"] : null, | ||
fetchStation, | ||
updateStation, | ||
reloadStations | ||
) | ||
|
||
const ctx = { | ||
station: entityData, | ||
updateStation: updateEntity, | ||
} | ||
|
||
return ( | ||
<CurrentStationContext.Provider value={ctx}> | ||
{children} | ||
</CurrentStationContext.Provider> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import React, { createContext, useCallback, useContext, useMemo } from "react" | ||
import { PluralResponse } from "coloquent" | ||
import useSWR, { responseInterface } from "swr" | ||
|
||
import Station from "../models/Station" | ||
import updateModelList from "../helpers/updateModelList" | ||
|
||
import { useCurrentPackageContext } from "./CurrentPackageContext" | ||
interface StationContextInterface { | ||
station: Station[] | ||
updateStation: (updated: Station, reload?: boolean) => Promise<void> | ||
reloadStations: () => Promise<boolean> | ||
mutate: responseInterface<Station[], any>["mutate"] | ||
} | ||
const StationContext = createContext<StationContextInterface | null>(null) | ||
|
||
export const useStationContext = () => { | ||
const ctx = useContext(StationContext) | ||
|
||
if (ctx === null) { | ||
throw new Error("No StationContextProvider available.") | ||
} | ||
|
||
return ctx | ||
} | ||
|
||
const fetchStationsForPackage = (byUser: boolean) => async ( | ||
packageId: string | ||
): Promise<Station[]> => { | ||
let query = Station.where("package", packageId) | ||
|
||
if (byUser) { | ||
query = query.with("stationUserProgress.user") | ||
} else { | ||
query = query.with("stationTotalProgress") | ||
} | ||
|
||
const stationItem = (await query.get()) as PluralResponse | ||
|
||
return stationItem.getData() as Station[] | ||
} | ||
|
||
export const StationContextProvider: React.FC<{ | ||
byUser?: boolean | ||
}> = ({ children, byUser }) => { | ||
const { currentPackage } = useCurrentPackageContext() | ||
const cacheKey = useMemo( | ||
() => (byUser ? "package/stations_by_user" : "package/stations"), | ||
[byUser] | ||
) | ||
const fetch = useMemo(() => fetchStationsForPackage(!!byUser), [byUser]) | ||
|
||
const { data, mutate, revalidate } = useSWR( | ||
() => | ||
currentPackage.getApiId() | ||
? [currentPackage.getApiId(), cacheKey] | ||
: null, | ||
fetch, | ||
{ suspense: true } | ||
) | ||
|
||
const updateStation = useCallback( | ||
async (updatedStation: Station, reload: boolean = false) => { | ||
await mutate(updateModelList(updatedStation), reload) | ||
}, | ||
[mutate] | ||
) | ||
|
||
const ctx = { | ||
station: (data as Station[]).sort((a, b) => { | ||
return a.getSort() - b.getSort() | ||
}), | ||
updateStation, | ||
reloadStations: revalidate, | ||
mutate, | ||
} | ||
|
||
return ( | ||
<StationContext.Provider value={ctx}> | ||
{children} | ||
</StationContext.Provider> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.