From 260cda62759cd487efcd6a7c27c6d6e688500bbe Mon Sep 17 00:00:00 2001 From: Christopher Date: Sat, 12 Dec 2020 18:08:57 +0100 Subject: [PATCH] feat(example-app): example app for nui --- .../src/apps/example/components/ExampleApp.tsx | 17 +++++++++++------ phone/src/apps/example/hooks/state.ts | 8 ++++++++ phone/src/apps/example/hooks/useExample.ts | 7 +++++++ .../src/apps/example/hooks/useExampleService.ts | 12 ++++++++++++ 4 files changed, 38 insertions(+), 6 deletions(-) create mode 100644 phone/src/apps/example/hooks/state.ts create mode 100644 phone/src/apps/example/hooks/useExample.ts create mode 100644 phone/src/apps/example/hooks/useExampleService.ts diff --git a/phone/src/apps/example/components/ExampleApp.tsx b/phone/src/apps/example/components/ExampleApp.tsx index 52ac15f..2889c9e 100644 --- a/phone/src/apps/example/components/ExampleApp.tsx +++ b/phone/src/apps/example/components/ExampleApp.tsx @@ -1,17 +1,22 @@ -import React from "react"; +import React from 'react'; -import { AppWrapper } from "../../../ui/components"; -import { AppTitle } from "../../../ui/components/AppTitle"; -import { AppContent } from "../../../ui/components/AppContent"; -import { useApp } from "../../../os/apps/hooks/useApps"; +import { AppWrapper } from '../../../ui/components'; +import { AppTitle } from '../../../ui/components/AppTitle'; +import { AppContent } from '../../../ui/components/AppContent'; +import { useApp } from '../../../os/apps/hooks/useApps'; +import { useExample } from '../hooks/useExample'; export const ExampleApp = () => { - const example = useApp("EXAMPLE"); + // calling the example hook, and we assign the value to a variable + const exampleString = useExample(); + const example = useApp('EXAMPLE'); return (

This is an example

+ {/* Here we are using the value in a h3 tag */} +

{exampleString}

); diff --git a/phone/src/apps/example/hooks/state.ts b/phone/src/apps/example/hooks/state.ts new file mode 100644 index 0000000..421ee31 --- /dev/null +++ b/phone/src/apps/example/hooks/state.ts @@ -0,0 +1,8 @@ +import { atom } from 'recoil'; + +export const exampleState = { + example: atom({ + key: 'exampleState', + default: null, + }), +}; diff --git a/phone/src/apps/example/hooks/useExample.ts b/phone/src/apps/example/hooks/useExample.ts new file mode 100644 index 0000000..dad84c2 --- /dev/null +++ b/phone/src/apps/example/hooks/useExample.ts @@ -0,0 +1,7 @@ +import { useRecoilValue } from 'recoil'; +import { exampleState } from './state'; + +export const useExample = () => { + const example = useRecoilValue(exampleState.example); + return example; +}; diff --git a/phone/src/apps/example/hooks/useExampleService.ts b/phone/src/apps/example/hooks/useExampleService.ts new file mode 100644 index 0000000..6720a0f --- /dev/null +++ b/phone/src/apps/example/hooks/useExampleService.ts @@ -0,0 +1,12 @@ +import { useNuiEvent } from '../../../os/nui-events/hooks/useNuiEvent'; +import { useSetRecoilState } from 'recoil'; +import { exampleState } from './state'; +import { useExample } from './useExample'; + +export const useExampleService = () => { + const setExample = useSetRecoilState(exampleState.example); + useNuiEvent('EXAMPLE', 'setExample', setExample); + return useExample(); +}; + +// call this function in the Phone.tsx file.