Skip to content

Commit

Permalink
Null handling for Api
Browse files Browse the repository at this point in the history
fix api null response handling

fix type

more null handling

more null handling, for api

remove unused css selector
  • Loading branch information
Ell1ott committed Feb 25, 2024
1 parent 419e8eb commit 162f053
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 21 deletions.
13 changes: 9 additions & 4 deletions src/lib/api/apiStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,11 @@ export interface Assignment {
export async function updateFiles() {
const api = getContext('api') as ApiHandler;

const userDocuments = await api.getUserDocuments();
const userDocumentsJson = await userDocuments.json();
const response = await api.getUserDocuments();
if (!response) {
throw new Error('Could not update files due to no response');
}
const userDocumentsJson = await response.json();
console.log(userDocumentsJson);

fileStore.set(userDocumentsJson);
Expand All @@ -57,18 +60,20 @@ export function updateUserInfo(state: AuthorizerState) {

// Explicitly specify the type of the store
export const fileStore = writable<FileInfo[]>([]);
export const assignmentStore = writable<Assignment[]>([]);

interface userInfo {
name: string;
}
export const userInfo = writable<userInfo>();

export const assignmentStore = writable<Assignment[]>([]);

export async function updateAssignments() {
const api = getContext('api') as ApiHandler;

const response = await api.getAssignments();
if (!response) {
throw new Error('Could not update assignments due to no response');
}
const json = await response.json();

assignmentStore.set(json);
Expand Down
2 changes: 1 addition & 1 deletion src/lib/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export default class ApiHandler {

// eslint-disable-next-line @typescript-eslint/no-explicit-any
debounce(func: any, timeout = 300) {
let timer: number;
let timer: NodeJS.Timeout;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return (...args: any) => {
clearTimeout(timer);
Expand Down
12 changes: 0 additions & 12 deletions src/routes/+layout.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,4 @@
flex-direction: column;
min-height: 100vh;
}
main {
flex: 1;
flex-direction: column;
/* padding: 1rem; */
width: 100%;
max-height: 100%;
/* max-width: 64rem; */
margin: 0 auto;
min-height: 100vh;
box-sizing: border-box;
}
</style>
5 changes: 4 additions & 1 deletion src/routes/workspace/FileViewer.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@
<SideBarElem active={false}>
<button
on:click={async () => {
const newFile = await (await api.createDocument(randomName())).json();
const newFile = await api.createDocument(randomName()).then((response) => {
if (!response) return;
return response.json();
});
activeFile.set(newFile);
fileStore.update((before) => [...before, newFile]);
}}
Expand Down
1 change: 1 addition & 0 deletions src/routes/workspace/editor/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
}
api.getDocument(id || '').then((file) => {
if (!file) return;
file.json().then((fileContent) => {
activeFile.set(fileContent);
});
Expand Down
6 changes: 3 additions & 3 deletions src/routes/workspace/editor/Toolbar/MoreActions.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
function deleteActiveFile() {
const id = $activeFile?.id;
if (!id) return;
api.deleteDocument(id).then((it) => {
if (it.status !== 200) return;
console.log(it);
api.deleteDocument(id).then((response) => {
if (!response || response.status !== 200) return;
console.log(response);
fileStore.update((prev) => prev.filter((it) => it !== $activeFile));
activeFile.set(null);
});
Expand Down

0 comments on commit 162f053

Please sign in to comment.