Skip to content
This repository was archived by the owner on May 1, 2025. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 48 additions & 47 deletions frontend/package.json
Original file line number Diff line number Diff line change
@@ -1,49 +1,50 @@
{
"name": "frontend",
"version": "0.1.0",
"private": true,
"dependencies": {
"@reduxjs/toolkit": "^1.8.6",
"@testing-library/jest-dom": "^5.14.1",
"@testing-library/react": "^12.1.2",
"@testing-library/user-event": "^13.5.0",
"@types/react-router": "^5.1.19",
"axios": "^0.24.0",
"connected-react-router": "^6.8.0",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-redux": "^7.2.1",
"react-router": "^5.2.0",
"react-router-dom": "^6.4.2",
"react-scripts": "^4.0.3",
"redux": "^4.0.5",
"redux-csrf": "^1.1.0",
"redux-thunk": "^2.3.0",
"typescript": "^4.8.4"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": "react-app"
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
},
"devDependencies": {
"@types/react": "^18.0.21",
"@types/react-dom": "^18.0.6"
}
"name": "frontend",
"version": "0.1.0",
"private": true,
"proxy": "http://localhost:8000",
"dependencies": {
"@reduxjs/toolkit": "^1.8.6",
"@testing-library/jest-dom": "^5.14.1",
"@testing-library/react": "^12.1.2",
"@testing-library/user-event": "^13.5.0",
"@types/react-router": "^5.1.19",
"axios": "^0.24.0",
"connected-react-router": "^6.8.0",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-redux": "^7.2.1",
"react-router": "^5.2.0",
"react-router-dom": "^6.4.2",
"react-scripts": "^4.0.3",
"redux": "^4.0.5",
"redux-csrf": "^1.1.0",
"redux-thunk": "^2.3.0",
"typescript": "^4.8.4"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": "react-app"
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
},
"devDependencies": {
"@types/react": "^18.0.21",
"@types/react-dom": "^18.0.6"
}
}
111 changes: 57 additions & 54 deletions frontend/src/store/slices/hero.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,79 +2,82 @@ import { createAsyncThunk, createSlice, PayloadAction } from "@reduxjs/toolkit";
import axios from "axios";
import { RootState } from "..";

axios.defaults.xsrfCookieName = "csrftoken";
axios.defaults.xsrfHeaderName = "X-CSRFToken";

export interface HeroType {
id: number;
name: string;
age: string;
id: number;
name: string;
age: string;
}

export interface HeroState {
heros: HeroType[];
selectedHero: HeroType | null;
heros: HeroType[];
selectedHero: HeroType | null;
}

const initialState: HeroState = {
heros: [],
selectedHero: null,
heros: [],
selectedHero: null,
};

export const fetchHeros = createAsyncThunk("hero/fetchHeros", async () => {
const response = await axios.get<HeroType[]>("/api/hero/info/");
return response.data;
const response = await axios.get<HeroType[]>("/api/hero/info/");
return response.data;
});

export const fetchHero = createAsyncThunk(
"hero/fetchHero",
async (id: HeroType["id"], { dispatch }) => {
const response = await axios.get(`/api/hero/info/${id}/`);
return response.data ?? null;
}
"hero/fetchHero",
async (id: HeroType["id"], { dispatch }) => {
const response = await axios.get(`/api/hero/info/${id}/`);
return response.data ?? null;
}
);

export const postHero = createAsyncThunk(
"hero/postHero",
async (hero: Pick<HeroType, "name" | "age" >, { dispatch }) => {
const response = await axios.post("/api/hero/info/", hero);
dispatch(heroActions.addHero(response.data));
}
"hero/postHero",
async (hero: Pick<HeroType, "name" | "age">, { dispatch }) => {
const response = await axios.post("/api/hero/info/", hero);
dispatch(heroActions.addHero(response.data));
}
);

export const heroSlice = createSlice({
name: "hero",
initialState,
reducers: {
getAll: (state, action: PayloadAction<{ heros: HeroType[] }>) => {},
getHero: (state, action: PayloadAction<{ targetId: number }>) => {
const target = state.heros.find(
(hero) => hero.id === action.payload.targetId
);
state.selectedHero = target ?? null;
},
addHero: (
state,
action: PayloadAction<{ name: string; age: string }>
) => {
const newHero = {
id: state.heros[state.heros.length - 1].id + 1, // temporary
name: action.payload.name,
age: action.payload.age,
};
state.heros.push(newHero);
},
},
extraReducers: (builder) => {
// Add reducers for additional action types here, and handle loading state as needed
builder.addCase(fetchHeros.fulfilled, (state, action) => {
// Add user to the state array
state.heros = action.payload;
});
builder.addCase(fetchHero.fulfilled, (state, action) => {
state.selectedHero = action.payload;
});
builder.addCase(postHero.rejected, (_state, action) => {
console.error(action.error);
});
},
name: "hero",
initialState,
reducers: {
getAll: (state, action: PayloadAction<{ heros: HeroType[] }>) => {},
getHero: (state, action: PayloadAction<{ targetId: number }>) => {
const target = state.heros.find(
(hero) => hero.id === action.payload.targetId
);
state.selectedHero = target ?? null;
},
addHero: (
state,
action: PayloadAction<{ name: string; age: string }>
) => {
const newHero = {
id: state.heros[state.heros.length - 1].id + 1, // temporary
name: action.payload.name,
age: action.payload.age,
};
state.heros.push(newHero);
},
},
extraReducers: (builder) => {
// Add reducers for additional action types here, and handle loading state as needed
builder.addCase(fetchHeros.fulfilled, (state, action) => {
// Add user to the state array
state.heros = action.payload;
});
builder.addCase(fetchHero.fulfilled, (state, action) => {
state.selectedHero = action.payload;
});
builder.addCase(postHero.rejected, (_state, action) => {
console.error(action.error);
});
},
});

export const heroActions = heroSlice.actions;
Expand Down