From b1d30c1bde05edc664d2c4d6463ebda171d3b62e Mon Sep 17 00:00:00 2001 From: Matthew Penner Date: Sun, 3 Jan 2021 16:25:32 -0700 Subject: [PATCH] Fix RolesContainer, refactor NestsContainer --- .../Api/Application/Nests/NestController.php | 2 +- app/Repositories/Eloquent/NestRepository.php | 3 +- resources/scripts/api/admin/nests/getNests.ts | 26 ++++-- resources/scripts/api/swr/getNests.ts | 25 ------ .../components/admin/nests/NestsContainer.tsx | 6 +- .../components/admin/nests/NewNestButton.tsx | 14 ++-- .../components/admin/roles/RolesContainer.tsx | 82 ++++++++++--------- resources/scripts/state/admin/nests.ts | 23 ------ 8 files changed, 74 insertions(+), 107 deletions(-) delete mode 100644 resources/scripts/api/swr/getNests.ts diff --git a/app/Http/Controllers/Api/Application/Nests/NestController.php b/app/Http/Controllers/Api/Application/Nests/NestController.php index d2ed970b0..87f3074c4 100644 --- a/app/Http/Controllers/Api/Application/Nests/NestController.php +++ b/app/Http/Controllers/Api/Application/Nests/NestController.php @@ -37,7 +37,7 @@ class NestController extends ApplicationApiController */ public function index(GetNestsRequest $request): array { - $nests = $this->repository->paginated(1); + $nests = $this->repository->paginated(10); return $this->fractal->collection($nests) ->transformWith($this->getTransformer(NestTransformer::class)) diff --git a/app/Repositories/Eloquent/NestRepository.php b/app/Repositories/Eloquent/NestRepository.php index 96b3df36f..cc3926805 100644 --- a/app/Repositories/Eloquent/NestRepository.php +++ b/app/Repositories/Eloquent/NestRepository.php @@ -28,7 +28,8 @@ class NestRepository extends EloquentRepository implements NestRepositoryInterfa /** * Return a nest or all nests with their associated eggs and variables. * - * @param int $id + * @param int|null $id + * * @return \Illuminate\Database\Eloquent\Collection|\Pterodactyl\Models\Nest * * @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException diff --git a/resources/scripts/api/admin/nests/getNests.ts b/resources/scripts/api/admin/nests/getNests.ts index 643cdd383..4ba1bb259 100644 --- a/resources/scripts/api/admin/nests/getNests.ts +++ b/resources/scripts/api/admin/nests/getNests.ts @@ -1,5 +1,7 @@ -import http from '@/api/http'; +import http, { getPaginationSet, PaginatedResult } from '@/api/http'; import { rawDataToNest } from '@/api/transformers'; +import { createContext, useContext } from 'react'; +import useSWR from 'swr'; export interface Nest { id: number; @@ -11,10 +13,22 @@ export interface Nest { updatedAt: Date; } -export default (): Promise => { - return new Promise((resolve, reject) => { - http.get('/api/application/nests') - .then(({ data }) => resolve((data.data || []).map(rawDataToNest))) - .catch(reject); +interface ctx { + page: number; + setPage: (value: number | ((s: number) => number)) => void; +} + +export const Context = createContext({ page: 1, setPage: () => 1 }); + +export default () => { + const { page } = useContext(Context); + + return useSWR>([ 'nests', page ], async () => { + const { data } = await http.get('/api/application/nests', { params: { page } }); + + return ({ + items: (data.data || []).map(rawDataToNest), + pagination: getPaginationSet(data.meta.pagination), + }); }); }; diff --git a/resources/scripts/api/swr/getNests.ts b/resources/scripts/api/swr/getNests.ts deleted file mode 100644 index ece682d62..000000000 --- a/resources/scripts/api/swr/getNests.ts +++ /dev/null @@ -1,25 +0,0 @@ -import { Nest } from '@/api/admin/nests/getNests'; -import useSWR from 'swr'; -import http, { getPaginationSet, PaginatedResult } from '@/api/http'; -import { rawDataToNest } from '@/api/transformers'; -import { createContext, useContext } from 'react'; - -interface ctx { - page: number; - setPage: (value: number | ((s: number) => number)) => void; -} - -export const Context = createContext({ page: 1, setPage: () => 1 }); - -export default () => { - const { page } = useContext(Context); - - return useSWR>([ 'nests', page ], async () => { - const { data } = await http.get(`/api/application/nests`, { params: { page } }); - - return ({ - items: (data.data || []).map(rawDataToNest), - pagination: getPaginationSet(data.meta.pagination), - }); - }); -}; diff --git a/resources/scripts/components/admin/nests/NestsContainer.tsx b/resources/scripts/components/admin/nests/NestsContainer.tsx index 29b920dbf..0450d6b87 100644 --- a/resources/scripts/components/admin/nests/NestsContainer.tsx +++ b/resources/scripts/components/admin/nests/NestsContainer.tsx @@ -1,5 +1,5 @@ import React, { useContext, useEffect, useState } from 'react'; -import getNests, { Context as NestsContext } from '@/api/swr/getNests'; +import getNests, { Context as NestsContext } from '@/api/admin/nests/getNests'; import NewNestButton from '@/components/admin/nests/NewNestButton'; import FlashMessageRender from '@/components/FlashMessageRender'; import useFlash from '@/plugins/useFlash'; @@ -39,11 +39,11 @@ const NestsContainer = () => { useEffect(() => { if (!error) { - clearFlashes('backups'); + clearFlashes('nests'); return; } - clearAndAddHttpError({ error, key: 'backups' }); + clearAndAddHttpError({ error, key: 'nests' }); }, [ error ]); const length = nests?.items?.length || 0; diff --git a/resources/scripts/components/admin/nests/NewNestButton.tsx b/resources/scripts/components/admin/nests/NewNestButton.tsx index c585001ea..9a4f9ab27 100644 --- a/resources/scripts/components/admin/nests/NewNestButton.tsx +++ b/resources/scripts/components/admin/nests/NewNestButton.tsx @@ -1,15 +1,14 @@ import React, { useState } from 'react'; import createNest from '@/api/admin/nests/createNest'; -import { httpErrorToHuman } from '@/api/http'; -import { AdminContext } from '@/state/admin'; +import getNests from '@/api/admin/nests/getNests'; import Button from '@/components/elements/Button'; import Field from '@/components/elements/Field'; import Modal from '@/components/elements/Modal'; import FlashMessageRender from '@/components/FlashMessageRender'; import useFlash from '@/plugins/useFlash'; import { Form, Formik, FormikHelpers } from 'formik'; -import tw from 'twin.macro'; import { object, string } from 'yup'; +import tw from 'twin.macro'; interface Values { name: string, @@ -26,9 +25,8 @@ const schema = object().shape({ export default () => { const [ visible, setVisible ] = useState(false); - const { addError, clearFlashes } = useFlash(); - - const appendNest = AdminContext.useStoreActions(actions => actions.nests.appendNest); + const { clearFlashes, clearAndAddHttpError } = useFlash(); + const { mutate } = getNests(); const submit = ({ name, description }: Values, { setSubmitting }: FormikHelpers) => { clearFlashes('nest:create'); @@ -36,11 +34,11 @@ export default () => { createNest(name, description) .then(nest => { - appendNest(nest); + mutate(data => ({ ...data, items: data.items.concat(nest) }), false); setVisible(false); }) .catch(error => { - addError({ key: 'nest:create', message: httpErrorToHuman(error) }); + clearAndAddHttpError({ key: 'nest:create', error }); setSubmitting(false); }); }; diff --git a/resources/scripts/components/admin/roles/RolesContainer.tsx b/resources/scripts/components/admin/roles/RolesContainer.tsx index c0c992602..5c054bcb2 100644 --- a/resources/scripts/components/admin/roles/RolesContainer.tsx +++ b/resources/scripts/components/admin/roles/RolesContainer.tsx @@ -1,4 +1,4 @@ -/* import React, { useEffect, useState } from 'react'; +import React, { useEffect, useState } from 'react'; import { useDeepMemoize } from '@/plugins/useDeepMemoize'; import { AdminContext } from '@/state/admin'; import { httpErrorToHuman } from '@/api/http'; @@ -10,7 +10,7 @@ import tw from 'twin.macro'; import AdminContentBlock from '@/components/admin/AdminContentBlock'; import getRoles from '@/api/admin/roles/getRoles'; import AdminCheckbox from '@/components/admin/AdminCheckbox'; -import AdminTable, { TableBody, TableHead, TableHeader, TableRow } from '@/components/admin/AdminTable'; +import AdminTable, { ContentWrapper, Loading, NoItems, TableBody, TableHead, TableHeader, TableRow } from '@/components/admin/AdminTable'; const RowCheckbox = ({ id }: { id: number}) => { const isChecked = AdminContext.useStoreState(state => state.roles.selectedRoles.indexOf(id) >= 0); @@ -74,47 +74,49 @@ export default () => { - 0} - checked={selectedRolesLength === (roles.length === 0 ? -1 : roles.length)} - onSelectAllClick={onSelectAllClick} - > - - - - - + + { loading ? + + : + roles.length < 1 ? + + : + +
+ + + + + + - - { - roles.map(role => ( - - + + { + roles.map(role => ( + + - - - - - )) - } - + + + + + )) + } + +
- - + + {role.id} - - {role.name} - - {role.description}{role.id} + + {role.name} + + {role.description}
+
+
+ }
); -}; */ - -import React from 'react'; - -export default () => { - return ( - <> - - ); }; diff --git a/resources/scripts/state/admin/nests.ts b/resources/scripts/state/admin/nests.ts index a0d817a17..f8e205614 100644 --- a/resources/scripts/state/admin/nests.ts +++ b/resources/scripts/state/admin/nests.ts @@ -1,39 +1,16 @@ import { action, Action } from 'easy-peasy'; -import { Nest } from '@/api/admin/nests/getNests'; export interface AdminNestStore { - data: Nest[]; selectedNests: number[]; - setNests: Action; - appendNest: Action; - removeNest: Action; - setSelectedNests: Action; appendSelectedNest: Action; removeSelectedNest: Action; } const nests: AdminNestStore = { - data: [], selectedNests: [], - setNests: action((state, payload) => { - state.data = payload; - }), - - appendNest: action((state, payload) => { - if (state.data.find(nest => nest.id === payload.id)) { - state.data = state.data.map(nest => nest.id === payload.id ? payload : nest); - } else { - state.data = [ ...state.data, payload ]; - } - }), - - removeNest: action((state, payload) => { - state.data = [ ...state.data.filter(nest => nest.id !== payload) ]; - }), - setSelectedNests: action((state, payload) => { state.selectedNests = payload; }),