Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Logged in user provider via JSONAPI. #75

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
23 changes: 13 additions & 10 deletions src/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import ApiDocsSpecific from './templates/dataset/api';
import Publishers from './templates/publishers';
import '@civicactions/data-catalog-components/dist/index.css';
import './theme/index.scss';
import { UserState } from "./config/User/UserState";

import { library } from '@fortawesome/fontawesome-svg-core';
import { fab } from '@fortawesome/free-brands-svg-icons';
Expand All @@ -19,16 +20,18 @@ library.add(fab, fas);

function App() {
return (
<Router>
<NotFound default />
<Home path="/" />
<About path="/about"/>
<Publishers path="/publishers" />
<SearchTemplate path="/search" />
<ApiDocsFull path="/api" />
<Dataset path="/dataset/:id" />
<ApiDocsSpecific path="/dataset/:id/api" />
</Router>
<UserState>
<Router>
<NotFound default />
<Home path="/" />
<About path="/about"/>
<Publishers path="/publishers" />
<SearchTemplate path="/search" />
<ApiDocsFull path="/api" />
<Dataset path="/dataset/:id" />
<ApiDocsSpecific path="/dataset/:id/api" />
</Router>
</UserState>
);
}

Expand Down
45 changes: 45 additions & 0 deletions src/config/User/UserAction.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import axios from "axios";

// Set Loading
export const setLoading = (dispatch, status) =>
dispatch({ type: "SET_LOADING", payload: status });

// Set Error
export const setError = (dispatch, error) =>
dispatch({
type: "SET_ERROR",
payload: { error: error.status, message: error.message }
});

export const pathToJSONAPI = (apiUrl) => {
return apiUrl.replace("api/1", "jsonapi");
};

// Set User (get user info)
export const getUser = async dispatch => {
setLoading(dispatch, true);

// do fetch
await axios
.get(pathToJSONAPI(process.env.REACT_APP_ROOT_URL))
.then(res => {
const result = res.data;
// set user info
dispatch({
type: "SET_USER",
payload: result.meta.links.me
});
})
.catch(error => {
const result = error;

// set error if has any
dispatch({
type: "SET_ERROR",
payload: {
error: true,
message: result
}
});
});
};
3 changes: 3 additions & 0 deletions src/config/User/UserContext.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { createContext } from "react";

export const UserContext = createContext();
22 changes: 22 additions & 0 deletions src/config/User/UserReducer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
export default (state, action) => {
switch (action.type) {
case "SET_USER":
return {
...state,
user: action.payload
};
case "SET_ERROR":
return {
...state,
error: action.payload.error,
message: action.payload.message
};
case "SET_LOADING":
return {
...state,
loading: action.payload
};
default:
return state;
}
};
30 changes: 30 additions & 0 deletions src/config/User/UserState.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import React, { useContext, useReducer } from "react";
import { UserContext } from "./UserContext";
import UserReducer from "./UserReducer";

export const useUser = () => {
const { state, dispatch } = useContext(UserContext);
return [state, dispatch];
};

export const UserState = ({ children }) => {
const initialState = {
user: {},
loading: false,
error: false,
message: ""
};

const [state, dispatch] = useReducer(UserReducer, initialState);

return (
<UserContext.Provider
value={{
state: state,
dispatch: dispatch
}}
>
{children}
</UserContext.Provider>
);
};
7 changes: 7 additions & 0 deletions src/config/User/user.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import React from 'react';
import {pathToJSONAPI} from './UserAction';

test('Converts a URL with an API path to one with JSONAPI path.', () => {
expect(pathToJSONAPI("http://datamarinescotland.localtest.me/api/1"))
.toEqual("http://datamarinescotland.localtest.me/jsonapi");
});