Skip to content

Commit

Permalink
feat: add update settings to service
Browse files Browse the repository at this point in the history
  • Loading branch information
acnormun committed Feb 10, 2025
1 parent 4f5276b commit 4e78f0c
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 97 deletions.
33 changes: 11 additions & 22 deletions src/components/settings/SettingsContainer/SettingsContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import { PreferencesOrderStatusActive } from "../OrderStatusActive";
import { PreferencesAbandonedCartActive } from "../AbandonedCartActive";
import { useState } from "react";
import { SettingsContext, SettingsFormData } from "./SettingsContext";
import { VTEXFetch } from "../../../utils/VTEXFetch";
import { useSelector } from "react-redux";
import { selectToken } from "../../../store/authSlice";
import { selectProject } from "../../../store/projectSlice";
import { featureLoading, selectProject } from "../../../store/projectSlice";
import { updateAgentSettings } from "../../../services/features.service";

type codes = 'abandoned-cart' | 'order-status';

Expand All @@ -24,10 +24,9 @@ export function SettingsContainer({ open, toggleOpen, code, agentUuid }: Setting
const projectUuid = useSelector(selectProject);

const [formData, setFormData] = useState<SettingsFormData>();
const [isUpdating, setIsUpdating] = useState(false);
const isUpdating = useSelector(featureLoading)

function updateAgentSettings() {
setIsUpdating(true);
async function updateAgent() {

const body = {
"feature_uuid": agentUuid,
Expand All @@ -49,23 +48,13 @@ export function SettingsContainer({ open, toggleOpen, code, agentUuid }: Setting
}
};

VTEXFetch<{
message: string;
}>(`/_v/update-feature-settings?token=${token}`, {
method: 'PUT',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(body),
}).then(() => {
toast.success(t('agents.common.configure.success'));
})
.catch(() => {
const response = await updateAgentSettings(body, token)

if(response?.error){
toast.critical(t('agents.common.configure.error'));
}).finally(() => {
setIsUpdating(false);
toggleOpen();
});
return
}
toast.success(t('agents.common.configure.success'));
}

return (
Expand Down Expand Up @@ -94,7 +83,7 @@ export function SettingsContainer({ open, toggleOpen, code, agentUuid }: Setting

<Button
variant="primary"
onClick={updateAgentSettings}
onClick={updateAgent}
size="large"
style={{ width: '50%' }}
disabled={isUpdating}
Expand Down
17 changes: 10 additions & 7 deletions src/pages/setup/useUserSetup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import { checkProject, createUserAndProject, fetchUserData } from '../../service
import { setToken } from '../../store/authSlice';
import store from '../../store/provider.store';
import { getToken } from '../../services/auth.service';
import { setAgent, setFeatureList, setFlowsChannelUuid, setProjectUuid, setWppCloudAppUuid } from '../../store/projectSlice';
import { setAgent, setFlowsChannelUuid, setProjectUuid, setWppCloudAppUuid } from '../../store/projectSlice';
import { checkWppIntegration } from '../../services/channel.service';
import { checkAgentIntegration } from '../../services/agent.service';
import { getFeatureList } from '../../services/features.service';
// import { getFeatureList } from '../../services/features.service';
import { useCallback } from 'react';
import { toast } from '@vtex/shoreline';

Expand Down Expand Up @@ -49,11 +49,14 @@ export function useUserSetup() {
throw new Error(response.error)
}

const featureList = await getFeatureList(project_uuid, token);
if (featureList?.error) {
throw new Error(JSON.stringify(featureList.error))
}
store.dispatch(setFeatureList(featureList.data.features))
// TODO: get the complete list of fetaures
// const featureList = await getFeatureList(project_uuid, token);
// if (featureList?.error) {
// throw new Error(JSON.stringify(featureList.error))
// }
// if (featureList.data.features.length > 0) {
// store.dispatch(setFeatureList(featureList.data.features))
// }

const agentIntegration = await checkAgentIntegration(project_uuid, token);
if (agentIntegration.error) {
Expand Down
2 changes: 1 addition & 1 deletion src/services/agent.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,4 @@ export async function setAgentBuilder(payload: any, project_uuid: string, token:
return { success: false, error: response?.message || 'Error creating agent' };
}
return { success: true, data: response };
}
}
31 changes: 30 additions & 1 deletion src/services/features.service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import { VTEXFetch } from "../utils/VTEXFetch";
import storeProvider from "../store/provider.store";
import { setFeatureLoading } from "../store/projectSlice";

export async function getFeatureList(project_uuid: string, token: string) {
try {
Expand Down Expand Up @@ -34,7 +36,6 @@ export async function integrateFeature(feature_uuid: string, project_uuid: strin
flows_channel_uuid,
wpp_cloud_app_uuid,
};


try {
const response = await VTEXFetch(`/_v/integrate-feature?token=${token}`, {
Expand Down Expand Up @@ -86,3 +87,31 @@ export async function integrateAvailableFeatures(project_uuid: string, token: st
return { success: false, error: error || 'unknown error' };
}
}

export async function updateAgentSettings(body: any, token: string) {
storeProvider.dispatch(setFeatureLoading(true))

try {
const response = await VTEXFetch<{
message: string;
error: string;
}>(`/_v/update-feature-settings?token=${token}`, {
method: 'PUT',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(body),
});

if (!response || response.error) {
throw new Error(response?.message || 'error updating agent.');
}

return { success: true, data: response };
} catch (error) {
console.error('error updating agent:', error);
return { success: false, error: error || 'unknown error' };
} finally {
storeProvider.dispatch(setFeatureLoading(false))
}
}
143 changes: 77 additions & 66 deletions src/store/projectSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,75 +2,86 @@
import { createSlice } from '@reduxjs/toolkit';

const projectSlice = createSlice({
name: 'project',
initialState: {
project_uuid: null,
wpp_cloud_app_uuid: null,
flows_channel_uuid: null,
loadingSetup: false,
setupError: false,
agentLoading: false,
wppLoading: false,
agent: {
name: '',
links: [],
occupation: '',
objective: ''
},
featureList: [{
feature_uuid: '82738123',
name: 'project',
initialState: {
project_uuid: null,
wpp_cloud_app_uuid: null,
flows_channel_uuid: null,
loadingSetup: false,
setupError: false,
agentLoading: false,
wppLoading: false,
featureLoading: false,
agent: {
name: '',
links: [],
occupation: '',
objective: ''
},
featureList: [
{
feature_uuid: 'a3d77bf9-1e06-44cb-a550-c691e6d44687',
name: 'Order status change notification',
description: 'Solution that notifies the customer whenever the order status changes (BR)',
disclaimer: '',
code: 'order-status',
restrict_test: true
},
{
feature_uuid: '827381ede23',
code: 'abandoned-cart'

}],
integratedFeatures: [] as any[]
},
reducers: {
setProjectUuid: (state, action) => {
state.project_uuid = action.payload
},
setWppCloudAppUuid: (state, action) => {
state.wpp_cloud_app_uuid = action.payload
},
setFlowsChannelUuid: (state, action) => {
state.flows_channel_uuid = action.payload
},
setLoadingSetup: (state, action) => {
state.loadingSetup = action.payload
},
setSetupError: (state, action) => {
state.setupError = action.payload
},
setAgentLoading: (state, action) => {
state.agentLoading = action.payload
},
setWppLoading: (state, action) => {
state.wppLoading = action.payload
},
setAgent: (state, action) => {
state.agent = action.payload
},
setFeatureList: (state, action) => {
state.featureList = action.payload
},
setIntegratedFeatures: (state, action) => {
state.integratedFeatures = action.payload
feature_uuid: '83fe991a-1677-45cf-9096-83fdbb086df7',
name: 'Opinionated Abandoned Cart',
description: 'A pre-configured solution for abandoned cart notifications, without user input.',
disclaimer: '',
code: 'abandoned-cart',
}
],
integratedFeatures: [] as any[]
},
reducers: {
setProjectUuid: (state, action) => {
state.project_uuid = action.payload
},
setWppCloudAppUuid: (state, action) => {
state.wpp_cloud_app_uuid = action.payload
},
setFlowsChannelUuid: (state, action) => {
state.flows_channel_uuid = action.payload
},
setLoadingSetup: (state, action) => {
state.loadingSetup = action.payload
},
setSetupError: (state, action) => {
state.setupError = action.payload
},
setAgentLoading: (state, action) => {
state.agentLoading = action.payload
},
setWppLoading: (state, action) => {
state.wppLoading = action.payload
},
setFeatureLoading: (state, action) => {
state.featureLoading = action.payload
},
setAgent: (state, action) => {
state.agent = action.payload
},
setFeatureList: (state, action) => {
state.featureList = action.payload
},
setIntegratedFeatures: (state, action) => {
state.integratedFeatures = action.payload
}
})
}
})

export const {setProjectUuid, setLoadingSetup, setSetupError, setWppCloudAppUuid, setFlowsChannelUuid, setAgentLoading, setAgent, setFeatureList, setWppLoading, setIntegratedFeatures} = projectSlice.actions
export const selectProject = (state: any) => state.project.project_uuid
export const wppCloudAppUuid = (state: any) => state.project.wpp_cloud_app_uuid
export const flowsChannelUuid = (state: any) => state.project.flows_channel_uuid
export const loadingSetup = (state: any) => state.project.loadingSetup
export const setupError = (state: any) => state.project.setupError
export const agentLoading = (state: any) => state.project.agentLoading
export const getAgent = (state: any) => state.project.agent
export const featureList = (state: any) => state.project.featureList
export const wppLoading = (state: any) => state.project.wppLoading
export default projectSlice.reducer;
export const { setProjectUuid, setLoadingSetup, setSetupError, setWppCloudAppUuid, setFlowsChannelUuid, setAgentLoading, setAgent, setFeatureList, setWppLoading, setIntegratedFeatures, setFeatureLoading } = projectSlice.actions
export const selectProject = (state: any) => state.project.project_uuid
export const wppCloudAppUuid = (state: any) => state.project.wpp_cloud_app_uuid
export const flowsChannelUuid = (state: any) => state.project.flows_channel_uuid
export const loadingSetup = (state: any) => state.project.loadingSetup
export const setupError = (state: any) => state.project.setupError
export const agentLoading = (state: any) => state.project.agentLoading
export const getAgent = (state: any) => state.project.agent
export const featureList = (state: any) => state.project.featureList
export const wppLoading = (state: any) => state.project.wppLoading
export const featureLoading = (state: any) => state.project.featureLoading
export default projectSlice.reducer;

0 comments on commit 4e78f0c

Please sign in to comment.