Skip to content

Commit

Permalink
feat: apislice
Browse files Browse the repository at this point in the history
  • Loading branch information
tobiasprima committed Mar 19, 2024
1 parent 570fc58 commit 3ffb5c0
Show file tree
Hide file tree
Showing 7 changed files with 117 additions and 18 deletions.
91 changes: 83 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
},
"devDependencies": {
"@babel/core": "^7.24.1",
"@babel/plugin-proposal-private-property-in-object": "^7.21.11",
"@babel/preset-env": "^7.24.1",
"@babel/preset-react": "^7.24.1",
"@testing-library/react": "^14.2.1",
Expand Down
File renamed without changes.
13 changes: 13 additions & 0 deletions src/app/store.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { configureStore } from "@reduxjs/toolkit";
import { apiSlice } from "./api/apiSlice";
import { setupListeners } from "@reduxjs/toolkit/query";

export const store = configureStore({
reducer: {
[apiSlice.reducerPath]: apiSlice.reducer,
},
middleware: getDefaultMiddleware => getDefaultMiddleware().concat(apiSlice.middleware),
devTools: false
})

setupListeners(store.dispatch);
16 changes: 16 additions & 0 deletions src/features/todos/TodoList.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,19 @@ import React from 'react'
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'
import { faTrash, faUpload } from '@fortawesome/free-solid-svg-icons'
import { useState } from 'react'
import { useGetTodosQuery } from '../../app/api/apiSlice'

const TodoList = () => {
const [ newTodo, setNewTodo ] = useState('')

const {
data: todos,
isLoading,
isSuccess,
isError,
error
} = useGetTodosQuery()

const handleSubmit = (e)=> {
e.preventDefault()
//addTodo
Expand All @@ -30,6 +39,13 @@ const TodoList = () => {
</form>

let content;
if (isLoading) {
content = <p>Loading ...</p>
} else if (isSuccess){
content = JSON.stringify(todos)
} else if (isError) {
content = <p>{error.message || 'Error fetching todos'}</p>
}

return (
<main>
Expand Down
10 changes: 5 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import React from 'react';
import ReactDOM from 'react-dom/client';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';

import { ApiProvider } from '@reduxjs/toolkit/dist/query/react';
import { apiSlice } from './features/api/apiSlice';
import { Provider } from 'react-redux';
import { store } from './app/store';

const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(
<React.StrictMode>
<ApiProvider api={apiSlice}>
<Provider store={store}>
<App />
</ApiProvider>
</Provider>
</React.StrictMode>
);
4 changes: 0 additions & 4 deletions src/setupTests.js
Original file line number Diff line number Diff line change
@@ -1,5 +1 @@
// jest-dom adds custom jest matchers for asserting on DOM nodes.
// allows you to do things like:
// expect(element).toHaveTextContent(/react/i)
// learn more: https://github.com/testing-library/jest-dom
import '@testing-library/jest-dom';

0 comments on commit 3ffb5c0

Please sign in to comment.