Advertisement
Guest User

Untitled

a guest
Sep 22nd, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React, { useState, useReducer, useEffect } from 'react';
  2. import { useSNAPI } from '../context/snapi-context';
  3.  
  4. const LOADING = Symbol('LOADING');
  5. const SUCCESS = Symbol('SUCCESS');
  6. const UNAUTHORIZED = Symbol('UNAUTHORIZED');
  7. const ERROR = Symbol('ERROR');
  8.  
  9. const RecordHOC = ({ table, sysId, options, renderSuccess, renderLoading, renderError, renderUnauthorized }) => {
  10.  
  11.     const [state, dispatch] = useReducer((state, action) => {
  12.         switch (action.type) {
  13.             case 'success':
  14.                 return {
  15.                     stage: SUCCESS,
  16.                     component: renderSuccess,
  17.                     data: action.data,
  18.                 };
  19.             case 'error':
  20.                 return {
  21.                     stage: ERROR,
  22.                     component: renderError,
  23.                     data: action. data,
  24.                 };
  25.             case 'unauthorized':
  26.                 return {
  27.                     stage: UNAUTHORIZED,
  28.                     component: renderUnauthorized,
  29.                     data: null,
  30.                 };
  31.             case 'loading':
  32.                 return {
  33.                     stage: LOADING,
  34.                     component: renderLoading,
  35.                     data: null,
  36.                 };
  37.             default: throw new Error();
  38.         }
  39.     }, {
  40.         stage: LOADING,
  41.         component: renderLoading,
  42.         data: null,
  43.     });
  44.  
  45.  
  46.     const sn = useSNAPI();
  47.  
  48.     const fetch = async (table, sysId, options) => {
  49.         const response = await sn.getRecord(table, sysId, options);
  50.         if (response.ok) {
  51.             const data = await response.json();
  52.             dispatch({
  53.                 type: 'success',
  54.                 data
  55.             });
  56.         } else if (response.status == 401) {
  57.             dispatch({ type: 'unauthorized' });
  58.         } else {
  59.             try {
  60.                 const data = await response.json();
  61.                 dispatch({
  62.                     type: 'error',
  63.                     data
  64.                 });
  65.             } catch (e) {
  66.                 console.log(response);
  67.                 dispatch({
  68.                     type: 'error',
  69.                     data: response,
  70.                 });
  71.             }
  72.         }
  73.     }
  74.  
  75.     useEffect(() => {
  76.         (async () => {
  77.             fetch(table, sysId, options);
  78.         })();
  79.     }, []);
  80.  
  81.     console.log(state.component);
  82.  
  83.     return (
  84.         <div>
  85.         </div>
  86.     );
  87. }
  88.  
  89. export default RecordHOC;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement