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

Introduce Notification Center #3

Merged
merged 1 commit into from
Aug 6, 2024
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
10 changes: 10 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"react-native-paper": "^5.12.3",
"react-native-safe-area-context": "^4.10.8",
"react-native-screens": "^3.27.0",
"react-native-uuid": "^2.0.2",
"react-native-vector-icons": "^10.1.0",
"xstate": "^5.14.0"
},
Expand Down
63 changes: 54 additions & 9 deletions src/api.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,68 @@
function timeout(data: any, milliseconds: number) {
import uuid from "react-native-uuid";

export interface Item {
title: string;
id: string | number[];
}

function timeout<T>(response: T, milliseconds: number): Promise<T> {
return new Promise((resolve) =>
setTimeout(() => resolve(data), milliseconds),
setTimeout(() => resolve(response), milliseconds),
);
}

export async function signIn() {
return await timeout({ status: "success", user: { name: "John" } }, 500);
}

export async function getItems<T>() {
let items = [
{ title: "Title", id: uuid.v4() },
{ title: "Title", id: uuid.v4() },
{ title: "Title", id: uuid.v4() },
];

export async function getItems(): Promise<{
status: string;
items: Item[];
}> {
return await timeout<{ status: string; items: Item[] }>(
{
status: "success",
items,
},
800,
);
}

export async function addItem(title: Item["title"]): Promise<{
status: string;
item: Item;
}> {
const newItem: Item = { title, id: uuid.v4() };
items = [...items, newItem];

return await timeout(
{
status: "success",
item: newItem,
},
500,
);
}

export async function deleteItem(id: Item["id"]): Promise<{
status: string;
id: Item["id"];
}> {
items = items.filter((item) => {
return item.id !== id;
});

return await timeout(
{
status: "success",
items: [
{ title: "Title 1", id: 1 },
{ title: "Title 2", id: 2 },
{ title: "Title 3", id: 3 },
],
} as T,
id,
},
500,
);
}
40 changes: 40 additions & 0 deletions src/components/NotificationModal.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import React from "react";
import { Portal, Text, Dialog, Button } from "react-native-paper";
import { NotificationCenterMachineActor } from "../machines/notificationCenter";
import { useSelector } from "@xstate/react";

interface Props {
actor: NotificationCenterMachineActor;
}

export function NotificationModal({ actor }: Props) {
const state = useSelector(actor, (snapshot) => {
return snapshot;
});

const {
context: {
modal: { title, message },
},
} = state;

const isOpen = state.matches({ modal: "opened" });

function onDismissHandler() {
actor.send({ type: "CLOSE" });
}

return (
<Portal>
<Dialog visible={isOpen} onDismiss={onDismissHandler}>
{title && <Dialog.Title>{title}</Dialog.Title>}
<Dialog.Content>
<Text>{message}</Text>
</Dialog.Content>
<Dialog.Actions>
<Button onPress={onDismissHandler}>Close</Button>
</Dialog.Actions>
</Dialog>
</Portal>
);
}
64 changes: 64 additions & 0 deletions src/components/NotificationSnackbar.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import React from "react";
import { Snackbar, Text, MD3LightTheme } from "react-native-paper";
import { NotificationCenterMachineActor } from "../machines/notificationCenter";
import { useSelector } from "@xstate/react";

interface Props {
actor: NotificationCenterMachineActor;
}

export function NotificationSnackbar({ actor }: Props) {
const state = useSelector(actor, (snapshot) => {
return snapshot;
});

const {
context: {
snackbar: { severity, message },
},
} = state;

const isOpen = state.matches({ snackbar: "opened" });

const isError = severity === "error";

function onDismissHandler() {
actor.send({ type: "CLOSE" });
}

return (
<>
<Snackbar
visible={isOpen}
action={{
label: "Close",
icon: "close",
onPress: onDismissHandler,
}}
onDismiss={onDismissHandler}
style={
isError
? { backgroundColor: MD3LightTheme.colors.error }
: { backgroundColor: MD3LightTheme.colors.primaryContainer }
}
theme={{
colors: {
inversePrimary: isError
? MD3LightTheme.colors.onError
: MD3LightTheme.colors.onPrimaryContainer,
},
}}
>
<Text
style={
isError
? { color: MD3LightTheme.colors.onError }
: { color: MD3LightTheme.colors.onPrimaryContainer }
}
>
{message}
</Text>
</Snackbar>
</>
);
}
24 changes: 22 additions & 2 deletions src/contexts/useApp.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ import {
AuthenticatedMachineActor,
authenticatedMachine,
} from "../machines/authenticated.navigator";
import {
NotificationCenterMachineActor,
notificationCenterMachine,
} from "../machines/notificationCenter";

export const appMachine = setup({
types: {
Expand All @@ -17,6 +21,7 @@ export const appMachine = setup({
username: string;
refAuthenticating: AuthenticatingMachineActor | null;
refAuthenticated: AuthenticatedMachineActor | null;
refNotificationCenter: NotificationCenterMachineActor | undefined;
},
},
actions: {
Expand All @@ -36,23 +41,38 @@ export const appMachine = setup({
stopRefAuthenticated() {
stopChild("refAuthenticated");
},
setRefNotificationCenter: assign({
refNotificationCenter: ({ spawn }) => {
return spawn("notificationCenterMachine", {
systemId: "notificationCenter",
});
},
}),
setUsername: assign({
username: (_, { username }: { username: string }) => {
return username;
},
}),
},
actors: { authenticatingMachine, authenticatedMachine },
actors: {
authenticatingMachine,
authenticatedMachine,
notificationCenterMachine,
},
}).createMachine({
id: "application",
initial: "initializing",
context: {
username: "",
refAuthenticating: null,
refAuthenticated: null,
refNotificationCenter: undefined,
},
states: {
initializing: { on: { START_APP: { target: "authenticating" } } },
initializing: {
entry: "setRefNotificationCenter",
on: { START_APP: { target: "authenticating" } },
},
authenticating: {
entry: ["setRefAuthenticating"],
on: {
Expand Down
1 change: 0 additions & 1 deletion src/machines/home.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ export type HomeMachineActor = ActorRefFrom<typeof homeMachine>;
export const homeMachine = setup({
types: {},
actors: {},
actions: {},
}).createMachine({
id: "home",
initial: "idle",
Expand Down
Loading