From ed8f20072fe0f90c692b4bc2ce3658c53f5211a3 Mon Sep 17 00:00:00 2001 From: pikiou Date: Fri, 11 Mar 2022 13:29:04 +0100 Subject: [PATCH] =?UTF-8?q?Rename=20JavGame=20to=20Game=20because=20all=20?= =?UTF-8?q?games=20are=20not=20from=20or=20at=20Jav=C2=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../__tests__/GameList.tsx} | 9 ++-- .../__snapshots__/GameList.tsx.snap} | 2 +- .../{JavGameList => GameList}/index.tsx | 10 ++-- .../styles.module.scss | 2 +- src/components/index.ts | 4 +- src/pages/Wish/Wish.tsx | 10 ++-- src/server/gsheets/games.ts | 13 +++++ src/server/gsheets/javGames.ts | 13 ----- src/server/gsheets/localDb.ts | 2 +- src/server/index.ts | 4 +- src/services/{javGames.ts => games.ts} | 17 ++----- src/services/gamesAccessors.ts | 9 ++++ src/services/javGamesAccessors.ts | 9 ---- src/store/__tests__/javGameList.ts | 33 ++++++------- src/store/gameList.ts | 49 +++++++++++++++++++ src/store/javGameList.ts | 49 ------------------- src/store/rootReducer.ts | 4 +- 17 files changed, 112 insertions(+), 127 deletions(-) rename src/components/{JavGameList/__tests__/JavGameList.tsx => GameList/__tests__/GameList.tsx} (83%) rename src/components/{JavGameList/__tests__/__snapshots__/JavGameList.tsx.snap => GameList/__tests__/__snapshots__/GameList.tsx.snap} (90%) rename src/components/{JavGameList => GameList}/index.tsx (75%) rename src/components/{JavGameList => GameList}/styles.module.scss (92%) create mode 100644 src/server/gsheets/games.ts delete mode 100644 src/server/gsheets/javGames.ts rename src/services/{javGames.ts => games.ts} (60%) create mode 100644 src/services/gamesAccessors.ts delete mode 100644 src/services/javGamesAccessors.ts create mode 100644 src/store/gameList.ts delete mode 100644 src/store/javGameList.ts diff --git a/src/components/JavGameList/__tests__/JavGameList.tsx b/src/components/GameList/__tests__/GameList.tsx similarity index 83% rename from src/components/JavGameList/__tests__/JavGameList.tsx rename to src/components/GameList/__tests__/GameList.tsx index 91922c2..8871d70 100644 --- a/src/components/JavGameList/__tests__/JavGameList.tsx +++ b/src/components/GameList/__tests__/GameList.tsx @@ -5,15 +5,15 @@ import { render } from "@testing-library/react" import { MemoryRouter } from "react-router-dom" import mockStore from "../../../utils/mockStore" -import JavGameList from "../index" +import GameList from "../index" describe("", () => { const renderHelper = (reducer = { readyStatus: "idle" }) => { - const { dispatch, ProviderWithStore } = mockStore({ javGameList: reducer }) + const { dispatch, ProviderWithStore } = mockStore({ gameList: reducer }) const { container } = render( - + ) @@ -39,9 +39,6 @@ describe("", () => { bggPhoto: "https://cf.geekdo-images.com/thumb/img/lzczxR5cw7an7tRWeHdOrRtLyes=/fit-in/200x150/pic772547.jpg", bggId: 432, - copies: 1, - lendAvailability: 1, - notStored: 0, ean: "3421272101313", }, }, diff --git a/src/components/JavGameList/__tests__/__snapshots__/JavGameList.tsx.snap b/src/components/GameList/__tests__/__snapshots__/GameList.tsx.snap similarity index 90% rename from src/components/JavGameList/__tests__/__snapshots__/JavGameList.tsx.snap rename to src/components/GameList/__tests__/__snapshots__/GameList.tsx.snap index 0987cf0..8400e78 100644 --- a/src/components/JavGameList/__tests__/__snapshots__/JavGameList.tsx.snap +++ b/src/components/GameList/__tests__/__snapshots__/GameList.tsx.snap @@ -2,7 +2,7 @@ exports[` renders 1`] = `

Jeux JAV diff --git a/src/components/JavGameList/index.tsx b/src/components/GameList/index.tsx similarity index 75% rename from src/components/JavGameList/index.tsx rename to src/components/GameList/index.tsx index 1ba4b0d..a6b843d 100644 --- a/src/components/JavGameList/index.tsx +++ b/src/components/GameList/index.tsx @@ -10,14 +10,14 @@ interface Props { ids: EntityId[] } -const JavGameList = ({ ids }: Props) => { - const { entities: jeuxJav } = useSelector((state: AppState) => state.javGameList, shallowEqual) +const GameList = ({ ids }: Props) => { + const { entities: jeux } = useSelector((state: AppState) => state.gameList, shallowEqual) return ( -
+

Jeux JAV

    {ids.map((id) => { - const jeu = jeuxJav[id] + const jeu = jeux[id] if (!jeu) { return
  • Le jeu #{id} n'existe pas
  • } @@ -33,4 +33,4 @@ const JavGameList = ({ ids }: Props) => { ) } -export default memo(JavGameList) +export default memo(GameList) diff --git a/src/components/JavGameList/styles.module.scss b/src/components/GameList/styles.module.scss similarity index 92% rename from src/components/JavGameList/styles.module.scss rename to src/components/GameList/styles.module.scss index 7ebaf64..8901dae 100644 --- a/src/components/JavGameList/styles.module.scss +++ b/src/components/GameList/styles.module.scss @@ -1,6 +1,6 @@ @import "../../theme/variables"; -.JavGameList { +.GameList { color: $color-white; ul { diff --git a/src/components/index.ts b/src/components/index.ts index 79f8a8f..5b3c1b9 100755 --- a/src/components/index.ts +++ b/src/components/index.ts @@ -1,6 +1,6 @@ import AnnouncementLink from "./AnnouncementLink" import ErrorBoundary from "./ErrorBoundary" -import JavGameList from "./JavGameList" +import GameList from "./GameList" import Loading from "./Loading" import LoginForm from "./LoginForm" import Notifications, { fetchFor as fetchForNotifications } from "./Notifications" @@ -16,7 +16,7 @@ import WishAdd from "./WishAdd" export { AnnouncementLink, ErrorBoundary, - JavGameList, + GameList, Loading, LoginForm, Notifications, diff --git a/src/pages/Wish/Wish.tsx b/src/pages/Wish/Wish.tsx index 6a5805d..1e41319 100755 --- a/src/pages/Wish/Wish.tsx +++ b/src/pages/Wish/Wish.tsx @@ -4,9 +4,9 @@ import { useDispatch, useSelector, shallowEqual } from "react-redux" import { Helmet } from "react-helmet" import { AppDispatch, AppState, AppThunk, EntitiesRequest } from "../../store" -import { fetchJavGameListIfNeed } from "../../store/javGameList" +import { fetchGameListIfNeed } from "../../store/gameList" import { fetchWishListIfNeed } from "../../store/wishList" -import { JavGameList, WishAdd } from "../../components" +import { GameList, WishAdd } from "../../components" import styles from "./styles.module.scss" export type Props = RouteComponentProps @@ -30,7 +30,7 @@ function useList( if (readyStatus === "failure") return

    Oops, Failed to load list!

    - return + return } } @@ -41,7 +41,7 @@ const Wish: FC = (): JSX.Element => { {/* {useList((state: AppState) => state.wishList, fetchWishListifNeed)()} */} - {useList((state: AppState) => state.javGameList, fetchJavGameListIfNeed)()} + {useList((state: AppState) => state.gameList, fetchGameListIfNeed)()} {/* */} @@ -50,6 +50,6 @@ const Wish: FC = (): JSX.Element => { } // Fetch server-side data here -export const loadData = (): AppThunk[] => [fetchWishListIfNeed(), fetchJavGameListIfNeed()] +export const loadData = (): AppThunk[] => [fetchWishListIfNeed(), fetchGameListIfNeed()] export default memo(Wish) diff --git a/src/server/gsheets/games.ts b/src/server/gsheets/games.ts new file mode 100644 index 0000000..492b71f --- /dev/null +++ b/src/server/gsheets/games.ts @@ -0,0 +1,13 @@ +import ExpressAccessors from "./expressAccessors" +import { Game, GameWithoutId, translationGame } from "../../services/games" + +const expressAccessor = new ExpressAccessors( + "Games", + new Game(), + translationGame +) + +export const gameListGet = expressAccessor.listGet() +export const gameGet = expressAccessor.get() +export const gameAdd = expressAccessor.add() +export const gameSet = expressAccessor.set() diff --git a/src/server/gsheets/javGames.ts b/src/server/gsheets/javGames.ts deleted file mode 100644 index 9c2f8b9..0000000 --- a/src/server/gsheets/javGames.ts +++ /dev/null @@ -1,13 +0,0 @@ -import ExpressAccessors from "./expressAccessors" -import { JavGame, JavGameWithoutId, translationJavGame } from "../../services/javGames" - -const expressAccessor = new ExpressAccessors( - "JavGames", - new JavGame(), - translationJavGame -) - -export const javGameListGet = expressAccessor.listGet() -export const javGameGet = expressAccessor.get() -export const javGameAdd = expressAccessor.add() -export const javGameSet = expressAccessor.set() diff --git a/src/server/gsheets/localDb.ts b/src/server/gsheets/localDb.ts index 723d50f..6e59cf9 100644 --- a/src/server/gsheets/localDb.ts +++ b/src/server/gsheets/localDb.ts @@ -12,7 +12,7 @@ const ANONYMIZED_DB_PATH = path.resolve(process.cwd(), "access/dbAnonymized.json export class SheetNames { Announcements = "Annonces" - JavGames = "Jeux JAV" + Games = "Jeux" PreVolunteers = "PreMembres" diff --git a/src/server/index.ts b/src/server/index.ts index 62e6b93..d65524f 100755 --- a/src/server/index.ts +++ b/src/server/index.ts @@ -18,7 +18,7 @@ import ssr from "./ssr" import certbotRouter from "../routes/certbot" import { hasSecret, secure } from "./secure" import { announcementListGet } from "./gsheets/announcements" -import { javGameListGet } from "./gsheets/javGames" +import { gameListGet } from "./gsheets/games" import { preVolunteerAdd, preVolunteerCountGet } from "./gsheets/preVolunteers" import { teamListGet } from "./gsheets/teams" import { @@ -79,7 +79,7 @@ app.get( * APIs */ // Google Sheets API -app.get("/JavGameListGet", javGameListGet) +app.get("/GameListGet", gameListGet) app.get("/WishListGet", wishListGet) app.post("/WishAdd", wishAdd) app.post("/PreVolunteerAdd", preVolunteerAdd) diff --git a/src/services/javGames.ts b/src/services/games.ts similarity index 60% rename from src/services/javGames.ts rename to src/services/games.ts index 7d40a72..1d1768c 100644 --- a/src/services/javGames.ts +++ b/src/services/games.ts @@ -1,4 +1,4 @@ -export class JavGame { +export class Game { id = 0 title = "" @@ -19,18 +19,12 @@ export class JavGame { bggId = 0 - copies = 1 - - lendAvailability = 0 - - notStored = 0 - ean = "" bggPhoto = "" } -export const translationJavGame: { [k in keyof JavGame]: string } = { +export const translationGame: { [k in keyof Game]: string } = { id: "id", title: "titre", author: "auteur", @@ -41,13 +35,10 @@ export const translationJavGame: { [k in keyof JavGame]: string } = { type: "type", poufpaf: "poufpaf", bggId: "bggId", - copies: "exemplaires", - lendAvailability: "dispoPret", - notStored: "nonRangee", ean: "ean", bggPhoto: "bggPhoto", } -export const elementName = "JavGame" +export const elementName = "Game" -export type JavGameWithoutId = Omit +export type GameWithoutId = Omit diff --git a/src/services/gamesAccessors.ts b/src/services/gamesAccessors.ts new file mode 100644 index 0000000..c0ebe27 --- /dev/null +++ b/src/services/gamesAccessors.ts @@ -0,0 +1,9 @@ +import ServiceAccessors from "./accessors" +import { elementName, Game, GameWithoutId } from "./games" + +const serviceAccessors = new ServiceAccessors(elementName) + +export const gameListGet = serviceAccessors.listGet() +export const gameGet = serviceAccessors.get() +export const gameAdd = serviceAccessors.add() +export const gameSet = serviceAccessors.set() diff --git a/src/services/javGamesAccessors.ts b/src/services/javGamesAccessors.ts deleted file mode 100644 index c27c58d..0000000 --- a/src/services/javGamesAccessors.ts +++ /dev/null @@ -1,9 +0,0 @@ -import ServiceAccessors from "./accessors" -import { elementName, JavGame, JavGameWithoutId } from "./javGames" - -const serviceAccessors = new ServiceAccessors(elementName) - -export const javGameListGet = serviceAccessors.listGet() -export const javGameGet = serviceAccessors.get() -export const javGameAdd = serviceAccessors.add() -export const javGameSet = serviceAccessors.set() diff --git a/src/store/__tests__/javGameList.ts b/src/store/__tests__/javGameList.ts index 1489545..ab91913 100644 --- a/src/store/__tests__/javGameList.ts +++ b/src/store/__tests__/javGameList.ts @@ -2,18 +2,18 @@ import axios from "axios" import _ from "lodash" import mockStore from "../../utils/mockStore" -import JavGameList, { +import GameList, { initialState, getRequesting, getSuccess, getFailure, - fetchJavGameList, -} from "../javGameList" -import { JavGame } from "../../services/javGames" + fetchGameList, +} from "../gameList" +import { Game } from "../../services/games" jest.mock("axios") -const mockData: JavGame[] = [ +const mockData: Game[] = [ { id: 5, title: "6 qui prend!", @@ -27,22 +27,19 @@ const mockData: JavGame[] = [ bggPhoto: "https://cf.geekdo-images.com/thumb/img/lzczxR5cw7an7tRWeHdOrRtLyes=/fit-in/200x150/pic772547.jpg", bggId: 432, - copies: 1, - lendAvailability: 1, - notStored: 0, ean: "3421272101313", }, ] const mockError = "Oops! Something went wrong." -describe("JavGameList reducer", () => { +describe("GameList reducer", () => { it("should handle initial state", () => { // @ts-expect-error - expect(JavGameList(undefined, {})).toEqual(initialState) + expect(GameList(undefined, {})).toEqual(initialState) }) it("should handle requesting correctly", () => { - expect(JavGameList(undefined, { type: getRequesting.type })).toEqual({ + expect(GameList(undefined, { type: getRequesting.type })).toEqual({ readyStatus: "request", ids: [], entities: {}, @@ -50,7 +47,7 @@ describe("JavGameList reducer", () => { }) it("should handle success correctly", () => { - expect(JavGameList(undefined, { type: getSuccess.type, payload: mockData })).toEqual({ + expect(GameList(undefined, { type: getSuccess.type, payload: mockData })).toEqual({ ...initialState, readyStatus: "success", ids: _.map(mockData, "id"), @@ -59,7 +56,7 @@ describe("JavGameList reducer", () => { }) it("should handle failure correctly", () => { - expect(JavGameList(undefined, { type: getFailure.type, payload: mockError })).toEqual({ + expect(GameList(undefined, { type: getFailure.type, payload: mockError })).toEqual({ ...initialState, readyStatus: "failure", error: mockError, @@ -67,8 +64,8 @@ describe("JavGameList reducer", () => { }) }) -describe("JavGameList action", () => { - it("fetches JavGame list successful", async () => { +describe("GameList action", () => { + it("fetches Game list successful", async () => { const { dispatch, getActions } = mockStore() const expectedActions = [ { type: getRequesting.type, payload: undefined }, @@ -78,11 +75,11 @@ describe("JavGameList action", () => { // @ts-expect-error axios.get.mockResolvedValue({ data: mockData }) - await dispatch(fetchJavGameList()) + await dispatch(fetchGameList()) expect(getActions()).toEqual(expectedActions) }) - it("fetches JavGame list failed", async () => { + it("fetches Game list failed", async () => { const { dispatch, getActions } = mockStore() const expectedActions = [ { type: getRequesting.type }, @@ -92,7 +89,7 @@ describe("JavGameList action", () => { // @ts-expect-error axios.get.mockRejectedValue({ message: mockError }) - await dispatch(fetchJavGameList()) + await dispatch(fetchGameList()) expect(getActions()).toEqual(expectedActions) }) }) diff --git a/src/store/gameList.ts b/src/store/gameList.ts new file mode 100644 index 0000000..dddf493 --- /dev/null +++ b/src/store/gameList.ts @@ -0,0 +1,49 @@ +import { PayloadAction, createSlice, createEntityAdapter } from "@reduxjs/toolkit" + +import { StateRequest, toastError, elementListFetch } from "./utils" +import { Game } from "../services/games" +import { AppThunk, AppState } from "." +import { gameListGet } from "../services/gamesAccessors" + +const gameAdapter = createEntityAdapter() + +export const initialState = gameAdapter.getInitialState({ + readyStatus: "idle", +} as StateRequest) + +const gameList = createSlice({ + name: "gameList", + initialState, + reducers: { + getRequesting: (state) => { + state.readyStatus = "request" + }, + getSuccess: (state, { payload }: PayloadAction) => { + state.readyStatus = "success" + gameAdapter.setAll(state, payload) + }, + getFailure: (state, { payload }: PayloadAction) => { + state.readyStatus = "failure" + state.error = payload + }, + }, +}) + +export default gameList.reducer +export const { getRequesting, getSuccess, getFailure } = gameList.actions + +export const fetchGameList = elementListFetch( + gameListGet, + getRequesting, + getSuccess, + getFailure, + (error: Error) => toastError(`Erreur lors du chargement des jeux JAV: ${error.message}`) +) + +const shouldFetchGameList = (state: AppState) => state.gameList.readyStatus !== "success" + +export const fetchGameListIfNeed = (): AppThunk => (dispatch, getState) => { + if (shouldFetchGameList(getState())) return dispatch(fetchGameList()) + + return null +} diff --git a/src/store/javGameList.ts b/src/store/javGameList.ts deleted file mode 100644 index eb4106f..0000000 --- a/src/store/javGameList.ts +++ /dev/null @@ -1,49 +0,0 @@ -import { PayloadAction, createSlice, createEntityAdapter } from "@reduxjs/toolkit" - -import { StateRequest, toastError, elementListFetch } from "./utils" -import { JavGame } from "../services/javGames" -import { AppThunk, AppState } from "." -import { javGameListGet } from "../services/javGamesAccessors" - -const javGameAdapter = createEntityAdapter() - -export const initialState = javGameAdapter.getInitialState({ - readyStatus: "idle", -} as StateRequest) - -const javGameList = createSlice({ - name: "javGameList", - initialState, - reducers: { - getRequesting: (state) => { - state.readyStatus = "request" - }, - getSuccess: (state, { payload }: PayloadAction) => { - state.readyStatus = "success" - javGameAdapter.setAll(state, payload) - }, - getFailure: (state, { payload }: PayloadAction) => { - state.readyStatus = "failure" - state.error = payload - }, - }, -}) - -export default javGameList.reducer -export const { getRequesting, getSuccess, getFailure } = javGameList.actions - -export const fetchJavGameList = elementListFetch( - javGameListGet, - getRequesting, - getSuccess, - getFailure, - (error: Error) => toastError(`Erreur lors du chargement des jeux JAV: ${error.message}`) -) - -const shouldFetchJavGameList = (state: AppState) => state.javGameList.readyStatus !== "success" - -export const fetchJavGameListIfNeed = (): AppThunk => (dispatch, getState) => { - if (shouldFetchJavGameList(getState())) return dispatch(fetchJavGameList()) - - return null -} diff --git a/src/store/rootReducer.ts b/src/store/rootReducer.ts index b30ac58..7b3b653 100644 --- a/src/store/rootReducer.ts +++ b/src/store/rootReducer.ts @@ -2,7 +2,7 @@ import { History } from "history" import { connectRouter } from "connected-react-router" import auth from "./auth" -import javGameList from "./javGameList" +import gameList from "./gameList" import announcementList from "./announcementList" import preVolunteerAdd from "./preVolunteerAdd" import preVolunteerCount from "./preVolunteerCount" @@ -25,7 +25,7 @@ import wishList from "./wishList" // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types export default (history: History) => ({ auth, - javGameList, + gameList, announcementList, preVolunteerAdd, preVolunteerCount,