mirror of
https://github.com/pawelmalak/flame.git
synced 2025-07-22 21:09:36 +02:00
Pinned Apps
This commit is contained in:
parent
d3c5e2a5ec
commit
fa5c35b619
6 changed files with 90 additions and 12 deletions
|
@ -16,6 +16,7 @@
|
||||||
display: flex;
|
display: flex;
|
||||||
align-items: center;
|
align-items: center;
|
||||||
height: 40px;
|
height: 40px;
|
||||||
|
transition: all 0.3s;
|
||||||
}
|
}
|
||||||
|
|
||||||
.SettingsNavLink:hover {
|
.SettingsNavLink:hover {
|
||||||
|
|
|
@ -3,11 +3,18 @@ export interface App {
|
||||||
name: string;
|
name: string;
|
||||||
url: string;
|
url: string;
|
||||||
icon: string;
|
icon: string;
|
||||||
|
isPinned: boolean;
|
||||||
createdAt: Date;
|
createdAt: Date;
|
||||||
updatedAt: Date;
|
updatedAt: Date;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface AppResponse {
|
export interface AppResponse<T> {
|
||||||
success: boolean;
|
success: boolean;
|
||||||
data: App[]
|
data: T;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface NewApp {
|
||||||
|
name: string;
|
||||||
|
url: string;
|
||||||
|
icon: string;
|
||||||
}
|
}
|
|
@ -1,13 +1,18 @@
|
||||||
import {
|
import {
|
||||||
GetAppsAction,
|
GetAppsAction,
|
||||||
SetTheme
|
SetTheme,
|
||||||
|
PinAppAction,
|
||||||
|
AddAppAction
|
||||||
} from './';
|
} from './';
|
||||||
|
|
||||||
export enum ActionTypes {
|
export enum ActionTypes {
|
||||||
setTheme,
|
setTheme = 'SET_THEME',
|
||||||
getApps,
|
getApps = 'GET_APPS',
|
||||||
getAppsSuccess,
|
getAppsSuccess = 'GET_APPS_SUCCESS',
|
||||||
getAppsError
|
getAppsError = 'GET_APPS_ERROR',
|
||||||
|
pinApp = 'PIN_APP',
|
||||||
|
addApp = 'ADD_APP',
|
||||||
|
addAppSuccess = 'ADD_APP_SUCCESS'
|
||||||
}
|
}
|
||||||
|
|
||||||
export type Action = GetAppsAction<any> | SetTheme;
|
export type Action = GetAppsAction<any> | SetTheme | PinAppAction | AddAppAction;
|
|
@ -1,11 +1,11 @@
|
||||||
import axios from 'axios';
|
import axios from 'axios';
|
||||||
import { Dispatch } from 'redux';
|
import { Dispatch } from 'redux';
|
||||||
import { ActionTypes } from './actionTypes';
|
import { ActionTypes } from './actionTypes';
|
||||||
import { App, AppResponse } from '../../interfaces/App';
|
import { App, AppResponse, NewApp } from '../../interfaces/App';
|
||||||
|
|
||||||
export interface GetAppsAction<T> {
|
export interface GetAppsAction<T> {
|
||||||
type: ActionTypes.getApps | ActionTypes.getAppsSuccess | ActionTypes.getAppsError,
|
type: ActionTypes.getApps | ActionTypes.getAppsSuccess | ActionTypes.getAppsError;
|
||||||
payload: T
|
payload: T;
|
||||||
}
|
}
|
||||||
|
|
||||||
export const getApps = () => async (dispatch: Dispatch) => {
|
export const getApps = () => async (dispatch: Dispatch) => {
|
||||||
|
@ -15,7 +15,7 @@ export const getApps = () => async (dispatch: Dispatch) => {
|
||||||
});
|
});
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const res = await axios.get<AppResponse>('/api/apps');
|
const res = await axios.get<AppResponse<App[]>>('/api/apps');
|
||||||
|
|
||||||
dispatch<GetAppsAction<App[]>>({
|
dispatch<GetAppsAction<App[]>>({
|
||||||
type: ActionTypes.getAppsSuccess,
|
type: ActionTypes.getAppsSuccess,
|
||||||
|
@ -28,3 +28,39 @@ export const getApps = () => async (dispatch: Dispatch) => {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface PinAppAction {
|
||||||
|
type: ActionTypes.pinApp;
|
||||||
|
payload: App;
|
||||||
|
}
|
||||||
|
|
||||||
|
export const pinApp = (id: number, isPinned: boolean) => async (dispatch: Dispatch) => {
|
||||||
|
try {
|
||||||
|
const res = await axios.put<AppResponse<App>>(`/api/apps/${id}`, { isPinned: !isPinned });
|
||||||
|
|
||||||
|
dispatch<PinAppAction>({
|
||||||
|
type: ActionTypes.pinApp,
|
||||||
|
payload: res.data.data
|
||||||
|
})
|
||||||
|
} catch (err) {
|
||||||
|
console.log(err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface AddAppAction {
|
||||||
|
type: ActionTypes.addAppSuccess;
|
||||||
|
payload: App;
|
||||||
|
}
|
||||||
|
|
||||||
|
export const addApp = (formData: NewApp) => async (dispatch: Dispatch) => {
|
||||||
|
try {
|
||||||
|
const res = await axios.post<AppResponse<App>>('/api/apps', formData);
|
||||||
|
|
||||||
|
dispatch<AddAppAction>({
|
||||||
|
type: ActionTypes.addAppSuccess,
|
||||||
|
payload: res.data.data
|
||||||
|
})
|
||||||
|
} catch (err) {
|
||||||
|
console.log(err);
|
||||||
|
}
|
||||||
|
}
|
|
@ -37,11 +37,36 @@ const getAppsError = (state: State, action: Action): State => {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const pinApp = (state: State, action: Action): State => {
|
||||||
|
const tmpApps = [...state.apps];
|
||||||
|
const changedApp = tmpApps.find((app: App) => app.id === action.payload.id);
|
||||||
|
|
||||||
|
if (changedApp) {
|
||||||
|
changedApp.isPinned = action.payload.isPinned;
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
...state,
|
||||||
|
apps: tmpApps
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const addAppSuccess = (state: State, action: Action): State => {
|
||||||
|
const tmpApps = [...state.apps, ...action.payload];
|
||||||
|
|
||||||
|
return {
|
||||||
|
...state,
|
||||||
|
apps: tmpApps
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const appReducer = (state = initialState, action: Action) => {
|
const appReducer = (state = initialState, action: Action) => {
|
||||||
switch (action.type) {
|
switch (action.type) {
|
||||||
case ActionTypes.getApps: return getApps(state, action);
|
case ActionTypes.getApps: return getApps(state, action);
|
||||||
case ActionTypes.getAppsSuccess: return getAppsSuccess(state, action);
|
case ActionTypes.getAppsSuccess: return getAppsSuccess(state, action);
|
||||||
case ActionTypes.getAppsError: return getAppsError(state, action);
|
case ActionTypes.getAppsError: return getAppsError(state, action);
|
||||||
|
case ActionTypes.pinApp: return pinApp(state, action);
|
||||||
|
case ActionTypes.addAppSuccess: return addAppSuccess(state, action);
|
||||||
default: return state;
|
default: return state;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,6 +14,10 @@ const App = sequelize.define('App', {
|
||||||
type: DataTypes.STRING,
|
type: DataTypes.STRING,
|
||||||
allowNull: false,
|
allowNull: false,
|
||||||
defaultValue: 'cancel'
|
defaultValue: 'cancel'
|
||||||
|
},
|
||||||
|
isPinned: {
|
||||||
|
type: DataTypes.BOOLEAN,
|
||||||
|
defaultValue: false
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue