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

review changes #12

Merged
merged 1 commit into from
Feb 20, 2023
Merged
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
11 changes: 5 additions & 6 deletions frontend/src/Navigation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -108,17 +108,16 @@ export default function Navigation({
>
<img src={Hamburger} alt="menu toggle" className="w-7" />
</button>

<APIKeyModal
modalState={apiKeyModalState}
setModalState={setApiKeyModalState}
isCancellable={isApiKeySet}
/>
<SelectDocsModal
modalState={selectedDocsModalState}
setModalState={setSelectedDocsModalState}
isCancellable={isSelectedDocsSet}
/>
<APIKeyModal
modalState={apiKeyModalState}
setModalState={setApiKeyModalState}
isCancellable={isApiKeySet}
/>
</>
);
}
22 changes: 0 additions & 22 deletions frontend/src/api/docs.ts

This file was deleted.

11 changes: 0 additions & 11 deletions frontend/src/models/misc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,3 @@ export type ActiveState = 'ACTIVE' | 'INACTIVE';
export type User = {
avatar: string;
};

export type Doc = {
name: string;
language: string;
version: string;
description: string;
fullName: string;
dat: string;
docLink: string;
model: string;
};
17 changes: 11 additions & 6 deletions frontend/src/preferences/SelectDocsModal.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import { useEffect, useState } from 'react';
import { useDispatch } from 'react-redux';
import { ActiveState, Doc } from '../models/misc';
import { setSelectedDocs } from './preferenceSlice';
import { getDocs } from '../api/docs';
import { useDispatch, useSelector } from 'react-redux';
import { ActiveState } from '../models/misc';
import { Doc } from './selectDocsApi';
import {
setSelectedDocs,
setSourceDocs,
selectSourceDocs,
} from './preferenceSlice';
import { getDocs } from './selectDocsApi';

export default function APIKeyModal({
modalState,
Expand All @@ -14,7 +19,7 @@ export default function APIKeyModal({
isCancellable?: boolean;
}) {
const dispatch = useDispatch();
const [docs, setDocs] = useState<Doc[] | null>(null);
const docs = useSelector(selectSourceDocs);
const [localSelectedDocs, setLocalSelectedDocs] = useState<Doc | null>(null);
const [isDocsListOpen, setIsDocsListOpen] = useState(false);
const [isError, setIsError] = useState(false);
Expand All @@ -39,7 +44,7 @@ export default function APIKeyModal({
useEffect(() => {
async function requestDocs() {
const data = await getDocs();
setDocs(data);
dispatch(setSourceDocs(data));
}

requestDocs();
Expand Down
13 changes: 11 additions & 2 deletions frontend/src/preferences/preferenceSlice.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import { createSlice } from '@reduxjs/toolkit';
import { Doc } from '../models/misc';
import { Doc } from './selectDocsApi';
import store from '../store';

interface Preference {
apiKey: string;
selectedDocs: Doc | null;
sourceDocs: Doc[] | null;
}

const initialState: Preference = {
apiKey: '',
selectedDocs: null,
sourceDocs: null,
};

export const prefSlice = createSlice({
Expand All @@ -21,11 +23,16 @@ export const prefSlice = createSlice({
},
setSelectedDocs: (state, action) => {
state.selectedDocs = action.payload;
console.log('setSelectedDocs', state.selectedDocs);
},
setSourceDocs: (state, action) => {
state.sourceDocs = action.payload;
console.log('setSourceDocs', state.sourceDocs);
},
},
});

export const { setApiKey, setSelectedDocs } = prefSlice.actions;
export const { setApiKey, setSelectedDocs, setSourceDocs } = prefSlice.actions;
export default prefSlice.reducer;

type RootState = ReturnType<typeof store.getState>;
Expand All @@ -35,3 +42,5 @@ export const selectApiKeyStatus = (state: RootState) =>
!!state.preference.apiKey;
export const selectSelectedDocsStatus = (state: RootState) =>
!!state.preference.selectedDocs;
export const selectSourceDocs = (state: RootState) =>
state.preference.sourceDocs;
33 changes: 33 additions & 0 deletions frontend/src/preferences/selectDocsApi.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
//Exporting Doc type from here since its the first place its used and seems needless to make an entire file for it.
export type Doc = {
name: string;
language: string;
version: string;
description: string;
fullName: string;
dat: string;
docLink: string;
model: string;
};

//Fetches all JSON objects from the source. We only use the objects with the "model" property in SelectDocsModal.tsx. Hopefully can clean up the source file later.
export async function getDocs(): Promise<Doc[] | null> {
try {
//Fetch default source docs
const response = await fetch(
'https://d3dg1063dc54p9.cloudfront.net/combined.json',
);
const data = await response.json();

//Create array of Doc objects
const docs: Doc[] = [];

data.forEach((doc: Doc) => {
docs.push(doc);
});

return docs;
} catch (error) {
return null;
}
}