-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
203 additions
and
18 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,17 @@ | ||
import { ICategories } from "../../shared/types" | ||
import { ICategories, IPlan } from "../../shared/types" | ||
|
||
const API_ENDPOINT = `${location.protocol}//${location.hostname}:3000/` | ||
const API_ENTRYPOINT = `${location.protocol}//${location.hostname}:3000/` | ||
|
||
alert(API_ENDPOINT) | ||
const fetchData = async (endpoint: string) => { | ||
const response = await fetch(`${API_ENTRYPOINT}${endpoint}`) | ||
if (!response.ok) { | ||
throw new Error(`Could not fetch ${endpoint}, received ${response.status}`) | ||
} | ||
return response.json() | ||
} | ||
|
||
export const categories = () => | ||
fetch(API_ENDPOINT + "categories").then(res => | ||
res.json() | ||
) as Promise<ICategories> | ||
export const categories = () => fetchData("categories") as Promise<ICategories> | ||
|
||
export default { categories } | ||
export const plan = (id: number) => fetchData(`plans/${id}`) as Promise<IPlan> | ||
|
||
export default { categories, plan } |
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
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,166 @@ | ||
import React from "react" | ||
|
||
import { | ||
Box, | ||
Link, | ||
Typography, | ||
Table, | ||
TableHead, | ||
TableBody, | ||
TableRow, | ||
TableCell | ||
} from "@mui/material" | ||
import ErrorOutlineIcon from "@mui/icons-material/ErrorOutline" | ||
import CircularProgress from "@mui/material/CircularProgress" | ||
|
||
import { IPlan, IPlanEntry, Weekday } from "../../shared/types" | ||
import apiCalls from "../apiCalls" | ||
|
||
type Props = { | ||
planId: number | null | ||
weekday: Weekday | ||
} | ||
|
||
type State = { | ||
displayedPlan: IPlan | null | ||
loading: boolean | ||
error: boolean | ||
} | ||
|
||
export default class WeekDayTab extends React.Component<Props, State> { | ||
constructor(props: Props) { | ||
super(props) | ||
|
||
this.state = { | ||
displayedPlan: null, | ||
loading: false, | ||
error: false | ||
} | ||
} | ||
|
||
fetchPlanIfNeeded() { | ||
if (this.props.planId === null) { | ||
return | ||
} | ||
|
||
if (this.props.planId !== this.state.displayedPlan?.id) { | ||
this.setState({ | ||
loading: true, | ||
error: false | ||
}) | ||
|
||
apiCalls | ||
.plan(this.props.planId) | ||
.then(plan => { | ||
this.setState({ | ||
displayedPlan: plan, | ||
loading: false | ||
}) | ||
}) | ||
.catch(() => { | ||
this.setState({ | ||
loading: false, | ||
error: true | ||
}) | ||
}) | ||
} | ||
} | ||
|
||
componentDidUpdate(prevProps: Props) { | ||
if (prevProps.planId !== this.props.planId) { | ||
this.setState({ | ||
loading: false, | ||
error: false | ||
}) | ||
this.fetchPlanIfNeeded() | ||
} | ||
} | ||
|
||
componentDidMount() { | ||
this.fetchPlanIfNeeded() | ||
} | ||
|
||
errorMessage = ( | ||
<Box | ||
sx={{ | ||
display: "flex", | ||
flexDirection: "column", | ||
alignItems: "center", | ||
pt: "64px", | ||
px: "32px", | ||
gap: "16px" | ||
}} | ||
> | ||
<ErrorOutlineIcon sx={{ fontSize: "64px" }} /> | ||
<Typography textAlign="center"> | ||
Wystąpił błąd podczas ładowania planu. Spróbuj ponownie później. | ||
</Typography> | ||
<a href="https://zst-radom.edu.pl/plan_www/"> | ||
<Link | ||
variant="button" | ||
onClick={() => { | ||
console.info("I'm a button.") | ||
}} | ||
> | ||
Przejdź do starszej strony planu | ||
</Link> | ||
</a> | ||
</Box> | ||
) | ||
|
||
loadingMessage = ( | ||
<Box | ||
sx={{ | ||
display: "flex", | ||
flexDirection: "column", | ||
alignItems: "center", | ||
pt: "64px", | ||
px: "32px", | ||
gap: "16px" | ||
}} | ||
> | ||
<CircularProgress /> | ||
</Box> | ||
) | ||
|
||
render() { | ||
if (this.state.error) { | ||
return this.errorMessage | ||
} | ||
|
||
if (this.state.loading) { | ||
return this.loadingMessage | ||
} | ||
|
||
console.log(this.state.displayedPlan) | ||
|
||
return ( | ||
<Table> | ||
<TableBody> | ||
{this.state.displayedPlan?.hours.map((hour, i) => ( | ||
<TableRow key={hour}> | ||
<TableCell>{hour}</TableCell> | ||
<TableCell> | ||
{this.state.displayedPlan?.lessons[this.props.weekday][i] | ||
?.filter<IPlanEntry>( | ||
(entry): entry is IPlanEntry => entry !== null | ||
) | ||
.map(entry => ( | ||
<Box | ||
sx={{ | ||
display: "flex", | ||
justifyContent: "space-between" | ||
}} | ||
> | ||
<span>{entry.name}</span> | ||
<span>{entry.room}</span> | ||
</Box> | ||
))} | ||
</TableCell> | ||
</TableRow> | ||
))} | ||
</TableBody> | ||
</Table> | ||
) | ||
} | ||
} |