From bd033418577d3ce6c201a202adad16769aadf08c Mon Sep 17 00:00:00 2001 From: Alex Scott Date: Mon, 12 Apr 2021 14:49:50 +0200 Subject: [PATCH] Logged in user provider via JSONAPI. --- src/App.jsx | 23 +++++++++-------- src/config/User/UserAction.js | 45 ++++++++++++++++++++++++++++++++++ src/config/User/UserContext.js | 3 +++ src/config/User/UserReducer.js | 22 +++++++++++++++++ src/config/User/UserState.jsx | 30 +++++++++++++++++++++++ src/config/User/user.test.js | 7 ++++++ 6 files changed, 120 insertions(+), 10 deletions(-) create mode 100644 src/config/User/UserAction.js create mode 100644 src/config/User/UserContext.js create mode 100644 src/config/User/UserReducer.js create mode 100644 src/config/User/UserState.jsx create mode 100644 src/config/User/user.test.js diff --git a/src/App.jsx b/src/App.jsx index e8562c2..dc4720b 100644 --- a/src/App.jsx +++ b/src/App.jsx @@ -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'; @@ -19,16 +20,18 @@ library.add(fab, fas); function App() { return ( - - - - - - - - - - + + + + + + + + + + + + ); } diff --git a/src/config/User/UserAction.js b/src/config/User/UserAction.js new file mode 100644 index 0000000..bffd5d2 --- /dev/null +++ b/src/config/User/UserAction.js @@ -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 + } + }); + }); +}; diff --git a/src/config/User/UserContext.js b/src/config/User/UserContext.js new file mode 100644 index 0000000..c5d5d2e --- /dev/null +++ b/src/config/User/UserContext.js @@ -0,0 +1,3 @@ +import { createContext } from "react"; + +export const UserContext = createContext(); diff --git a/src/config/User/UserReducer.js b/src/config/User/UserReducer.js new file mode 100644 index 0000000..de205bb --- /dev/null +++ b/src/config/User/UserReducer.js @@ -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; + } +}; diff --git a/src/config/User/UserState.jsx b/src/config/User/UserState.jsx new file mode 100644 index 0000000..2e1af97 --- /dev/null +++ b/src/config/User/UserState.jsx @@ -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 ( + + {children} + + ); +}; diff --git a/src/config/User/user.test.js b/src/config/User/user.test.js new file mode 100644 index 0000000..96bb7c5 --- /dev/null +++ b/src/config/User/user.test.js @@ -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"); +});