Skip to content

Commit

Permalink
added update database API call
Browse files Browse the repository at this point in the history
  • Loading branch information
HetorusNL committed Aug 26, 2023
1 parent 37a72ff commit 4a41df8
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 1 deletion.
17 changes: 16 additions & 1 deletion dashboard/src/components/pages/About.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import React, { Fragment, useEffect, useState } from "react";
import React, { Fragment, useContext, useEffect, useState } from "react";
import packageJson from "../../../package.json";
import VocyaApiContext from "../../context/vocya_api/VocyaApiContext";

const About = () => {
const vocyaApiContext = useContext(VocyaApiContext);

const { dbUpdateStatus } = vocyaApiContext;

const [version, setVersion] = useState("loading build date/time...");

useEffect(() => {
Expand Down Expand Up @@ -33,6 +38,16 @@ const About = () => {
<p>
<i>Build date/time (local timezone): {version}</i>
</p>
<br></br>
<div>
<div
className="btn"
onClick={() => vocyaApiContext.actionUpdateDatabase()}
>
Update database
</div>
Database update result: {dbUpdateStatus}
</div>
</Fragment>
);
};
Expand Down
3 changes: 3 additions & 0 deletions dashboard/src/components/utils/Search.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,9 @@ const Search = ({
update={(checked) => setExactMatch(checked)}
defaultValue={false}
/>
{
// TODO: add if words: add benkyou button
}
</div>
<form onSubmit={onSubmit} className="form">
<input
Expand Down
4 changes: 4 additions & 0 deletions dashboard/src/components/utils/VocyaAPI.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,7 @@ export const apiWords = async () => {
export const apiWord = async (id) => {
return await axios.get(`${API_ENDPOINT}/word/${id}`);
};

export const apiUpdateDatabase = async () => {
return await axios.get(`${API_ENDPOINT}/update-database`);
};
1 change: 1 addition & 0 deletions dashboard/src/context/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ export const GET_WORDS = "GET_WORDS";
export const GET_WORD = "GET_WORD";
export const SET_LOADING = "SET_LOADING";
export const SET_IS_SEARCHING = "SET_IS_SEARCHING";
export const ACTION_UPDATE_DATABASE = "ACTION_UPDATE_DATABASE";
13 changes: 13 additions & 0 deletions dashboard/src/context/vocya_api/VocyaApiState.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
GET_WORD,
SET_LOADING,
SET_IS_SEARCHING,
ACTION_UPDATE_DATABASE,
} from "../types";
import {
apiChapter,
Expand All @@ -34,6 +35,7 @@ import {
apiCourseWords,
apiWord,
apiWords,
apiUpdateDatabase,
} from "../../components/utils/VocyaAPI";

const VocyaApiState = (props) => {
Expand All @@ -46,6 +48,7 @@ const VocyaApiState = (props) => {
word: {},
loading: false,
isSearching: false,
dbUpdateStatus: "no update performed",
};

const [state, dispatch] = useReducer(vocyaApiReducer, initialState);
Expand Down Expand Up @@ -148,6 +151,14 @@ const VocyaApiState = (props) => {
dispatch({ type: GET_WORD, payload: res.data[0] });
};

// update the Vocjem database
const actionUpdateDatabase = async () => {
dispatch({ type: ACTION_UPDATE_DATABASE, payload: "updating database..." });
const res = await apiUpdateDatabase();
const updateResult = res.data.result || "failure";
dispatch({ type: ACTION_UPDATE_DATABASE, payload: updateResult });
};

// setup page for new content, clearing loading and is searching
const setupPageForNewContent = () => {
setLoading(true);
Expand All @@ -173,6 +184,7 @@ const VocyaApiState = (props) => {
word: state.word,
loading: state.loading,
isSearching: state.isSearching,
dbUpdateStatus: state.dbUpdateStatus,
getCourses,
getCourse,
getCourseChapters,
Expand All @@ -189,6 +201,7 @@ const VocyaApiState = (props) => {
getWord,
setLoading,
setIsSearching,
actionUpdateDatabase,
}}
>
{props.children}
Expand Down
3 changes: 3 additions & 0 deletions dashboard/src/context/vocya_api/vocyaApiReducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
GET_WORD,
SET_LOADING,
SET_IS_SEARCHING,
ACTION_UPDATE_DATABASE,
} from "../types";

const vocyaApiReducer = (state, action) => {
Expand Down Expand Up @@ -51,6 +52,8 @@ const vocyaApiReducer = (state, action) => {
return { ...state, loading: action.payload };
case SET_IS_SEARCHING:
return { ...state, isSearching: action.payload };
case ACTION_UPDATE_DATABASE:
return { ...state, dbUpdateStatus: action.payload };
default:
return state;
}
Expand Down

0 comments on commit 4a41df8

Please sign in to comment.