parent
da02d7f543
commit
1063026e2d
@ -0,0 +1,61 @@
|
|||||||
|
// components/AlertModal.tsx
|
||||||
|
import { Button, Modal, StyleSheet, Text, View } from 'react-native';
|
||||||
|
import React from 'react';
|
||||||
|
|
||||||
|
type AlertModalProps = {
|
||||||
|
visible: boolean;
|
||||||
|
message: string;
|
||||||
|
onClose: () => void;
|
||||||
|
};
|
||||||
|
|
||||||
|
const AlertModal = ({ visible, message, onClose }: AlertModalProps) => {
|
||||||
|
return (
|
||||||
|
<Modal
|
||||||
|
animationType="slide"
|
||||||
|
transparent={true}
|
||||||
|
visible={visible}
|
||||||
|
onRequestClose={onClose}
|
||||||
|
>
|
||||||
|
<View style={styles.centeredView}>
|
||||||
|
<View style={styles.modalView}>
|
||||||
|
<Text style={styles.modalText}>{message}</Text>
|
||||||
|
|
||||||
|
<Button
|
||||||
|
title="Close"
|
||||||
|
onPress={onClose}
|
||||||
|
/>
|
||||||
|
</View>
|
||||||
|
</View>
|
||||||
|
</Modal>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
const styles = StyleSheet.create({
|
||||||
|
centeredView: {
|
||||||
|
flex: 1,
|
||||||
|
justifyContent: "center",
|
||||||
|
alignItems: "center",
|
||||||
|
marginTop: 16
|
||||||
|
},
|
||||||
|
modalView: {
|
||||||
|
margin: 16,
|
||||||
|
backgroundColor: "white",
|
||||||
|
borderRadius: 16,
|
||||||
|
padding: 32,
|
||||||
|
alignItems: "center",
|
||||||
|
shadowColor: "#000",
|
||||||
|
shadowOffset: {
|
||||||
|
width: 0,
|
||||||
|
height: 2
|
||||||
|
},
|
||||||
|
shadowOpacity: 0.25,
|
||||||
|
shadowRadius: 4,
|
||||||
|
elevation: 5
|
||||||
|
},
|
||||||
|
modalText: {
|
||||||
|
marginBottom: 16,
|
||||||
|
textAlign: "center"
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
export default AlertModal;
|
@ -1,71 +1,105 @@
|
|||||||
// redux/actions/moveAction.ts
|
// redux/actions/moveAction.ts
|
||||||
import { CREATE_MOVE, DELETE_MOVE, GET_MOVES, UPDATE_MOVE } from '../constants';
|
import { CREATE_MOVE, DELETE, DELETE_MOVE, GET, GET_MOVES, MOVE_ERROR, POST, PUT, UPDATE_MOVE } from '../constants';
|
||||||
import { Move } from "../../entities/Move";
|
import {
|
||||||
import { Dispatch } from "redux";
|
Move
|
||||||
import { API_BASE_URL } from "../../config";
|
} from "../../entities/Move";
|
||||||
|
import { Dispatch } from "redux";
|
||||||
|
import { API_BASE_URL } from "../../config";
|
||||||
|
|
||||||
|
|
||||||
export const createMove = (move: Move) => {
|
export const createMove = (move: Move) => {
|
||||||
|
const verb = POST
|
||||||
return async (dispatch: Dispatch) => {
|
return async (dispatch: Dispatch) => {
|
||||||
try {
|
try {
|
||||||
const response = await fetch(`${API_BASE_URL}/move`, {
|
const response = await fetch(`${API_BASE_URL}/move`, {
|
||||||
method: 'POST',
|
method: verb,
|
||||||
headers: {
|
headers: {
|
||||||
'Content-Type': 'application/json',
|
'Content-Type': 'application/json',
|
||||||
},
|
},
|
||||||
body: JSON.stringify(move),
|
body: JSON.stringify(move),
|
||||||
});
|
});
|
||||||
|
|
||||||
|
if (!response.ok) {
|
||||||
|
throw new Error(`Failed to ${verb}: ${response.statusText}`);
|
||||||
|
}
|
||||||
|
|
||||||
const data = await response.json();
|
const data = await response.json();
|
||||||
dispatch({ type: CREATE_MOVE, payload: data });
|
dispatch({ type: CREATE_MOVE, payload: data });
|
||||||
}
|
}
|
||||||
catch (error) {
|
catch (error) {
|
||||||
console.error(error);
|
console.error(error);
|
||||||
|
// @ts-ignore
|
||||||
|
dispatch({ type: MOVE_ERROR, payload: error.message });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export const getMoves = () => {
|
export const getMoves = () => {
|
||||||
|
const verb = GET
|
||||||
return async (dispatch: Dispatch) => {
|
return async (dispatch: Dispatch) => {
|
||||||
try {
|
try {
|
||||||
const response = await fetch(`${API_BASE_URL}/move`);
|
const response = await fetch(`${API_BASE_URL}/move`);
|
||||||
|
|
||||||
|
if (!response.ok) {
|
||||||
|
throw new Error(`Failed to ${verb}: ${response.statusText}`);
|
||||||
|
}
|
||||||
|
|
||||||
const data = await response.json();
|
const data = await response.json();
|
||||||
dispatch({ type: GET_MOVES, payload: data });
|
dispatch({ type: GET_MOVES, payload: data });
|
||||||
}
|
}
|
||||||
catch (error) {
|
catch (error) {
|
||||||
console.error(error);
|
console.error(error);
|
||||||
|
// @ts-ignore
|
||||||
|
dispatch({ type: MOVE_ERROR, payload: error.message });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export const updateMove = (id: string, move: Move) => {
|
export const updateMove = (id: string, move: Move) => {
|
||||||
|
const verb = PUT
|
||||||
return async (dispatch: Dispatch) => {
|
return async (dispatch: Dispatch) => {
|
||||||
try {
|
try {
|
||||||
const response = await fetch(`${API_BASE_URL}/move/${id}`, {
|
const response = await fetch(`${API_BASE_URL}/move/${id}`, {
|
||||||
method: 'PUT',
|
method: verb,
|
||||||
headers: {
|
headers: {
|
||||||
'Content-Type': 'application/json',
|
'Content-Type': 'application/json',
|
||||||
},
|
},
|
||||||
body: JSON.stringify(move),
|
body: JSON.stringify(move),
|
||||||
});
|
});
|
||||||
|
|
||||||
|
if (!response.ok) {
|
||||||
|
throw new Error(`Failed to ${verb}: ${response.statusText}`);
|
||||||
|
}
|
||||||
|
|
||||||
const updatedMove = await response.json();
|
const updatedMove = await response.json();
|
||||||
dispatch({ type: UPDATE_MOVE, payload: updatedMove });
|
dispatch({ type: UPDATE_MOVE, payload: updatedMove });
|
||||||
}
|
}
|
||||||
catch (error) {
|
catch (error) {
|
||||||
console.error(error);
|
console.error(error);
|
||||||
|
// @ts-ignore
|
||||||
|
dispatch({ type: MOVE_ERROR, payload: error.message });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export const deleteMove = (id: string) => {
|
export const deleteMove = (id: string) => {
|
||||||
|
const verb = DELETE
|
||||||
return async (dispatch: Dispatch) => {
|
return async (dispatch: Dispatch) => {
|
||||||
try {
|
try {
|
||||||
await fetch(`${API_BASE_URL}/move/${id}`, {
|
const response = await fetch(`${API_BASE_URL}/move/${id}`, {
|
||||||
method: 'DELETE',
|
method: verb,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
if (!response.ok) {
|
||||||
|
throw new Error(`Failed to ${verb}: ${response.statusText}`);
|
||||||
|
}
|
||||||
|
|
||||||
dispatch({ type: DELETE_MOVE, payload: id });
|
dispatch({ type: DELETE_MOVE, payload: id });
|
||||||
}
|
}
|
||||||
catch (error) {
|
catch (error) {
|
||||||
console.error(error);
|
console.error(error);
|
||||||
|
// @ts-ignore
|
||||||
|
dispatch({ type: MOVE_ERROR, payload: error.message });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,12 @@
|
|||||||
// redux/constants.ts
|
// redux/constants.ts
|
||||||
|
|
||||||
|
export const GET = 'GET';
|
||||||
|
export const PUT = 'PUT';
|
||||||
|
export const POST = 'POST';
|
||||||
|
export const DELETE = 'DELETE';
|
||||||
|
|
||||||
export const GET_MOVES = 'GET_MOVES';
|
export const GET_MOVES = 'GET_MOVES';
|
||||||
export const CREATE_MOVE = 'CREATE_MOVE';
|
export const CREATE_MOVE = 'CREATE_MOVE';
|
||||||
export const UPDATE_MOVE = 'UPDATE_MOVE';
|
export const UPDATE_MOVE = 'UPDATE_MOVE';
|
||||||
export const DELETE_MOVE = 'DELETE_MOVE';
|
export const DELETE_MOVE = 'DELETE_MOVE';
|
||||||
|
export const MOVE_ERROR = 'MOVE_ERROR';
|
||||||
|
Loading…
Reference in new issue