Skip to content

Commit

Permalink
Merge pull request #198 from canyener/clean-up
Browse files Browse the repository at this point in the history
Clean up
  • Loading branch information
canyener authored Dec 29, 2020
2 parents b0a5a07 + 36ea762 commit ee5a77a
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 23 deletions.
66 changes: 46 additions & 20 deletions src/app/stores/activityStore.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import { makeObservable, observable, action, computed } from "mobx"
import { makeObservable, observable, action, computed, configure, runInAction } from "mobx"
import { createContext, SyntheticEvent } from 'react'

import { IActivity } from "../models/activity"
import agent from '../api/agent'

configure({
enforceActions: 'always'
})

class ActivityStore {

constructor() {
Expand All @@ -27,27 +31,38 @@ class ActivityStore {
try {
const activityList = await agent.Activities.list()

activityList.forEach((activity) => {
activity.date = activity.date.split('.')[0]
this.activityRegistry.set(activity.id, activity)
runInAction(() => {
activityList.forEach((activity) => {
activity.date = activity.date.split('.')[0]
this.activityRegistry.set(activity.id, activity)
})

this.loadingInitial = false
})

this.loadingInitial = false
} catch (error) {
runInAction(() => {
this.loadingInitial = false
})
console.log(error)
this.loadingInitial = false
}
}

@action createActivity = async (activity: IActivity) => {
this.submitting = true
try {
await agent.Activities.create(activity)
this.activityRegistry.set(activity.id, activity)
this.editMode = false
this.submitting = false

runInAction(() => {
this.activityRegistry.set(activity.id, activity)
this.editMode = false
this.submitting = false
})

} catch (error) {
this.submitting = false
runInAction(() => {
this.submitting = false
})
console.log(error)
}
}
Expand All @@ -56,12 +71,18 @@ class ActivityStore {
this.submitting = true
try {
await agent.Activities.update(activity)
this.activityRegistry.set(activity.id, activity)
this.selectedActivity = activity
this.editMode = false
this.submitting = false

runInAction(() => {
this.activityRegistry.set(activity.id, activity)
this.selectedActivity = activity
this.editMode = false
this.submitting = false
})

} catch (error) {
this.submitting = false
runInAction(() => {
this.submitting = false
})
console.log(error)
}
}
Expand All @@ -71,13 +92,18 @@ class ActivityStore {
this.target = event.currentTarget.name
try {
await agent.Activities.delete(id)
this.activityRegistry.delete(id)
this.submitting = false
this.target = ''

runInAction(() => {
this.activityRegistry.delete(id)
this.submitting = false
this.target = ''
})
} catch (error) {
this.submitting = false
this.target = ''
runInAction(() => {
this.submitting = false
this.target = ''
})

console.log(error)
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/features/activities/dashboard/ActivityDashboard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import ActivityStore from '../../../app/stores/activityStore'

import ActivityList from './ActivityList'
import ActivityDetails from '../details/ActivityDetails'
import { ActivityForm } from '../form/ActivityForm'
import ActivityForm from '../form/ActivityForm'

const ActivityDashboard: React.FC = () => {
const activityStore = useContext(ActivityStore)
Expand Down
5 changes: 3 additions & 2 deletions src/features/activities/form/ActivityForm.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React, { FormEvent, useContext, useState } from 'react'
import { Segment, Form, Button } from 'semantic-ui-react'
import { v4 as uuid } from 'uuid'
import { observer } from 'mobx-react-lite'

import { IActivity } from '../../../app/models/activity'

Expand All @@ -15,7 +16,7 @@ const ActivityForm: React.FC<IProps> = ({

const activityStore = useContext(ActivityStore)
const { createActivity, editActivity, submitting, cancelFormOpen } = activityStore

const initializeForm = () => {
if (initialFormState) {
return initialFormState
Expand Down Expand Up @@ -100,4 +101,4 @@ const ActivityForm: React.FC<IProps> = ({

ActivityForm.displayName = 'ActivityForm'

export { ActivityForm }
export default observer(ActivityForm)

0 comments on commit ee5a77a

Please sign in to comment.