Skip to content

Commit

Permalink
Update staging to resolve the naming issue and fetching the token if …
Browse files Browse the repository at this point in the history
…expired
  • Loading branch information
mustafasaifee42 authored and mykolaskrynnyk committed Nov 6, 2024
1 parent e63c821 commit 6047395
Show file tree
Hide file tree
Showing 36 changed files with 806 additions and 820 deletions.
14 changes: 4 additions & 10 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,20 +63,14 @@ npm install

To start the project locally, you can run `npm run dev` in the project folder in terminal or command prompt. This will run the app in development mode. Open [http://localhost:5173/](http://localhost:5173/) to view it in the browser. The page will reload if you make edits. You will also see any lint errors in the console.

To deploy locally successfully you will need to make some changes to the `Constants.tsx`:

- `export const WEB_ADDRESS = 'https://signals.data.undp.org/';` to `export const WEB_ADDRESS = './';`
- `export const API_ACCESS_TOKEN = process.env.INPUT_ACCESS_TOKEN_FOR_API';` to `export const API_ACCESS_TOKEN = import.meta.env.VITE_ACCESS_CODE`
- `export const REDIRECT_URL = process.env.INPUT_REDIRECT_URI_FOR_MSAL;` to `export const REDIRECT_URL = import.meta.env.VITE_REDIRECT_URL;`

These changes are only required for local deployment. For production, you will need to keep the value same.

Also you will need a `.env.local` file in the root folder. the `env` file would require the following variable and values
You will need a `.env.local` file in the root folder. the `env` file would require the following variable and values

```
VITE_API_LINK=https://signals-and-trends-api.azurewebsites.net/v1/
VITE_ACCESS_CODE={{contact the team for API secret}}
VITE_REDIRECT_URL=http://localhost:5173/
VITE_AUTHORITY=https://login.microsoftonline.com/{{contact the team for the complete URL}}
VITE_CLIENT_ID={{contact the team forClient ID}}
VITE_API_BASEURL=https://ftss-api-dev.azurewebsites.net/
```

### Tooling Setup
Expand Down
41 changes: 41 additions & 0 deletions src/API/apiConfig.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/* eslint-disable no-param-reassign */
import axios from 'axios';
import QueryString from 'qs';
import { API_BASEURL } from '../Constants';
import { refreshHandler } from '../Utils/AuthStatusHandler';
import { getLocalStorage, setLocalStorage } from '../Utils/UpdateLocalStrage';

export const axiosInstance = axios.create({
baseURL: API_BASEURL,
headers: {
'Content-Type': 'application/json',
access_token: getLocalStorage().token,
},
paramsSerializer: {
serialize: params => {
return QueryString.stringify(params, { arrayFormat: 'repeat' });
},
},
timeout: 60000,
});

axiosInstance.interceptors.request.use(
async config => {
const { token, tokenExp } = getLocalStorage();
if (!token || !tokenExp || new Date(tokenExp as string) < new Date()) {
const [newAccessToken, newAccessTokenExp] = await refreshHandler();
if (newAccessToken && newAccessTokenExp) {
config.headers.access_token = newAccessToken;
setLocalStorage('token', String(newAccessToken));
setLocalStorage('tokenExp', String(newAccessTokenExp));
}
} else {
config.headers.access_token = token;
}

return config;
},
error => {
return Promise.reject(error);
},
);
2 changes: 1 addition & 1 deletion src/api/choices_calls.ts → src/API/choicesCalls.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable @typescript-eslint/naming-convention */
import { isAxiosError } from 'axios';
import { axiosInstance } from './api_config';
import { axiosInstance } from './apiConfig';

export function getChoices() {
return axiosInstance
Expand Down
4 changes: 4 additions & 0 deletions src/API/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export * from './userCalls';
export * from './choicesCalls';
export * from './signalsCall';
export * from './trendsCalls';
105 changes: 89 additions & 16 deletions src/api/signals_call.ts → src/API/signalsCall.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,87 @@
/* eslint-disable @typescript-eslint/naming-convention */
import { isAxiosError } from 'axios';
import { axiosInstance } from './api_config';
import {
BaseSignalsParams,
CreateSignalParams,
ReadMySignalsParams,
SignalsSearchResponse,
SignalDataType,
UpdateSignalParams,
} from '../Types';

export function searchSignals(params: BaseSignalsParams = {}) {
import { axiosInstance } from './apiConfig';
import { SignalDataType, StatusDataType } from '../Types';

interface BaseSignalsParamsDataType {
page?: number;
per_page?: number;
order_by?: string;
direction?: 'desc' | 'asc';
ids?: number[];
statuses?: StatusDataType[];
created_by?: string;
created_for?: string;
steep_primary?: string;
steep_secondary?: string[];
signature_primary?: string;
signature_secondary?: string[];
sdgs?: string[];
query?: string;
location?: string;
bureau?: string;
score?: string;
unit?: string;
}

interface SignalsSearchResponseDataType {
current_page: number;
per_page: number;
total_pages: number;
total_count: number;
data: SignalDataType[];
}

export interface ReadMySignalsParamsDataType {
status: StatusDataType;
}

export interface CreateSignalParamsDataType {
headline: string | null;
description: string | null;
attachment?: string | null;
steep_primary: string | null;
steep_secondary?: string[] | null;
signature_primary: string | null;
signature_secondary?: string[] | null;
sdgs: string[] | null;
created_unit: string | null;
url: string | null;
relevance: string | null;
keywords: string[] | null;
location: string | null;
score?: string | null;
created_for?: string | null;
status: string | null;
connected_trends: number[] | null;
}

interface UpdateSignalParamsDataType {
id: number;
attachment?: string | null;
description: string | null;
headline: string | null;
keywords: string[] | null;
location?: string | null;
relevance?: string | null;
sdgs: string[] | null;
signature_primary?: string | null;
signature_secondary?: string[] | null;
steep?: string | null;
url?: string | null;
connected_trends?: number[] | null;
status?: string | null;
modified_by?: string | null;
steep_primary: string | null;
steep_secondary?: string[] | null;
assigned_to?: string | null;
created_for?: string | null;
created_by: string | null;
created_unit?: string | null;
score?: string | null;
}

export function searchSignals(params: BaseSignalsParamsDataType = {}) {
const {
page = 1,
per_page = 10,
Expand Down Expand Up @@ -56,7 +127,9 @@ export function searchSignals(params: BaseSignalsParams = {}) {
if (unit) queryParams.unit = unit;

return axiosInstance
.get<SignalsSearchResponse>('/signals/search', { params: queryParams })
.get<SignalsSearchResponseDataType>('/signals/search', {
params: queryParams,
})
.then(response => response.data)
.catch(error => {
if (isAxiosError(error)) {
Expand All @@ -71,7 +144,7 @@ export function searchSignals(params: BaseSignalsParams = {}) {
});
}

export function exportSignals(params: BaseSignalsParams = {}) {
export function exportSignals(params: BaseSignalsParamsDataType = {}) {
const {
page = 1,
per_page = 10,
Expand Down Expand Up @@ -175,7 +248,7 @@ export function readSignal(uid: number) {
});
}

export function readMySignals(params: ReadMySignalsParams) {
export function readMySignals(params: ReadMySignalsParamsDataType) {
const { status } = params;

const queryParams: Record<string, unknown> = {
Expand All @@ -198,7 +271,7 @@ export function readMySignals(params: ReadMySignalsParams) {
});
}

export function createSignal(params: CreateSignalParams) {
export function createSignal(params: CreateSignalParamsDataType) {
return axiosInstance
.post<SignalDataType>('/signals', params)
.then(response => response.data)
Expand All @@ -215,7 +288,7 @@ export function createSignal(params: CreateSignalParams) {
});
}

export function updateSignal(uid: number, params: UpdateSignalParams) {
export function updateSignal(uid: number, params: UpdateSignalParamsDataType) {
return axiosInstance
.put<SignalDataType>(`/signals/${uid}`, params)
.then(response => response.data)
Expand Down
46 changes: 36 additions & 10 deletions src/api/trends_calls.ts → src/API/trendsCalls.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,41 @@
/* eslint-disable @typescript-eslint/naming-convention */
import { isAxiosError } from 'axios';
import { axiosInstance } from './api_config';
import { axiosInstance } from './apiConfig';
import {
BaseTrendsParams,
CreateTrendParams,
CreateTrendParamsDataType,
TrendDataType,
TrendSearchResponse,
UpdateTrendParams,
UpdateTrendParamsDataType,
} from '../Types';

export function searchTrends(params: BaseTrendsParams = {}) {
interface BaseTrendsParamsDataType {
page?: number;
per_page?: number;
order_by?: string;
direction?: 'asc' | 'desc';
statuses?: string[];
ids?: number[];
created_by?: string;
created_for?: string;
steep_primary?: string;
steep_secondary?: string[];
signature_primary?: string;
signature_secondary?: string[];
sdgs?: string[];
query?: string;
assigned_to?: string;
time_horizon?: string;
impact_rating?: string;
}

interface TrendSearchResponseDataType {
per_page: number;
current_page: number;
total_pages: number;
total_count: number;
data: TrendDataType[];
}

export function searchTrends(params: BaseTrendsParamsDataType = {}) {
const {
page = 1,
per_page = 10,
Expand Down Expand Up @@ -53,7 +79,7 @@ export function searchTrends(params: BaseTrendsParams = {}) {
if (impact_rating) queryParams.impact_rating = impact_rating;

return axiosInstance
.get<TrendSearchResponse>('/trends/search', { params: queryParams })
.get<TrendSearchResponseDataType>('/trends/search', { params: queryParams })
.then(response => response.data)
.catch(error => {
if (isAxiosError(error)) {
Expand Down Expand Up @@ -85,7 +111,7 @@ export function readTrend(uid: number) {
});
}

export function exportTrends(params: BaseTrendsParams = {}) {
export function exportTrends(params: BaseTrendsParamsDataType = {}) {
const {
page = 1,
per_page = 10,
Expand Down Expand Up @@ -147,7 +173,7 @@ export function exportTrends(params: BaseTrendsParams = {}) {
});
}

export function createTrend(params: CreateTrendParams) {
export function createTrend(params: CreateTrendParamsDataType) {
return axiosInstance
.post<TrendDataType>('/trends', params)
.then(response => response.data)
Expand All @@ -164,7 +190,7 @@ export function createTrend(params: CreateTrendParams) {
});
}

export function updateTrend(uid: number, params: UpdateTrendParams) {
export function updateTrend(uid: number, params: UpdateTrendParamsDataType) {
return axiosInstance
.put<TrendDataType>(`/trends/${uid}`, params)
.then(response => response.data)
Expand Down
Loading

0 comments on commit 6047395

Please sign in to comment.