generated from ConductionNL/skeleton-app
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #373 from ConductionNL/development
Development to main, week 18
- Loading branch information
Showing
27 changed files
with
759 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { AxiosInstance } from "axios"; | ||
import { TSendFunction } from "../apiService"; | ||
|
||
export default class Database { | ||
private _instance: AxiosInstance; | ||
private _send: TSendFunction; | ||
|
||
constructor(instance: AxiosInstance, send: TSendFunction) { | ||
this._instance = instance; | ||
this._send = send; | ||
} | ||
|
||
public getAll = async (): Promise<any> => { | ||
const { data } = await this._send(this._instance, "GET", "/admin/databases"); | ||
|
||
return data; | ||
}; | ||
|
||
public getAllSelectOptions = async (): Promise<any> => { | ||
const { data } = await this._send(this._instance, "GET", "/admin/databases?limit=200"); | ||
|
||
return data?.map((database: any) => ({ label: database.name, value: database.id })); | ||
}; | ||
|
||
public getOne = async (id: string): Promise<any> => { | ||
const { data } = await this._send(this._instance, "GET", `/admin/databases/${id}`); | ||
|
||
return data; | ||
}; | ||
|
||
public delete = async (variables: { id: string }): Promise<any> => { | ||
const { id } = variables; | ||
|
||
const { data } = await this._send(this._instance, "DELETE", `/admin/databases/${id}`, undefined, { | ||
loading: "Removing database...", | ||
success: "Database successfully removed.", | ||
}); | ||
return data; | ||
}; | ||
|
||
public createOrUpdate = async (variables: { payload: any; id?: string }): Promise<any> => { | ||
const { payload, id } = variables; | ||
|
||
if (id) { | ||
const { data } = await this._send(this._instance, "PUT", `/admin/databases/${id}`, payload, { | ||
loading: "Updating database...", | ||
success: "Database successfully updated.", | ||
}); | ||
return data; | ||
} | ||
|
||
const { data } = await this._send(this._instance, "POST", "/admin/databases", payload, { | ||
loading: "Creating database...", | ||
success: "Database successfully created.", | ||
}); | ||
return data; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import * as React from "react"; | ||
import { QueryClient, useMutation, useQuery } from "react-query"; | ||
import APIService from "../apiService/apiService"; | ||
import APIContext from "../apiService/apiContext"; | ||
import { addItem, deleteItem, updateItem } from "../services/mutateQueries"; | ||
import { navigate } from "gatsby"; | ||
import { useDeletedItemsContext } from "../context/deletedItems"; | ||
|
||
export const useDatabase = (queryClient: QueryClient) => { | ||
const API: APIService | null = React.useContext(APIContext); | ||
const { isDeleted, addDeletedItem, removeDeletedItem } = useDeletedItemsContext(); | ||
|
||
const getAll = () => | ||
useQuery<any[], Error>("databases", API.Database.getAll, { | ||
onError: (error) => { | ||
console.warn(error.message); | ||
}, | ||
}); | ||
|
||
const getAllSelectOptions = () => | ||
useQuery<any[], Error>("database_select_options", API.Database.getAllSelectOptions, { | ||
onError: (error) => { | ||
console.warn(error.message); | ||
}, | ||
}); | ||
|
||
const getOne = (databaseId: string) => | ||
useQuery<any, Error>(["databases", databaseId], () => API?.Database.getOne(databaseId), { | ||
initialData: () => queryClient.getQueryData<any[]>("databases")?.find((_database) => _database.id === databaseId), | ||
onError: (error) => { | ||
console.warn(error.message); | ||
}, | ||
enabled: !!databaseId && !isDeleted(databaseId), | ||
}); | ||
|
||
const remove = () => | ||
useMutation<any, Error, any>(API.Database.delete, { | ||
onMutate: ({ id }) => addDeletedItem(id), | ||
onSuccess: async (_, variables) => { | ||
deleteItem(queryClient, "database", variables.id); | ||
navigate("/settings/databases"); | ||
}, | ||
onError: (error, { id }) => { | ||
removeDeletedItem(id); | ||
console.warn(error.message); | ||
}, | ||
}); | ||
|
||
const createOrEdit = (databaseId?: string) => | ||
useMutation<any, Error, any>(API.Database.createOrUpdate, { | ||
onSuccess: async (newDatabase) => { | ||
if (databaseId) { | ||
updateItem(queryClient, "databases", newDatabase); | ||
navigate("/settings"); | ||
} | ||
|
||
if (!databaseId) { | ||
addItem(queryClient, "databases", newDatabase); | ||
navigate(`/settings/databases/${newDatabase.id}`); | ||
} | ||
}, | ||
onError: (error) => { | ||
console.warn(error.message); | ||
}, | ||
}); | ||
|
||
return { getAll, getAllSelectOptions, getOne, createOrEdit, remove }; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import * as React from "react"; | ||
import { navigate } from "gatsby"; | ||
|
||
const DatabasesPage: React.FC = () => { | ||
React.useEffect(() => { | ||
navigate("/settings"); | ||
}); | ||
|
||
return <></>; | ||
}; | ||
|
||
export default DatabasesPage; |
18 changes: 18 additions & 0 deletions
18
pwa/src/pages/settings/databases/[databaseId]/DatabaseDetailPage.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import * as React from "react"; | ||
import { PageProps } from "gatsby"; | ||
import { DashboardTemplate } from "../../../../templates/dashboard/DashboardTemplate"; | ||
import { CreateDatabaseTemplate } from "../../../../templates/templateParts/databaseForm/CreateDatabaseTemplate"; | ||
import { EditDatabaseTemplate } from "../../../../templates/templateParts/databaseForm/EditDatabaseTemplate"; | ||
|
||
const DatabaseDetailPage: React.FC<PageProps> = (props: PageProps) => { | ||
const databaseId = props.params.databaseId === "new" ? null : props.params.databaseId; | ||
|
||
return ( | ||
<DashboardTemplate> | ||
{!databaseId && <CreateDatabaseTemplate />} | ||
{databaseId && <EditDatabaseTemplate {...{ databaseId }} />} | ||
</DashboardTemplate> | ||
); | ||
}; | ||
|
||
export default DatabaseDetailPage; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import DatabaseDetailPage from "./DatabaseDetailPage"; | ||
|
||
export default DatabaseDetailPage; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import DatabasesPage from "./DatabasesPage"; | ||
|
||
export default DatabasesPage; |
3 changes: 3 additions & 0 deletions
3
pwa/src/templates/databasesTemplate/DatabasesTemplate.module.css
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
.container > *:not(:last-child) { | ||
margin-block-end: var(--gateway-ui-size-2xl); | ||
} |
Oops, something went wrong.